<?php

class TTStandardMgmt
{

    public $school_id;
    public $working_days;
    public $periods_per_day;
    public $periods_lastday;
    public $ttstandard_msg;
    public $ttstandard_count;
    public $ttstandard_redirectfile;
    public $bullet;

    public $recordstatus;
    public $created_userid;
    public $created_datetime;
    public $created_terminalid;
    public $modified_userid;
    public $modified_datetime;
    public $modified_terminalid;
    public $needto_synch;
    public $synchid;
    public $user_id;
    public $token_id;

    function TTStandardMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->ttstandard_count = 0;
    }


    /**
     * This is a function for getting the all records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Mix Result Set
     */
    function getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
    {
        $this->school_count = 0;

        $qry = "SELECT * FROM tt_standardmaster 
				WHERE school_id= '" . $_SESSION["VedantnrCollegeId"] . "'";

        if ($condition != "") {
            $qry .= $condition;
        }

        if ($order_field != "" && $order_type != "") {
            $qry .= " order by $order_field $order_type";
        }

        if (intval($limit) > 0 && intval($start) >= 0) {
            $qry .= " Limit $start , $limit";
        }
        //echo $qry; exit;

        $out = dbQuery($qry);


        if (dbRows($out) > 0) {
            $this->ttstandard_count = dbRows($out);
        }
        return $out;

    }

    /**
     * This is a function for getting the total no of records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Total Records Found
     */
    function getCountRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
    {
        $this->user_count = 0;

        $qry = "SELECT Count(*) AS Count FROM tt_standardmaster where school_id= '" . $_SESSION["VedantnrSchoolId"] . "' ";

        if ($condition != "") {
            $qry .= $condition;
        }


        $out = dbQuery($qry);

        if (dbRows($out) > 0) {
            $row = dbArray($out);
            extract($row);
            $this->ttstandard_count = $Count;
            dbResult($out);
        }
        return $this->ttstandard_count;
    }




    /**
     * This is a function for adding new record for Subject.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
        $Qry1 = "";

        $Qry1 .= "school_id='" . Escape($this->school_id) . "',";
        $Qry1 .= "working_days='" . Escape($this->working_days) . "',";
        $Qry1 .= "periods_per_day='" . Escape($this->periods_per_day) . "',";
        $Qry1 .= "periods_lastday='" . Escape($this->periods_lastday) . "',";

        $Qry1 .= "recordstatus='1',";

        $Qry1 .= "created_userid='" . Escape($this->user_id) . "',";

        $Qry1 .= "created_datetime='" . date('Y-m-d H:i:s') . "',";
        $Qry1 .= "created_terminalid='" . $_SERVER['REMOTE_ADDR']  . "',";
        $Qry1 .= "modified_datetime='" . date('Y-m-d H:i:s') . "',";

        if ($Qry1 != "") {
            $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
            $qry = "insert into tt_standardmaster set " . $Qry1 . " ";
          //  echo $qry;
          //  exit;
            dbQuery($qry);
            $this->subject_id = dbInsertedId();
        }
    }

    /**
     * This is a function for editing a Subject record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
        $Qry1 = "";


        $Qry1 .= "school_id='" . Escape($this->school_id) . "',";
        $Qry1 .= "working_days='" . Escape($this->working_days) . "',";
        $Qry1 .= "periods_per_day='" . Escape($this->periods_per_day) . "',";
        $Qry1 .= "periods_lastday='" . Escape($this->periods_lastday) . "',";

        $Qry1 .= "recordstatus='1',";
        //  $Qry1 .= "modified_userid='".$_SESSION['SSA_UserID']."',";
        $Qry1 .= "modified_datetime='" . date('Y-m-d H:i:s') . "',";
        $Qry1 .= "modified_terminalid='" . $_SERVER['REMOTE_ADDR'] . "',";

        if ($Qry1 != "") {
            $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
            $qry = "update tt_standardmaster set " . $Qry1 . " where school_id = '" . $_SESSION['VedantnrCollegeId'] . "'";

            dbQuery($qry);
        }
    }
    /**
     * This is a function for deleting the subject record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function delete()
    {
        $qry = "delete from  tt_standardmaster
             	where subject_id in (" . $this->deleteSubject_Id . ")
				AND school_id= '" . $_SESSION["VedantnrCollegeId"] . "' ";
        dbQuery($qry);
    }
    function getTt_standardDataForUploadToServer($data)
    {


        $temp_data = array();
        if (isset($data)) {
            $obj_synchdetail = new synchdetail_masterMgmt();
            $obj_synchdetail->school_id = $data["school_id"];
            $obj_synchdetail->token_id = $data["token_id"];
            $obj_synchdetail->table_name = $data["table_name"];
            $last_Synch_datetime = $obj_synchdetail->getLastSynchDateTime();
            if (isset($last_Synch_datetime)) {
                $Qry1 = "SELECT * FROM " . $data["table_name"] . " Where (modified_datetime > '" . $last_Synch_datetime . "' OR modified_datetime = '" . $last_Synch_datetime . "')";
                $out = dbQuery($Qry1);
                if ($out) {
                    $i = 0;
                    while ($row = dbArray($out)) {
                        $temp_data[$i]["school_id"] = $row["school_id"];
                        $temp_data[$i]["working_days"] = $row["working_days"];
                        $temp_data[$i]["periods_per_day"] = $row["periods_per_day"];
                        $temp_data[$i]["periods_lastday"] = $row["periods_lastday"];
                        $temp_data[$i]["recordstatus"] = $row["recordstatus"];
                        $temp_data[$i]["created_userid"] = $row["created_userid"];
                        $temp_data[$i]["created_datetime"] = $row["created_datetime"];
                        $temp_data[$i]["created_terminalid"] = $row["created_terminalid"];
                        $temp_data[$i]["modified_userid"] = $row["modified_userid"];
                        $temp_data[$i]["modified_datetime"] = $row["modified_datetime"];
                        $temp_data[$i]["modified_terminalid"] = $row["modified_terminalid"];
                        $temp_data[$i]["needto_synch"] = $row["needto_synch"];
                        $temp_data[$i]["synchid"] = $row["synchid"];
                        $i++;
                    }
                    if (!empty($temp_data)) {
                        $obj_synchdetail = new synchdetail_masterMgmt();
                        $obj_synchdetail->synch_id = $this->synchid;
                        $obj_synchdetail->server_synchdetail_id = "";
                        $obj_synchdetail->school_id = $this->school_id;
                        $obj_synchdetail->user_id = $this->user_id;
                        $obj_synchdetail->start_datetime = date("Y-m-d H:i:s");
                        $obj_synchdetail->end_datetime = date("Y-m-d H:i:s");
                        $obj_synchdetail->token_id = $this->token_id;
                        $obj_synchdetail->table_name = "" . $data['table_name'] . "";
                        $obj_synchdetail->recordstatus = 0;
                        $obj_synchdetail->error_msg = "";
                        $res_synch_detail = $obj_synchdetail->add();
                        if ($res_synch_detail) {
                            return $temp_data;
                        } else {
                            return "Synch Detail Entry Failed in" . $data["table_name"] . "!";
                        }
                    }
                }
            }
        }
        return $temp_data;
    }

}
?>