/Src/assign_course/AssignCourse.php

https://gitlab.com/tonmoy1a/Bitm-Course-Manager · PHP · 466 lines · 389 code · 61 blank · 16 comment · 99 complexity · d786d2631669b1487a2e0949b05a56ce MD5 · raw file

  1. <?php
  2. namespace BitmCourseApp\assign_course;
  3. use PDO;
  4. include_once (dirname(__FILE__) . '/../DBConnection/DBConnection.php');
  5. class AssignCourse extends \DBConnection {
  6. public $id = '';
  7. public $coursetitle = '';
  8. public $batchno = '';
  9. public $leadtrai = '';
  10. public $assistrai = '';
  11. public $labassis = '';
  12. public $labid = '';
  13. public $startdate = '';
  14. public $enddate = '';
  15. public $starttime = '';
  16. public $endtime = '';
  17. public $day = '';
  18. public $data = '';
  19. public $viewby = '';
  20. public $action = '';
  21. public $error;
  22. public function prepare($data = '') {
  23. if (!empty($data['id'])) {
  24. $this->id = $data['id'];
  25. }
  26. if (!empty($data['course_title'])) {
  27. $this->coursetitle = $data['course_title'];
  28. }
  29. if (!empty($data['batch_no'])) {
  30. $this->batchno = $data['batch_no'];
  31. }
  32. if (!empty($data['lead_trainer'])) {
  33. $this->leadtrai = $data['lead_trainer'];
  34. }
  35. if (!empty($data['assistant_trainer'])) {
  36. $this->assistrai = $data['assistant_trainer'];
  37. }
  38. if (!empty($data['lab_assistant'])) {
  39. $this->labassis = $data['lab_assistant'];
  40. }
  41. if (!empty($data['Lab_Id'])) {
  42. $this->labid = $data['Lab_Id'];
  43. }
  44. if (!empty($data['start_date'])) {
  45. $this->startdate = $data['start_date'];
  46. }
  47. if (!empty($data['end_date'])) {
  48. $this->enddate = $data['end_date'];
  49. }
  50. if (!empty($data['start_time'])) {
  51. $this->starttime = $data['start_time'];
  52. }
  53. if (!empty($data['end_time'])) {
  54. $this->endtime = $data['end_time'];
  55. }
  56. if (!empty($data['day_session'])) {
  57. $this->day = $data['day_session'];
  58. }
  59. if (!empty($data['viewBy'])) {
  60. $this->viewby = $data['viewBy'];
  61. }
  62. if (!empty($data['action'])) {
  63. $this->action = $data['action'];
  64. }
  65. return $this;
  66. }
  67. public function assignCourseValidation() {
  68. $required = array(
  69. 'Course_Tilte' => $this->coursetitle,
  70. 'Batch_No' => $this->batchno,
  71. 'Lead_Trainer' => $this->leadtrai,
  72. 'Assis_Trainer' => $this->assistrai,
  73. 'Lab_Assistant' => $this->labassis,
  74. 'Lab_Id' => $this->labid,
  75. 'Start_Time' => $this->starttime,
  76. 'End_Time' => $this->endtime,
  77. 'Day' => $this->day
  78. );
  79. foreach ($required as $key => $field) {
  80. if (empty($field)) {
  81. $_SESSION["$key"] = str_replace('_', ' ', $key) . ' Required';
  82. $this->error = TRUE;
  83. }
  84. }
  85. $explode_start_date_input = explode('/', $this->startdate);
  86. $explode_end_date_input = explode('/', $this->enddate);
  87. $start_date_input = $explode_start_date_input[2] . $explode_start_date_input[1] . $explode_start_date_input[0];
  88. $end_date_input = $explode_end_date_input[2] . $explode_end_date_input[1] . $explode_end_date_input[0];
  89. if ($start_date_input > $end_date_input) {
  90. $_SESSION['inv_date'] = 'Invalid Date';
  91. $this->error = TRUE;
  92. }
  93. if ($this->starttime == $this->endtime) {
  94. $_SESSION['inv_time'] = 'Invalid Time';
  95. $this->error = TRUE;
  96. }
  97. if ($this->day == 'sess-3') {
  98. if (empty($this->startdate)) {
  99. $this->startdate = 'N/A';
  100. }
  101. if (empty($this->enddate)) {
  102. $this->enddate = 'N/A';
  103. }
  104. } else {
  105. if (empty($this->startdate) || $this->startdate == 'N/A') {
  106. $_SESSION['Start_Date'] = "Start Date Required";
  107. $this->error = TRUE;
  108. }
  109. if (empty($this->enddate) || $this->enddate == 'N/A') {
  110. $_SESSION['End_Date'] = "End Date Required";
  111. $this->error = TRUE;
  112. }
  113. }
  114. if ($this->error == FALSE) {
  115. $query = $this->conn->query("SELECT * FROM course_trainer_lab_mapping WHERE id='$this->id' ");
  116. $row = $query->fetch();
  117. if ($row['day'] == $this->day && $row['lab_id'] == $this->labid && $row['start_date'] == $this->startdate && $row['ending_date'] == $this->enddate && $row['start_time'] == $this->starttime && $row['ending_time'] == $this->endtime) {
  118. } else {
  119. if ($this->day !== 'sess-3') {
  120. try {
  121. //input date match format
  122. $explode_start_date_input = explode('/', $this->startdate);
  123. $explode_end_date_input = explode('/', $this->enddate);
  124. $start_date_input = $explode_start_date_input[2] . $explode_start_date_input[1] . $explode_start_date_input[0];
  125. $end_date_input = $explode_end_date_input[2] . $explode_end_date_input[1] . $explode_end_date_input[0];
  126. $vq = "SELECT * FROM `course_trainer_lab_mapping` WHERE is_running=1 ";
  127. $view_query = $this->conn->prepare($vq);
  128. $view_query->execute();
  129. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  130. if ($row['day'] == $this->day && $row['lab_id'] == $this->labid) {
  131. //database date match format
  132. $explode_start_date = explode('/', $row['start_date']);
  133. $explode_end_date = explode('/', $row['ending_date']);
  134. $start_date = $explode_end_date[2] . $explode_start_date[1] . $explode_start_date[0];
  135. $end_date = $explode_end_date[2] . $explode_end_date[1] . $explode_end_date[0];
  136. if ((($start_date_input <= $start_date && $end_date_input >= $start_date) || ($start_date_input <= $end_date && $end_date_input >= $end_date)) ||
  137. (($start_date <= $start_date_input && $end_date >= $start_date_input) || ($start_date <= $end_date_input && $end_date >= $end_date_input))) {
  138. //echo 'Date Match.';
  139. //time from input
  140. $time_start_input_string = strtotime($this->starttime);
  141. $time_end_input_string = strtotime($this->endtime);
  142. $start_time_input = date("Hi", $time_start_input_string);
  143. $end_time_input = date("Hi", $time_end_input_string);
  144. // time from database
  145. $time_start_db = $row['start_time'];
  146. $time_end_db = $row['ending_time'];
  147. $time_start_string = strtotime($time_start_db);
  148. $time_end_string = strtotime($time_end_db);
  149. $start_time = date("Hi", $time_start_string);
  150. $end_time = date("Hi", $time_end_string);
  151. if (($start_time_input <= $start_time && $end_time_input > $start_time) || ($start_time_input < $end_time && $end_time_input > $end_time)) {
  152. //Final Step Time Found
  153. $_SESSION['time_match'] = "Same time, Same Room, Same Day Match Found";
  154. $_SESSION['match_id'] = $row['id'];
  155. $this->error = TRUE;
  156. } else {
  157. if (($start_time < $start_time_input && $end_time > $start_time_input) || ($start_time < $end_time_input && $end_time > $end_time_input)) {
  158. //Final Step Time Found
  159. $_SESSION['time_match'] = "Same time, Same Room, Same Day Match Found";
  160. $_SESSION['match_id'] = $row['id'];
  161. $this->error = TRUE;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. } catch (Exception $ex) {
  168. }
  169. }
  170. }
  171. //die();
  172. }
  173. if ($this->error == TRUE) {
  174. $input_value = array(
  175. 'Course_Tilte' => $this->coursetitle,
  176. 'Batch_No' => $this->batchno,
  177. 'Lead_Trainer' => $this->leadtrai,
  178. 'Assis_Trainer' => $this->assistrai,
  179. 'Lab_Assistant' => $this->labassis,
  180. 'Lab_Id' => $this->labid,
  181. 'Start_Date' => $this->startdate,
  182. 'End_Date' => $this->enddate,
  183. 'Start_Time' => $this->starttime,
  184. 'End_Time' => $this->endtime,
  185. 'Day' => $this->day
  186. );
  187. foreach ($input_value as $key => $i_value) {
  188. if (!empty($i_value)) {
  189. $_SESSION["v_$key"] = $i_value;
  190. }
  191. }
  192. }
  193. }
  194. public function store() {
  195. if ($this->error == FALSE) {
  196. try {
  197. $query = "INSERT INTO `course_trainer_lab_mapping` (`id`, `course_id`, `batch_no`, `lead_trainer`, `asst_trainer`, `lab_asst`, `lab_id`, `start_date`, `ending_date`, `start_time`, `ending_time`, `day`, `is_running`, `assigned_by`, `created`, `updated`, `deleted`) VALUES ('', :courseid , :batchno, :leadtrainer , :assttrainer , :labasst , :labid , :startdate , :endingdate , :starttime , :endtime , :day , :isrunning , :assignby , :created , '', '')";
  198. $stmt = $this->conn->prepare($query);
  199. $stmt->execute(array(
  200. ':courseid' => $this->coursetitle,
  201. ':batchno' => $this->batchno,
  202. ':leadtrainer' => $this->leadtrai,
  203. ':assttrainer' => $this->assistrai,
  204. ':labasst' => $this->labassis,
  205. ':labid' => $this->labid,
  206. ':startdate' => $this->startdate,
  207. ':endingdate' => $this->enddate,
  208. ':starttime' => $this->starttime,
  209. ':endtime' => $this->endtime,
  210. ':day' => $this->day,
  211. ':isrunning' => 1,
  212. ':assignby' => $_SESSION['login_confirm']['username'],
  213. ':created' => date("Y-m-d h:i:s"),
  214. ));
  215. $_SESSION['message'] = "New Course Assigned Successfully.";
  216. } catch (PDOException $e) {
  217. echo 'Error: ' . $e->getMessage();
  218. }
  219. }
  220. header('location:assign_new_course.php');
  221. }
  222. public function updateAssignCourse() {
  223. if ($this->error == FALSE) {
  224. try {
  225. $query = "UPDATE `course_trainer_lab_mapping` SET `course_id` = :course_id , `batch_no` = :batch_no , `lead_trainer` = :lead_trainer , `asst_trainer` = :asst_trainer , `lab_asst` = :lab_asst , `lab_id` = :lab_id , `start_date` = :start_date , `ending_date` = :ending_date , `start_time` = :start_time , `ending_time` = :ending_time , `day` = :day , `updated` = :updated WHERE `course_trainer_lab_mapping`.`id` = $this->id ";
  226. $stmt = $this->conn->prepare($query);
  227. $stmt->execute(array(
  228. ':course_id' => $this->coursetitle,
  229. ':batch_no' => $this->batchno,
  230. ':lead_trainer' => $this->leadtrai,
  231. ':asst_trainer' => $this->assistrai,
  232. ':lab_asst' => $this->labassis,
  233. ':lab_id' => $this->labid,
  234. ':start_date' => $this->startdate,
  235. ':ending_date' => $this->enddate,
  236. ':start_time' => $this->starttime,
  237. ':ending_time' => $this->endtime,
  238. ':day' => $this->day,
  239. ':updated' => date("Y-m-d h:i:s"),
  240. ));
  241. $_SESSION['message'] = "User Data Updated Successfully.";
  242. } catch (PDOException $e) {
  243. echo 'Error: ' . $e->getMessage();
  244. }
  245. }
  246. header("location:edit_assign_course.php?id=$this->id");
  247. }
  248. public function viewAssignedCourse() {
  249. try {
  250. if ($this->viewby == 'running') {
  251. $vq = "SELECT * FROM `course_trainer_lab_mapping` WHERE is_running=1 ";
  252. } else {
  253. $vq = "SELECT * FROM `course_trainer_lab_mapping` WHERE is_running=0 ";
  254. }
  255. $view_query = $this->conn->prepare($vq);
  256. $view_query->execute();
  257. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  258. $this->data[] = $row;
  259. }
  260. return $this->data;
  261. } catch (Exception $ex) {
  262. }
  263. }
  264. public function showAssignedCourse() {
  265. try {
  266. $query = $this->conn->query("SELECT * FROM course_trainer_lab_mapping WHERE id='$this->id' ");
  267. $row = $query->fetch();
  268. return $row;
  269. } catch (Exception $ex) {
  270. }
  271. return $row;
  272. }
  273. public function courseTitle() {
  274. try {
  275. $vq = "SELECT * FROM `courses` ";
  276. $view_query = $this->conn->prepare($vq);
  277. $view_query->execute();
  278. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  279. $course_title[] = $row;
  280. }
  281. return $course_title;
  282. } catch (Exception $ex) {
  283. }
  284. }
  285. public function leadTrainer() {
  286. try {
  287. $vq = "SELECT * FROM `trainers` WHERE `trainer_status` = 'lead_trainer' ";
  288. $view_query = $this->conn->prepare($vq);
  289. $view_query->execute();
  290. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  291. $lead_trainer[] = $row;
  292. }
  293. return $lead_trainer;
  294. } catch (Exception $ex) {
  295. }
  296. }
  297. public function assitTrainer() {
  298. try {
  299. $vq = "SELECT * FROM `trainers` WHERE `trainer_status` = 'assist_trainer' ";
  300. $view_query = $this->conn->prepare($vq);
  301. $view_query->execute();
  302. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  303. $assit_trainer[] = $row;
  304. }
  305. return $assit_trainer;
  306. } catch (Exception $ex) {
  307. }
  308. }
  309. public function labAssit() {
  310. try {
  311. $vq = "SELECT * FROM `trainers` WHERE `trainer_status` = 'lab_assist' ";
  312. $view_query = $this->conn->prepare($vq);
  313. $view_query->execute();
  314. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  315. $lab_assit[] = $row;
  316. }
  317. return $lab_assit;
  318. } catch (Exception $ex) {
  319. }
  320. }
  321. public function labInfo() {
  322. try {
  323. $vq = "SELECT * FROM `labinfo` ";
  324. $view_query = $this->conn->prepare($vq);
  325. $view_query->execute();
  326. while ($row = $view_query->fetch(PDO::FETCH_ASSOC)) {
  327. $lab_info[] = $row;
  328. }
  329. return $lab_info;
  330. } catch (Exception $ex) {
  331. }
  332. }
  333. public function search($search = "") { //print_r($search);
  334. //die();
  335. if ($search['search_key'] == 'course' && !empty($search['var1'])) {
  336. $keywords = $search['var1'];
  337. //print_r($keywords);
  338. //die();
  339. $keywords = preg_split('/[\s]+/', $keywords);
  340. $totalKeywords = count($keywords);
  341. $query = "SELECT * FROM courses LEFT JOIN course_trainer_lab_mapping on courses.id = course_trainer_lab_mapping.course_id LEFT JOIN labinfo on courses.id = labinfo.course_id WHERE courses.title LIKE ?";
  342. for ($i = 1; $i < $totalKeywords; $i++) {
  343. $query .= " AND title LIKE ? ";
  344. }
  345. $sql = $this->conn->prepare($query);
  346. foreach ($keywords as $key => $keyword) {
  347. $search = '%' . $keyword . '%';
  348. $sql->bindParam($key + 1, $search);
  349. }
  350. $sql->execute();
  351. if ($sql->rowCount() > 0) {
  352. while ($result = $sql->fetch(PDO::FETCH_ASSOC)) {
  353. //echo"<pre>".print_r($result,true)."</pre>";
  354. $this->data[] = $result;
  355. }
  356. } else {
  357. $_SESSION['msg'] = 'There was nothing to show. Please search with right keyword.';
  358. }
  359. //return $this->data;
  360. } elseif ($search['search_key'] == 'lab' && !empty($search['var1'])) {
  361. $keywords = $search['var1'];
  362. //print_r($keywords);
  363. //die();
  364. $keywords = preg_split('/[\s]+/', $keywords);
  365. $totalKeywords = count($keywords);
  366. $query = "SELECT * FROM labinfo LEFT JOIN course_trainer_lab_mapping on labinfo.id = course_trainer_lab_mapping.lab_id LEFT JOIN courses on course_trainer_lab_mapping.course_id = courses.id WHERE labinfo.lab_no LIKE ?";
  367. for ($i = 1; $i < $totalKeywords; $i++) {
  368. $query .= " AND lab_no LIKE ? ";
  369. }
  370. $sql = $this->conn->prepare($query);
  371. foreach ($keywords as $key => $keyword) {
  372. $search = '%' . $keyword . '%';
  373. $sql->bindParam($key + 1, $search);
  374. }
  375. $sql->execute();
  376. if ($sql->rowCount() > 0) {
  377. while ($result = $sql->fetch(PDO::FETCH_ASSOC)) {
  378. //echo"<pre>".print_r($result,true)."</pre>";
  379. $this->data[] = $result;
  380. }
  381. } else {
  382. $_SESSION['msg'] = 'There was nothing to show. Please search with right keyword.';
  383. }
  384. } else {
  385. echo "Please search with right keyword";
  386. }
  387. return $this->data;
  388. }
  389. public function deleteAssigned() {
  390. try {
  391. if ($this->action == 'restore') {
  392. $query = "UPDATE `course_trainer_lab_mapping` SET `is_running` = '1', `deleted` = :date WHERE `course_trainer_lab_mapping`.`id` = $this->id";
  393. } else {
  394. $query = "UPDATE `course_trainer_lab_mapping` SET `is_running` = '0', `deleted` = :date WHERE `course_trainer_lab_mapping`.`id` = $this->id";
  395. }
  396. $stmt = $this->conn->prepare($query);
  397. $stmt->execute(array(
  398. ':date' => date("Y-m-d h:i:s")
  399. ));
  400. header("location:view_assigned_course.php?viewBy=$this->viewby");
  401. } catch (PDOException $e) {
  402. echo 'Error: ' . $e->getMessage();
  403. }
  404. }
  405. }