<?php
include("../../db.php");
header('Content-Type: application/json');

$status = 0;
$data_document = array();
$data_youtube = array();
$msg = "Unable to get data..!";

if (isset($_GET['subject_id']) && isset($_GET['session'])){

    $image_path = "https://".$_SERVER['HTTP_HOST']."/uploads/econtent/";

    $subject_id = $_GET['subject_id'];
    $session = $_GET['session'];


    $result = $db->getRows("SELECT econtent_files.* FROM econtent INNER JOIN econtent_files ON econtent.id = econtent_files.econtent_id WHERE econtent.subject = '$subject_id'");

    if ($result) {
        $status = 1;
        $msg = "Data found";

        foreach ($result as $item) {
            if ($item['type'] == 'document'){

                if (!empty($item['name'])){
                    $item['name'] = $image_path.$item['name'];
                }
                $data_document[] = $item;

            }elseif ($item['type'] == 'youtube_link'){
                $video_id = explode("embed/", $item['name'],2);
                $item['name'] = $video_id[1];
                $data_youtube[] = $item;
            }
        }
    }

}

echo json_encode(array("status" => $status, "doc_data" => $data_document, "youtube_data" => $data_youtube, "msg" => $msg));