Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.16 KB | None | 0 0
  1. <?php
  2.  
  3. //include_once "http.php";
  4.  
  5. class Service{
  6.  
  7. public $dbh;
  8. public $host='localhost';
  9. public $dbname='tutor';
  10. public $username='root';
  11. public $password='';
  12.  
  13.  
  14. //connect to database , initiates the handler
  15. public function connect($host='localhost',$dbname='irelandt_ireland',$username='irelandt_vlat',$password='vlat@123'){
  16. try {
  17.  
  18.  
  19. $this->dbh=new PDO('mysql:host='.$host.';dbname='.$dbname.";charset=utf8",$username,$password);
  20. $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  21. } catch (Exception $e) {
  22. die("this site in under maintenance");
  23. }
  24. }
  25.  
  26. /*
  27. public function connect(){
  28. $this->dbh=new PDO('mysql:host=localhost','irelandt_vlat','vlat@123');
  29. }*/
  30.  
  31. public function disconnect(){
  32. $this->dbh=null;
  33. }
  34.  
  35. public function splitAndAddStars($var){
  36.  
  37. if(strlen($var)>0){
  38.  
  39. $tab = explode(" ", $var);
  40. $n=count($tab);
  41. $res="";
  42. for($i=0;$i<$n;$i++){
  43. if(strlen($tab[$i])>0)
  44. $res=$res.'+'.$tab[$i].'* ';
  45. }
  46. return $res;
  47. }else{
  48.  
  49. return '';
  50. }
  51. }
  52.  
  53.  
  54.  
  55. public function getAllCourses(){
  56. $this->connect();
  57. $stm = $this->dbh->prepare("select * from courses");
  58. $stm->execute();
  59. $courses = $stm->fetchAll();
  60. return $courses;
  61. }
  62.  
  63. /*public function getTutors($x,$courseid){
  64. $this->connect();
  65. $q="SELECT *, match(firstname,lastname,email,telephone,address,title,description) against(:var in boolean mode)
  66. as relevance1
  67. from users left join tutorscourses on users.userid = tutorscourses.tutorid where tutor = 1 AND
  68. (:courseid=0 OR tutorscourses.courseid=:courseid) AND
  69. match(firstname,lastname,email,telephone,address,title,description) against(:var in boolean mode)
  70. order by relevance1";
  71. $emptyQ="select * from users left join
  72. tutorscourses on users.userid = tutorscourses.tutorid
  73. where tutor = 1 AND
  74. (:courseid=0 OR tutorscourses.courseid=:courseid)
  75. order by firstname,lastname";
  76. $r=$q;
  77. if(empty($x)){
  78. $r=$emptyQ;
  79. }
  80. $sth=$this->dbh->prepare($r);
  81. $var=$this->splitAndAddStars($x);
  82. $sth->bindParam(':var',$var);
  83. $sth->bindParam(':courseid',$courseid);
  84. $sth->execute();
  85. $users=$sth->fetchAll();
  86. $this->disconnect();
  87. return $users;
  88. }*/
  89.  
  90. public function getTutors($x,$courseid){
  91. $this->connect();
  92. $q="SELECT *,(SELECT avg(rating) FROM ratings WHERE ratings.tutorid=users.userid) rating, match(firstname,lastname,email,telephone,address,descriptionStudent) against(:var in boolean mode)
  93. as relevance1
  94. from users where tutor = 1 and
  95. match(firstname,lastname,email,telephone,address,descriptionStudent) against(:var in boolean mode)
  96. order by relevance1";
  97. $emptyQ="SELECT *,(SELECT avg(rating) FROM ratings WHERE ratings.tutorid=users.userid) as rating from users where tutor=1 order by firstname,lastname";
  98. $r=$q;
  99. if(empty($x)){
  100. $r=$emptyQ;
  101. }
  102. $sth=$this->dbh->prepare($r);
  103. $var=$this->splitAndAddStars($x);
  104. $sth->bindParam(':var',$var);
  105. $sth->bindParam(':courseid',$courseid);
  106. $sth->execute();
  107. $users=$sth->fetchAll();
  108. $this->disconnect();
  109. return $users;
  110. }
  111.  
  112. public function getStudents($x){
  113. $this->connect();
  114. $q="SELECT *, match(firstname,lastname,email,telephone,address,descriptionStudent) against(:var in boolean mode)
  115. as relevance1
  116. from users where
  117. match(firstname,lastname,email,telephone,address,descriptionStudent) against(:var in boolean mode)
  118. order by relevance1";
  119. $emptyQ="select * from users order by firstname,lastname";
  120. $r=$q;
  121. if(empty($x)){
  122. $r=$emptyQ;
  123. }
  124. $sth=$this->dbh->prepare($r);
  125. $var=$this->splitAndAddStars($x);
  126. $sth->bindParam(':var',$var);
  127. $sth->execute();
  128. $users=$sth->fetchAll();
  129. $this->disconnect();
  130. return $users;
  131. }
  132.  
  133.  
  134.  
  135. public function findWord($str,$x){
  136.  
  137. $split = explode(" ",$str);
  138.  
  139. for($i=0;$i<Count($split);$i++){
  140.  
  141. if(strlen($split[$i]) > 2 && stripos($split[$i],$x) !== false){
  142. return $split[$i];
  143.  
  144. }
  145.  
  146. }
  147.  
  148. return false;
  149.  
  150. }
  151.  
  152.  
  153.  
  154. public function predictStudents($x){
  155.  
  156. $users = $this->getStudents($x);
  157. $split = explode(" ", $x);
  158. $n = Count($split);
  159. $resArr=array();
  160. foreach ($users as $row) {
  161.  
  162.  
  163. $res="";
  164.  
  165.  
  166. for($i=0;$i<$n;$i++){
  167.  
  168.  
  169. $found=$this->findWord($row['firstname'],$split[$i]);
  170.  
  171. if($found!==false){
  172. if(stripos($res,$found) ===false)
  173. $res = $res." ".$found;
  174.  
  175. }
  176.  
  177.  
  178. $found=$this->findWord($row['lastname'],$split[$i]);
  179.  
  180. if($found!==false){
  181. if(stripos($res,$found) ===false)
  182. $res = $res." ".$found;
  183.  
  184. }
  185.  
  186.  
  187. $found=$this->findWord($row['address'],$split[$i]);
  188.  
  189. if($found!==false){
  190. if(stripos($res,$found) ===false)
  191. $res = $res." ".$found;
  192.  
  193. }
  194.  
  195.  
  196. $found=$this->findWord($row['title'],$split[$i]);
  197.  
  198. if($found!==false){
  199. if(stripos($res,$found) ===false)
  200. $res = $res." ".$found;
  201.  
  202. }
  203.  
  204.  
  205. $found=$this->findWord($row['descriptionStudent'],$split[$i]);
  206.  
  207. if($found!==false){
  208. if(stripos($res,$found) ===false)
  209. $res = $res." ".$found;
  210.  
  211. }
  212.  
  213.  
  214.  
  215. }
  216.  
  217.  
  218.  
  219.  
  220. array_push($resArr, $res);
  221.  
  222.  
  223.  
  224. }
  225.  
  226. return json_encode(array_unique($resArr));
  227.  
  228.  
  229. }
  230.  
  231. public function predictTutors($x){
  232.  
  233. $users = $this->getTutors($x,0);
  234. $split = explode(" ", $x);
  235. $n = Count($split);
  236. $resArr=array();
  237. foreach ($users as $row) {
  238.  
  239.  
  240. $res="";
  241.  
  242.  
  243. for($i=0;$i<$n;$i++){
  244.  
  245.  
  246. $found=$this->findWord($row['firstname'],$split[$i]);
  247.  
  248. if($found!==false){
  249. if(stripos($res,$found) ===false)
  250. $res = $res." ".$found;
  251.  
  252. }
  253.  
  254.  
  255. $found=$this->findWord($row['lastname'],$split[$i]);
  256.  
  257. if($found!==false){
  258. if(stripos($res,$found) ===false)
  259. $res = $res." ".$found;
  260.  
  261. }
  262.  
  263.  
  264. $found=$this->findWord($row['address'],$split[$i]);
  265.  
  266. if($found!==false){
  267. if(stripos($res,$found) ===false)
  268. $res = $res." ".$found;
  269.  
  270. }
  271.  
  272.  
  273. $found=$this->findWord($row['title'],$split[$i]);
  274.  
  275. if($found!==false){
  276. if(stripos($res,$found) ===false)
  277. $res = $res." ".$found;
  278.  
  279. }
  280.  
  281.  
  282. $found=$this->findWord($row['descriptionStudent'],$split[$i]);
  283.  
  284. if($found!==false){
  285. if(stripos($res,$found) ===false)
  286. $res = $res." ".$found;
  287.  
  288. }
  289.  
  290.  
  291.  
  292. }
  293.  
  294.  
  295.  
  296.  
  297. array_push($resArr, $res);
  298.  
  299.  
  300.  
  301. }
  302.  
  303. return json_encode(array_unique($resArr));
  304.  
  305.  
  306. }
  307.  
  308. public function getTutorsForRating($studentid){
  309. $this->connect();
  310. $sth = $this->dbh->prepare("
  311. SELECT tutors.* from users tutors
  312. JOIN bookings
  313. ON tutors.userid = bookings.tutorid
  314. WHERE bookings.studentid=:studentid
  315. AND bookings.status = 'Approved'
  316. ");
  317. $sth->bindParam(':studentid', $studentid);
  318. $sth->execute();
  319. $tutors=$sth->fetchAll();
  320. $this->disconnect();
  321. return $tutors;
  322. }
  323.  
  324. public function getRatingTutorStudent($studentid,$tutorid){
  325. $this->connect();
  326. $sth = $this->dbh->prepare("
  327. SELECT * FROM ratings WHERE tutorid=:tutorid and studentid=:studentid
  328. ");
  329. $sth->bindParam(':studentid', $studentid);
  330. $sth->bindParam(':tutorid', $tutorid);
  331. $sth->execute();
  332. $rating=$sth->fetchAll();
  333. $this->disconnect();
  334. return $rating;
  335. }
  336. public function getRating($tutorid){
  337. $this->connect();
  338. $sth = $this->dbh->prepare("
  339. SELECT avg(rating) FROM ratings WHERE tutorid=:tutorid
  340. ");
  341.  
  342. $sth->bindParam(':tutorid', $tutorid);
  343. $sth->execute();
  344. $rating=$sth->fetchAll();
  345. $this->disconnect();
  346. return $rating[0][0];
  347. }
  348.  
  349. public function checkIfTutorCanBeRatedByUser($userid,$tutorid){
  350. $this->connect();
  351. $sth = $this->dbh->prepare("
  352. SELECT count(*) as cnt FROM bookings WHERE tutorid=:tutorid and studentid=:studentid
  353. AND status IN ('Booked','Done')
  354. ");
  355. $sth->bindParam(':studentid', $userid);
  356. $sth->bindParam(':tutorid', $tutorid);
  357. $sth->execute();
  358. $res=$sth->fetchAll();
  359. if($res[0]['cnt']==0){
  360. $this->disconnect();
  361. return false;
  362. }
  363. $this->disconnect();
  364. return true;
  365. }
  366.  
  367. public function insertRating($studentid,$tutorid,$description,$rating,$title){
  368. $this->connect();
  369. $sth = $this->dbh->prepare("
  370. INSERT INTO ratings VALUES(null,:tutorid,:studentid,:description,:rating,:title)
  371. ");
  372. $sth->bindParam(':studentid', $studentid);
  373. $sth->bindParam(':tutorid', $tutorid);
  374. $sth->bindParam(':description', $description);
  375. $sth->bindParam(':rating', $rating);
  376. $sth->bindParam(':title', $title);
  377. $sth->execute();
  378. $this->disconnect();
  379. return true;
  380. }
  381.  
  382. public function updateRating($studentid,$tutorid,$description,$rating,$title){
  383. $this->connect();
  384. $sth = $this->dbh->prepare("
  385. UPDATE ratings SET
  386. description=:description,
  387. rating=:rating,
  388. title=:title
  389. WHERE tutorid=:tutorid
  390. AND studentid=:studentid
  391. ");
  392. $sth->bindParam(':studentid', $studentid);
  393. $sth->bindParam(':tutorid', $tutorid);
  394. $sth->bindParam(':description', $description);
  395. $sth->bindParam(':rating', $rating);
  396. $sth->bindParam(':title', $title);
  397. $sth->execute();
  398. $this->disconnect();
  399. return true;
  400. }
  401.  
  402. public function checkIfRatingExist($studentid,$tutorid){
  403. $this->connect();
  404. $sth = $this->dbh->prepare("
  405. SELECT COUNT(*) AS numOfRatings FROM ratings
  406. WHERE studentid=:studentid
  407. AND tutorid=:tutorid
  408. ");
  409. $sth->bindParam(':studentid', $studentid);
  410. $sth->bindParam(':tutorid', $tutorid);
  411. $sth->execute();
  412. $row=$sth->fetch();
  413. if($row["numOfRatings"]==0){
  414. $this->disconnect();
  415. return false;
  416. }else {
  417. $this->disconnect();
  418. return true;
  419. }
  420. }
  421.  
  422. public function addRating($studentid,$tutorid,$description,$rating,$title){
  423. $var = $this->checkIfRatingExist($studentid,$tutorid);
  424. if($var==false){
  425. return $this->insertRating($studentid,$tutorid,$description,$rating,$title);
  426. }else{
  427. return $this->updateRating($studentid,$tutorid,$description,$rating,$title);
  428. }
  429. }
  430.  
  431. public function getSchedule($tutorid){
  432. $this->connect();
  433. $sth = $this->dbh->prepare("
  434. SELECT * from schedule
  435. WHERE tutorid=:tutorid
  436. ");
  437. $sth->bindParam(':tutorid', $tutorid);
  438. $sth->execute();
  439. $tutors=$sth->fetchAll();
  440. $this->disconnect();
  441. return $tutors;
  442. }
  443.  
  444. public function getFreeSchedule($tutorid){
  445. $this->connect();
  446. $sth = $this->dbh->prepare("
  447. SELECT * from schedule
  448. WHERE tutorid=:tutorid
  449. AND status='Free'
  450. ");
  451. $sth->bindParam(':tutorid', $tutorid);
  452. $sth->execute();
  453. $tutors=$sth->fetchAll();
  454. $this->disconnect();
  455. return $tutors;
  456. }
  457.  
  458. public function checkIfFreeTimeExists($tutorid,$day,$timefrom,$timetill){
  459. $this->connect();
  460. $sth = $this->dbh->prepare("
  461. SELECT COUNT(*) as numOfFree FROM schedule
  462. WHERE tutorid=:tutorid
  463. AND day=:day
  464. AND ( (:timefrom >=timefrom AND :timefrom<timetill)
  465. OR (:timetill >timefrom AND :timetill<=timetill))
  466. ");
  467. $sth->bindParam(':tutorid', $tutorid);
  468. $sth->bindParam(':day', $day);
  469. $sth->bindParam(':timefrom', $timefrom);
  470. $sth->bindParam(':timetill', $timetill);
  471. $sth->execute();
  472. $row=$sth->fetch();
  473. if($row["numOfFree"]==0){
  474. $this->disconnect();
  475. return false;
  476. }else {
  477. $this->disconnect();
  478. return true;
  479. }
  480. }
  481.  
  482. public function insertFreeTime($tutorid,$day,$timefrom,$timetill){
  483. $var = $this->checkIfFreeTimeExists($tutorid,$day,$timefrom,$timetill);
  484. if($var==false){
  485. $this->connect();
  486. $sth = $this->dbh->prepare("
  487. INSERT INTO schedule values(null,:tutorid,:day,:timefrom,:timetill,'free')
  488. ");
  489. $sth->bindParam(':tutorid', $tutorid);
  490. $sth->bindParam(':day', $day);
  491. $sth->bindParam(':timefrom', $timefrom);
  492. $sth->bindParam(':timetill', $timetill);
  493. $sth->execute();
  494. $this->disconnect();
  495. return true;
  496. }
  497. return false;
  498. }
  499.  
  500. public function checkIfTimeIsFree($scheduleid){
  501. $this->connect();
  502. $sth = $this->dbh->prepare("
  503. SELECT COUNT(*) as numOfFree FROM schedule
  504. WHERE scheduleid=:scheduleid
  505. AND status='Free'
  506. ");
  507. $sth->bindParam(':scheduleid', $scheduleid);
  508. $sth->execute();
  509. $row=$sth->fetch();
  510. if($row["numOfFree"]==0){
  511. $this->disconnect();
  512. return false;
  513. }else {
  514. $this->disconnect();
  515. return true;
  516. }
  517. }
  518.  
  519. public function deleteFreeTime($tutorid,$day,$timefrom){
  520. if(true){
  521. $this->connect();
  522. $sth = $this->dbh->prepare("
  523. DELETE FROM schedule WHERE tutorid=:tutorid AND day=:day AND timefrom=:timefrom
  524. ");
  525. $sth->bindParam(':tutorid', $tutorid);
  526. $sth->bindParam(':day', $day);
  527. $sth->bindParam(':timefrom', $timefrom);
  528. $sth->execute();
  529. $this->disconnect();
  530. return true;
  531. }
  532. return false;
  533. }
  534.  
  535. public function checkIfHasBookingPending($studentid,$tutorid){
  536. $this->connect();
  537. $sth = $this->dbh->prepare("
  538. SELECT COUNT(*) as numOfBookings FROM bookings
  539. WHERE studentid=:studentid
  540. AND tutorid=:tutorid
  541. AND status='Pending'
  542. ");
  543. $sth->bindParam(':studentid', $studentid);
  544. $sth->bindParam(':tutorid', $tutorid);
  545. $sth->execute();
  546. $row=$sth->fetch();
  547. if($row["numOfBookings"]==0){
  548. $this->disconnect();
  549. return false;
  550. }else {
  551. $this->disconnect();
  552. return true;
  553. }
  554. }
  555.  
  556. public function getBookIdPending($studentid,$tutorid){
  557. $this->connect();
  558. $sth = $this->dbh->prepare("
  559. SELECT bookid FROM bookings
  560. WHERE studentid=:studentid
  561. AND tutorid=:tutorid
  562. AND status='Pending'
  563. ");
  564. $sth->bindParam(':studentid', $studentid);
  565. $sth->bindParam(':tutorid', $tutorid);
  566. $sth->execute();
  567. $row=$sth->fetch();
  568. return $row["bookid"];
  569. }
  570.  
  571. public function insertBooking($studentid,$tutorid){//returns bookid when inserted and 0 if cannot insert because already has pending book
  572. $var = $this->checkIfHasBookingPending($studentid,$tutorid);
  573. if($var==false){
  574. $this->connect();
  575. $sth = $this->dbh->prepare("
  576. INSERT INTO bookings VALUES(null,:studentid,:tutorid,'Pending')
  577. ");
  578. $sth->bindParam(':tutorid', $tutorid);
  579. $sth->bindParam(':studentid', $studentid);
  580. $sth->execute();
  581. $this->disconnect();
  582. return $this->getBookIdPending($studentid,$tutorid);
  583. }
  584. return 0;
  585. }
  586.  
  587. public function checkIfBookTimeExists($bookid,$day,$timefrom,$timetill){//check if booking has conflict
  588. $this->connect();
  589. $sth = $this->dbh->prepare("
  590. SELECT COUNT(*) as numOfBookings FROM bookingdetails
  591. WHERE bookid=:bookid
  592. AND day=:day
  593. AND ( (:timefrom >=timefrom AND :timefrom<timetill)
  594. OR (:timetill >timefrom AND :timetill<=timetill))
  595. ");
  596. $sth->bindParam(':bookid', $bookid);
  597. $sth->bindParam(':day', $day);
  598. $sth->bindParam(':courseid', $courseid);
  599. $sth->bindParam(':timefrom', $timefrom);
  600. $sth->bindParam(':timetill', $timetill);
  601. $sth->execute();
  602. $row=$sth->fetch();
  603. if($row["numOfBookings"]==0){
  604. $this->disconnect();
  605. return false;
  606. }else {
  607. $this->disconnect();
  608. return true;
  609. }
  610. }
  611.  
  612. public function insertBookingDetails($bookid,$day,$timefrom,$timetill,$courseid){//returns true if no conflict and insert successful
  613. $var = $this->checkIfBookTimeExists($bookid,$day,$timefrom,$timetill);
  614. if($var==false){
  615. $this->connect();
  616. $sth = $this->dbh->prepare("
  617. INSERT INTO bookingdetails VALUES(null,:bookid,:day,:timefrom,:timetill,:courseid)
  618. ");
  619. $sth->bindParam(':bookid', $bookid);
  620. $sth->bindParam(':day', $day);
  621. $sth->bindParam(':courseid', $courseid);
  622. $sth->bindParam(':timefrom', $timefrom);
  623. $sth->bindParam(':timetill', $timetill);
  624. $sth->execute();
  625. $this->disconnect();
  626. return true;
  627. }
  628. return false;
  629. }
  630. /*
  631. public function getPendingBookingsForTutor($tutorid){//returns all pending booking for a tutor
  632. $this->connect();
  633. $sth = $this->dbh->prepare("
  634. SELECT * FROM bookings JOIN bookingdetails
  635. ON bookingdetails.bookid=bookings.bookid
  636. WHERE tutorid=:tutorid
  637. AND status='Pending'
  638. ");
  639. $sth->bindParam(':tutorid', $tutorid);
  640. $sth->execute();
  641. $tutors=$sth->fetchAll();
  642. $this->disconnect();
  643. return $tutors;
  644. }*/
  645.  
  646. public function getPendingBookingsForTutor($tutorid){//returns all pending booking for a tutor
  647. $this->connect();
  648. $sth = $this->dbh->prepare("
  649. SELECT * FROM bookings
  650. WHERE tutorid=:tutorid
  651. AND status='Pending'
  652. ");
  653. $sth->bindParam(':tutorid', $tutorid);
  654. $sth->execute();
  655. $tutors=$sth->fetchAll();
  656. $this->disconnect();
  657. return $tutors;
  658. }
  659.  
  660. public function getBookingDetails($bookid){
  661. $this->connect();
  662. $sth = $this->dbh->prepare("
  663. SELECT * FROM bookingdetails bd JOIN bookings b
  664. ON bd.bookid=b.bookid
  665. WHERE bd.bookid=:bookid
  666. ");
  667. $sth->bindParam(':bookid', $bookid);
  668. $sth->execute();
  669. $res=$sth->fetchAll();
  670. $this->disconnect();
  671. return $res;
  672. }
  673.  
  674. public function compareWithSchedule($tutorid,$day,$timefrom,$timetill){//returns true if there is no conflict with schedule
  675. $this->connect();
  676. $sth = $this->dbh->prepare("
  677. SELECT COUNT(*) as num FROM schedule
  678. WHERE tutorid=:tutorid
  679. AND day=:day
  680. AND (:timefrom >=timefrom AND :timetill<=timetill)
  681. AND status='Free'
  682. ");
  683. $sth->bindParam(':tutorid', $tutorid);
  684. $sth->bindParam(':day', $day);
  685. $sth->bindParam(':timefrom', $timefrom);
  686. $sth->bindParam(':timetill', $timetill);
  687. $sth->execute();
  688. $row=$sth->fetch();
  689. if($row["num"]==0){
  690. $this->disconnect();
  691. return false;
  692. }else{
  693. $this->disconnect();
  694. return true;
  695. }
  696. }
  697.  
  698. public function checkIfApprovingBookingCreatesConflictWithSchedule($bookid){//return true if the bookingdetails presents a conflict with shcedule
  699. $bookingDetails=$this->getBookingDetails($bookid);
  700. foreach($bookingDetails as $details){
  701. $var = $this->compareWithSchedule($details["tutorid"],$details["day"],$details["timefrom"],$details["timetill"]);
  702. if($var==false){
  703. return true;
  704. }
  705. }
  706. return false;
  707. }
  708.  
  709. public function setScheduleStatusToBooked($tutorid,$day,$timefrom,$timetill){
  710. $this->connect();
  711. $sth = $this->dbh->prepare("
  712. UPDATE schedule SET status='Booked' WHERE tutorid=:tutorid AND day=:day AND timefrom=:timefrom AND timetill=:timetill
  713. ");
  714. $sth->bindParam(':tutorid', $tutorid);
  715. $sth->bindParam(':day', $day);
  716. $sth->bindParam(':timefrom', $timefrom);
  717. $sth->bindParam(':timetill', $timetill);
  718. $sth->execute();
  719. $this->disconnect();
  720. return true;
  721. }
  722.  
  723. public function updateBookingStatus($bookid,$status){
  724. $this->connect();
  725. $sth = $this->dbh->prepare("
  726. UPDATE bookings SET
  727. status=:status
  728. WHERE bookid=:bookid
  729. ");
  730. $sth->bindParam(':bookid', $bookid);
  731. $sth->bindParam(':status', $status);
  732. $sth->execute();
  733. $this->disconnect();
  734. return true;
  735. }
  736.  
  737. public function approveBooking($bookid){
  738. $var = $this->checkIfApprovingBookingCreatesConflictWithSchedule($bookid);
  739. $bookingDetails=$this->getBookingDetails($bookid);
  740. if($var==false){
  741. foreach($bookingDetails as $details){
  742. $this->setScheduleStatusToBooked($details["tutorid"],$details["day"],$details["timefrom"],$details["timetill"]);
  743. }
  744. $this->updateBookingStatus($bookid,'Approved');
  745. return true;
  746. }
  747. return false;
  748. }
  749.  
  750.  
  751.  
  752.  
  753. public function signIn($email,$password){
  754. $this->connect();
  755. $sth = $this->dbh->prepare("
  756. SELECT userid FROM users
  757. WHERE email= :email
  758. AND password= :password
  759. ");
  760. $sth->bindParam(':email', $email);
  761. $sth->bindParam(':password', $password);
  762. $sth->execute();
  763. $user=$sth->fetch();
  764. $this->disconnect();
  765. $_SESSION['userid']=$user['userid'];
  766. $_SESSION['type']=$user['tutor'];
  767. return $user;
  768. }
  769.  
  770. public function checkIfEmailExists($email){
  771. $this->connect();
  772. $sth = $this->dbh->prepare("
  773. SELECT COUNT(*) as num FROM users
  774. WHERE email=:email
  775. ");
  776. $sth->bindParam(':email', $email);
  777. $sth->execute();
  778. $row=$sth->fetch();
  779. if($row["num"]==0){
  780. $this->disconnect();
  781. return false;
  782. }else{
  783. $this->disconnect();
  784. return true;
  785. }
  786. }
  787.  
  788. public function signUp($firstname,$lastname,$email,$password,$telephone,$address,$descriptionStudent,$dateOfBirth,$tutor,$coordinates,$icon,$courses){
  789. if($this->checkIfEmailExists($email)){
  790.  
  791. return -1;
  792. }
  793. $this->connect();
  794. $sth = $this->dbh->prepare("
  795. INSERT INTO users (firstname,lastname,email,password,telephone,address,descriptionStudent,dateOfBirth,tutor,coordinates) VALUES(:firstname,:lastname,:email,:password,:telephone,:address,:descriptionStudent,:dateOfBirth,:tutor,:coordinates)
  796. ");
  797. $sth->bindParam(':firstname', $firstname);
  798. $sth->bindParam(':lastname', $lastname);
  799. $sth->bindParam(':email', $email);
  800. $sth->bindParam(':password', $password);
  801. $sth->bindParam(':telephone', $telephone);
  802. $sth->bindParam(':address', $address);
  803. $sth->bindParam(':descriptionStudent', $descriptionStudent);
  804. $sth->bindParam(':dateOfBirth', $dateOfBirth);
  805. $sth->bindParam(':tutor', $tutor);
  806. $sth->bindParam(':coordinates', $coordinates);
  807. try{
  808. $sth->execute();
  809. }catch (PDOException $e) {
  810. print "Error!: " . $e->getMessage() . "<br/>";
  811. die();
  812. }
  813. $userid = $this->dbh->lastInsertId();
  814. //var_dump($courses);
  815. foreach ($courses as $courseid) {
  816. $sth = $this->dbh->prepare("
  817. insert into studentscourses select * from (select :courseid, :userid ) as tmp where not exists (select * from studentscourses where courseid = :courseid and userid = :userid);
  818.  
  819. ");
  820. $sth->bindParam(":courseid",$courseid);
  821. $sth->bindParam(":userid",$userid);
  822. $sth->execute();
  823. }
  824.  
  825. if($icon){
  826. $imageName = md5(date("").time(""));
  827. $data = $icon;
  828. /*list($type, $data) = explode(';', $data);
  829. list(, $data) = explode(',', $data);*/
  830. $data = base64_decode($data);
  831. file_put_contents('./icons/'.$imageName.".png", $data);
  832. $stm = $this->dbh->prepare("update users set icon = :icon where userid = :userid");
  833. $stm->bindParam(":userid",$userid);
  834. $stm->bindParam(":icon",$imageName);
  835. $stm->execute();
  836. }
  837.  
  838.  
  839.  
  840. $this->disconnect();
  841. //echo "userid<br>";
  842. //var_dump($userid);
  843. return $userid;
  844. }
  845.  
  846.  
  847. public function getCourses(){
  848. $this->connect();
  849. $sth = $this->dbh->prepare("
  850. SELECT * FROM courses
  851. ");
  852. $sth->execute();
  853. $res=$sth->fetchAll();
  854. $this->disconnect();
  855. return $res;
  856. }
  857.  
  858. public function getCoursesOfTutor($tutorid){
  859. $this->connect();
  860. $sth = $this->dbh->prepare("
  861. SELECT * FROM courses c join tutorscourses t on c.courseid = t.courseid where t.tutorid = :tutorid
  862. ");
  863. $sth->bindParam(':tutorid', $tutorid);
  864. $sth->execute();
  865. $res=$sth->fetchAll();
  866. $this->disconnect();
  867. return $res;
  868. }
  869.  
  870. public function getCoursesOfStudent($studentId){
  871. $this->connect();
  872. $sth = $this->dbh->prepare("
  873. SELECT * FROM courses as c , studentscourses as t where c.courseid = t.courseid and t.userid = :studentId
  874. ");
  875. $sth->bindParam(':studentId', $studentId);
  876. $sth->execute();
  877. $res=$sth->fetchAll();
  878. $this->disconnect();
  879. return $res;
  880. }
  881.  
  882. public function sendMessage($receiver,$sender,$title,$message){
  883. $this->connect();
  884. $sth = $this->dbh->prepare("
  885. INSERT INTO messages VALUES(null,:receiver,:sender,:title,:message,0,'',0)
  886. ");
  887. $sth->bindParam(':receiver', $receiver);
  888. $sth->bindParam(':sender', $sender);
  889. $sth->bindParam(':title', $title);
  890. $sth->bindParam(':message', $message);
  891. $sth->execute();
  892. $this->disconnect();
  893. return true;
  894. }
  895.  
  896. public function getNotViewedMessagesOfUser($userid){
  897. $this->connect();
  898. $sth = $this->dbh->prepare("
  899. SELECT * FROM messages where
  900. receiver=:receiver AND
  901. viewed=0
  902. ");
  903. $sth->bindParam(':receiver', $userid);
  904. $sth->execute();
  905. $res=$sth->fetchAll();
  906. $this->disconnect();
  907. return $res;
  908. }
  909.  
  910. public function setMessageViewed($messageid){
  911. $this->connect();
  912. $sth = $this->dbh->prepare("
  913. UPDATE messages SET viewed=1 WHERE
  914. messageid=:messageid
  915. ");
  916. $sth->bindParam(':messageid', $messageid);
  917. $sth->execute();
  918. $this->disconnect();
  919. return true;
  920. }
  921.  
  922. public function replyTo($replyToMessageid,$message){
  923. $this->connect();
  924. $sth = $this->dbh->prepare("
  925. INSERT INTO messages VALUES(null,0,0,'',:message,:replyToMessageid,'',0)
  926. ");
  927. $sth->bindParam(':message', $message);
  928. $sth->bindParam(':replyToMessageid', $replyToMessageid);
  929. $sth->execute();
  930. $this->disconnect();
  931. return true;
  932. }
  933.  
  934.  
  935.  
  936.  
  937. public function getUserAddress($userid){
  938.  
  939. $this->connect();
  940. $sth = $this->dbh->prepare("select coordinates from users where userid = :userid");
  941. $sth->bindParam(':userid', $userid);
  942. $sth->execute();
  943. $res=$sth->fetchAll();
  944. $this->disconnect();
  945. return $res;
  946.  
  947.  
  948.  
  949. }
  950.  
  951. public function getUserById($userid){
  952.  
  953. $this->connect();
  954. $sth = $this->dbh->prepare("select * from users where userid = :userid");
  955. $sth->bindParam(':userid', $userid);
  956. $sth->execute();
  957. $res=$sth->fetchAll();
  958. $this->disconnect();
  959. return $res;
  960.  
  961. }
  962.  
  963.  
  964. /*
  965. public function addressToXY($address){
  966.  
  967. $str = "https://maps.googleapis.com/maps/api/geocode/json?address=";
  968. $arr = explode(" ",$address);
  969. $n = count($arr);
  970. for($i=0;$i<$n;$i++){
  971.  
  972. $str = $str.$arr[$i];
  973.  
  974. if($i<$n-1){
  975.  
  976. $str=$str."+";
  977. }
  978.  
  979.  
  980. }
  981. $str=$str."&key=AIzaSyAUmZlPZBk2GEPF-cMg_mROsuECPNGwXlo";
  982. $r = new HttpRequest($str, "GET");
  983. try {
  984. $r->send();
  985. if ($r->getResponseCode() == 200) {
  986. echo $r->getResponseBody();
  987. }
  988. } catch (HttpException $ex) {
  989. // echo $ex;
  990. }
  991.  
  992.  
  993.  
  994. }*/
  995.  
  996.  
  997.  
  998.  
  999.  
  1000. }
  1001. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement