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

$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;
        $item['recent_msg'] = $db->getRow("SELECT * FROM recent_message WHERE thread = '$thread_name' ") ?: $emptyJson;
        $classArr[] = $item;
    }

}

$recent1 = $db->getRow("SELECT * FROM recent_message WHERE thread = 'allclass' ") ?: $emptyJson;

$recent1['author_name'] = $db->getVal("SELECT name FROM staff WHERE idno = '$recent1[author]' ") ?: "";

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

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

$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)));