<?php 
include("../../db.php");

header('Content-Type: application/json');
 
  $chapter = $_REQUEST['chapter'];
  $uid = $_REQUEST['uid'];
//  $chapter = 406;
    
//    $chapter = 1542;

    $status = 0;

    $videosArray = array();
    $mindmapArray = array();
    $practicalArray = array();
    
    $keynotesArray = array();
    $keytermsArray = array();    
    
    $objectiveArray = array();
    $subjectiveArray = array();
    $ncertArray = array();
    
    $subpaperArray = array();
    $ncertpaperArray = array();
    
    $array = array();

	$assignment = $db->getRow("select * from assignment_new where chapter='$chapter' ");
    if(count($assignment)>0){

    	$allfiles = $db->getRows("select * from assignment_files where assignment_id='".$assignment['id']."' ");
            if(count($allfiles) > 0){
                $status = 1;

            	foreach($allfiles as $file){
   
                    if($file['type']=='key_terms'){
                    array_push($keytermsArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"https://$_SERVER[HTTP_HOST]/uploads/keyterms/$file[name]", "file_type"=>"$assignment[key_terms]" ]);
                    }
                    
                    if($file['type']=='mind_map'){
                    array_push($mindmapArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"https://$_SERVER[HTTP_HOST]/uploads/mindmap/$file[name]", "file_type"=>"$assignment[mind_map]" ]);
                    }
                    
                    if($file['type']=='key_notes'){
                    array_push($keynotesArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"https://$_SERVER[HTTP_HOST]/uploads/keynotes/$file[name]", "file_type"=>"$assignment[key_notes]" ]);
                    }
    
                    if($file['type']=='practical_projects'){
                    array_push($practicalArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"https://$_SERVER[HTTP_HOST]/uploads/practicalprojects/$file[name]", "file_type"=>"$assignment[practical_projects]" ]);
                    }
                    
                    if(($file['type']=='videos') || ($file['type']=='youtube_link')){
                        if($file['type']=='videos'){
                            $filepath = "https://$_SERVER[HTTP_HOST]/uploads/videos/$file[name]";
                        }
                        if($file['type']=='youtube_link'){
        			    $url_string = "$file[name]";
                        $filepath = str_replace('watch?v=', 'embed/',  $url_string);
                        if(strrpos($filepath,"&")){
                            $filepath = substr($filepath, 0, strpos($filepath, "&"));
                        }

                        }
                    array_push($videosArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"videos","file_path"=>"$filepath", "file_type"=>"$file[type]" ]);
                    }


            	}

            }
    }
    
    $objectivepapers = $db->getRows("select * from assignment_paper where chapter='$chapter' and paper_type=1");
    if(count($objectivepapers) > 0){

        foreach($objectivepapers as $objective){
            $flag = 0;
            $allquestions = $db->getRows("select * from assignment_questions where paper_id='$objective[id]' ");
            
            $checkattempt = $db->getRow("select * from assignment_attempt_paper where pid='".$objective[id]."' and uid='".$uid."' ");
            if(count($checkattempt)>0){
                $flag = 1;
            }

            $tq = count($allquestions);
            $marks = $objective['per_que_marks']*$tq;
            $time = $objective['duration'];
            $level = $objective['level'];
            $description = $objective['paper_description'];
 
            array_push($objectiveArray, ["id"=>"$objective[id]", "totalque"=>"$tq", "per_que_marks"=>"$objective[per_que_marks]", "marks"=>"$marks", "time"=>"$time", "level"=>"$level", "description"=>"$description", "attempted"=>$flag]);
        }
    }
    
    $subjective = $db->getRow("select * from assignment_paper where chapter='$chapter' and paper_type=2");
    if(count($subjective) > 0){
 
        // foreach($subjectivepapers as $subjective){

            $allquestions = $db->getRows("select * from assignment_questions where paper_id='$subjective[id]' ");

            $tq = count($allquestions);
            $marks = $subjective['per_que_marks']*$tq;
            $time = $subjective['duration'];
            $level = $subjective['level'];
            $description = $subjective['paper_description'];
            
            if(count($allquestions) > 0){
                $status = 1;

            	foreach($allquestions as $question){
            	    $options = json_decode($question[options], true);
            	    $m_options = json_decode($question[m_options], true);
            	    
                    $allimages = $db->getRows("SELECT * from assignment_question_files where question_id='$question[id]' ");
                    $imageArray = array();
                    $optionimage = array();

                    foreach($allimages as $image){
                        if($image[image_type]=="question_image"){
                            array_push($imageArray, ["image"=>"http://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]"]);
                        }
                        else {
                            array_push($optionimage, ["image"=>"http://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]", "optno"=>"$image[option_no]"]);
                        }
                    }

                  array_push($subpaperArray,["id"=>"$question[id]","question"=>"$question[question]","q_image"=>$imageArray,"options"=>$options, "opt_image"=>$optionimage, "m_options"=>$m_options, "answer"=>"$question[answer]", "hint"=>"$question[hint]", "solution"=>"$question[solution]", "qtype"=>$question[qtype], "quemark"=>$question[quemark] ]);
            	}

            }

            array_push($subjectiveArray, ["totalque"=>"$tq", "per_que_marks"=>"$subjective[per_que_marks]", "marks"=>"$marks", "time"=>"$time", "level"=>"$level", "description"=>"$description", "all_questions"=>$subpaperArray ]);
        // }
    }

    $ncert = $db->getRow("select * from assignment_paper where chapter='$chapter' and paper_type=3");
    if(count($ncert) > 0){

        // foreach($ncertpapers as $ncert){

            $allquestions = $db->getRows("select * from assignment_questions where paper_id='$ncert[id]' ");

            $tq = count($allquestions);
            $marks = $ncert['per_que_marks']*$tq;
            $time = $ncert['duration'];
            $level = $ncert['level'];
            $description = $ncert['paper_description'];

            if(count($allquestions) > 0){
                $status = 1;

            	foreach($allquestions as $question){
            	    $options = json_decode($question[options], true);
            	    $m_options = json_decode($question[m_options], true);
            	    
                    $allimages = $db->getRows("SELECT * from assignment_question_files where question_id='$question[id]' ");
                    $imageArray = array();
                    $optionimage = array();

                    foreach($allimages as $image){
                        if($image[image_type]=="question_image"){
                            array_push($imageArray, ["image"=>"http://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]"]);
                        }
                        else {
                            array_push($optionimage, ["image"=>"http://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]", "optno"=>"$image[option_no]"]);
                        }
                    }

                  array_push($ncertpaperArray,["id"=>"$question[id]","question"=>"$question[question]","q_image"=>$imageArray,"options"=>$options, "opt_image"=>$optionimage, "m_options"=>$m_options, "answer"=>"$question[answer]", "hint"=>"$question[hint]", "solution"=>"$question[solution]", "qtype"=>$question[qtype], "quemark"=>$question[quemark] ]);
            	}

            }

            array_push($ncertArray, ["totalque"=>"$tq", "per_que_marks"=>"$ncert[per_que_marks]", "marks"=>"$marks", "time"=>"$time", "level"=>"$level", "description"=>"$description", "all_questions"=>$ncertpaperArray]);
        // } 
    }
 
    $array['status'] = $status;
    $array['key_terms'] = $keytermsArray;
    $array['mind_map'] = $mindmapArray;
    $array['key_notes'] = $keynotesArray;
    $array['practical'] = $practicalArray;
    $array['videos'] = $videosArray;
    
    $array['objective'] = $objectiveArray;
    $array['subjective'] = $subjectiveArray;
    $array['ncert'] = $ncertArray;
 
 echo json_encode($array);
?>