<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");

//$idno = $_REQUEST['idno'];
$session = $_REQUEST['session'];

$status = 0;
$msg = "Unknown error....!";

//$arydate = $db->getRows("SELECT date,notice_detail,upload_notice,faculty,content_id FROM student_notice where school_id='1' and type='1' and session='$session' and Added_by='$idno' ORDER BY id DESC");
$arydate = $db->getRows("SELECT date,notice_detail,upload_notice,class,content_id,Added_by,student_id FROM student_notice where school_id='1' and type='2' and session='$session' ORDER BY id DESC");

$contentArray = array();

if (count($arydate) > 0) {
    $status = 1;
    $msg = "Data found";

    foreach ($arydate as $rows) {

        $ct_id = $rows['content_id'];
        if ($ct_id == 0) continue;

        $adminId = $rows['Added_by'];
        $rows['sender_name'] = $db->getVal("SELECT name from admin WHERE id = '$adminId'");

        $class_name = $rows['class'];

        if (empty($rows['student_id'])){
            $rows['type_student'] = 0;
            $rows['student_id'] = new stdClass();

            $rows['view_action'] = 'View Class';
        }else{
            $rows['type_student'] = 1;
            $rows['student_id'] = json_decode($rows['student_id']);

            $rows['view_action'] = 'View Student';
        }

        if (array_key_exists($ct_id, $contentArray)) {
            $contentArray[$ct_id]['class_list'][] = $class_name;
        } else {

            $fileArray = array();

            //($rows['upload_notice'] == "N/A" || $rows['upload_notice'] == "NA") ? $rows["file"]="" : $rows["file"]= URL_ROOT."uploads/notice/".$rows['upload_notice'];

            if ($rows['upload_notice'] == "N/A" || $rows['upload_notice'] == "NA" || $rows['upload_notice'] == '' || $rows['upload_notice'] == null){

            }else{
                $files = explode(",", $rows['upload_notice']);
                foreach ($files as $names){
                    $fileArray[] = URL_ROOT."uploads/notice/".$names;
                }
            }

            $rows["file"]= $fileArray;

            $rows['class_list'] = array($class_name);
            unset($rows['upload_notice']);
            $contentArray[$ct_id] = $rows;
        }

    }
}else{
    $msg = "Data not found";
}

echo json_encode(array("status"=>$status,"data"=>array_values($contentArray),"msg"=>$msg));