0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 3.144.21.237


Current Path : /var/www/html/admission/src/
Upload File :
Current File : /var/www/html/admission/src/examTimetable.php

<?php

function generateExamTimetable($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);

    $req = $aobj_context->req_body;

    $data = $req['data'];

    if (!array_key_exists('frmvalues', $data)) {
        $arr['msg'] = 'Invalid Params';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }

    $ttparms = $data['frmvalues'];

    if (
        $ttparms['fstrdate'] == '' || $ttparms['fdeggrp'] == '' || $ttparms['fexamrange'] == ''
        || $ttparms['fyear'] == '' || $ttparms['fexamtype'] == '' || $ttparms['fdaytype'] == ''
    ) {
        $arr['msg'] = 'Invalid Params values';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }
    // $date = str_replace('/', '-', $date);
    $strdate = str_replace('/', '-', $ttparms['fstrdate']);
    // var_dump($strdate);
    $deggrp = $ttparms['fdeggrp'];

    $exam = str_split($ttparms['fexamrange']);

    $examrange = implode("','", $exam);

    $query = "select distinct fdatecodep from subject  
    where ifnull(fdatecodep, '') <> '' and fdegree in(select fdegree from degree where fdeggrp = '{$deggrp}') 
    and fexamno in ('$examrange')
    order by fdatecodep";
    $datecodes = $aobj_context->pobj_db->GetAll($query);

    if (count($datecodes) == 0) {
        $arr['msg'] = 'Date Codes not defined';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }
    $fdate = date('Y-m-d', strtotime($strdate));
    $values = '';
    for ($i = 0; $i < count($datecodes); $i++) {

        $query = "select fdate from holiday where fdate = '{$fdate}' and ifnull(fdeleted,'') <>'T'";

        $holiday = $aobj_context->pobj_db->GetAll($query);
        if (count($holiday) > 0) {
            $fdate = date('Y-m-d', strtotime($fdate . ' + 1 days'));
            $i--;
            continue;
        }

        if ($ttparms['fsunday'] != true) {
            $query = "select dayname('{$fdate}') as fwday";
            $day = $aobj_context->pobj_db->GetRow($query);
            // echo $day['fwday']."----".$fdate."<br/>";
            if (strtolower($day['fwday']) == 'sunday')
                $fdate = date('Y-m-d', strtotime($fdate . ' + 1 days'));
        }
        // echo $datecodes[$i]['fdatecodep']."----".$fdate."<br/>";
        $values .= "('" . $datecodes[$i]['fdatecodep'] . "', '" . $deggrp . "', '" . $ttparms['fyear'] . "', '" . $ttparms['fexamtype'] . "', 
            '{$fdate}', 'F'),";

        $fdate = $ttparms['fdaytype'] == 'C' ? date('Y-m-d', strtotime($fdate . ' + 1 days')) : date('Y-m-d', strtotime($fdate . ' + 2 days'));
        // $i++;
    }
    $values = rtrim($values, ',');
    $query = "insert into masdate(fdatecode, fdeggrp, fyear, fexamtype, fdate, fdeleted ) values " . $values . "
            ON DUPLICATE KEY UPDATE
            fdate = VALUES(fdate), 
            fdeleted = VALUES(fdeleted)";
    $result = $aobj_context->pobj_db->Execute($query);

    if ($result) {
        $arr['msg'] = 'Time Table Generated Successfully';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
        return;
    } else {
        $arr['msg'] = 'Insertion / Updation Failed';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }
}

function geupdateExamTimetable($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);

    $req = $aobj_context->req_body;

    $data = $req['data'];

    if (!array_key_exists('frmvalues', $data)) {
        $arr['msg'] = 'Invalid Params';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }

    $ttparms = $data['frmvalues'];

    if (
        $ttparms['fdeggrp'] == '' || $ttparms['fexamrange'] == ''
        || $ttparms['fyear'] == '' || $ttparms['fexamtype'] == ''
    ) {
        $arr['msg'] = 'Invalid Params values';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }

    $deggrp = $ttparms['fdeggrp'];

    $exam = str_split($ttparms['fexamrange']);

    $examrange = implode("','", $exam);

    $query = "drop table if exists tmpttmasqp";

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "create table tmpttmasqp
    select distinct fqpcode from subject 
    where fdegree in (select fdegree from degree where fdeggrp = '{$deggrp}')
    and fexamno in ('{$examrange}')
    and ifnull(fqpcode,'') <> ''";
    // and concat(fdegree,fexamno,fsubcode) in 
    // (select distinct concat(fdegree,fexamno,fsubcode) from canddet 
    // where ifnull(fpassmth,'') = '')

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "create index qpmaster_inx on tmpttmasqp(fqpcode)";
    $result = $aobj_context->pobj_db->Execute($query);


    $query = "alter table tmpttmasqp add( fsubname varchar(100))";

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "update tmpttmasqp  m, subject s
    set m.fsubname = s.fsubname
    where m.fqpcode = s.fqpcode
    and ifnull(s.fqpcode,'') <> ''";

    $result = $aobj_context->pobj_db->Execute($query);
    

    $query = "select s.fqpcode, s.fsubname, t.fdatecodep, 
    t.fdatecodet, t.fsessiont as fsession
    from tmpttmasqp s left join timetable t 
    on s.fqpcode = t.fqpcode 
    inner join deggrp d
    on d.fdeggrp = t.fdeggrp
    and d.fyear = t.fyear and d.fexamtype = t.fexamtype 
    and s.fqpcode = t.fqpcode
    and d.fdeggrp = '{$deggrp}' 
    left join masdate m on t.fdatecodep = m.fdatecode 
    and m.fyear = t.fyear and m.fexamtype = t.fexamtype
    left join reason r on t.fsessionp = r.freasoncd
    group by s.fqpcode
    order by fdatecodep, s.fqpcode";

    $timetable = $aobj_context->pobj_db->GetAll($query);

    if (count($timetable) == 0) {

        $query = "select s.fqpcode, s.fsubname, t.fdatecodep, 
            t.fdatecodet, t.fsessiont as fsession
            from tmpttmasqp s left join timetable t 
            on s.fqpcode = t.fqpcode left join deggrp d
            on d.fdeggrp = t.fdeggrp
            and d.fyear = t.fyear and d.fexamtype = t.fexamtype 
            and s.fqpcode = t.fqpcode
            and d.fdeggrp = '{$deggrp}'
            left join masdate m on t.fdatecodep = m.fdatecode 
            and m.fyear = t.fyear and m.fexamtype = t.fexamtype
            left join reason r on t.fsessionp = r.freasoncd
            group by s.fqpcode
            order by fdatecodep, s.fqpcode";

        $timetable = $aobj_context->pobj_db->GetAll($query);

        if(count($timetable) == 0) {
            $arr['msg'] = "Qpcode not found for degree group $deggrp";
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
            return;
        }
        
    }

    $restt = array();
    foreach ($timetable as $key => $val) {
        $restt[$val['fqpcode']] = $val;
    }

    $query = "select freasoncd, fdescpn from reason where ifnull(fdeleted,'') <> 'T' order by freasoncd";
    $reason = $aobj_context->pobj_db->GetAll($query);

    $query = "select fdatecode, date_format(fdate,'%d/%m/%Y') as fdate 
    from masdate where ifnull(fdeleted,'') <> 'T' and fdeggrp = '{$deggrp}'
    and fyear = '{$ttparms['fyear']}' and fexamtype = '{$ttparms['fexamtype']}'
    order by fdatecode";
    $masdate = $aobj_context->pobj_db->GetAll($query);

    if (count($masdate) == 0) {
        $arr['msg'] = "Date codes not found for degree group $deggrp";
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }

    // $data = "Uploaded Successfully.";
    $arr['reason'] = $reason;
    $arr['masdate'] = $masdate;
    $arr['timetable'] = $timetable;
    echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
    return;
}

function getExamTimetable($aobj_context)
{

    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $univcode = $aobj_context->mobj_data["univcode"];
    $deggrp = $aobj_context->mobj_data["deggrp"];
    $examrange = $aobj_context->mobj_data["examrange"];

    $exam = str_split($examrange);

    $examrange = implode("','", $exam);

    $query = "SELECT fdbname, ffolder FROM dbname WHERE funivcode = $univcode";
    $result = $aobj_context->mobj_db->GetRow($query);
    $db = $result['fdbname'];
    $folder = $result['ffolder'];

    $query =  "select s.fqpcode, s.fsubname, group_concat(distinct s.fdegree SEPARATOR ', ')  as fdegree,
    ifnull(t.fdatecode,'') as fdatecode, ifnull(t.ftimecode,'') as ftimecode,
    ifnull(date_format(ifnull(md.fdate, ''),'%d/%m/%Y'),'') as fdate, 
    ifnull(mt.ftime,'') as ftime,
    ifnull(t.fdeleted,'F') as fdeleted  
    from  {$db}.subject s inner join {$db}.degree d on s.fdegree = d.fdegree and s.fexamno = d.fexamno 
    left join {$db}.examtt t on s.fqpcode = t.fqpcode and d.fdeggrp = t.fdeggrp
    left join {$db}.masdate md on t.fdatecode = md.fdatecode 
    left join {$db}.mastime mt on t.ftimecode = mt.ftimecode
    where d.fdeggrp = '{$deggrp}' and s.fexamno in('{$examrange}')
    and s.`FTHEORY` = 'T' and s.fintass <> 'T' and ifnull(s.fqpcode,'') <>''
    group by s.fqpcode";

    $result1 = $aobj_context->mobj_db->GetAll($query);

    $query = "select * from {$db}.mastime where ifnull(fdeleted,'') <> 'T'";
    $result2 = $aobj_context->mobj_db->GetAll($query);

    $query = "select fdatecode, date_format(fdate,'%d/%m/%Y') as  fdate from {$db}.masdate where ifnull(fdeleted,'') <> 'T'";
    $result3 = $aobj_context->mobj_db->GetAll($query);

    //var_dump($result);
    if ($result1) {
        $arr['qpdet'] =  $result1;
        $arr['mastime'] =  $result2;
        $arr['masdate'] =  $result3;

        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
    } else {
        $arr['msg'] = 'Could not get QP List';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
    }
}

function saveExamTimetable($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $req = $aobj_context->req_body;

    $data = $req['data'];

    // var_dump($data);return;

    if (!array_key_exists('timetable', $data) || !array_key_exists('params', $data)) {
        $arr['msg'] = 'Invalid Params';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }

    if (count($data['timetable']) == 0) {
        $arr['msg'] = 'Invalid Params data';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }


    $timetable = $data['timetable'];
    $params = $data['params'];
    $user = $data['user'];

    $fdeggrp = $user['fdeggrp'];
    $fyear = $user['fyear'];
    $fexamtype = $user['fexamtype'];
    $userid = $user['fcurtype']."-".$user['fuserid'];


    $values_tt = '';

    $values_mq = '';

    foreach ($timetable as $k => $v) {
        $values_tt .= "('" . $fdeggrp . "', '" . $fyear . "','" . $fexamtype . "','" 
        . $v['fqpcode'] . "', '" . $v['fdatecodep'] . "', '" . $v['fdatecodet'] . "',
        '" . $v['fsession'] . "', '" . $userid . "',now()),";

        $values_mq .= "('" . $v['fqpcode'] . "', '" . $v['fsubname'] . "', '" . $v['fdatecodep'] . "', '" . $v['fdatecodet'] . "',
        '" . $v['fsession'] . "'),";
    }

    $values_tt = rtrim($values_tt, ',');

    $values_mq = rtrim($values_mq, ',');

    $query = "create temporary table tmpmasqp
    (fqpcode varchar(5) primary key,
    fsubname varchar(200),
    fdatecodep varchar(5),
    fdatecodet varchar(5),
    fsession varchar(5))";

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "insert into timetable(fdeggrp, fyear, fexamtype, fqpcode, fdatecodep, fdatecodet, fsessiont, fuser, fmoddate) 
    values " . $values_tt . "
    ON DUPLICATE KEY UPDATE
    fdatecodep = VALUES(fdatecodep), fdatecodet = VALUES(fdatecodet), fsessiont = VALUES(fsessiont), 
    fuser = VALUES(fuser), fmoddate = VALUES(fmoddate), fdeggrp = VALUES(fdeggrp)";

    // var_dump($query);

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "insert into tmpmasqp(fqpcode, fsubname, fdatecodep, fdatecodet, fsession) values " . $values_mq . "
    ON DUPLICATE KEY UPDATE
    fsubname = VALUES(fsubname), fdatecodep = VALUES(fdatecodep), fdatecodet = VALUES(fdatecodet), 
    fsession = VALUES(fsession)";

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "update subject s, tmpmasqp m
    set s.fdatecodep = m.fdatecodep,
    s.fdatecodet = m.fdatecodet,
    s.fsessiont = m.fsession
    where s.fqpcode = m.fqpcode";
    $result = $aobj_context->pobj_db->Execute($query);

    if ($result) {
        $arr['msg'] = 'Successfully Updated';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
        return;
    } else {
        $arr['msg'] = 'Insertion / Updation Failed';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }
}

function saveExamTimetableUT($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $req = $aobj_context->req_body;

    $data = $req['data'];

    // var_dump($data);return;

    if (!array_key_exists('timetable', $data) || !array_key_exists('params', $data)) {
        $arr['msg'] = 'Invalid Params';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }

    if (count($data['timetable']) == 0) {
        $arr['msg'] = 'Invalid Params data';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }


    $timetable = $data['timetable'];
    $params = $data['params'];
    $user = $data['user'];
// var_dump($params);die();
    $fdeggrp = $params['fdeggrp'];
    $fyear = $params['fyear'];
    $fexamtype = $params['fexamtype'];
    $userid = $user['fcurtype']."-".$user['fuserid'];


    $values_tt = '';

    $values_mq = '';

    foreach ($timetable as $k => $v) {
        $values_tt .= "('" . $fdeggrp . "', '" . $fyear . "','" . $fexamtype . "','" 
        . $v['fqpcode'] . "', '" . $v['fdatecodep'] . "', '" . $v['fdatecodet'] . "',
        '" . $v['fsession'] . "', '" . $userid . "',now()),";

        $values_mq .= "('" . $v['fqpcode'] . "', '" . $v['fsubname'] . "', '" . $v['fdatecodep'] . "', '" . $v['fdatecodet'] . "',
        '" . $v['fsession'] . "'),";
    }

    $values_tt = rtrim($values_tt, ',');

    $values_mq = rtrim($values_mq, ',');

    $query = "create temporary table tmpmasqp
    (fqpcode varchar(5) primary key,
    fsubname varchar(200),
    fdatecodep varchar(5),
    fdatecodet varchar(5),
    fsession varchar(5))";
    //var_dump($query);
    $result = $aobj_context->pobj_db->Execute($query);

    $query = "insert into timetable(fdeggrp, fyear, fexamtype, fqpcode, fdatecodep, fdatecodet, fsessiont, fuser, fmoddate) 
    values " . $values_tt . "
    ON DUPLICATE KEY UPDATE
    fdatecodep = VALUES(fdatecodep), fdatecodet = VALUES(fdatecodet), fsessiont = VALUES(fsessiont), 
    fuser = VALUES(fuser), fmoddate = VALUES(fmoddate), fdeggrp = VALUES(fdeggrp)";

    // var_dump($query);

    $result = $aobj_context->pobj_db->Execute($query);

    // $query = "insert into tmpmasqp(fqpcode, fsubname, fdatecodep, fdatecodet, fsession) values " . $values_mq . "
    // ON DUPLICATE KEY UPDATE
    // fsubname = VALUES(fsubname), fdatecodep = VALUES(fdatecodep), fdatecodet = VALUES(fdatecodet), 
    // fsession = VALUES(fsession)";

    // $result = $aobj_context->pobj_db->Execute($query);

    // $query = "update subject s, tmpmasqp m
    // set s.fdatecodep = m.fdatecodep,
    // s.fdatecodet = m.fdatecodet,
    // s.fsessiont = m.fsession
    // where s.fqpcode = m.fqpcode";
    // $result = $aobj_context->pobj_db->Execute($query);

    if ($result) {
        $arr['msg'] = 'Successfully Updated';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
        return;
    } else {
        $arr['msg'] = 'Insertion / Updation Failed';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }
}

function releaseExamTimetable($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);

    $req = $aobj_context->req_body;

    $data = $req['data'];

    if (!array_key_exists('params', $data)) {
        $arr['msg'] = 'Invalid Params';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }

    $ttparms = $data['params'];

    if (
        $ttparms['fstrdate'] == '' || $ttparms['fdeggrp'] == '' || $ttparms['fexamrange'] == ''
        || $ttparms['fyear'] == '' || $ttparms['fexamtype'] == '' || $ttparms['fdaytype'] == ''
    ) {
        $arr['msg'] = 'Invalid Params values';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }

    $strdate = str_replace('/', '-', $ttparms['fstrdate']);
    // var_dump($strdate);
    $fdeggrp = $ttparms['fdeggrp'];

    $fyear = $ttparms['fyear'];

    $examtype = $ttparms['fexamtype'];

    $exam = str_split($ttparms['fexamrange']);

    $examrange = implode("','", $exam);

    // $query = "update subject s, masdate m
    // set s.fdoe = m.fdate,
    // s.fsession = s.fsessiont
    // where s.fdatecodet = m.fdatecode
    // and s.fdegree in (select distinct fdegree from degree where fdeggrp = '{$fdeggrp}')
    // and s.fexamno in ('{$examrange}')
    // and m.fdeggrp = '{$fdeggrp}' and fyear = '{$fyear}' and fexamtype = '{$examtype}'";

    $query = "update subject s, timetable t, masdate m
    set s.fdoe = m.fdate,
    s.fsession = t.fsessiont
    where t.fdatecodet = m.fdatecode
    and t.fdeggrp  = m.fdeggrp and t.fyear = m.fyear and t.fexamtype = m.fexamtype
    and t.fqpcode = s.fqpcode
    and s.fdegree in (select distinct fdegree from degree where fdeggrp = '{$fdeggrp}')
    and s.fexamno in ('{$examrange}')
    and m.fdeggrp = '{$fdeggrp}' and m.fyear = '{$fyear}' and m.fexamtype = '{$examtype}'";

    $result = $aobj_context->pobj_db->Execute($query);

    if ($result) {
        $arr['msg'] = 'Time Table released to public';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
        return;
    } else {
        $arr['msg'] = 'Error While releasing to public';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }
}


function getdegrefdet($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $univcode = $aobj_context->mobj_data["univcode"];

    $query = "SELECT fdbname, ffolder FROM dbname WHERE funivcode = $univcode";
    $result = $aobj_context->mobj_db->GetRow($query);
    $db = $result['fdbname'];

    $query = "select fdeggrp as code, concat(fdeggrp,' - ',fdescpn) as value 
        from {$db}.deggrp where ifnull(fdeleted,'F') <> 'T'";
    $result = $aobj_context->mobj_db->GetAll($query);
    $arr['deggrp'] = $result;

    $query = "select distinct concat(fyear,'-',fexamtype) as code, concat(fyear,' - ',fexamtype) as value 
        from {$db}.examtt where ifnull(fdeleted,'F') <> 'T'";
    $result = $aobj_context->mobj_db->GetAll($query);
    $arr['reftype'] = $result;

    echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
    return;
}

function getdegreettm($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $univcode = $aobj_context->mobj_data["univcode"];
    $deggrp = $aobj_context->mobj_data["deggrp"];

    $query = "select distinct fdegree, fdescpn from degree 
    where ifnull(fdeggrp,'') = '{$deggrp}'";
    $result = $aobj_context->pobj_db->GetAll($query);
    // var_dump($query);
    // var_dump($result);
    
    if($result){
    echo $aobj_context->mobj_output->ToJSONEnvelope($result, 0, "success");
    return;   
    }else{
        $arr['msg'] = "No degrees found..!";
    echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
    return;
    }
}




function getupdateExamTimetableUT($aobj_context)
{
    $aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
    $univcode = $aobj_context->mobj_data["univcode"];

    $req = $aobj_context->req_body;

    $data = $req['data'];

    if (!array_key_exists('frmvalues', $data)) {
        $arr['msg'] = 'Invalid Params';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }
    // var_dump($data);
    // die();

    $ttparms = $data['frmvalues'];
    // var_dump($ttparms);
    // die();

    if (
        $ttparms['fdeggrp'] == '' || $ttparms['fexamrange'] == ''
        || $ttparms['fyear'] == '' || $ttparms['fexamtype'] == ''
    ) {
        $arr['msg'] = 'Invalid Params values';
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 1, "failure");
        return;
    }

    $deggrp = $ttparms['fdeggrp'];

    $exam = str_split($ttparms['fexamrange']);

    $examrange = implode("','", $exam);

    $query = "drop table if exists tmpttmasqp";

    $result = $aobj_context->pobj_db->Execute($query);

    if($ttparms['fdegree'] == 'All'){
        $deg = "";
    }else{
        $deg = "and fdegree = '{$ttparms['fdegree']}'";

    }
    
    

    $query = "create table tmpttmasqp
    select distinct fqpcode from subject 
    where fdegree in (select fdegree from degree where fdeggrp = '{$deggrp}')
    and fexamno in ('{$examrange}') {$deg}
    and ifnull(fqpcode,'') <> ''";
    // and concat(fdegree,fexamno,fsubcode) in 
    // (select distinct concat(fdegree,fexamno,fsubcode) from canddet 
    // where ifnull(fpassmth,'') = '')
    // var_dump($query);

    $result = $aobj_context->pobj_db->Execute($query);
    // var_dump($ttparms['fdegree'], $query);
    // die();

    $query = "create index qpmaster_inx on tmpttmasqp(fqpcode)";
    $result = $aobj_context->pobj_db->Execute($query);


    $query = "alter table tmpttmasqp add( fsubname varchar(100))";

    $result = $aobj_context->pobj_db->Execute($query);

    $query = "update tmpttmasqp  m, subject s
    set m.fsubname = s.fsubname
    where m.fqpcode = s.fqpcode
    and ifnull(s.fqpcode,'') <> ''";

    $result = $aobj_context->pobj_db->Execute($query);
    
    $query = "";

    if($univcode == '051'){
        $table = "notif_deggrp";
    }else{
        $table = "deggrp";
    }

    $query = "insert ignore into timetable(fdeggrp, fyear, fexamtype, fqpcode)
    select dg.fdeggrp, dg.fyear, dg.fexamtype, s.fqpcode 
    from subject s inner join degree d on s.fdegree = d.fdegree and d.fexamno = s.fexamno
    inner join $table dg on d.fdeggrp = dg.fdeggrp
    where d.fdeggrp = '{$deggrp}'
    and d.fexamno in ('{$examrange}')
    and ifnull(s.fqpcode,'') <> ''
    group by s.fqpcode";
    $res = $aobj_context->pobj_db->Execute($query);

    if($univcode == '030'){
        $query = "select s.fqpcode, concat(sb.fsubname,' (',sb.fsubshort,')') as fsubname, t.fdatecodep, 
        t.fdatecodet, t.fsessiont as fsession,t.ffreeze,t.frelease
        from tmpttmasqp s left join timetable t 
        on s.fqpcode = t.fqpcode 
        inner join deggrp d
        on d.fdeggrp = t.fdeggrp
        and d.fyear = t.fyear and d.fexamtype = t.fexamtype 
        and s.fqpcode = t.fqpcode
        and d.fdeggrp = '{$deggrp}' 
        inner join subject sb on sb.fqpcode = s.fqpcode 
        left join masdate m on t.fdatecodep = m.fdatecode 
        and m.fyear = t.fyear and m.fexamtype = t.fexamtype
        left join reason r on t.fsessionp = r.freasoncd where ifnull(fsuspend,'')<>'T'
        group by s.fqpcode
        order by fdatecodep, s.fqpcode";

    }else{
        $query = "select s.fqpcode, concat(s.fsubname, '-', sub.fssubname)as fsubname, t.fdatecodep, 
        t.fdatecodet, t.fsessiont as fsession,t.ffreeze,t.frelease
        from tmpttmasqp s left join timetable t 
        on s.fqpcode = t.fqpcode 
        inner join $table d
        on d.fdeggrp = t.fdeggrp
        and d.fyear = t.fyear and d.fexamtype = t.fexamtype 
        and s.fqpcode = t.fqpcode
        and d.fdeggrp = '{$deggrp}' 
        left join masdate m on t.fdatecodep = m.fdatecode 
        and m.fyear = t.fyear and m.fexamtype = t.fexamtype
        left join reason r on t.fsessionp = r.freasoncd
        inner join subject sub on s.fqpcode = sub.fqpcode
        group by s.fqpcode
        order by fdatecodep, s.fqpcode";

    }

    

    $timetable = $aobj_context->pobj_db->GetAll($query);

    



    if (count($timetable) == 0) {

        $query = "select s.fqpcode, concat(s.fsubname, '-', sub.fssubname)as fsubname, t.fdatecodep, 
            t.fdatecodet, t.fsessiont as fsession,t.ffreeze,t.frelease
            from tmpttmasqp s left join timetable t 
            on s.fqpcode = t.fqpcode left join deggrp d
            on d.fdeggrp = t.fdeggrp
            and d.fyear = t.fyear and d.fexamtype = t.fexamtype 
            and s.fqpcode = t.fqpcode
            and d.fdeggrp = '{$deggrp}'
            left join masdate m on t.fdatecodep = m.fdatecode 
            and m.fyear = t.fyear and m.fexamtype = t.fexamtype
            left join reason r on t.fsessionp = r.freasoncd
            inner join subject sub on s.fqpcode = sub.fqpcode
            group by s.fqpcode
            order by fdatecodep, s.fqpcode";

        $timetable = $aobj_context->pobj_db->GetAll($query);

        

        if(count($timetable) == 0) {
            $arr['msg'] = "Qpcode not found for degree group $deggrp";
            echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
            return;
        }
        
    }

    $restt = array();
    foreach ($timetable as $key => $val) {
        $restt[$val['fqpcode']] = $val;
    }

    $query = "select freasoncd, fdescpn from reason where ifnull(fdeleted,'') <> 'T' order by freasoncd";
    $reason = $aobj_context->pobj_db->GetAll($query);

    $query = "select fdatecode, date_format(fdate,'%d/%m/%Y') as fdate 
    from masdate where ifnull(fdeleted,'') <> 'T' and fdeggrp = '{$deggrp}'
    and fyear = '{$ttparms['fyear']}' and fexamtype = '{$ttparms['fexamtype']}'
    order by fdatecode";
    $masdate = $aobj_context->pobj_db->GetAll($query);

    if (count($masdate) == 0) {
        $arr['msg'] = "Date codes not found for degree group $deggrp";
        echo $aobj_context->mobj_output->ToJSONEnvelope($arr, -1, "failure");
        return;
    }

    array_push($masdate, (object)[
        'fdatecode' => '',
        'fdate' => '-Select--',
        
    ]);

    array_push($reason, (object)[
        'freasoncd' => '',
        'fdescpn' => '-Select--',
        
    ]);

    

    // $data = "Uploaded Successfully.";
    $arr['reason'] = $reason;
    $arr['masdate'] = $masdate;
    $arr['timetable'] = $timetable;
    echo $aobj_context->mobj_output->ToJSONEnvelope($arr, 0, "success");
    return;
}