forked from SusoGym/SEST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.model.php
1911 lines (1604 loc) · 60.9 KB
/
class.model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The model class
*/
class Model {
/**
* @var Connection
*/
protected static $connection;
/**
* @var Model
*/
protected static $model;
/**
* @var monate
*/
private $monate = null;//Array("mnum"=>string,"mstring"=>string,"jahr"=>int) der Monate mit Terminen
/**
*Konstruktor
*/
protected function __construct() {
if (self::$connection == null)
self::$connection = new Connection();
}
static function getInstance() {
return self::$model == null ? self::$model = new Model() : self::$model;
}
/**
*getOptions
*returns option from DB table options
*e.g. slot assignment, booking period, allowed bookings etc
*
* @return array()
*/
public function getOptions() {
$options = array();
$data = self::$connection->SelectAssociativeValues("SELECT * FROM options");
foreach ($data as $d) {
$options[$d['type']] = $d['value'];
}
return $options;
}
/**
* get values from ini-file
*
* @return string
*/
public function getIniParams() {
return self::$connection->getIniParams();
}
/**
* @param string $vorname Schueler Vorname
* @param string $name Schueler Nachname
* @return Student
**/
public function getStudentByName($name, $surname = null) {
$name = self::$connection->escape_string($name);
if ($surname != null) {
$surname = self::$connection->escape_string($surname);
$wholeName = str_replace(' ', '', $name . $surname);
} else {
$wholeName = $name;
}
$data = self::$connection->selectAssociativeValues("SELECT * FROM schueler WHERE Replace(CONCAT(vorname, name), ' ', '') = '$wholeName'");
if ($data == null)
return null;
$data = $data[0];
return new Student($data['id'], $data['klasse'], $data['name'], $data['vorname'], $data['gebdatum'], $data['eid']);
}
/**
* @param $uid int
* @return User | Teacher | Admin | Guardian
*/
public function getUserById($uid, $data = null) {
if ($data == null)
$data = self::$connection->selectAssociativeValues("SELECT * FROM user WHERE id=$uid");
if ($data == null)
return null;
if (isset($data[0]))
$data = $data[0];
$type = $data['user_type'];
switch ($type) {
case 0: // Admin
return new Admin($data['id'], $data['email']);
break;
case 1: // Parent / Guardian
$data2 = self::$connection->selectAssociativeValues("SELECT * FROM eltern WHERE userid=$uid")[0];
return new Guardian($data['id'], $data['email'], $data2['id'], $data2['name'], $data2['vorname']);
case 2:
// non-existend
die("Why are we here?!");
default:
return null;
break;
}
}
/**
* @param string $email the user email
* @return User user
*/
public function getUserByMail($email) {
$email = self::$connection->escape_string($email);
$data = self::$connection->selectAssociativeValues("SELECT * FROM user WHERE email='$email'");
if ($data == null)
return null;
return $this->getUserById($data[0]['id'], $data);
}
/**
* @param int $tchrId
* @return array[String => String]
*/
public function getTeacherNameByTeacherId($teacherId, $data = null) {
if ($data == null)
$data = self::$connection->selectAssociativeValues("SELECT * FROM lehrer WHERE lehrer.id=$teacherId");
if (isset($data[0]))
$data = $data[0];
$surname = isset($data["name"]) ? $data["name"] : null;
$name = isset($data["vorname"]) ? $data["vorname"] : null;
return array("name" => $name, "surname" => $surname);
}
/**
* @param int $usrId UserId
* @return array[Student] array[childrenId]
*/
public function getChildrenByParentUserId($usrId, $limit = null) {
if (isset($limit)) {
$query = "SELECT schueler.* FROM schueler, eltern WHERE schueler.eid=eltern.id AND eltern.userid=$usrId AND schueler.klasse < $limit"; //a bit crude, isn't it
} else {
$query = "SELECT schueler.* FROM schueler, eltern WHERE schueler.eid=eltern.id AND eltern.userid=$usrId";
}
$data = self::$connection->selectAssociativeValues($query);
if ($data == null)
return array();
$students = array();
foreach ($data as $item) {
$pid = intval($item['id']);
$student = $this->getStudentById($pid);
array_push($students, $student);
}
return $students;
}
/**
* @param $studentId int
* @return Student
*/
public function getStudentById($studentId) {
$data = self::$connection->selectAssociativeValues("SELECT * FROM schueler WHERE id=$studentId");
if ($data == null)
return null;
$data = $data[0];
return new Student($data['id'], $data['klasse'], $data['name'], $data['vorname'], $data['gebdatum'], $data['eid']);
}
/**
* @param string $class
* @return array[Teacher] array with teacherIds
*/
public function getTeachersByClass($class) {
$class = self::$connection->escape_string($class);
$data = self::$connection->selectValues("SELECT lehrer.id FROM lehrer, unterricht WHERE unterricht.klasse='$class' AND unterricht.lid=lehrer.id"); // returns data[n][data]
if ($data == null)
return null;
$ids = array();
foreach ($data as $item) {
$tid = intval($item[0]);
array_push($ids, $this->getTeacherByTeacherId($tid));
}
return $ids;
}
/**
* Returns all Teachers
*
* @return array[Teacher]
*/
public function getTeachers() {
$data = self::$connection->selectAssociativeValues("SELECT * FROM lehrer ORDER BY name,vorname"); // returns data[n][data]
$teachers = array();
foreach ($data as $item) {
$tid = intval($item['id']);
array_push($teachers, $this->getTeacherByTeacherId($tid, $item));
}
return $teachers;
}
/**
* @param $tchrId int teacherId
* @return Teacher
*/
public function getTeacherByTeacherId($tchrId, $data = null) {
if ($data == null)
$data = self::$connection->selectAssociativeValues("SELECT * FROM lehrer WHERE id=$tchrId");
if (isset($data[0]))
$data = $data[0];
if ($data == null)
return null;
return new Teacher($data['email'], $data['id'], $data);
}
/**
* @param $teacherId
* @param $rawData
* @return string
*/
public function getTeacherLdapNameByTeacherId($teacherId, $rawData = null) {
if ($rawData == null)
$rawData = self::$connection->selectAssociativeValues("SELECT ldapname FROM lehrer WHERE id=$teacherId");
if ($rawData == null)
return null; // empty / not found
if (isset($rawData[0]))
$rawData = $rawData[0];
return $rawData;
}
/**
* @param $teacherId
* @param $rawData
* @return string
*/
public function getTeacherUntisNameByTeacherId($teacherId, $rawData = null) {
$returnData = null;
if ($rawData == null) {
$data = self::$connection->selectValues("SELECT untisname FROM lehrer WHERE id=$teacherId");
if ($data == null) {
$returnData = null; // empty / not found
} else {
$returnData = $data[0][0];
}
}
if (isset($rawData["untisName"])) {
$returnData = $rawData["untisName"];
}
return $returnData;
}
/**
* @param $teacherId
* @param $rawData
* @return string
*/
public function getTeacherShortNameByTeacherId($teacherId, $rawData = null) {
$returnData = null;
if (!isset($rawData["shortName"])) {
$data = self::$connection->selectValues("SELECT kuerzel FROM lehrer WHERE id=$teacherId");
if ($data == null) {
$returnData = null; // empty / not found
} else {
$returnData = $data[0][0];
}
}
if (isset($rawData["shortName"]))
$returnData = $rawData["shortName"];
return $returnData;
}
/**
* @param $teacherId int
* @return int
*/
public function getTeacherLessonAmountByTeacherId($teacherId) {
$data = self::$connection->selectValues("SELECT deputat FROM lehrer WHERE id=$teacherId");
$lessons = $data[0][0];
return $lessons;
}
/**
* @param $email
* @param $pwd
* @return Teacher | null
*/
public function getTeacherByEmailAndLdapPwd($email, $pwd) {
$data = self::$connection->selectAssociativeValues("SELECT * FROM lehrer WHERE email='$email'");
if (isset($data[0]))
$data = $data[0];
if ($data == null)
return null;
$tId = $data['id'];
$ldapName = $this->getTeacherLdapNameByTeacherId($tId, $data);
if ($ldapName == null)
die("LDAP name not set for $email! If you are 1000% sure this is your real suso email, please contact you system admin of choice."); // rip
return $this->getLdapUserByLdapNameAndPwd($ldapName, $pwd);
}
public function getStudentUserById($id) {
$data = self::$connection->selectAssociativeValues("SELECT * FROM schueler WHERE id=$id");
if (!isset($data[0])) {
return null;
}
$data = $data[0];
return new StudentUser($data['id'], $data['name'], $data['vorname'], $data['klasse'], $data['gebdatum'], $data['eid'], $data['kurse']);
}
/**
* @param $ldapName
* @param $pwd
* @param $data
* @return null|Teacher | StudentUser
*/
public function getLdapUserByLdapNameAndPwd($ldapName, $pwd, $data = null) {
if ($data == null) {
$novelData = $this->checkNovellLogin($ldapName, $pwd);
if (!isset($novelData->{'code'}) || !isset($novelData->{'type'}) || $novelData->{'code'} != "200" || $novelData->{'type'} != 'Teacher') {
ChromePhp::info(json_encode($novelData));
if (isset($novelData->{'type'}) && $novelData->{'type'} == "student" && $novelData->{'code'} == "200") {
$query = "SELECT * FROM schueler WHERE klasse='" . $novelData->{'class'} . "' AND name LIKE '%" . $novelData->{'surname'} . "%' AND (";
$names = explode(' ', $novelData->{'givenname'});
for ($i = 0; $i < sizeof($names); $i++) {
if ($i != 0)
$query .= " OR";
$query .= " vorname LIKE '%" . $names[$i] . "%'";
}
$query.=")";
$data = self::$connection->selectAssociativeValues($query);
if (!isset($data[0])) {
die(var_dump($names) . " LDAP ist valide, MySQL jedoch nicht. Bitte wende dich an einen Systemadministrator.");
}
$data = $data[0];
return new StudentUser($data['id'], $data['name'], $data['vorname'], $data['klasse'], $data['gebdatum'], $data['eid'], $data['kurse']);
} else {
return null;
}
}
$data = self::$connection->selectAssociativeValues("SELECT * FROM lehrer WHERE ldapname='$ldapName'");
}
if (isset($data[0]))
$data = $data[0];
if ($data == null)
return null;
$tId = $data['id'];
$email = $data['email'];
return new Teacher($email, $tId);
}
/**
*returns if slot already assigned - reloading
*
* @param int slotId
* @param int teacherId
* @return bool
*/
private function checkAssignedSlot($slotId, $teacherId) {
$data = self::$connection->selectvalues("SELECT slotid FROM bookable_slot WHERE slotid=$slotId AND lid=$teacherId");
if (isset($data)) {
return true;
} else {
return false;
}
}
/**
*get existing slots for parent-teacher meeting
*
* @return array(array("id","start","ende"))
*/
public function getSlots() {
$slots = array();
$data = $tchrs = self::$connection->selectValues("SELECT id,anfang,ende FROM time_slot ORDER BY anfang ");
if (isset($data)) {
foreach ($data as $d) {
$slots[] = array("id" => $d[0], "anfang" => $d[1], "ende" => $d[2]);
}
}
return $slots;
}
/**
*enters a bookable Teacher Slot into DB
*
* @param int slotId
* @param int teacherId
*/
public function setAssignedSlot($slot, $teacherId) {
if (!$this->checkAssignedSlot($slot, $teacherId)) {
self::$connection->straightQuery("INSERT INTO bookable_slot (`slotid`,`lid`) VALUES ('$slot','$teacherId')");
}
}
/**
*deletes an assigned Slot from DB
*
* @param slotId
* @param teacherId
*/
public function deleteAssignedSlot($slotId, $teacherId) {
self::$connection->straightQuery("DELETE FROM bookable_slot WHERE slotid=$slotId AND lid=$teacherId");
}
/**
*returns assigned slots of a teacher
*
* @param int teacherId
* @returns array(int)
*/
public function getAssignedSlots($teacher) {
$slots = array();
$data = self::$connection->selectValues("SELECT slotid FROM bookable_slot WHERE lid=$teacher");
if (isset($data)) {
foreach ($data as $d) {
$slots[] = $d[0];
}
}
return $slots;
}
/**
* @param $eid int parentId
* @return Guardian
*/
public function getParentByParentId($eid) {
$data = self::$connection->selectAssociativeValues("SELECT userid FROM eltern WHERE id=$eid");
if ($data == null)
return null;
$data = $data[0];
return $this->getUserById($data['userid']);
}
/**
* @param int $slotId
* @param int $userId
* @param int $teacherId
* @return int appointmentId
*/
public function bookingAdd($slotId, $userId) {
return self::$connection->insertValues("UPDATE bookable_slot SET eid=$userId WHERE id=$slotId");
}
/**
* @param int $appointment
*/
public function bookingDelete($appointment) {
self::$connection->straightQuery("UPDATE bookable_slot SET eid=NULL WHERE id=$appointment");
}
/**
* @param $parentId int
* @param $appointment int
* @return boolean
*/
public function parentOwnsAppointment($parentId, $appointment) {
$data = self::$connection->selectAssociativeValues("SELECT * FROM bookable_slot WHERE id=$appointment");
if (isset($data[0]))
$data = $data[0];
if (!isset($data) || $data['eid'] == null)
return true; //throw exception?
return $data['eid'] == $parentId;
}
/**
* @param $slotId int
* @param $userId int
* @return int appointmentId
*/
public function getAppointment($slotId, $userId) {
return -1;
}
/**
*/
/**
* returns all bookable or booked slots of a teacher for a parent
*
* @param teacherId
* @return array
*/
public function getAllBookableSlotsForParent($teacherId, $parentId) {
$slots = array();
$data = self::$connection->selectValues("SELECT bookable_slot.id,anfang,ende,eid,time_slot.id FROM bookable_slot,time_slot
WHERE lid=$teacherId
AND bookable_slot.slotid=time_slot.id
AND (eid IS NULL OR eid=$parentId)
ORDER BY anfang");
if (isset($data)) {
foreach ($data as $d) {
$slots[] = array("bookingId" => $d[0], "anfang" => $d[1], "ende" => $d[2], "eid" => $d[3], "slotId" => $d[4]);
}
}
return $slots;
}
/**
*returns appointments of parent
*
* @param int parentId
* @return array(slotId, bookingId, teacherId)
*/
public function getAppointmentsOfParent($parentId) {
$appointments = array();
$data = self::$connection->selectValues("SELECT time_slot.id,bookable_slot.id,bookable_slot.lid FROM time_slot,bookable_slot
WHERE time_slot.id=bookable_slot.slotid
AND bookable_slot.eid=$parentId ORDER BY anfang");
if (isset($data)) {
foreach ($data as $d) {
$appointments[] = array("slotId" => $d[0], "bookingId" => $d[1], "teacherId" => $d[2]);
}
}
return $appointments;
}
/**
* returns taught classes of teacher
*
* @param int teacherId
* @return array(string)
*/
public function getTaughtClasses($teacherId) {
$data = self::$connection->selectValues("SELECT klasse FROM unterricht WHERE lid = $teacherId ORDER BY klasse");
$classes = array();
if (isset($data)) {
foreach ($data as $d) {
$classes[] = $d[0];
}
}
return $classes;
}
/**
*returns appointments of teacher
*
* @param int teacherId
* @return array(slotId, bookingId, Guardian)
*/
public function getAppointmentsOfTeacher($teacherId) {
$appointments = array();
$data = self::$connection->selectValues("SELECT time_slot.id,bookable_slot.id,bookable_slot.eid,eltern.userid,eltern.name,eltern.vorname,user.email
FROM time_slot,bookable_slot,eltern,user
WHERE time_slot.id=bookable_slot.slotid
AND bookable_slot.eid=eltern.id
AND eltern.userid=user.id
AND bookable_slot.lid=$teacherId ORDER BY anfang");
if (isset($data)) {
foreach ($data as $d) {
$parentId = $d[2];
$userId = $d[3];
$surname = $d[4];
$name = $d[5];
$email = $d[6];
$parent = new Guardian($userId, $email, $parentId, $surname, $name);
$parent->getESTChildren($this->getOptions()['limit']);
$appointments[] = array("slotId" => $d[0], "bookingId" => $d[1], "parent" => $parent);
}
}
return $appointments;
}
/**
*retrieve all relevant booking Data for parent
*
* @param int parentId
* @return array("anfang","ende","teacher");
*/
public function getBookingDetails($parentId) {
$bookingDetails = array();
$data = self::$connection->selectValues("SELECT anfang,ende,lid
FROM bookable_slot,time_slot
WHERE bookable_slot.slotid = time_slot.id
AND eid = $parentId
ORDER BY anfang");
if (isset($data)) {
foreach ($data as $d) {
$teacher = new Teacher(null, $d[2]);
$bookingDetails[] = array("anfang" => $d[0], "ende" => $d[1], "teacher" => $teacher);
unset($teacher);
}
}
return $bookingDetails;
}
/**
* @param $email
* @param $password
* @return bool user exists in database and password is equal with the one in the database
*/
public function passwordValidate($email, $password) {
$email = self::$connection->escape_string($email);
//$password = self::$connection->escape_string($userName);
$data = self::$connection->selectAssociativeValues("SELECT password_hash from user WHERE email='$email'");
if ($data == null)
return false;
$data = $data[0];
$pwd_hash = $data['password_hash'];
return password_verify($password, $pwd_hash);
}
/**
* @param $pid array or int parents children ids (array[int] || int)
* @param $email string parents email
* @param $pwd string parents password
* @param $name string parent name
* @param $surname string parent surname
* @return array newly created ids of parent (userid and parentid)
*/
public function registerParent($email, $pwd, $name, $surname) {
$email = self::$connection->escape_string($email);
$pwd = password_hash($pwd, PASSWORD_DEFAULT);
$query = "INSERT INTO user (user_type, password_hash, email) VALUES (1,'$pwd', '$email');";
//Create parent in database and return eid
$usrId = self::$connection->insertValues($query);
$parentId = self::$connection->insertValues("INSERT INTO eltern (userid, vorname, name) VALUES ($usrId, '$name', '$surname');");
//return eid
return array("uid" => $usrId, "pid" => $parentId);
}
/**
* Adds new student as child to parent
*
* @param $parentId int Parent ID
* @param $studentIds array Student ID
* @return string success
*/
public function parentAddStudents($parentId, $studentIds) {
if (!is_array($studentIds))
$studentIds = array($studentIds);
$parent = $this->getParentByParentId($parentId);
if ($parent == null)
return false;
$query = "";
foreach ($studentIds as $id) {
$student = $this->getStudentById($id);
if ($student == null)
return false;
$query = "UPDATE schueler SET eid=$parentId WHERE id=$id;";
}
self::$connection->straightQuery($query);
return true;
}
/**
* @param $usr string novell user
* @param $pwd string novell passwd
* @returns array(string) [user => username case sensitive, type => student / teacher [, class => if student: students class]]
* @throws Exception when error was thrown while connection to remote server or response was empty
*/
public
function checkNovellLogin($usr, $pwd) {
$apiUrl = self::$connection->getIniParams()["ldap"]; //used to be hard coded "https://intranet.suso.schulen.konstanz.de/gpuntis/susointern.php";
$headers = array('Authorization: Basic ' . base64_encode("$usr:$pwd"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //fixme: ssl unsafe!!! -> is certificate correctly installed @ server? if yes we can remove this file and make everything save
$result = utf8_encode(curl_exec($ch));
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
if ($result == false) {
throw new Exception("Response was empty!");
}
$res = json_decode($result);
ChromePhp::info("Response from ldap [$usr, $pwd]: " . json_encode($res));
return $res;
}
/**
*Termine aus Datenbank auslesen
*
* @param $includeStaff Boolean
* @return Array(Terminobjekt)
*/
public function getEvents($isTeacher = null) {
isset($isTeacher) ? $query = "SELECT typ,start,ende,staff FROM termine ORDER BY start" : $query = "SELECT typ,start,ende,staff FROM termine WHERE staff=0 ORDER BY start";
$data = self::$connection->selectValues($query);
foreach ($data as $d) {
$termin = new Termin();
$termin->createFromDB($d);
$this->makeMonthsArray($termin->monatNum, $termin->monat, $termin->jahr);
$termine[] = $termin->createFromDB($d);
}
return $termine;
}
/**
*Ermittelt die kommenden Termine
*
* @param $staff boolean
* @return Array(Terminobjekte)
*/
public function getNextDates($staff) {
$staff ? $query = "SELECT typ,start,ende,staff FROM termine ORDER BY start" : $query = "SELECT typ,start,ende,staff FROM termine WHERE staff=0 ORDER BY start";
$data = self::$connection->selectValues($query);
$x = 0;
foreach ($data as $d) {
$termin = new Termin();
$termine[$x] = $termin->createFromDB($d);
$x++;
}
//Ermittle die neuesten Termine
$today = date('d.m.Y');
$added = strtotime("+21 day", strtotime($today));
$limit = date("d.m.Y", $added);
$todayTimestamp = strtotime($today);
$limitTimestamp = strtotime($limit);
$nextDates = array();
$x = 0;
foreach ($termine as $t) {
if (strtotime($t->sday) >= $todayTimestamp && strtotime($t->sday) <= $limitTimestamp) {
$nextDates[$x] = $t;
$x++;
}
}
return $nextDates;
}
/**
*Monatsarray mit Terminen erstellen
*
* @param string Monat als Zahl
* @param string Monat als Text
* @param string jahr
*/
private function makeMonthsArray($monatZahl, $monat, $jahr) {
$noAdd = false;
if (isset($this->monate)) {
foreach ($this->monate as $m) {
if ($m["mnum"] == $monatZahl) {
$noAdd = true;
}
}
}
if (!$noAdd) $this->monate[] = array("mnum" => $monatZahl, "mstring" => $monat, "jahr" => $jahr);
}
/**
*Monatarray abrufen
*
* @return array(string) monate
*/
public function getMonths() {
return $this->monate;
}
/** Changes the password
*
* @param $usrId
* @param $newPwd
*/
public function changePwd($usrId, $newPwd) {
$pwdhash = $pwd = password_hash($newPwd, PASSWORD_DEFAULT);
self::$connection->straightQuery("UPDATE user SET password_hash='$pwdhash' WHERE id=$usrId");
}
/** Change userdata
*
* @param $usrId
* @param $name
* @param $surname
* @param $email
* @return bool success
*/
public function updateUserData($usrId, $name, $surname, $email) {
$name = self::$connection->escape_string($name);
$surname = self::$connection->escape_string($surname);
$email = self::$connection->escape_string($email);
$check = self::$connection->selectValues("SELECT * FROM `user` WHERE email='$email' AND NOT id = $usrId");
if (isset($check[0]))
return false;
self::$connection->straightMultiQuery("UPDATE user SET email='$email' WHERE id=$usrId; UPDATE eltern SET vorname='$name', name='$surname' WHERE userid=$usrId");
return true;
}
/**
* get teacher's VPMail status
*
* @param int $id
* @return bool
*/
public function getTeacherVpMailStatus($id) {
$data = self::$connection->selectValues("SELECT receive_vpmail from lehrer WHERE id = $id");
return $data[0][0];
}
/**
* get teacher's NewsMail status
*
* @param int $id
* @return bool
*/
public function getTeacherNewsMailStatus($id) {
$data = self::$connection->selectValues("SELECT receive_news from lehrer WHERE id = $id");
return $data[0][0];
}
/**
* get teacher's VP View status
* false => view is set to personally relevant entries only
*
* @param int $id
* @return bool
*/
public function getTeacherVpViewStatus($id) {
$data = self::$connection->selectValues("SELECT vpview_all from lehrer WHERE id = $id");
return $data[0][0];
}
/**
* get student's courses
*
* @param int $id
* @return String
*/
public function getStudentCourses($id) {
$data = self::$connection->selectValues("SELECT kurse from schueler WHERE id = $id");
return $data[0][0];
}
/** Change teacherData
*
* @param $usrId
* @param bool $vpview
* @param bool $vpmail
* @param bool $newsmail
* @return bool
*/
public function updateTeacherData($usrId, $vpview, $vpmail, $newsmail) {
self::$connection->straightQuery("update lehrer set receive_vpmail = $vpmail, vpview_all = $vpview, receive_news = $newsmail WHERE id = $usrId");
return true;
}
/** Change teacherData
*
* @param $usrId
* @param string $courseList
* @return bool
*/
public function updateStudentData($usrId, $courseList) {
$courseList = self::$connection->escape_string($courseList);
self::$connection->straightQuery("update schueler set kurse = '$courseList' WHERE id = $usrId");
return true;
}
/**
* Creates random token used for password forgotten
*
* @param $email
* @return array
*/
public function generatePasswordReset($email) {
$resp = array("success" => true, "key" => null, "message" => "OK");
$email = self::$connection->escape_string($email);
$randomKey = uniqid() . uniqid(); // random 26 char digit
$user = $this->getUserByMail($email);
if ($user == null || $user->getType() == 0) {
$resp['success'] = false;
$resp['message'] = 'No valid user email';
return $resp;
}
$userId = $user->getId();
self::$connection->straightQuery("INSERT INTO pwd_reset (token, uid, validuntil) VALUES ('$randomKey', $userId, NOW() + INTERVAL 24 HOUR);");