<?php
include("../../db.php");
//header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

    $class = $_REQUEST['class'];
    $subjectid = $_REQUEST['subject_id'];
    $sub_subjectid = $_REQUEST['sub_subject_id'];
    $chapterid = $_REQUEST['chapter_id'];

	$array = array();
	$status = 0;

    $keytermsArray = array();
    $mindmapArray = array();
    $keynotesArray = array();
    $practicalArray = array();
    $videosArray = array();

    $assignment_id = $db->getRow("SELECT * from assignment_new where class='$class' and parent_subject='".$subjectid."' and sub_subject='".$sub_subjectid."' and chapter='".$chapterid."' ");

    if(count($assignment_id)>0){

        $allfiles = $db->getRows("select * from assignment_files where assignment_id='".$assignment_id['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"=>"$_SERVER[HTTP_HOST]/uploads/keyterms/$file[name]", "file_type"=>"$assignment_id[key_terms]" ]);
                }
                
                if($file['type']=='mind_map'){
                array_push($mindmapArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"$_SERVER[HTTP_HOST]/uploads/mindmap/$file[name]", "file_type"=>"$assignment_id[mind_map]" ]);
                }
                
                if($file['type']=='key_notes'){
                array_push($keynotesArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"$_SERVER[HTTP_HOST]/uploads/keynotes/$file[name]", "file_type"=>"$assignment_id[key_notes]" ]);
                }

                if($file['type']=='practical_projects'){
                array_push($practicalArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"$file[type]","file_path"=>"$_SERVER[HTTP_HOST]/uploads/practicalprojects/$file[name]", "file_type"=>"$assignment_id[practical_projects]" ]);
                }
                
                if(($file['type']=='videos') || ($file['type']=='youtube_link')){
                    if($file['type']=='videos'){
                        $filepath = "$_SERVER[HTTP_HOST]/uploads/videos/$file[name]";
                    }
                    if($file['type']=='youtube_link'){
//                        $filepath = "$file[name]";
    			    $url_string = "$file[name]";
                    $filepath = str_replace('embed/','watch?v=', $url_string);
                    }
                array_push($videosArray,["id"=>"$file[id]","description"=>"$file[description]","order"=>"$file[ordering]","type"=>"videos","file_path"=>"$filepath", "file_type"=>"$file[type]" ]);
                }
                
            }
   
        }
   
    }


    $array['status'] = $status;
    $array['key_terms'] = $keytermsArray;
    $array['mind_map'] = $mindmapArray;
    $array['key_notes'] = $keynotesArray;
    $array['practical'] = $practicalArray;
    $array['videos'] = $videosArray;

    echo json_encode($array);

?>