<?php
defined('BASEPATH') or exit('No direct script access allowed');

use Nilambar\NepaliDate\NepaliDate;

require_once 'vendor/autoload.php';

class Teacher_model extends CI_Model
	{
		private $nepaliDateObject;
		private $nepali_current_date;

		public function __construct()
		{
			$this->load->database();
			date_default_timezone_set("Asia/Kolkata");
			$this->load->helper('common_helper');
			$this->nepaliDateObject = new NepaliDate();
			$this->nepali_current_date = cuurentNepaliDate($this->nepaliDateObject);
		}

		public function login($uname, $pwd)
		{
			$pwd = base64_encode($pwd);
			$sql = $this->db->query("SELECT * FROM teacher WHERE teacherId='$uname' AND password='$pwd'");
			if ($sql->num_rows() > 0) {
				return $sql->row_object();
			} else {
				return false;
			}
		}

		public function get_myClasses($id, $pday)
		{

			//$this->db->select('session_details.id,classroom.subject_id,classroom.teacher_id,classroom.classroom_name,subject_name,subject_icon,start_link,day,start,duration');
			// $this->db->from('session_details');
			// $this->db->join('subject', 'session_details.subject_id = subject.id');
			// $this->db->join('classroom','classroom.subject_id = subject.id' );
			// $this->db->where('classroom.flag', 'Y');
			// $this->db->where('teacher_id', $id);
			// if($pday != '')
			//     $this->db->where('day', $pday);

			// $query = $this->db->get();
			//// echo $this->db->last_query();
			// return $query->result_array();

			//shankar code start
			$this->db->select('session_details.id,session_details.subject_id,classroom_teacher_subject.teacher_id,classroom.classroom_name,subject_name,subject_icon,start_link,day,start,duration');
			$this->db->from('session_details');
			$this->db->join('subject', 'session_details.subject_id = subject.id');
			$this->db->join('classroom_teacher_subject', 'subject.id = classroom_teacher_subject.subject_id');
			$this->db->join('classroom', 'classroom.id = classroom_teacher_subject.classroom_id');
			$this->db->where('classroom.is_active', 'yes');
			$this->db->where('teacher_id', $id);
			if ($pday != '')
				$this->db->where('day', $pday);

			$query = $this->db->get();
			// echo $this->db->last_query();
			return $query->result_array();
			//shankar code end

		}


		//shankar code start from here

		public function get_course($id)
		{
			$this->db->select('sc.id as section_id, co.id as course_id,c.id as classroom_id, co.course_name, sc.section_name');
			$this->db->from('classroom_teacher_subject ts, section sc, classroom c, course co');
			$this->db->where('ts.teacher_id', $id);
			$this->db->where('c.id = ts.classroom_id');
			$this->db->where('c.course_id = co.id');
			$this->db->where('sc.id = c.section_id');
			$this->db->group_by('course_name, section_name');
			$query = $this->db->get();
			return $query->result_array();
		}

		public function get_subjects($t_id, $cls_id)
		{
			$this->db->select('s.id as subject_id,s.subject_icon, c.id as classroom_id, s.subject_name, t.teacher_name, sc.section_name');
			$this->db->from('classroom c, subject s, teacher t, classroom_teacher_subject ts, course co, section sc');
			$this->db->where('t.id', $t_id);
			$this->db->where('c.id', $cls_id);
			$this->db->where('ts.classroom_id', $cls_id);
			$this->db->where('ts.teacher_id', $t_id);
			// $this->db->where('ss.course_id = c.course_id');
			$this->db->where('s.id = ts.subject_id');

			$this->db->group_by('subject_name, teacher_name');
			$query = $this->db->get();
			// echo $this->db->last_query();
			return $query->result_array();
		}


		public function create_assignment($data)
		{
			$this->db->insert('assignment', $data);
			$res = $this->db->insert_id();
			if ($res) {
				return $res;
			}
		}

		public function get_assignments($tid, $cls_id, $agn_id = '', $month = '', $year = '')
		{
			$this->db->select('a.id as asgn_id, title, subject_name, a.remark, name as student_name, s.id as student_id, due_date, teacher_name');
			$this->db->from('assignment a');
			$this->db->join('teacher t', 'a.teacher_id = t.id');
			$this->db->join('subject sub', 'a.subject_id = sub.id');
			$this->db->join('teacher_subjects ts', 't.id = ts.teacher_id');

			$this->db->join('student_batch sb', 'a.classroom_id = sb.classroom_id');
			$this->db->join('students s', 's.id = sb.student_id AND s.batch_id = sb.batch_id');

			if (empty($agn_id) || $agn_id == 0) {
				$this->db->where('a.teacher_id', $tid);
				$this->db->where('a.classroom_id', $cls_id);
				$this->db->where('a.classroom_id = sb.classroom_id');
			} else {
				$this->db->where('a.id', $agn_id);
				$this->db->where('t.id', $tid);
				$this->db->where('a.classroom_id', $cls_id);
			}
			if ((!empty($month) || $month != '') && (!empty($year) || $year != '')) {
				$this->db->where("DATE_FORMAT(due_date,'%m')", $month);
				$this->db->where("DATE_FORMAT(due_date,'%Y')", $year);
			}

			$this->db->group_by('student_name, student_id');
			$this->db->order_by('a.id', 'desc');
			// $this->db->limit(1); 	

			$query = $this->db->get();
			return $query->result_array();
		}

		public function list_of_assignments($tid, $cls_id, $month = '', $year = '')
		{
			$this->db->select('a.id as assignment_id, a.classroom_id, a.teacher_id, a.subject_id, a.title, a.remark, a.due_date, s.subject_name, t.teacher_name, sc.section_name as classroom_name, s.subject_name, s.subject_icon');
			$this->db->from('assignment a, subject s, teacher t, classroom c, section sc');

			$this->db->where('a.teacher_id', $tid);
			$this->db->where('t.id = a.teacher_id');
			$this->db->where('a.subject_id = s.id');
			$this->db->where('a.classroom_id', $cls_id);
			$this->db->where('c.id = a.classroom_id');
			$this->db->where('sc.id = c.section_id');

			if ((!empty($month) || $month != '') && (!empty($year) || $year != '')) {
				$this->db->where("DATE_FORMAT(due_date,'%m')", $month);
				$this->db->where("DATE_FORMAT(due_date,'%Y')", $year);
			}

			$this->db->group_by('a.id');
			$this->db->order_by('a.due_date', 'desc');
			$query = $this->db->get();
			return $query->result_array();
		}

		public function uploaded_assignment($s_id, $a_id)
		{
			if ($s_id == 0 || $s_id == '') {
				$da = $this->db->query('select * from assignments_uploaded where assignment_id =' . $a_id . ' order by id desc');
				return $da->result_array();
			}
			$da = $this->db->query('select * from assignments_uploaded where student_id = ' . $s_id . ' and assignment_id =' . $a_id . '');
			return $da->result_array();
		}



		public function show_student_assignment($asgn_id, $student_id)
		{
			$this->db->select('subject_name,au.assignment_id, teacher_name,due_date,title, section_name as classroom_name, classroom_id, a.subject_id, au.uploaded_date ,au.id as uploaded_id, au.remark as uploaded_remark');
			$this->db->from('assignment a, subject s, teacher t, students st, assignments_uploaded au, classroom c, section');

			$this->db->where('au.assignment_id', $asgn_id);
			$this->db->where('au.student_id', $student_id);
			$this->db->where('a.id', $asgn_id);
			$this->db->where('st.id', $student_id);
			$this->db->where('a.teacher_id = t.id ');
			$this->db->where('a.subject_id = s.id ');
			$this->db->where('a.classroom_id = c.id');
			$this->db->where('c.section_id = section.id');

			$query = $this->db->get();
			return $query->result_array();
		}

		public function upload_assignment($data, $id)
		{
			$this->db->where('id', $id);
			$this->db->update('assignments_uploaded', $data);
			return true;
		}

		//end of shankar code

		//Notification Models - 14-10-2021

		public function getTeachersNotification($id, $date, $tid = '')
		{
			$month = $date['month'];
			$year = $date['year'];
			if($tid != '') {
				$id = $tid;
			} else {
				$id = $_SESSION['teacher_id'];
			}

			$query = $this->db->query("Select * from notifications where (notification_to = 'teacher' OR notification_to = 'both') AND ((month(created_at) = '$month') AND (year(created_at) = '$year')) AND teacher_id = '$id' ORDER BY notification_id DESC");


			if ($query->num_rows() > 0) {
				return $query->result_array();
			} else {
				return false;
			}
		}

		public function get_notification_info($table_name, $where = '', $sqlQry = '', $order = 'DESC', $limit = FALSE, $offset = FALSE)
		{

			if ($limit)
				$this->db->limit($limit, $offset);

			if ($where != '')
				$this->db->where($where);

			$this->db->order_by($table_name . '.notification_id', $order);
			$query = $this->db->get($table_name);

			if ($sqlQry != '')
				$query = $this->db->query($sqlQry);
			return $query->result_array();
		}

		//Notification Models End - 14-10-2021

		public function get_table_info($table_name, $where = '', $sqlQry = '', $order = 'DESC', $limit = FALSE, $offset = FALSE)
		{

			if ($limit)
				$this->db->limit($limit, $offset);

			if ($where != '')
				$this->db->where($where);

			$this->db->order_by($table_name . '.id', $order);
			$query = $this->db->get($table_name);

			if ($sqlQry != '')
				$query = $this->db->query($sqlQry);
			return $query->result_array();
		}

		public function add_edit_student_attendance($data, $status, $where = '')
		{
			$this->db->trans_start();

			if ($status == 'Update') {
				$this->db->update('student_attendence', $data, $where);
			} else {
				$this->db->insert_batch('student_attendence', $data);
			}

			$this->db->trans_complete();
			if ($this->db->trans_status() === FALSE) {
				return 0;
			} else {
				return 1;
			}
		}


		//shankar code teacher syllabus view 01-11-21

		public function get_syllabus($tid)
		{
			$query = $this->db->query("SELECT cts.*,cl.* FROM classroom_teacher_subject cts LEFT JOIN classroom cl ON cl.id=cts.classroom_id WHERE cts.teacher_id=$tid");

			$syllabus = array();
			if ($query->num_rows() > 0) {
				$data =  $query->result_array();

				foreach ($data as $key => $value) {

					$sq = 'SELECT ss.*, s.subject_name, s.subject_icon FROM subject_syllabus ss, subject s WHERE ss.course_id = ' . $value['course_id'] . ' and ss.subject_id = ' . $value['subject_id'] . ' and s.id = ss.subject_id';

					$query1 = $this->db->query($sq);

					if ($query1->num_rows() > 0) {
						$data2 =  $query1->result_array();
						if ($query1->num_rows() > 0) {
							foreach ($data2 as $k => $v) {
								array_push($syllabus, array(
									'subject_syllabus_id' => $v['id'],
									'subject_id' => $v['subject_id'],
									'subject_name' => $v['subject_name'],
									'subject_icon'	=> $v['subject_icon'],
									'syllabus'		=> $v['syllabus'],
									'course_id'		=> $v['course_id'],
									'classroom_name' => $value['classroom_name']
								));
							}
						}
					}
				}
			}

			return $syllabus;
		}


		public function view_all_exam_details($tid, $cls_id)
		{
			$query = $this->db->query("SELECT questions.*,subject_name,e_name, subject_icon, classroom_name, classroom.id as classroom_id FROM questions, subject, exam, classroom WHERE teacher_id = $tid and subject.id = questions.subject_id and exam.id = questions.exam_id and questions.course_id = classroom.course_id and classroom.id = $cls_id order by questions.id desc");


			if ($query->num_rows() > 0) {

				return $query->result_array();
			}
			return false;
		}

		public function get_all_questions($e_id)
		{

			//get the classroom id
			$query = $this->db->query("SELECT questions.*,subject_name,e_name, subject_icon, classroom_name, classroom.id as classroom_id FROM questions, subject, exam, classroom WHERE questions.id = $e_id and subject.id = questions.subject_id and exam.id = questions.exam_id and questions.course_id = classroom.course_id ");

			if ($query->num_rows() > 0) {
				return $query->row_array();
			} else {
				return false;
			}
		}


		public function get_all_questions_details($exam_que_id, $e_id = '')
		{

			$this->db->select('*');
			$this->db->from('exam_questions');


			if ($exam_que_id != '' || !empty($exam_que_id)) {
				$this->db->where('exam_questions.id', $exam_que_id);
				$query = $this->db->get();
				if ($query->num_rows() > 0) {
					return $query->row_array();
				}
			} else if ($e_id) {
				$this->db->where('exam_id', $e_id);
				$this->db->order_by('question_mark');
				$query = $this->db->get();
				if ($query->num_rows() > 0) {
					return $query->result_array();
				}
			}

			return false;
		}

		public function get_count_questions_details($e_id, $marks = '', $count = '')
		{
			if ($marks != '' && $count == 0) {
				$q = 'select count(question_mark) from exam_questions where exam_id = ' . $e_id . ' and question_mark =' . $marks;
			} else if ($count == 1) {
				$q = 'select count(question_mark),exam_id,question_mark from exam_questions where exam_id = ' . $e_id . ' group by (question_mark)';
				$query = $this->db->query($q);
				if ($query->num_rows() > 0) {

					return $query->result_array();
				}
			} else {
				$q = 'select sum(question_mark),exam_id from exam_questions where exam_id = ' . $e_id;
			}

			$query = $this->db->query($q);

			if ($query->num_rows() > 0) {
				// echo $this->db->last_query();
				return $query->row_array();
			} else {
				return false;
			}
		}


		public function add_questions_details($data, $exam_questions_id)
		{
			if ($exam_questions_id != '') {
				$data['modified'] = $this->nepali_current_date;
				//cheking image if there then delete old one from folder
				if (isset($data['image']) && !empty($data['image'])) {
					$query = 'select image from exam_questions where id =' . $exam_questions_id;
					$img_q = $this->db->query($query);
					$img = $img_q->row_array();
					$source = $img['image'];
					if (file_exists($source)) {
						unlink($source);
					}
				}

				$this->db->where('id', $exam_questions_id);
				$this->db->update('exam_questions', $data);
				$updated_status = $this->db->affected_rows();
				if ($updated_status) {
					return $exam_questions_id;
				} else {
					return 0;
				}
			}
			$data['created'] = $this->nepali_current_date;
			$data['modified'] = $this->nepali_current_date;
			$this->db->insert('exam_questions', $data);
			$res = $this->db->insert_id();
			if ($res) {
				return $res;
			} else {
				return 0;
			}
		}

		//Add Test/Exam Creation end code shankar - 08-11-21

	//getStudent_id from user_api 
    public function getTeacherId($id)
    {
        $result = $this->db->query('select id from teacher where teacherId = "'. $id . '"')->row_array();
        return $result;
    }

    public function update_table($id, $data, $table) {
		$this->db->trans_start();
		$this->db->where('id', $id);
		$this->db->update($table, $data);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return 0;

		return true;
	}

	}
?>