<?php


class SchoolMgmt
{
	public $school_id;
	public $school_name;
	public $school_grant;
	public $school_type;
	public $school_address;
	public $school_district;
	public $school_taluka;
	public $school_city;
	public $school_phone1;
	public $school_phone2;
	public $school_fax;
	public $school_emailid;
	public $school_url;

	public $jila_seat;
	public $taluka_seat;
	public $building_location;
	public $area_type;
	public $school_location;
	public $school_population;
	public $school_ward;
	public $school_t_distance;
	public $schoo_d_distance;
	public $school_legislative;
	public $school_parlament;
	public $school_medium;
	public $school_salarycompuno;
	public $school_sscindex;
	public $school_hscindex;
	public $school_svsno;
	public $school_qdcno;
	public $school_admtype;
	public $school_shiftbase;


	public $school_matrixgrade;
	public $school_activitygrade;
	public $school_sscgrade;
	public $school_hscgrade;
	public $school_edutype;
	public $school_building;
	public $school_owneradd;
	public $school_rent;
	public $school_photo;
	public $login_id;
	public $createdon;

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

	/**
	 * This function is used to get a particular record.
	 *
	 * @param integer $school_id This is the database school_id.
	 *
	 * @return array
	 */
	function getRecordById($school_id)
	{
		$qry = "select * from sms_schoolmaster where 1=1 ";

		if ($school_id != "") {
			$qry .= " and school_id='" . $_SESSION['aSchoolId'] . "' ";
		}

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {

			$this->school_count = 1;

			$row = dbArray($out);
			extract($row);

			$this->school_id = ($school_id);
			$this->school_name = ($school_name);
			$this->school_address = ($school_address);
			$this->school_phone = ($school_phone);
			$this->school_fax = ($school_fax);
			$this->school_emailid = ($school_emailid);
			$this->school_url = ($school_url);

			$this->school_principal_name = ($school_principal_name);
			$this->school_principal_addr = ($school_principal_addr);
			$this->school_principal_phone = ($school_principal_phone);
			$this->school_principal_mobileno = ($school_principal_mobileno);
			$this->school_principal_emailid = ($school_principal_emailid);

			$this->school_medium = ($school_medium);
			$this->school_salarycompuno = ($school_salarycompuno);
			$this->school_sscindex = ($school_sscindex);
			$this->school_hscindex = ($school_hscindex);
			$this->dist_id = ($dist_id);
			$this->school_svsno = ($school_svsno);
			$this->school_qdcno = ($school_qdcno);
			$this->school_admtype = ($school_admtype);
			$this->school_shiftbase = ($school_shiftbase);
			$this->school_locality = ($school_locality);
			$this->school_trustname = ($school_trustname);
			$this->school_trustadd = ($school_trustadd);
			$this->school_trustphone = ($school_trustphone);
			$this->school_trustregno = ($school_trustregno);
			$this->school_softwareversion = ($school_softwareversion);
			$this->school_matrixgrade = ($school_matrixgrade);
			$this->school_activitygrade = ($school_activitygrade);
			$this->school_sscgrade = ($school_sscgrade);
			$this->school_hscgrade = ($school_hscgrade);
			$this->school_type = ($school_type);
			$this->school_building = ($school_building);
			$this->login_id = ($login_id);
			$this->createdon = ($createdon);

			dbResult($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->school_count = 0;

		$qry = "SELECT 
					sm.*,
					sdi.dist_id,				
					sdi.dist_nameeng,
					sdi.dist_nameguj  
				FROM 
					sms_schoolmaster sm left join sms_distmaster sdi ON sm.dist_id=sdi.dist_id WHERE 1=1 ";

		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";
		}
		$out = dbQuery($qry);

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

	/**
	 * 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 
					sm.*,
					sdi.dist_id,					
					sdi.dist_nameeng,
					sdi.dist_nameguj,
					st.taluka_id,
					st.taluka_name,
					ssvs.svs_id,
					ssvs.svs_name,
					sl.legislative_id,
					sl.legislative_name,
					sp.parlament_id,
					sp.parlament_name 
					
				FROM 
					sms_schoolmaster sm, 
					sms_distmaster sdi,
					sms_talukamaster st,
					sms_svs ssvs,
					sms_legislative sl,
					sms_parlament sp
			   WHERE 
			   		sm.school_district=sdi.dist_id
					AND st.dist_id=sdi.dist_id
					AND ssvs.svs_id = sm.school_svsno
					AND sl.legislative_id=sm.school_legislative
					AND sp.parlament_id=sm.school_parlament";

		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";
		}

		$out = dbQuery($qry);

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

	/**
	 * 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 getSchoolIdbydist($dist_id = "")
	{
		$this->school_count = 0;
		$qry = "SELECT 
					sm.school_id 
				FROM sms_schoolmaster sm  WHERE sm.dist_id= '$dist_id' ";
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			$this->school_count = dbRows($out);
			return $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 getExistingRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->school_count = 0;

		$qry = "SELECT Count(*) AS Count FROM sms_schoolmaster sm WHERE 1=1 ";

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

		$out = dbQuery($qry);

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


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


		$Qry1 .= "school_name='" . Escape($this->school_name) . "',";
		$Qry1 .= "school_type='" . Escape($this->school_type) . "',";
		$Qry1 .= "school_address='" . replaceHtmlEditor(Escape($this->school_address)) . "',";

		$Qry1 .= "school_grant='" . Escape($this->school_grant) . "',";
		$Qry1 .= "jila_seat='" . Escape($this->jila_seat) . "',";
		$Qry1 .= "taluka_seat='" . Escape($this->taluka_seat) . "',";
		$Qry1 .= "area_type='" . Escape($this->area_type) . "',";
		$Qry1 .= "building_location='" . Escape($this->building_location) . "',";
		$Qry1 .= "school_rent='" . Escape($this->school_rent) . "',";
		$Qry1 .= "school_owneradd='" . replaceHtmlEditor(Escape($this->school_owneradd)) . "',";
		$Qry1 .= "school_rent='" . Escape($this->school_rent) . "',";

		$Qry1 .= "school_district='" . Escape($this->school_district) . "',";
		$Qry1 .= "school_taluka='" . Escape($this->school_taluka) . "',";
		$Qry1 .= "school_city='" . Escape($this->school_city) . "',";
		$Qry1 .= "school_phone1='" . Escape($this->school_phone1) . "',";
		$Qry1 .= "school_phone2='" . Escape($this->school_phone2) . "',";
		$Qry1 .= "school_fax='" . Escape($this->school_fax) . "',";
		$Qry1 .= "school_emailid='" . Escape($this->school_emailid) . "',";
		$Qry1 .= "school_url='" . Escape($this->school_url) . "',";
		$Qry1 .= "school_location='" . Escape($this->school_location) . "',";
		$Qry1 .= "school_population='" . Escape($this->school_population) . "',";
		$Qry1 .= "school_ward='" . Escape($this->school_ward) . "',";
		$Qry1 .= "school_t_distance='" . Escape($this->school_t_distance) . "',";
		$Qry1 .= "schoo_d_distance='" . Escape($this->schoo_d_distance) . "',";
		$Qry1 .= "school_legislative='" . Escape($this->school_legislative) . "',";
		$Qry1 .= "school_parlament='" . Escape($this->school_parlament) . "',";
		$Qry1 .= "school_salarycompuno='" . Escape($this->school_salarycompuno) . "',";
		$Qry1 .= "school_sscindex='" . Escape($this->school_sscindex) . "',";
		$Qry1 .= "school_hscindex='" . Escape($this->school_hscindex) . "',";
		$Qry1 .= "school_svsno='" . Escape($this->school_svsno) . "',";
		$Qry1 .= "school_qdcno='" . Escape($this->school_qdcno) . "',";
		$Qry1 .= "school_admtype='" . Escape($this->school_admtype) . "',";
		$Qry1 .= "school_shiftbase ='" . Escape($this->school_shiftbase) . "',";
		$Qry1 .= "school_matrixgrade='" . Escape($this->school_matrixgrade) . "',";
		$Qry1 .= "school_activitygrade='" . Escape($this->school_activitygrade) . "',";
		$Qry1 .= "school_sscgrade='" . Escape($this->school_sscgrade) . "',";
		$Qry1 .= "school_hscgrade='" . Escape($this->school_hscgrade) . "',";
		$Qry1 .= "school_edutype='" . Escape($this->school_edutype) . "',";
		$Qry1 .= "school_building='" . Escape($this->school_building) . "',";
		$Qry1 .= "school_photo='" . Escape($this->school_photo) . "',";
		$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";


		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "insert into sms_schoolmaster set " . $Qry1 . " ";
			dbQuery($qry);
			$this->school_id = dbInsertedId();

			if ($this->school_id > 0) {
				//$this->createSectionMaster($this->school_id,$_SESSION['aUserId']);
				//$this->createStandardMaster($this->school_id,$_SESSION['aUserId']);
				//$this->createAdminUser($this->school_id);
				$this->CreateSchoolFolder($this->school_id);
			}
		}
	}

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

		$Qry1 .= "school_name='" . Escape($this->school_name) . "',";
		$Qry1 .= "school_type='" . Escape($this->school_type) . "',";
		$Qry1 .= "school_address='" . replaceHtmlEditor(Escape($this->school_address)) . "',";

		$Qry1 .= "school_grant='" . Escape($this->school_grant) . "',";
		$Qry1 .= "jila_seat='" . Escape($this->jila_seat) . "',";
		$Qry1 .= "taluka_seat='" . Escape($this->taluka_seat) . "',";
		$Qry1 .= "area_type='" . Escape($this->area_type) . "',";
		$Qry1 .= "building_location='" . Escape($this->building_location) . "',";
		$Qry1 .= "school_rent='" . Escape($this->school_rent) . "',";
		$Qry1 .= "school_owneradd='" . replaceHtmlEditor(Escape($this->school_owneradd)) . "',";
		$Qry1 .= "school_rent='" . Escape($this->school_rent) . "',";

		$Qry1 .= "school_district='" . Escape($this->school_district) . "',";
		$Qry1 .= "school_taluka='" . Escape($this->school_taluka) . "',";
		$Qry1 .= "school_city='" . Escape($this->school_city) . "',";
		$Qry1 .= "school_phone1='" . Escape($this->school_phone1) . "',";
		$Qry1 .= "school_phone2='" . Escape($this->school_phone2) . "',";
		$Qry1 .= "school_fax='" . Escape($this->school_fax) . "',";
		$Qry1 .= "school_emailid='" . Escape($this->school_emailid) . "',";
		$Qry1 .= "school_url='" . Escape($this->school_url) . "',";
		$Qry1 .= "school_location='" . Escape($this->school_location) . "',";
		$Qry1 .= "school_population='" . Escape($this->school_population) . "',";
		$Qry1 .= "school_ward='" . Escape($this->school_ward) . "',";
		$Qry1 .= "school_t_distance='" . Escape($this->school_t_distance) . "',";
		$Qry1 .= "schoo_d_distance='" . Escape($this->schoo_d_distance) . "',";
		$Qry1 .= "school_legislative='" . Escape($this->school_legislative) . "',";
		$Qry1 .= "school_parlament='" . Escape($this->school_parlament) . "',";
		$Qry1 .= "school_salarycompuno='" . Escape($this->school_salarycompuno) . "',";
		$Qry1 .= "school_sscindex='" . Escape($this->school_sscindex) . "',";
		$Qry1 .= "school_hscindex='" . Escape($this->school_hscindex) . "',";
		$Qry1 .= "school_svsno='" . Escape($this->school_svsno) . "',";
		$Qry1 .= "school_qdcno='" . Escape($this->school_qdcno) . "',";
		$Qry1 .= "school_admtype='" . Escape($this->school_admtype) . "',";
		$Qry1 .= "school_shiftbase ='" . Escape($this->school_shiftbase) . "',";
		$Qry1 .= "school_matrixgrade='" . Escape($this->school_matrixgrade) . "',";
		$Qry1 .= "school_activitygrade='" . Escape($this->school_activitygrade) . "',";
		$Qry1 .= "school_sscgrade='" . Escape($this->school_sscgrade) . "',";
		$Qry1 .= "school_hscgrade='" . Escape($this->school_hscgrade) . "',";
		$Qry1 .= "school_edutype='" . Escape($this->school_edutype) . "',";
		$Qry1 .= "school_building='" . Escape($this->school_building) . "',";
		$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update sms_schoolmaster set " . $Qry1 . " where school_id=" . $this->school_id;
			dbQuery($qry);
		}
	}

	/**
	 * This is a function for deleting the Employee record.
	 *
	 * @return void The redirect file name and the message are passed.
	 *
	 */
	function delete()
	{
		$qry = "delete from  sms_schoolmaster 
             	where school_id in (" . $this->deleteId . ") ";
		dbQuery($qry);
	}

	/**
	 * This function is used to generate record in Section Master dynamically once school created.
	 *
	 * @param integer $school_id This is the database school_id.
	 * @param integer $login_id  This is the current login_id.
	 *
	 * @return total count
	 */

	function createSectionMaster($school_id, $login_id)
	{
		$qry = "INSERT into sms_sectionmaster
				(section_id,
				 section_name,
				 school_id,
				 section_active,
				 login_id,
				 createdon,
				 blnBackup
				)
				SELECT 
					section_no,
					section_name,
					'$school_id',
					'0',
					'$login_id',
					now(),
					'0'
				from sms_section ";


		$count = dbQuery($qry);

		return $count;
	}

	/**
	 * This function is used to generate record in Standard Master dynamically once school created.
	 *
	 * @param integer $school_id This is the database school_id.
	 * @param integer $login_id  This is the current login_id.
	 *
	 * @return total count
	 */

	function createStandardMaster($school_id, $login_id)
	{
		$qry = "INSERT into sms_standardmaster
				(standard_id,
				 standard_name,
				 standard_noofdiv,
				 section_id,
				 school_id,
				 login_id,
				 createdon,
				 blnBackup
				)
				SELECT 
					standard_id,
					standard_name,
					'1',
					section_no,
					'$school_id',
					'$login_id',
					now(),
					'0'
				from sms_standard ";


		$count = dbQuery($qry);

		return $count;
	}

	/**
	 * This function is used to generate user in User Master dynamically once school created.
	 *
	 * @param integer $school_id This is the database school_id.
	 *
	 */

	function createAdminUser($schoolid)
	{

		//module
		$objMenuMgmt = new MenuMgmt();
		$condition = " AND  module_active = 1 ";
		$moduleids = $objMenuMgmt->getAllModuleIds($condition);

		//menu
		$condition = " AND module_id in ($moduleids) AND menu_active = 1  ";
		$menuids = $objMenuMgmt->getAllMenuIds($condition);

		//page
		$objPageMgmt = new PageMgmt();
		$condition = "AND menu_id in ($menuids) AND page_active = 1 ";
		$pageids = $objPageMgmt->getAllPageIds($condition);


		$objUserMgmt = new UserMgmt();
		$unicno = $objUserMgmt->generateUniqueNo('U');
		$username = $objUserMgmt->generatePIN(($unicno));

		///entry in user master

		$objUserMgmt = new UserMgmt();
		$objUserMgmt->user_name = $username;
		$objUserMgmt->user_role = "User";
		$objUserMgmt->user_password = $username;
		$objUserMgmt->school_id = $schoolid;
		$objUserMgmt->module_ids = $moduleids;
		$objUserMgmt->menu_ids = $menuids;
		$objUserMgmt->page_ids = $pageids;

		$objUserMgmt->add();

		if ($objUserMgmt->user_id > 0) {

			$user_id = $objUserMgmt->user_id;
			$objUserAccess = new UserAccessMgmt();

			$access_array = array();

			$arrmoduleid = explode(",", $moduleids);
			$arrmenuid = explode(",", $menuids);
			if (count($moduleids) > 0) {

				for ($mod = 0; $mod < count($moduleids); $mod++) {
					if (count($arrmenuid) > 0) {

						for ($m = 0; $m < count($arrmenuid); $m++) {
							$condition = "AND page_active ='1' AND menu_id= '" . $arrmenuid[$m] . "'";
							$out = $objPageMgmt->getAllRecords(0, 0, $condition, "", "");

							if (dbRows($out) > 0) {
								while ($row = dbArray($out)) {
									$access = explode(",", $row["access"]);
									$objUserAccess->user_id = $user_id;
									$objUserAccess->page_id = $row["page_id"];
									$objUserAccess->useraccess_add = $access[0];
									$objUserAccess->useraccess_edit = $access[1];
									$objUserAccess->useraccess_delete = $access[2];

									$objUserAccess->add();
								}

							}
						}
					}
				}
			}
		}
	}

	function CreateSchoolFolder($school_id)
	{

		global $SiteDefaultImage;
		global $SiteImagePath;
		global $SchoolImagePath;
		global $EmployeeImagePath;
		global $StudentImagePath;


		if (!is_dir(str_replace("/" . $_SESSION["VedantnrSchoolId"] . "/", "/" . $school_id . "/", $SchoolImagePath))) {

			$schimpath = (str_replace("/" . $_SESSION["VedantnrSchoolId"] . "/", "/" . $school_id . "/", $SchoolImagePath));

			mkdir($schimpath, 0777);
			chmod($schimpath, 0777);

			copy($SiteImagePath . $SiteDefaultImage, $schimpath . $SiteDefaultImage);
			$file_path = realpath($schimpath);
			chmod($file_path, 0755);
		}
		if (!is_dir(str_replace("/" . $_SESSION["VedantnrSchoolId"] . "/", "/" . $school_id . "/", $EmployeeImagePath))) {

			$Emppath = (str_replace("/" . $_SESSION["VedantnrSchoolId"] . "/", "/" . $school_id . "/", $EmployeeImagePath));

			mkdir($Emppath, 0777);
			chmod($Emppath, 0777);

			copy($SiteImagePath . $SiteDefaultImage, $Emppath . $SiteDefaultImage);
			$file_path = realpath($Emppath);
			chmod($file_path, 0755);
		}
		if (!is_dir(str_replace("/" . $_SESSION["VedantnrSchoolId"] . "/", "/" . $school_id . "/", $StudentImagePath))) {

			$stupath = (str_replace("/" . $_SESSION["VedantnrSchoolId"] . "/", "/" . $school_id . "/", $StudentImagePath));
			mkdir($stupath, 0777);
			chmod($stupath, 0777);

			copy($SiteImagePath . $SiteDefaultImage, $stupath . $SiteDefaultImage);
			$file_path = realpath($stupath);
			chmod($file_path, 0755);
		}
	}

	/**
	 * This is a function for updating the Student Photo.
	 *
	 * @return void The redirect file name and the message are passed.
	 *
	 */
	function updatePhoto()
	{
		$qry = "update sms_schoolmaster 
				set school_photo = '" . $this->school_photo . "'
             	where school_id =   " . $this->school_id . "";
		dbQuery($qry);
	}
}
?>