PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/MappingInfo/MappingInfo.php

https://gitlab.com/cserobiul/BITM-LabMs-New
PHP | 517 lines | 377 code | 95 blank | 45 comment | 51 complexity | fa2022ea11b6fd62bca750a0517bf6f5 MD5 | raw file
  1. <?php
  2. namespace Apps\MappingInfo;
  3. use PDO;
  4. use PDOException;
  5. class MappingInfo {
  6. public $id, $course_id, $batch_no,$lead_trainer,$asst_trainer,$lab_asst,$lab_id;
  7. public $start_date,$ending_date,$start_time,$ending_time;
  8. public $day;
  9. public $data;
  10. public $assigned_by;
  11. public function __construct() {
  12. if (session_status() == PHP_SESSION_NONE) {
  13. session_start();
  14. }
  15. try {
  16. $this->conn = new PDO('mysql:host=localhost;dbname=lab', 'root', '');
  17. } catch (PDOException $e) {
  18. echo 'Error: ' . $e->getMessage();
  19. }
  20. }
  21. // assign data into variable
  22. public function assign($data) {
  23. if (!empty($data['courseName'])) {
  24. $this->course_id = $data['courseName'];
  25. }
  26. if (!empty($data['labNo'])) {
  27. $this->lab_id = $data['labNo'];
  28. }
  29. if (!empty($data['lead_trainer'])) {
  30. $this->lead_trainer = $data['lead_trainer'];
  31. }
  32. if (!empty($data['asst_trainer'])) {
  33. $this->asst_trainer = $data['asst_trainer'];
  34. }
  35. if (!empty($data['lab_asst'])) {
  36. $this->lab_asst = $data['lab_asst'];
  37. }
  38. if (!empty($data['batch_no'])) {
  39. $this->batch_no = $data['batch_no'];
  40. }
  41. if (!empty($data['start_date'])) {
  42. $originalDate = $data['start_date'];
  43. $newDate = date("Y-m-d", strtotime($originalDate));
  44. $this->start_date = $newDate;
  45. }
  46. if (!empty($data['ending_date'])) {
  47. $ending_date = $data['ending_date'];
  48. $ending_date = date("Y-m-d", strtotime($ending_date));
  49. $this->ending_date = $ending_date;
  50. }
  51. if (!empty($data['start_time'])) {
  52. $this->start_time = $data['start_time'];
  53. }
  54. if (!empty($data['ending_time'])) {
  55. $this->ending_time = $data['ending_time'];
  56. }
  57. if (!empty($data['day'])) {
  58. $this->day = $data['day'];
  59. }
  60. if (!empty($data['assigned_by'])) {
  61. $this->assigned_by = $_SESSION['loginUser'];
  62. }
  63. if (!empty($data['id'])) {
  64. $this->id = $data['id'];
  65. }
  66. return $this;
  67. }
  68. public function getLab() {
  69. try {
  70. $qry = "SELECT * FROM labinfo WHERE course_id = :cid";
  71. $q = $this->conn->prepare($qry);
  72. $q->execute(array(
  73. ':cid' => '84832sd8384d6d',
  74. ));
  75. $rows = $q->rowCount();
  76. if($rows>0){
  77. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  78. $this->data[] = $row;
  79. }
  80. return $this->data;
  81. }else{
  82. return $rows;
  83. }
  84. } catch (Exception $ex) {
  85. echo 'Error: ' . $ex->getMessage();
  86. }
  87. return $this;
  88. }
  89. // chk today's class schedule
  90. public function todaysClass(){
  91. $tdate = date('Y-m-d', time() - 6*4800);
  92. try{
  93. $qry = "SELECT * FROM course_trainer_lab_mapping
  94. WHERE start_date <= :sd && ending_date >= :sd
  95. ORDER BY start_time ASC";
  96. $q = $this->conn->prepare($qry);
  97. $q->execute(array(
  98. ':sd' => $tdate,
  99. ));
  100. $rows = $q->rowCount();
  101. if($rows>0){
  102. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  103. $this->data[] = $row;
  104. }
  105. return $this->data;
  106. }else{
  107. return $rows;
  108. }
  109. }catch (PDOException $e){
  110. echo 'Error: '.$e->getMessage;
  111. }
  112. return $this;
  113. }
  114. // assign courses (store)
  115. public function store() {
  116. // if (!empty($this->courseName) && !empty($this->labNo) && !empty($this->seatCapacity)) {
  117. try {
  118. $query = "INSERT INTO course_trainer_lab_mapping (course_id, batch_no, lead_trainer, asst_trainer, lab_asst, lab_id, start_date, ending_date, start_time,ending_time,day,assigned_by, created)
  119. VALUES(:cid,:bid,:lt,:at,:la,:lid,:sd,:ed,:st,:et,:day,:assby,:created)";
  120. $q = $this->conn->prepare($query);
  121. $cdate = date('Y-m-d, H:i', time() - 6*4800);
  122. $q->execute(array(
  123. ':cid' => $this->course_id,
  124. ':bid' => $this->batch_no,
  125. ':lt' => $this->lead_trainer,
  126. ':at' => $this->asst_trainer,
  127. ':la' => $this->lab_asst,
  128. ':lid' => $this->lab_id,
  129. ':sd' => $this->start_date,
  130. ':ed' => $this->ending_date,
  131. ':st' => $this->start_time,
  132. ':et' => $this->ending_time,
  133. ':day' => $this->day,
  134. ':assby' => $_SESSION['loginUser'],
  135. ':created' => $cdate,
  136. ));
  137. $rows = $q->rowCount();
  138. return $rows;
  139. } catch (PDOException $e) {
  140. echo 'Error: ' . $e->getMessage();
  141. }
  142. // } else {
  143. // echo '<script type="text/javascript">location.replace("?p=addLab");</script>';
  144. // }
  145. return $this;
  146. }
  147. // batch already exist ?
  148. public function chkBatchno(){
  149. try{
  150. $qry = "SELECT * FROM course_trainer_lab_mapping
  151. WHERE course_id = :cid && batch_no = :bid ";
  152. $q = $this->conn->prepare($qry);
  153. $q->execute(array(
  154. ':cid' => $this->course_id,
  155. ':bid' => $this->batch_no,
  156. ));
  157. return $rows = $q->rowCount();
  158. }catch (PDOException $e){
  159. echo 'Error: '.$e->getMessage;
  160. }
  161. return $this;
  162. }
  163. // Lab busy ?
  164. public function labShedule(){
  165. try{
  166. $qry = "SELECT * FROM course_trainer_lab_mapping
  167. WHERE day = :day && lab_id = :lid && start_date <= :sd && ending_date >= :sd
  168. && start_time <= :st && ending_time >=:et ";
  169. $q = $this->conn->prepare($qry);
  170. $q->execute(array(
  171. ':sd' => $this->start_date,
  172. ':ed' => $this->ending_date,
  173. ':day' => $this->day,
  174. ':st' => $this->start_time,
  175. ':et' => $this->ending_time,
  176. ':lid' => $this->lab_id
  177. ));
  178. return $rows = $q->rowCount();
  179. }catch (PDOException $e){
  180. echo 'Error: '.$e->getMessage;
  181. }
  182. return $this;
  183. }
  184. // lead trainer busy ?
  185. public function leadTrainerShedule(){
  186. try{
  187. $qry = "SELECT * FROM course_trainer_lab_mapping
  188. WHERE day = :day && lead_trainer = :lt && start_date <= :sd && ending_date >= :sd
  189. && start_time <= :st && ending_time >=:et ";
  190. $q = $this->conn->prepare($qry);
  191. $q->execute(array(
  192. ':sd' => $this->start_date,
  193. ':ed' => $this->ending_date,
  194. ':day' => $this->day,
  195. ':st' => $this->start_time,
  196. ':et' => $this->ending_time,
  197. ':lt' => $this->lead_trainer
  198. ));
  199. return $rows = $q->rowCount();
  200. }catch (PDOException $e){
  201. echo 'Error: '.$e->getMessage;
  202. }
  203. return $this;
  204. }
  205. // assist trainer busy ?
  206. public function assistTrainerShedule(){
  207. try{
  208. $qry = "SELECT * FROM course_trainer_lab_mapping
  209. WHERE day = :day && asst_trainer = :ast && start_date <= :sd && ending_date >= :sd
  210. && start_time <= :st && ending_time >=:et ";
  211. $q = $this->conn->prepare($qry);
  212. $q->execute(array(
  213. ':sd' => $this->start_date,
  214. ':ed' => $this->ending_date,
  215. ':day' => $this->day,
  216. ':st' => $this->start_time,
  217. ':et' => $this->ending_time,
  218. ':ast' => $this->asst_trainer
  219. ));
  220. return $rows = $q->rowCount();
  221. }catch (PDOException $e){
  222. echo 'Error: '.$e->getMessage;
  223. }
  224. return $this;
  225. }
  226. // lab assistant busy ?
  227. public function labAsstShedule(){
  228. try{
  229. $qry = "SELECT * FROM course_trainer_lab_mapping
  230. WHERE day = :day && lab_asst = :labas && start_date <= :sd && ending_date >= :sd
  231. && start_time <= :st && ending_time >=:et ";
  232. $q = $this->conn->prepare($qry);
  233. $q->execute(array(
  234. ':sd' => $this->start_date,
  235. ':ed' => $this->ending_date,
  236. ':day' => $this->day,
  237. ':st' => $this->start_time,
  238. ':et' => $this->ending_time,
  239. ':labas' => $this->lab_asst
  240. ));
  241. return $rows = $q->rowCount();
  242. }catch (PDOException $e){
  243. echo 'Error: '.$e->getMessage;
  244. }
  245. return $this;
  246. }
  247. // get lab list
  248. public function getLabList() {
  249. try {
  250. $qry = "SELECT * FROM labinfo ORDER BY lab_no ASC";
  251. $q = $this->conn->query($qry);
  252. $rows = $q->rowCount();
  253. if($rows>0){
  254. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  255. $this->data[] = $row;
  256. }
  257. return $this->data;
  258. }else{
  259. return $rows;
  260. }
  261. } catch (Exception $ex) {
  262. echo 'Error: ' . $ex->getMessage();
  263. }
  264. return $this;
  265. }
  266. public function update() {
  267. $tdate = date('Y-m-d, H:i', time() - 6*4800);
  268. try {
  269. $trushQry = "UPDATE course_trainer_lab_mapping SET
  270. batch_no = :bno,
  271. updated = :updated
  272. WHERE course_id = :id";
  273. $q = $this->conn->prepare($trushQry);
  274. $q->execute(array(
  275. ':bno' => $this->batch_no,
  276. ':updated' => $tdate,
  277. ':id' => $this->id
  278. ));
  279. $rows = $q->rowCount();
  280. return $rows;
  281. } catch (Exception $ex) {
  282. echo 'Error: ' . $ex->getMessage();
  283. }
  284. }
  285. // update lab info
  286. // public function update() {
  287. // echo $this->id;
  288. // echo '<br>';
  289. // echo $this->batch_no;
  290. // //die();
  291. //
  292. // try {
  293. // $upQry = "UPDATE course_trainer_lab_mapping SET
  294. // batch_no = :bn
  295. // WHERE course_id = :id";
  296. // $q = $this->conn->prepare($upQry);
  297. // $q->execute(array(
  298. // ':bn' => $this->batch_no,
  299. // ':id' => $this->id
  300. // ));
  301. //
  302. // $rows = $q->rowCount();
  303. // return $rows;
  304. // } catch (Exception $ex) {
  305. // echo 'Error: ' . $ex->getMessage();
  306. // }
  307. //
  308. // return $this;
  309. // }
  310. // delete form database
  311. public function trush() {
  312. $tdate = date('Y-m-d, H:i', time() - 6*4800);
  313. try {
  314. $trushQry = "UPDATE course_trainer_lab_mapping SET
  315. is_delete = :tid,
  316. updated = :updated
  317. WHERE id = :id";
  318. $q = $this->conn->prepare($trushQry);
  319. $q->execute(array(
  320. ':tid' => '1',
  321. ':updated' => $tdate,
  322. ':id' => $this->id
  323. ));
  324. $rows = $q->rowCount();
  325. return $rows;
  326. } catch (Exception $ex) {
  327. echo 'Error: ' . $ex->getMessage();
  328. }
  329. }
  330. // lab show function
  331. public function show() {
  332. try {
  333. $loginQuery = "SELECT * FROM course_trainer_lab_mapping WHERE course_id = :cid ";
  334. $q = $this->conn->prepare($loginQuery);
  335. $q->execute(array(
  336. ':cid' => $this->id,
  337. ));
  338. $rowCount = $q->rowCount();
  339. if ($rowCount > 0) {
  340. $row = $q->fetch(PDO::FETCH_ASSOC);
  341. return $row;
  342. } else {
  343. return $rowCount;
  344. }
  345. } catch (PDOException $e) {
  346. echo 'Error: ' . $e->getMessage();
  347. }
  348. return $this;
  349. }
  350. // get all data from database
  351. public function index() {
  352. $today = date('Y-m-d', time() - 6*4800);
  353. try {
  354. $qry = "SELECT * FROM course_trainer_lab_mapping WHERE ending_date >= '$today' AND is_delete = 0 ORDER BY created DESC";
  355. $q = $this->conn->query($qry) or die(mysql_error());
  356. $rows = $q->rowCount();
  357. if($rows>0){
  358. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  359. $this->data[] = $row;
  360. }
  361. return $this->data;
  362. }else{
  363. return $rows;
  364. }
  365. } catch (Exception $ex) {
  366. echo 'Error: ' . $ex->getMessage();
  367. }
  368. return $this;
  369. }
  370. // get course name from id
  371. public function getCourseName(){
  372. try {
  373. $Query = "SELECT * FROM courses WHERE unique_id = :id";
  374. $q = $this->conn->prepare($Query);
  375. $q->execute(array(
  376. ':id' => $this->courseName,
  377. ));
  378. $rowCount = $q->rowCount();
  379. if ($rowCount > 0) {
  380. $data = $q->fetch(PDO::FETCH_ASSOC);
  381. return $data;
  382. }else{
  383. return $rowCount;
  384. }
  385. } catch (PDOException $e) {
  386. echo 'Error: ' . $e->getMessage();
  387. }
  388. return $this;
  389. }
  390. // get runnig courses
  391. public function getRunningCourses(){
  392. try {
  393. //$Query = "SELECT COUNT(course_id) FROM course_trainer_lab_mapping;";
  394. $Query = "SELECT DISTINCT course_id FROM course_trainer_lab_mapping";
  395. $q = $this->conn->query($Query);
  396. $rowCount = $q->rowCount();
  397. return $rowCount;
  398. } catch (PDOException $e) {
  399. echo "Error: " . $e->getMessage();
  400. }
  401. return $this;
  402. }
  403. // get course list
  404. public function getCourseList() {
  405. try {
  406. $Query = "SELECT * FROM courses ORDER BY title ASC";
  407. $q = $this->conn->query($Query);
  408. $rowCount = $q->rowCount();
  409. if ($rowCount > 0) {
  410. while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
  411. $this->data[] = $row;
  412. }
  413. return $this->data;
  414. } else {
  415. return $rowCount;
  416. }
  417. } catch (PDOException $e) {
  418. echo "Error: " . $e->getMessage();
  419. }
  420. return $this;
  421. }
  422. }