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

/*ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);*/

$classArr = array();
$secArr = array();

$myUid = $_REQUEST['my_uid'];

$class = $db->getRows("select class from class");
$section = $db->getRows("select name from section");

$emptyJson = new stdClass();

if ($class && count($class) > 0){

    foreach ($class as $item) {
        $thread_name = $item['class']."/T".$myUid;

        $tempRow = $db->getRow("SELECT * FROM recent_message WHERE thread = '$thread_name' ");
        if ($tempRow == null || $tempRow == ''){
            $tempRow = $emptyJson;
        }

        $item['recent_msg'] =  $tempRow;
        $classArr[] = $item;
    }

}

$recent1 = $db->getRow("SELECT * FROM recent_message WHERE thread = 'allclass' ");
if ($recent1 == null || $recent1 == ''){
    $recent1 = $emptyJson;
}else{
    $stfName = $db->getVal("SELECT name FROM staff WHERE idno = '$recent1[author]' ");
    $recent1['author_name'] = $stfName;
}

$classArr[] = array("class" => "All Class", "recent_msg" => $recent1);

$recent2 = $db->getRow("SELECT * FROM recent_message WHERE thread = 'allteacher' ");

if ($recent2 == null || $recent2 == ''){
    $recent2 = $emptyJson;
}else{
    $recent2['author_name'] = $db->getVal("SELECT name FROM staff WHERE idno = '$recent2[author]' ");
}

$classArr[] = array("class" => "All Teacher", "recent_msg" => $recent2);

if ($section && count($section) > 0){

    foreach ($section as $item) {
        $secArr[] = $item['name'];
    }

}

echo json_encode(array("status" => 1, "data" => array("class" => $classArr, "section" => $secArr)));