avm99963 | 472b0de | 2020-09-28 17:15:42 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | namespace DAFME\Covid; |
| 3 | |
| 4 | class Classes { |
| 5 | public static function getCurrentClasses() { |
| 6 | global $con; |
| 7 | |
| 8 | $isSignedIn = Users::isSignedIn(); |
| 9 | |
| 10 | $sentence = 'SELECT c.id, c.calendar_name, c.room, c.begins, c.ends, s.id subject_id, s.friendly_name'.($isSignedIn ? ', u_s.id user_subject_id' : '').' |
| 11 | FROM classes c |
| 12 | INNER JOIN subjects s |
| 13 | ON c.calendar_name = s.calendar_name |
| 14 | '.($isSignedIn ? 'LEFT OUTER JOIN user_subjects u_s |
| 15 | ON s.id = u_s.subject_id |
| 16 | ' : ''). |
| 17 | 'WHERE |
| 18 | c.begins < NOW() AND |
| 19 | c.ends > NOW()'.($isSignedIn ? ' AND |
| 20 | ( |
| 21 | u_s.user_id = :user_id OR |
| 22 | u_s.subject_id IS NULL |
| 23 | )': ''); |
| 24 | $query = $con->prepare($sentence); |
| 25 | |
| 26 | if (!$query->execute(($isSignedIn ? ['user_id' => Users::getUserId()] : []))) |
| 27 | return false; |
| 28 | |
| 29 | $classes = $query->fetchAll(\PDO::FETCH_ASSOC); |
| 30 | |
| 31 | foreach ($classes as &$class) { |
| 32 | if ($isSignedIn) |
| 33 | $class['user_subject_id'] = null; |
| 34 | |
| 35 | $class['user_selected'] = $class['user_subject_id'] !== null; |
| 36 | } |
| 37 | |
| 38 | return $classes; |
| 39 | } |
| 40 | |
| 41 | public static function handleAPIGetClasses($body) { |
| 42 | if (!isset($body['type'])) |
| 43 | return false; |
| 44 | |
| 45 | $response = []; |
| 46 | |
| 47 | switch ($body['type']) { |
| 48 | case 'current': |
| 49 | $classes = self::getCurrentClasses(); |
| 50 | if ($classes === false) |
| 51 | return false; |
| 52 | |
| 53 | $response['classes'] = $classes; |
| 54 | break; |
| 55 | |
| 56 | default: |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return $response; |
| 61 | } |
| 62 | } |