<?php
namespace App\Controllers;
SESSION_START();
use \Core\View;
use \Core\Inputs;
use \Core\Userlog;
use \Core\Menu;
use \Core\Form;
use App\Models\Homem;
use App\Email\Emails;
class Timetable extends \Core\Controller
{
    private static function css()
    {
        $css = 
        '<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
            <title>Hindu College of Engineering & Technology || Guntur</title>
            <link rel="icon" href = "https://assets.hinduengg.com/hindu/assets/img/logo.png" type = "image/x-icon"> 
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
            <title>Hindu College of Engineering & Technology || Guntur</title>
            <link rel="stylesheet" href="https://assets.hinduengg.com/hindu/assets/css/bootstrap.min.css">
            <link rel="stylesheet" href="https://assets.hinduengg.com/hindu/assets/css/style.css">
            <link rel="stylesheet" href="https://assets.hinduengg.com/hindu/assets/sweetalert2/sweetalert2.min.css" >
            <style>
                @media print
                {
                    body * {
                        visibility: hidden;
                    }
                    .print-this, .print-this * {
                        visibility: visible;
                    }
                }
            </style>
            <script>
                function exportTableToExcel(tableID, filename = "")
                {
                    var downloadLink;
                    var dataType = "application/vnd.ms-excel";
                    var tableSelect = document.getElementById(tableID);
                    var tableHTML = tableSelect.outerHTML.replace(/ /g, "%20");
                    
                    // Specify file name
                    filename = filename?filename+".xls":"excel_data.xls";
                    
                    // Create download link element
                    downloadLink = document.createElement("a");
                    
                    document.body.appendChild(downloadLink);
                    
                    if(navigator.msSaveOrOpenBlob){
                        var blob = new Blob(["\ufeff", tableHTML], {
                            type: dataType
                        });
                        navigator.msSaveOrOpenBlob( blob, filename);
                    }else{
                        // Create a link to the file
                        downloadLink.href = "data:" + dataType + ", " + tableHTML;
                    
                        // Setting the file name
                        downloadLink.download = filename;
                        
                        //triggering the function
                        downloadLink.click();
                    }
                }
            </script>
        </head>';
        return $css;  
    }
    private static function script()
    {
        $script =
                '
                <div class="footer navbar-fixed-bottom text-center">        
                    <span class="text-muted text-center">Copyright &#169 2020. Hindu College of Engineering & Technology, Guntur</span>
                </div>
                <script src="https://assets.hinduengg.com/hindu/assets/word/jquery.min.js" ></script>
                <script src="https://assets.hinduengg.com/hindu/assets/js/bootstrap.min.js" ></script>
                <script src="https://assets.hinduengg.com/hindu/assets/sweetalert2/dist/sweetalert2.min.js" ></script>
                <!-- Script to stop mouse right click start-->
            <script>
                //Disable right click script
                var message = "";
                ///////////////////////////////////
                function clickIE() {
                    if (document.all) {
                        (message);
                        return false;
                    }
                }

                function clickNS(e) {
                    if (document.layers || (document.getElementById && !document.all)) {
                        if (e.which == 2 || e.which == 3) {
                            (message);
                            return false;
                        }
                    }
                }
                if (document.layers) {
                    document.captureEvents(Event.MOUSEDOWN);
                    document.onmousedown = clickNS;
                } else {
                    document.onmouseup = clickNS;
                    document.oncontextmenu = clickIE;
                }

                document.oncontextmenu = new Function("return false")
            </script>
            <!-- Script to stop mouse right click end-->

            <!-- Script to prevent selection start-->
            <script>
                $(document).ready(function () {
                    $("body").bind("cut copy", function (e) {
                        e.preventDefault();
                    });
                });
            </script>
            <!-- Script to prevent selection end-->
                ';
        return $script;  
    }
    private static function sscript()
    {
        $script =
                '
                <div class="footer navbar-fixed-bottom text-center">        
                    <span class="text-muted text-center">Copyright &#169 2020. Hindu College of Engineering & Technology, Guntur</span>
                </div>
                <script src="https://assets.hinduengg.com/hindu/assets/word/jquery.min.js" ></script>
                <script src="https://assets.hinduengg.com/hindu/assets/js/bootstrap.min.js" ></script>
                <script src="https://assets.hinduengg.com/hindu/assets/sweetalert2/dist/sweetalert2.min.js" ></script>
                ';
        return $script;  
    }
    private static function year()
    {
        $year = date('Y')+5;
        $a = '';
        for($i=0; $i<=40; $i++)
        {
            $y = $year-$i;
            $a .= '<option value="'.$y.'">'.$y.'</option>';
        }
        return $a;
    }
    private static function getdaybydate($date)
    {
        $dates = strtotime($date);
        $date = date('l', $dates);
        return $date;
    }
    public static function datesinmonth()
    {
        $year = date('Y');
        $lyear = $year%4;
        if($lyear == 0)
        {
            $leap = 1;
        }
        else
        {
            $leap = 0;
        }
        $mdays =  static::monthdays(date('m'), $leap);
        for($i=0; $i<$mdays; $i++)
        {
            $j = $i+1;
            if($j < 10)
            {
                $date = date('Y').'-'.date('m').'-'.sprintf("%02d", $j);
                $dates[$i]['date'] = $date;
                $dates[$i]['day'] = static::getdaybydate($date);
            }
            else
            {
                $date = date('Y').'-'.date('m').'-'.$j;
                $dates[$i]['date'] = $date;
                $dates[$i]['day'] = static::getdaybydate($date);
            }
        }
        return $dates;
    }
    public static function monthdays($month, $leap)
    {
        if($month == 1)
        {
            return 31;
        }
        else if($month == 2 && $leap ==0)
        {
            return 28;
        }
        else if($month == 2 && $leap == 1)
        {
            return 29;
        }
        else if ($month == 3)
        {
            return 31;
        }
        else if ($month == 4)
        {
            return 30;
        }
        else if ($month == 5)
        {
            return 31;
        }
        else if ($month == 6)
        {
            return 30;
        }
        else if ($month == 7)
        {
            return 31;
        }
        else if ($month == 8)
        {
            return 31;
        }
        else if ($month == 9)
        {
            return 30;
        }
        else if ($month == 10)
        {
            return 31;
        }
        else if ($month == 11)
        {
            return 30;
        }
        else
        {
            return 31;
        }
    }
    public static function filehandler($file_name, $file_tmp)
    {
        $file_ext1 = explode('.', $file_name);
        $file_ext = $file_ext1[sizeof($file_ext1)-1];
        $data = file_get_contents($file_tmp);
        $base64 = 'data:image/' . $file_ext . ';base64,' . base64_encode($data);
        $img = '<img src="'.$base64.'" style="max-width:100%; height: auto;">';
        return $img;
    }
    
    
    /** Starting of Time Table master - workingdays() start*/
    public static function workingdays()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            $data = '
            <div class="table-responsive">
                <table class="table table-responsive-sm">
                    <thead>
                        <tr>
                            <th><input type="checkbox" name="allworking" id="allworking" onchange="setallpresent()">Working days</th>
                            <th>Date</th>
                            <th>Day</th>
                        </tr>
                    </thead>
                    <tbody>';
            $date = static::datesinmonth();
            for($i=0; $i<sizeof($date); $i++)
            {
                $data .= '<tr><td><input type="hidden" name="work['.$i.']" id="work['.$i.']" value="0"> <input type="checkbox" name="work['.$i.']" id="work['.$i.']" value="1"></td>';
                $data .= '<td><input type="hidden" name="date['.$i.']" id="date['.$i.']" value="'.$date[$i]['date'].'">'.$date[$i]['date'].'</td>';
                $data .= '<td>'.$date[$i]['day'].'</td>';
                $data .= '</tr>';
            }
            $data .= '</tbody></table>';
            echo static::css();
            echo '<body>';
                echo $_SESSION['menu'];
                echo '
                <div class="clearfix"></div>
                <form action="/Timetable/saveworkingdays" method="POST">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-3"></div>
                        <div class="col-lg-6">                                       
                            <div class="row">
                                <div class="col-lg-12">
                                    <section class="box" style="margin-top: 30px;">
                                        <header class="panel_header">
                                            <h4 class="title pull-left">ADD WORKING DAYS</h4>
                                            <div class="actions panel_actions pull-right">
                                                <a class="fa fa-refresh" onclick="refreshClick()"></a>
                                                <a class="box_toggle fa fa-chevron-down"></a>
                                            </div>
                                        </header>
                                        <div class="content-body">    
                                            <div class="row">
                                                <div class="col-lg-12">
                                                    <div class="form-group">
                                                        <input type="hidden" name="dcount" id="dcount" value="'.sizeof($date).'">                                            
                                                        '.$data.'
                                                    </div>
                                                </div>  										
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-12">
                                                    <div class="form-group">                                            
                                                        <input type="hidden" name="edit_id" id="edit_id" class="form-control" placeholder="Edit ID">
                                                    </div>
                                                </div>  										
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12">  
                                                    <div class="form-group">
                                                        <button id="btn_reg" class="btn btn-primary pull-right" type="submit" style="background-color: #002366 !important;">Submit</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>                        
                                    </section>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3"></div>
                    </div>
                </div>
                </form>
                ';
            echo '</body>';
            echo static::script();
            $cdate = date('Y-m-');
            $mdata = (Homem::getdata("SELECT dates, work FROM `tt_workdays` WHERE `dates` LIKE '%$cdate%'"));
            if(sizeof($mdata) > 0)
            {
                echo '
                    <script>
                    var data= '.json_encode($mdata).';
                    for(var i=0; i<data.length; i++)
                    {
                        // console.log(data[i]["dates"]);
                        var a = "work["+i+"]";
                        var ele=document.getElementsByName(a);
                        for(var j=0; j<ele.length; j++)
                        {  
                            if(ele[j].type=="checkbox" && data[i]["work"]==1)  
                            {
                                ele[j].checked=true;
                            }
                            if(ele[j].type=="checkbox" && data[i]["work"]==0) 
                            {
                                ele[j].checked=false;
                            }
                            if(ele[j].type!="checkbox")
                            {
                                var newval = ele[j].value;
                            }
                        }
                    }
                    </script>
                ';
            }
            echo '
            <script type="text/javascript">  
                $(document).ready(function() {
                });
                function setallpresent()
                {
                    var present = document.getElementById("allworking");
                    if(present.checked == true)
                    {
                        selects();
                    }
                    else
                    {
                        deselects();
                    }
                }
                function selects()
                {  
                    var scount = document.getElementById("dcount");
                    for(var i=0; i<scount.value; i++)
                    { 
                        var a = "work["+i+"]";
                        var ele=document.getElementsByName(a);
                        for(var j=0; j<ele.length; j++)
                        {  
                            if(ele[j].type=="checkbox")  
                            {
                                ele[j].checked=true;
                            }
                        }
                    }
                }   
                function deselects()
                {  
                    var scount = document.getElementById("dcount");
                    for(var i=0; i<scount.value; i++)
                    { 
                        var a = "work["+i+"]";
                        var ele=document.getElementsByName(a);
                        for(var j=0; j<ele.length; j++)
                        {  
                            if(ele[j].type=="checkbox")  
                            {
                                ele[j].checked=false;
                            }
                        }
                    }
                }          
            </script>  
            ';
            if(isset($_GET['editid']))
            {
                echo '';
            }
            if(isset($_GET['status']))
            {
                if($_GET['status'] == 1)
                {
                    echo '<script>swal("", "Inserted Successfully", "success");</script>';
                }
                else if($_GET['status'] == 2)
                {
                    echo '<script>swal("", "Updated Successfully", "success");</script>';
                }
                else
                {
                    echo '<script>swal("", "Something went Wrong.. Please try again later..", "error");</script>';
                }
            }
        }
    }
    public static function saveworkingdays()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            $work = array();
            $work = $_POST['work'];
            $date = array();
            $date = $_POST['date'];
            for($i=0; $i<sizeof($_POST['work']); $i++)
            {
                if(sizeof(Homem::getdata("SELECT * FROM `tt_workdays` WHERE `dates`='$date[$i]'")) > 0)
                {
                    $status = Homem::insert("UPDATE `tt_workdays` SET `dates`='$date[$i]', `work`='$work[$i]', `fk_user_id`='$_SESSION[user_id]' WHERE `dates`='$date[$i]'");
                }
                else
                {
                    $status = Homem::insert("INSERT INTO `tt_workdays`(`dates`, `work`, `fk_user_id`) VALUES ('$date[$i]', '$work[$i]', '$_SESSION[user_id]')");
                }
            }
            echo '<script>window.location = "/Timetable/workingdays?status=1";</script>';
        }
    }
    /** Ending of Time Table master - workingdays() end*/
    
    
    /** Starting of Time Table master - createshift() start*/
    public static function createshift()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            $data = Homem::getdata("SELECT * FROM `tt_slotsmaster` WHERE `status`='1'");
            $table = Form::dbtableed($data, ['Shift Name', 'No of Hours', 'From Time', 'To Time'], ['s_name', 'noofhours', 'fromtime', 'totime'], '/Timetable/createshift', 's_id', '');
            
            echo static::css();
            echo '<body>';
                echo $_SESSION['menu'];
                echo '
                <div class="clearfix"></div>
                <form action="/Timetable/savecreateshift" method="POST">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-3"></div>
                        <div class="col-lg-6">                                       
                            <div class="row">
                                <div class="col-lg-12">
                                    <section class="box" style="margin-top: 30px;">
                                        <header class="panel_header">
                                            <h4 class="title pull-left">ADD WORKING DAY TIME SLOTS</h4>
                                            <div class="actions panel_actions pull-right">
                                                <a class="fa fa-refresh" onclick="refreshClick()"></a>
                                                <a class="box_toggle fa fa-chevron-down"></a>
                                            </div>
                                        </header>
                                        <div class="content-body">    
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <div class="form-group">
                                                        <input type="text" name="name" id="name" value="" placeholder="Shift Name" class="form-control" required>                                            
                                                    </div>
                                                </div>  	
                                                <div class="col-lg-6">
                                                    <div class="form-group">
                                                        <select name="hours" id="hours" class="form-control" onchange="getslot()" required>
                                                            <option value="">Select Number of Hours including Lunch & Breaks</option>
                                                            <option value="1">One Hour</option>
                                                            <option value="2">Two Hours</option>
                                                            <option value="3">Three Hours</option>
                                                            <option value="4">Four Hours</option>
                                                            <option value="5">Five Hours</option>
                                                            <option value="6">Six Hours</option>
                                                            <option value="7">Seven Hours</option>
                                                            <option value="8">Eight Hours</option>
                                                            <option value="9">Nine Hours</option>
                                                            <option value="10">Ten Hours</option>
                                                            <option value="11">Eleven Hours</option>
                                                            <option value="12">Twelve Hours</option>
                                                            <option value="13">Thirteen Hours</option>
                                                            <option value="14">Fourteen Hours</option>
                                                            <option value="15">Fifteen Hours</option>
                                                        </select>                                            
                                                    </div>
                                                </div> 									
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <div class="form-group">
                                                        <input type="text" name="fromtime" id="fromtime" onfocus="(this.type=`time`)" onblur="(this.type=`text`)" placeholder="From Time" class="form-control" required>                                            
                                                    </div>
                                                </div> 
                                                <div class="col-lg-6">
                                                    <div class="form-group">
                                                        <input type="text" name="totime" id="totime" min="09:00" max="18:00" onfocus="(this.type=`time`)" onblur="(this.type=`text`)" placeholder="To Time" class="form-control" required>                                            
                                                    </div>
                                                </div> 										
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-12">
                                                    <div class="form-group">                                            
                                                        <div id="getslots">
                                                        </div>
                                                    </div>
                                                </div>  										
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-12">
                                                    <div class="form-group">                                            
                                                        <input type="hidden" name="edit_id" id="edit_id" class="form-control" placeholder="Edit ID">
                                                    </div>
                                                </div>  										
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12">  
                                                    <div class="form-group">
                                                        <button id="btn_reg" class="btn btn-primary pull-right" type="submit" style="background-color: #002366 !important;">Submit</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>                        
                                    </section>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3"></div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3"></div>
                        <div class="col-lg-6">'.$table.'</div>
                        <div class="col-lg-3"></div>
                    </div>
                </div>
                </form>
                ';
            echo '</body>';
            echo static::script();
            echo '
            <script>
                function getslot()
                {
                    var hours = document.getElementById("hours").value;
                    $.ajax({
                        type: "POST",
                        data : {hours: hours},
                        url: "/Timetable/getslots",
                        success: function(d){
                            $("#getslots").html(d);
                        }
                    });
                    //getslots
                }
            </script>';
            echo '
            <script type="text/javascript">  
                $(document).ready(function() {
                });
                function setallpresent()
                {
                }        
            </script>  
            ';
            
            if(isset($_GET['editid']))
            {
                if(isset($_GET['editid']))
                {
                    for($i=0; $i<sizeof($data); $i++)
                    {
                        if($_GET['editid'] == $data[$i]['s_id'])
                        {
                            echo '
                            <script type="text/javascript">  
                                $("#edit_id").val("'.$data[$i]['s_id'].'");
                                $("#name").val("'.$data[$i]['s_name'].'");
                                $("#hours").val("'.$data[$i]['noofhours'].'");
                                $("#fromtime").val("'.$data[$i]['fromtime'].'");
                                $("#totime").val("'.$data[$i]['totime'].'");
                            </script>  
                            ';
                        }
                    }
                }
            }
            if(isset($_GET['status']))
            {
                if($_GET['status'] == 1)
                {
                    echo '<script>swal("", "Inserted Successfully", "success");</script>';
                }
                else if($_GET['status'] == 2)
                {
                    echo '<script>swal("", "Updated Successfully", "success");</script>';
                }
                else
                {
                    echo '<script>swal("", "Something went Wrong.. Please try again later..", "error");</script>';
                }
            }
        }
    }
    public static function savecreateshift()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            if(sizeof(Homem::getdata("SELECT * FROM `tt_slotsmaster` WHERE `s_name`='$_POST[name]'")) ==0)
            {
                Homem::insert("INSERT INTO `tt_slotsmaster` (`s_id`, `s_name`, `noofhours`, `fromtime`, `totime`, `add_by`, `status`) VALUES (NULL, '$_POST[name]', '$_POST[hours]', '$_POST[fromtime]', '$_POST[totime]', '$_SESSION[user_id]', '1')");
                $data = Homem::getdata("SELECT * FROM `tt_slotsmaster` WHERE `s_name`='$_POST[name]' AND `add_by`='$_SESSION[user_id]'");
                $id = $data[0]['s_id'];
                $h = array();
                $h = $_POST['hourno'];
                $f = array();
                $f = $_POST['ftime'];
                $t = array();
                $t = $_POST['ttime'];
                $date = date('Y-m-d');
                for($i=0; $i<sizeof($h); $i++)
                {
                    if(sizeof(Homem::getdata("SELECT * FROM `tt_slottime` WHERE `fk_slot_id`='$id' AND `hour_no`='$h[$i]' AND `status`='1'")) ==0)
                    {
                        Homem::insert("INSERT INTO `tt_slottime` (`id`, `fk_slot_id`, `hour_no`, `f_time`, `t_time`, `add_by`, `edate`, `status`) VALUES (NULL, '$id', '$h[$i]', '$f[$i]', '$t[$i]', '$_SESSION[user_id]', '$date', '1')");
                    }
                }
                echo '<script>window.location = "/Timetable/createshift?status=1";</script>';
            }
            else
            {
                echo '<script>window.location = "/Timetable/createshift?status=0";</script>';
            }
        }
    }
    public static function getslots()
    {
        if(isset($_POST['hours']))
        {
            echo '
                <div class="table-responsive">
                    <table class="table table-responsive-sm">
                        <thead>
                            <tr>
                                <th>Hour Number</th>
                                <th>From Time</th>
                                <th>To Time</th>
                            </tr>
                        </thead>
                        <tbody>';
            for($i=0; $i<$_POST['hours']; $i++)
            {
                $j = $i+1;
                echo '<tr>';
                    echo '<td><input type="hidden" name="hourno['.$i.']" id="hourno['.$i.']" value="'.$j.'" class="form-control">'.$j.'</td>';
                    echo '<td><input type="text" name="ftime['.$i.']" id="ftime['.$i.']" onfocus="(this.type=`time`)" onblur="(this.type=`text`)" placeholder="From Time" class="form-control"></td>';
                    echo '<td><input type="text" name="ttime['.$i.']" id="ttime['.$i.']" onfocus="(this.type=`time`)" onblur="(this.type=`text`)" placeholder="To Time" class="form-control"></td>';
                echo '</tr>';
            }
            echo '</tbody></thead>';
        }
    }
    /** Starting of Time Table master - createshift() end*/



    public static function createtimetable()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            $course = Inputs::optionsdb(Homem::getdata("SELECT * FROM `degree_name_master`"), 'id', 'code');
            $dept = Inputs::optionsdb(Homem::getdata("SELECT * FROM `degree_dept_master`"), 'id', 'name');
            $slot = Inputs::optionsdb(Homem::getdata("SELECT * FROM `tt_slotsmaster` WHERE `status`='1'"), 's_id', 's_name');
            echo static::css();
            echo '<body>';
            echo $_SESSION['menu'];
            echo '
                <div class="clearfix"></div>
                <form action="/Timetable/savecreatetimetable" method="POST">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-lg-2"></div>
                            <div class="col-lg-8">                                       
                                <div class="row">
                                    <div class="col-lg-12">
                                        <section class="box" style="margin-top: 10px;">
                                            <header class="panel_header">
                                                <h4 class="title pull-left">CREATE TIME TABLE</h4>
                                            </header>
                                            <div class="content-body"> 
                                                <div class="row">
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="course" id="course" class="form-control" onchange="getstdjoinyear()">
                                                            <option value="">Course</option>
                                                            ';
                                                            echo $course;
                                                            echo '
                                                            </select>
                                                        </div>
                                                    </div>    
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="dept" id="dept" class="form-control" onchange="getregulation()">
                                                            <option value="">Select Department</option>
                                                            </select>
                                                        </div>
                                                    </div> 
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="regulation" id="regulation" class="form-control" >
                                                            <option value="">Select Regulation</option>
                                                            </select>
                                                        </div>
                                                    </div>                           
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="year" id="year" class="form-control">
                                                            <option value="">Select Year</option>
                                                            <option value="1">First Year</option>
                                                            <option value="2">Second Year</option>
                                                            <option value="3">Third Year</option>
                                                            <option value="4">Fourth Year</option>
                                                            </select>
                                                        </div>
                                                    </div>   
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="sem" id="sem" class="form-control" onchange="getstudentyear()">
                                                                <option value="">Select Semester</option>
                                                                <option value="1">First Sem</option>
                                                                <option value="2">Second Sem</option>
                                                            </select>
                                                        </div>
                                                    </div> 
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                        <select name="syear" id="syear" class="form-control" onchange="getstudentdetails()">
                                                            <option value="">Select Students Joining Year</option>
                                                        </select>
                                                        </div>
                                                    </div> 										
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="slot" id="slot" class="form-control" onchange="getttslotdetails()">
                                                                <option value="">Select Time Slot</option>
                                                                '.$slot.'
                                                            </select>
                                                            <input type="hidden" name="edit_id" id="edit_id" class="form-control" placeholder="Edit ID">
                                                        </div>
                                                    </div>
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <input type="text" name="noofhours" id="noofhours" class="form-control" placeholder="No of hours" readonly>
                                                        </div>
                                                    </div> 
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <input type="text" name="fromtime" id="fromtime" class="form-control" onfocus="(this.type=`time`)" onblur="(this.type=`time`)" placeholder="From Time" required readonly>
                                                        </div>
                                                    </div>  										
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <input type="text" name="totime" id="totime" class="form-control" placeholder="To Time" readonly>
                                                        </div>
                                                    </div>
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <input type="text" name="sdate" id="sdate" class="form-control" onfocus="(this.type=`date`)" onblur="(this.type=`text`)" placeholder="Start Date" required="">
                                                        </div>
                                                    </div> 
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <input type="text" name="edate" id="edate" class="form-control" onfocus="(this.type=`date`)" onblur="(this.type=`text`)" placeholder="End Date" required="">
                                                        </div>
                                                    </div>  										
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div id="timetable" class="timetable">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div id="students" class="students">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-12">  
                                                        <div class="form-group">
                                                        <!--
                                                            <button id="btn_reg" class="btn btn-primary pull-right" type="submit" style="background-color: #002366 !important;">Submit</button>
                                                        -->
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>                        
                                        </section>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-2"></div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1"></div>
                            <div class="col-lg-10">
                            </div>
                            <div class="col-lg-1"></div>
                        </div>
                    </div>
                </form>
                ';
            echo '</body>';
            echo static::script();
            echo '
            <script>
                function getstdjoinyear()
                {
                    var course = document.getElementById("course").value;
                    var dept = document.getElementById("dept").value;
                    $.ajax({
                        type: "POST",
                        data : {course: course},
                        url: "/Timetable/getstdjoinyear",
                        success: function(d){
                            $("#dept").html(d);
                            
                        }
                    });
                }
                function getregulation()
                {
                    var course = document.getElementById("course").value;
                    var dept = document.getElementById("dept").value;
                    $.ajax({
                        type: "POST",
                        data : {course: course, dept: dept},
                        url: "/Timetable/getregulation",
                        success: function(d){
                            $("#regulation").html(d);
                            
                        }
                    });
                }
                function getsubjects()
                {
                    var course = document.getElementById("course").value;
                    var dept = document.getElementById("dept").value;
                    var regulation = document.getElementById("regulation").value;
                    var year = document.getElementById("year").value;
                    var sem = document.getElementById("sem").value;
                    $.ajax({
                        type: "POST",
                        data : {course: course, dept: dept, regulation: regulation, year: year, sem: sem},
                        url: "/Timetable/getsubjectsfortt",
                        success: function(d){
                            $("#subject").html(d);
                            console.log(d);
                        }
                    });
                }
                function getfaculty()
                {
                    var fdept = document.getElementById("fdept").value;
                    $.ajax({
                        type: "POST",
                        data : {fdept: fdept},
                        url: "/Timetable/getfaculty",
                        success: function(d){
                            $("#faculty").html(d);
                            
                        }
                    });
                }
                function getstudentyear()
                {
                    var course = document.getElementById("course").value;
                    var dept = document.getElementById("dept").value;
                    var regulation = document.getElementById("regulation").value;
                    $.ajax({
                        type: "POST",
                        data : {course: course, dept: dept, regulation: regulation},
                        url: "/Timetable/getstudentyear",
                        success: function(d){
                            $("#syear").html(d);
                        }
                    });
                }
                function getstudentdetails()
                {
                    var course = document.getElementById("course").value;
                    var dept = document.getElementById("dept").value;
                    var regulation = document.getElementById("regulation").value;
                    var syear = document.getElementById("syear").value;
                    $.ajax({
                        type: "POST",
                        data : {course: course, dept: dept, regulation: regulation, syear: syear},
                        url: "/Timetable/getstudentdetails",
                        success: function(d){
                            $("#students").html(d);
                        }
                    });
                }
                function getttslotdetails()
                {
                    var fdept = document.getElementById("slot").value;
                    $.ajax({
                        type: "POST",
                        data : {slot: fdept},
                        url: "/Timetable/getttslotdetails",
                        success: function(d){
                            // $("#faculty").html(d);
                            var obj = JSON.parse(d);
                            $("#noofhours").val(obj.length);
                            $("#fromtime").val(obj[0]["fromtime"]);
                            $("#totime").val(obj[0]["totime"]);
                            getemptytimetable();
                        }
                    });
                }
                function getemptytimetable()
                {
                    var course = document.getElementById("course").value;
                    var dept = document.getElementById("dept").value;
                    var regulation = document.getElementById("regulation").value;
                    var year = document.getElementById("year").value;
                    var sem = document.getElementById("sem").value;
                    var fdept = document.getElementById("slot").value;
                    $.ajax({
                        type: "POST",
                        data : {slot: fdept, course: course, dept: dept, regulation: regulation, year: year, sem: sem},
                        url: "/Timetable/getemptytimetable",
                        success: function(d){
                            $("#timetable").html(d);
                        }
                    });
                }

            </script>
            ';
            if(isset($_GET['status']))
            {
                if($_GET['status'] == 1)
                {
                    echo '<script>swal("", "Inserted Successfully", "success");</script>';
                }
                else if($_GET['status'] == 2)
                {
                    echo '<script>swal("", "Updated Successfully", "success");</script>';
                }
                else
                {
                    echo '<script>swal("", "Something went Wrong.. Please try again later..", "error");</script>';
                }
            }
        }
    }
    public static function getstdjoinyear()
    {
        if(isset($_POST['course']))
        {
            $data = Homem::getdata("SELECT ddr.dept_id, ddm.name FROM degree_name_master dnm JOIN degree_dept_relation ddr ON dnm.id=ddr.course_id JOIN degree_dept_master ddm ON ddr.dept_id=ddm.id WHERE ddr.course_id='$_POST[course]'");
            echo '<option value="">Select Department</option>';
            echo Inputs::optionsdb($data, 'dept_id', 'name');
        }
    }
    public static function getregulation()
    {
        if(isset($_POST['course']) && isset($_POST['dept']))
        {
            $data = Homem::getdata("SELECT DISTINCT regulation FROM `subject_data` WHERE `course`='$_POST[course]' AND `dept`='$_POST[dept]'");
            echo '<option value="">Select Regulation</option>';
            echo Inputs::optionsdb($data, 'regulation', 'regulation');
        }
    }
    public static function getstudentyear()
    {
        if(isset($_POST['course']) && isset($_POST['dept']))
        {
            $data = Homem::getdata("SELECT DISTINCT `fromyear` FROM `user_master` WHERE `fk_course_id`='$_POST[course]' AND `fk_dept_id`='$_POST[dept]' AND `org_id`='$_SESSION[org_id]' AND `user_role`='3' AND `regulation`='$_POST[regulation]' ORDER BY `fromyear` ASC");
            echo '<option value="">Select Students Joining Year</option>';
            echo Inputs::optionsdb($data, 'fromyear', 'fromyear');
        }
    }
    public static function getstudentdetails()
    {
        if(isset($_POST['course']) && isset($_POST['dept']))
        {
            $data = Homem::getdata("SELECT * FROM `user_master` WHERE `org_id`='$_SESSION[org_id]' AND `fk_course_id`='$_POST[course]' AND `fk_dept_id`='$_POST[dept]' AND `regulation`='$_POST[regulation]' AND `fromyear`='$_POST[syear]' ORDER BY `no` ASC");
            echo '<h6>List of students in the class: (Number of students: '.sizeof($data).')</h6>';
            echo Form::dbtableed($data, ['Hall Ticket Number', 'Name', 'Gender'], ['no', 'name', 'gender'], '', '', '');
        }
    }
    public static function getsubjectsfortt()
    {
        if(isset($_POST['course']) && isset($_POST['dept']) && isset($_POST['year']) && isset($_POST['sem']))
        {
            $data = Homem::getdata("SELECT sub_id, subname FROM `subject_data` WHERE `course`='$_POST[course]' AND `dept`='$_POST[dept]' AND `regulation`='$_POST[regulation]' AND `year`='$_POST[year]' AND `sem`='$_POST[sem]' UNION SELECT id, name FROM `tt_extrasubj`");
            echo '<option value="">Select Subject</option>';
            echo Inputs::optionsdb($data, 'sub_id', 'subname');
        }
    }
    public static function getttslotdetails()
    {
        if(isset($_POST['slot']))
        {
            $data = Homem::getdata("SELECT * FROM tt_slotsmaster ttm JOIN tt_slottime ttt ON ttm.s_id=ttt.fk_slot_id WHERE ttt.fk_slot_id='$_POST[slot]'");
            if(sizeof($data) == $data[0]['noofhours'])
            {
                echo json_encode($data);
            }
        }
    }
    public static function getemptytimetable()
    {
        if(isset($_POST['slot']))
        {
            $data = Homem::getdata("SELECT sub_id, subname FROM `subject_data` WHERE `course`='$_POST[course]' AND `dept`='$_POST[dept]' AND `regulation`='$_POST[regulation]' AND `year`='$_POST[year]' AND `sem`='$_POST[sem]' UNION SELECT id, name FROM `tt_extrasubj`");
            $option = '<option value="">Select Subject</option>';
            $option .= Inputs::optionsdb($data, 'sub_id', 'subname');
            $tdata = Homem::getdata("SELECT * FROM `tt_days`");
            $data = Homem::getdata("SELECT * FROM tt_slotsmaster ttm JOIN tt_slottime ttt ON ttm.s_id=ttt.fk_slot_id WHERE ttt.fk_slot_id='$_POST[slot]' ORDER BY ttt.hour_no ASC");
            if(sizeof($data) == $data[0]['noofhours'])
            {
                $width = number_format(100/(sizeof($data)+1), 2);
                $width = 'width="'.$width.'%"';
                $table = '';
                $tbody = '<thead><tr>';
                for($i=0; $i<sizeof($data); $i++)
                {
                    if($i == 0)
                    {
                        $tbody .= '<td '.$width.'> Days & Timings</td>';
                    }
                    $tbody .= '<td '.$width.'>'.$data[$i]['f_time'].'-'.$data[$i]['t_time'].'</td>';
                }
                $tbody .= '</tr></thead><tbody>';
                for($i=0; $i<sizeof($tdata); $i++)
                {
                    $table .= '<tr><td '.$width.'>'.$tdata[$i]['name'].'</td>';
                    for($j=0; $j<sizeof($data); $j++)
                    {
                        $a = $i+1;
                        $b = $j+1;
                        $table .= '<td '.$width.'><select class="form-control" id="subj['.$a.']['.$b.']" name="subj['.$a.']['.$b.']" required>'.$option.'</select></td>';
                    }
                    $table .= '</tr>';
                }
                $table .= '</tbody>';
            }
        }
        echo '<table border="1px" width="100%">
        '.$tbody.$table.'
        </table>';
    }
    public static function savecreatetimetable()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            $date = date('Y-m-d');
            if(sizeof(Homem::getdata("SELECT * FROM `tt_regularmain` WHERE `fk_course_id`='$_POST[course]' AND `fk_dept_id`='$_POST[dept]' AND `regulation`='$_POST[regulation]' AND `year`='$_POST[year]' AND `sem`='$_POST[sem]' AND `joinyear`='$_POST[syear]' AND `status`='1'")) == 0)
            {
                Homem::insert("INSERT INTO `tt_regularmain` (`tm_id`, `fk_course_id`, `fk_dept_id`, `regulation`, `year`, `sem`, `joinyear`, `fk_ttslot_id`, `slot_hours`, `sdate`, `edate`, `add_by`, `add_date`, `status`) VALUES (NULL, '$_POST[course]', '$_POST[dept]', '$_POST[regulation]', '$_POST[year]', '$_POST[sem]', '$_POST[syear]', '$_POST[slot]', '$_POST[noofhours]', '$_POST[sdate]', '$_POST[edate]', '$_SESSION[user_id]', '$date', '1')");
                $ndata = Homem::getdata("SELECT * FROM `tt_regularmain` WHERE `fk_course_id`='$_POST[course]' AND `fk_dept_id`='$_POST[dept]' AND `regulation`='$_POST[regulation]' AND `year`='$_POST[year]' AND `sem`='$_POST[sem]' AND `joinyear`='$_POST[syear]' AND `status`='1' AND `add_by`='$_SESSION[user_id]' AND `add_date`='$date' ORDER BY `tm_id` DESC");
                if(sizeof($ndata) > 0)
                {
                    $id = $ndata[0]['tm_id'];
                    $subj = $_POST['subj'];
                    $k = 0;
                    for($i=1; $i<=sizeof($subj); $i++)
                    {
                        for($j=1; $j<=$_POST['noofhours']; $j++)
                        {
                            $k++;
                            $a = $subj[$i][$j]; //Subj ID
                            Homem::insert("INSERT INTO `tt_regularsub` (`fk_ttregmain_id`, `fk_slot_id`, `day_id`, `hour_no`, `fk_sub_id`) VALUES ('$id', '$_POST[slot]', '$i', '$j', '$a')");
                        }
                    }
                }
                echo '<script>window.location = "/Timetable/createtimetable?status=1";</script>';
            }
            else
            {
                echo '<script>window.location = "/Timetable/createtimetable?status=0";</script>';
            }
        }
    }

    public static function requestanhour()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6 || $_SESSION['user_role'] == 2)
        {
            $fdept = Inputs::optionsdb($data = Homem::getdata("SELECT * FROM (SELECT DISTINCT `fk_dept_id` FROM `user_master` WHERE (`user_role`='2' OR `user_role`='7' OR `user_role`='8') AND `org_id`='$_SESSION[org_id]') a JOIN degree_dept_master ddm ON a.fk_dept_id=ddm.id"), 'id', 'name');
            $subj = Inputs::optionsdb(Homem::getdata("SELECT * FROM subject_faculty sf JOIN subject_data sd ON sf.fk_sub_id=sd.sub_id WHERE sf.fk_user_id='$_SESSION[user_id]' AND sf.status='1' AND sf.completed='0'"), 'sub_id', 'subname');
            echo static::css();
            echo '<body>';
            echo $_SESSION['menu'];
            echo '
                <div class="clearfix"></div>
                <form action="/Timetable/saverequestanhour" method="POST">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-lg-2"></div>
                            <div class="col-lg-8">                                       
                                <div class="row">
                                    <div class="col-lg-12">
                                        <section class="box" style="margin-top: 10px;">
                                            <header class="panel_header">
                                                <h4 class="title pull-left">HOUR ADJUSTMENTS</h4>
                                            </header>
                                            <div class="content-body"> 
                                                <div class="row">
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <input type="text" name="classdate" id="classdate" class="form-control" onfocus="(this.type=`date`)" onblur="(this.type=`text`)" onchange="getdayhours()" placeholder="Class Date" required>
                                                        </div>
                                                    </div>  
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="hours" id="hours" class="form-control" onchange="gethoursubject()" required>
                                                                <option value="">Select Hour</option>
                                                            </select>
                                                        </div>
                                                    </div>   
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="subject" id="subject" class="form-control" required>
                                                                <option value="">Select Subject</option>
                                                                
                                                            </select>
                                                        </div>
                                                    </div>                         
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="fdept" id="fdept" class="form-control" onchange="getfaculty()" required>
                                                            <option value="">Select Faculty Department</option>
                                                            '.$fdept.'
                                                            </select>
                                                        </div>
                                                    </div> 
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                            <select name="faculty" id="faculty" class="form-control" onchange="getnewfacsub()" required>
                                                            <option value="">Select Faculty Name</option>
                                                            </select>
                                                        </div>
                                                    </div>  
                                                    <div class="col-lg-4">
                                                        <div class="form-group">                                            
                                                        <select name="newsub" id="newsub" class="form-control" required>
                                                            <option value="">Select New Faculty Subject</option>
                                                        </select>
                                                        </div>
                                                    </div> 										
                                                </div>
                                                <div class="row">
                                                    <div class="col-md-12">  
                                                        <div class="form-group">
                                                            <input type="hidden" name="edit_id" id="edit_id" class="form-control" placeholder="Edit ID">
                                                            <button id="btn_reg" class="btn btn-primary pull-right" type="submit" style="background-color: #002366 !important;">Submit</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>                        
                                        </section>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-2"></div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1"></div>
                            <div class="col-lg-10">
                            </div>
                            <div class="col-lg-1"></div>
                        </div>
                    </div>
                </form>
                ';
            echo '</body>';
            echo static::script();
            echo '
            <script>
                function getdayhours()
                {
                    var date = document.getElementById("classdate").value;
                    $.ajax({
                        type: "POST",
                        data : {date: date},
                        url: "/Timetable/getdayhours",
                        success: function(d){
                            $("#hours").html(d);
                            
                        }
                    });
                }
                function gethoursubject()
                {
                    var date = document.getElementById("classdate").value;
                    var hour = document.getElementById("hours").value;
                    $.ajax({
                        type: "POST",
                        data : {date: date, hour: hour},
                        url: "/Timetable/gethoursubject",
                        success: function(d){
                            $("#subject").html(d);
                            
                        }
                    });
                }
                function getfaculty()
                {
                    var fdept = document.getElementById("fdept").value;
                    $.ajax({
                        type: "POST",
                        data : {fdept: fdept},
                        url: "/Timetable/getfaculty",
                        success: function(d){
                            $("#faculty").html(d);
                            
                        }
                    });
                }
                function getnewfacsub()
                {
                    var subject = document.getElementById("subject").value;
                    var faculty = document.getElementById("faculty").value;
                    $.ajax({
                        type: "POST",
                        data : {subject: subject, faculty: faculty},
                        url: "/Timetable/getnewfacsub",
                        success: function(d){
                            $("#newsub").html(d);
                            
                        }
                    });
                }
            </script>
            ';
            if(isset($_GET['status']))
            {
                if($_GET['status'] == 1)
                {
                    echo '<script>swal("", "Inserted Successfully", "success");</script>';
                }
                else if($_GET['status'] == 2)
                {
                    echo '<script>swal("", "Updated Successfully", "success");</script>';
                }
                else
                {
                    echo '<script>swal("", "Something went Wrong.. Please try again later..", "error");</script>';
                }
            }
        }
    }
    public static function getdayhours()
    {
        if(isset($_POST['date']))
        {
            $det = static::getdaybydate($_POST['date']);
            $a = Homem::getdata("SELECT * FROM `tt_days` WHERE `name`='$det'");
            $day = $a[0]['id'];
            Homem::getdata("SELECT * FROM `tt_workdays` WHERE `dates`='2021-04-30' AND `work`='1'");
            $data = Homem::getdata("SELECT sd.sub_id, sd.subname, tts.day_id, tts.hour_no FROM subject_data sd JOIN subject_faculty sf ON sd.sub_id=sf.fk_sub_id JOIN tt_regularmain ttr ON ttr.regulation=sd.regulation JOIN tt_regularsub tts ON ttr.tm_id=tts.fk_ttregmain_id WHERE sf.fk_user_id='$_SESSION[user_id]' AND sf.status='1' AND sf.completed='0' AND tts.day_id='$day' AND sd.course=ttr.fk_course_id AND sd.dept=ttr.fk_dept_id AND sd.year=ttr.year AND sd.sem=ttr.sem AND tts.fk_sub_id=sd.sub_id ORDER BY tts.hour_no ASC");
            echo '<option value="">Select Hour</option>';
            // echo '<option value="">'.$a[0]['id'].'-day</option>';
            for($i=0; $i<sizeof($data); $i++)
            {
                echo '<option value="'.$data[$i]['hour_no'].'">'.$data[$i]['hour_no'].'</option>';
            }
        }
    }
    public static function gethoursubject()
    {
        if(isset($_POST['date']))
        {
            $det = static::getdaybydate($_POST['date']);
            $a = Homem::getdata("SELECT * FROM `tt_days` WHERE `name`='$det'");
            $day = $a[0]['id'];
            Homem::getdata("SELECT * FROM `tt_workdays` WHERE `dates`='2021-04-30' AND `work`='1'");
            $data = Homem::getdata("SELECT sd.sub_id, sd.subname, tts.day_id, tts.hour_no FROM subject_data sd JOIN subject_faculty sf ON sd.sub_id=sf.fk_sub_id JOIN tt_regularmain ttr ON ttr.regulation=sd.regulation JOIN tt_regularsub tts ON ttr.tm_id=tts.fk_ttregmain_id WHERE sf.fk_user_id='$_SESSION[user_id]' AND sf.status='1' AND sf.completed='0' AND tts.day_id='$day' AND sd.course=ttr.fk_course_id AND sd.dept=ttr.fk_dept_id AND sd.year=ttr.year AND sd.sem=ttr.sem AND tts.fk_sub_id=sd.sub_id AND tts.hour_no='$_POST[hour]' ORDER BY tts.hour_no ASC");
            echo '<option value="">Select Subject</option>';
            for($i=0; $i<sizeof($data); $i++)
            {
                echo '<option value="'.$data[$i]['sub_id'].'">'.$data[$i]['subname'].'</option>';
            }
        }
    }
    public static function getnewfacsub()
    {
        if(isset($_POST['subject']))
        {
            $data = Homem::getdata("SELECT DISTINCT b.sub_id, b.subname FROM (SELECT sd.course, sd.dept, sd.regulation, sd.year, sd.sem, sf.student_year, sd.sub_id, sd.subname FROM subject_faculty sf JOIN subject_data sd ON sf.fk_sub_id=sd.sub_id WHERE sf.fk_user_id='$_SESSION[user_id]' AND sd.sub_id='$_POST[subject]' AND sf.status='1' AND sf.completed='0') a JOIN (SELECT sd.course, sd.dept, sd.regulation, sd.year, sd.sem, sf.student_year, sd.sub_id, sd.subname FROM subject_faculty sf JOIN subject_data sd ON sf.fk_sub_id=sd.sub_id WHERE sf.fk_user_id='$_POST[faculty]' AND sf.status='1' AND sf.completed='0') b ON a.course=b.course AND a.dept=b.dept AND a.regulation=b.regulation AND a.year=b.year AND a.sem=b.sem");
            echo '<option value="">Select New Faculty Subject</option>';
            for($i=0; $i<sizeof($data); $i++)
            {
                echo '<option value="'.$data[$i]['sub_id'].'">'.$data[$i]['subname'].'</option>';
            }
        }
    }

    public static function approveadjustments()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6 || $_SESSION['user_role'] == 2)
        {
            echo '';
        }
    }
    public static function dailyttapproval()
    {
        if($_SESSION['user_role'] == 8 || $_SESSION['user_role'] == 7 || $_SESSION['user_role'] == 6)
        {
            echo '';
        }
    }
    public static function getstdntbelongtosub()
    {
        if(isset($_POST['subject']))
        {
            $a = '';
            $sdata = Homem::getdata("SELECT um.id, um.no, um.name FROM subject_faculty sf JOIN user_master um ON sf.student_year=um.fromyear JOIN subject_data sd ON sf.fk_sub_id=sd.sub_id WHERE sf.fk_sub_id='$_POST[subject]' AND sf.fk_user_id='$_SESSION[user_id]' AND sd.course=um.fk_course_id AND sd.dept=um.fk_dept_id AND sd.regulation=um.regulation");
            $a .= '<div class="table-responsive"><table class="table" border="1px"><thead><tr><th colspan="3">Total Number of Studnets Eligible for this subject is : '.sizeof($sdata).'</th></tr><tr>';
            $a .= '<th>S. No.</th><th>Hallticket Number</th><th>Name</th>';
            $a .= '</tr></thead>';
            $a .= '<tbody>';
            for($i=0; $i<sizeof($sdata); $i++)
            {
                $j = $i+1;
                $a .= '<tr><td>'.$j.'</td><td>'.$sdata[$i]['no'].'</td><td>'.$sdata[$i]['name'].'</td></tr>';
            }
            $a .= '</tbody></table>';
            echo $a;
            // echo sizeof($sdata);
        }
    }
    public static function datem($id)
    {
        $data = explode('-', $id);
        $final = $data[2].'-'.$data[1].'-'.$data[0];
        return $final;
    }
    public static function datetimem($id)
    {
        $date = explode(" ", $id);
        $data = explode('-', $date[0]);
        $final = $data[2].'-'.$data[1].'-'.$data[0].' '.$date[1];
        return $final;
    }
    // Supporting Scripts for every page common
    public static function checkbrower()
    {
        $mystring = $_SERVER['HTTP_USER_AGENT'];
        $word = 'Chrome';
        if(strpos($mystring, $word) !== false)
        {
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
    public static function checkurl()
    {
        $url= $_SERVER['HTTP_HOST'];   
        return $url;
    }

    // Non used function
    public static function blockingscripts()
    {
        $a .= '
            <!--Script to confirm refresh start-->
            <script type="text/javascript">
                window.onbeforeunload = function () {
                    return "The Data in this page may lost. Do you want to refresh this page now.?";
                }
            </script>
            <!--Script to confirm refresh end-->
            <!--Script to Disable keyboard start-->
            <!--
                <script  type="text/javascript">
                    document.onkeydown = function (e) {	return false;}
                    $(function() {
                        $(this).bind("contextmenu", function(e) {
                            e.preventDefault();
                        });
                    });	
                </script>
            -->
            <!--Script to Disable keyboard end-->

            <!--Script to stop mouse move up and down start-->
            <script  type="text/javascript">
                $(document).ready(function () {
                        addEvent(document, "mouseout", function(e) {
                            e = e ? e : window.event;
                            var from = e.relatedTarget || e.toElement;
                            if (!from || from.nodeName == "HTML") {
                                console.log("Out of zone");
                                // swal("Sorry! Not Permitted to enter this area..", "You are not permitted to enter in to this area...", "error");
                            }
                        });
                });
                    function addEvent(obj, evt, fn) {
                        if (obj.addEventListener) {
                            obj.addEventListener(evt, fn, false);
                        }
                        else if (obj.attachEvent) {
                            obj.attachEvent("on" + evt, fn);
                        }
                    }
            </script>
            <!--Script to stop mouse move up and down end-->

            <!-- Script to prevent back button start-->
            <script>
                (function (global) {
                    if (typeof (global) === "undefined") {
                        throw new Error("window is undefined");
                    }
                    var _hash = "!";
                    var noBackPlease = function () {
                        global.location.href += "#";
                
                        // making sure we have the fruit available for juice....
                        // 50 milliseconds for just once do not cost much (^__^)
                        global.setTimeout(function () {
                            global.location.href += "!";
                        }, 50);
                    };
                    // Earlier we had setInerval here....
                    global.onhashchange = function () {
                        if (global.location.hash !== _hash) {
                            global.location.hash = _hash;
                        }
                    };
                    global.onload = function () {
                        noBackPlease();
                        // disables backspace on page except on input fields and textarea..
                        document.body.onkeydown = function (e) {
                            var elm = e.target.nodeName.toLowerCase();
                            if (e.which === 8 && (elm !== "input" && elm !== "textarea")) {
                                e.preventDefault();
                            }
                            // stopping event bubbling up the DOM tree..
                            e.stopPropagation();
                        };
                    };
                })(window);
                
            </script>
            <!-- Script to prevent back button end-->

            <!-- Script to stop mouse right click start-->
            <script>
                //Disable right click script
                var message = "";
                ///////////////////////////////////
                function clickIE() {
                    if (document.all) {
                        (message);
                        return false;
                    }
                }

                function clickNS(e) {
                    if (document.layers || (document.getElementById && !document.all)) {
                        if (e.which == 2 || e.which == 3) {
                            (message);
                            return false;
                        }
                    }
                }
                if (document.layers) {
                    document.captureEvents(Event.MOUSEDOWN);
                    document.onmousedown = clickNS;
                } else {
                    document.onmouseup = clickNS;
                    document.oncontextmenu = clickIE;
                }

                document.oncontextmenu = new Function("return false")
            </script>
            <!-- Script to stop mouse right click end-->

            <!-- Script to prevent selection start-->
            <script>
                $(document).ready(function () {
                    $("body").bind("cut copy", function (e) {
                        e.preventDefault();
                    });
                });
            </script>
            <!-- Script to prevent selection end-->
            ';
    }
    public static function jscripttime()
    {
        echo '
        <p id="demo"></p>
        <script>
        function presentdatetime()
        {
            var d = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
            var d = new Date(d);
            var y = d.getFullYear()+"-";
            var m = parseInt(d.getMonth())+1;
            var da = "-"+d.getDate();
            var h = " "+d.getHours();
            var min = ":"+d.getMinutes();
            var s = ":"+d.getSeconds();
            var total = y+m+da+h+min+s;
            return total;
        }
            document.getElementById("demo").innerHTML = presentdatetime();
        </script>
        ';
    }
    public static function emailverificationapi()
    {
        // header('content-type: text/xml;charset=utf-8');
        $name = "Fianl Test";
        $email = "s.krishnarjuna@gmail.com";
        $token = "1234567890qwertyui";
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "http://apie.mycvmaker.com/email/index.php",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS =>"{\"apikey\":\"s.krishnarjuna@gmail.com\", \"secrectkey\":\"SSRSKJR\", \"toemail\":\"$email\", \"toname\":\"$name\", \"token\":\"$token\"}",
        CURLOPT_HTTPHEADER => array(
            ": ",
            "Content-Type: application/json"
        ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $data = json_decode($response, true);
        if($data['statuscode'] == 200)
        {
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
}