<?php
class StudentDocumentMgmt
{
    public $studentdocument_id;
    public $document_id;
    public $college_id;
    public $semester_id;
    public $year_id;
    public $student_id;
    public $branch_id;
    public $branchyear_id;
    public $noofdocument;
    public $docrecive_date;


    function recordExists($student_id, $document_id)
    {
        // Sanitize inputs to prevent SQL injection
        $student_id = Escape($student_id);
        $document_id = Escape($document_id);
        $college_id = $_SESSION['VedantnrCollegeId'];
        // Query to check for the existence of the record
        $query = "
            SELECT COUNT(*) AS count
            FROM cms_studentdocument
            WHERE student_id = '$student_id'
              AND document_id = '$document_id'
              AND college_id = '$college_id'
        ";
        // Execute the query
        $result = dbQuery($query);
        // Fetch the result
        $row = dbArray($result);
        // Return true if count is greater than 0, else false
        return $row['count'] > 0;
    }

    function getAllRecords($branch_id, $limit, $start, $where, $order_field, $order_type)
    {
        // Start building the query
        $qry = "SELECT sd.*, dm.document_name, dm.document_id as doc_id,dm.branch_id
            FROM cms_documentmaster dm 
            LEFT JOIN cms_studentdocument sd 
            ON dm.document_id = sd.document_id ";


        if ($branch_id !== null) {
            $where .= " WHERE 1=1";
            if ($branch_id == 1 || $branch_id == 2 || $branch_id == 3) {
                $where .= " AND dm.branch_id = 'ALL'";
            } elseif ($branch_id == 4) {
                $where .= " AND dm.branch_id IN ('ALL', 4)";
            } elseif ($branch_id == 5) {
                $where .= " AND dm.branch_id IN ('ALL', 5)";
            }
        }
        if ($where != "") {
            $qry .= " " . $where;
        }
        if ($order_field != "" && $order_type != "") {
            $qry .= " ORDER BY " . $order_field . " " . $order_type;
        }

        if (intval($limit) > 0 && intval($start) >= 0) {
            $qry .= " LIMIT " . intval($start) . ", " . intval($limit);
        }

        $out = dbQuery($qry);


        if (dbRows($out) > 0) {
            return $out;
        }
        return $out;
    }

    function add()
    {
        global $SQLDateFormat;

        $Qry1 = "";
        $Qry1 .= "college_id='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
        $Qry1 .= "document_id='" . Escape($this->document_id) . "',";
        $Qry1 .= "student_id='" . Escape($this->student_id) . "',";
        $Qry1 .= "branch_id='" . Escape($this->branch_id) . "',";
        $Qry1 .= "semester_id='" . Escape($this->semester_id) . "',";
        $Qry1 .= "year_id='" . Escape($this->year_id) . "',";
        $Qry1 .= "branchyear_id='" . Escape($this->branchyear_id) . "',";
        $Qry1 .= "noofdocument='" . Escape($this->noofdocument) . "',";
        $Qry1 .= "docrecive_date='" . Escape($this->docrecive_date) . "',";


        if ($Qry1 != "") {
            $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
            $qry = "INSERT INTO cms_studentdocument  set " . $Qry1 . " ";

            dbQuery($qry);
            $this->studentdocument_id = dbInsertedId();
        }
    }
    function edit()
    {
        global $SQLDateFormat;
        $Qry1 = "";

        $Qry1 .= "college_id='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
        $Qry1 .= "document_id='" . Escape($this->document_id) . "',";
        $Qry1 .= "student_id='" . Escape($this->student_id) . "',";
        $Qry1 .= "branch_id='" . Escape($this->branch_id) . "',";
        $Qry1 .= "semester_id='" . Escape($this->semester_id) . "',";
        $Qry1 .= "year_id='" . Escape($this->year_id) . "',";
        $Qry1 .= "branchyear_id='" . Escape($this->branchyear_id) . "',";
        $Qry1 .= "noofdocument='" . Escape($this->noofdocument) . "',";
        $Qry1 .= "docrecive_date='" . Escape($this->docrecive_date) . "',";

        if ($Qry1 != "") {
            $Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
            $qry = "UPDATE cms_studentdocument SET " . $Qry1 . " 
                          where studentdocument_id=" . $this->studentdocument_id;
            dbQuery($qry);

        }
    }
//report-student-document.php
	
	function getdocument($condition)
	{
		$qry =  "
        SELECT DISTINCT sd.docrecive_date
        FROM cms_studentdocument sd
        WHERE 
            sd.college_id = " . $_SESSION["VedantnrCollegeId"] . " ";


		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

    function getstudentdocument($condition)
	{
		$qry =  "  SELECT 
                        sm.student_id,
                        CONCAT(sm.student_surname, ' ', sm.student_firstname, ' ', sm.student_lastname) AS student_name,
                        bry.branchyear_name,
						b.branch_name,
						sem.semester_name,
						y.year_name
                    FROM 
                        cms_studentdocument sd
                       
						 LEFT JOIN cms_branchmaster b ON sd.branch_id = b.branch_id
						  LEFT JOIN cms_branchyear bry ON sd.branchyear_id = bry.branchyear_id
						 LEFT JOIN cms_semestermaster sem ON sd.semester_id =sem.semester_id
						 LEFT JOIN cms_yearmaster y ON sd.year_id =y.year_id
                        LEFT JOIN cms_studentmaster sm ON sd.student_id = sm.student_id
                    WHERE 
                        sd.college_id = " . $_SESSION["VedantnrCollegeId"] . " "; 

		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

    function getstudentdocumentdata($condition)
	{
		$qry =  "
                        SELECT 
                            dm.document_name,
                            sd.noofdocument
                        FROM 
                            cms_studentdocument sd
                            LEFT JOIN cms_documentmaster dm ON sd.document_id = dm.document_id
                        WHERE 
                            sd.college_id = " . $_SESSION["VedantnrCollegeId"] . " ";

		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}
}
?>