avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 1 | <?php |
| 2 | namespace DAFME\Covid; |
| 3 | |
| 4 | class Subjects { |
| 5 | public static function getAll() { |
| 6 | global $con; |
| 7 | $query = $con->prepare('SELECT * FROM subjects'); |
| 8 | |
| 9 | if (!$query->execute()) |
| 10 | return false; |
| 11 | |
| 12 | return $query->fetchAll(\PDO::FETCH_ASSOC); |
| 13 | } |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 14 | |
avm99963 | 559f938 | 2020-10-12 21:58:28 +0200 | [diff] [blame] | 15 | public static function addIsUserSelected(&$entries, $doIfSignedOut = true) { |
| 16 | $isSignedIn = Users::isSignedIn(); |
| 17 | |
avm99963 | b5dde96 | 2020-10-12 22:02:19 +0200 | [diff] [blame^] | 18 | if (!$doIfSignedOut && !$isSignedIn) return; |
avm99963 | 559f938 | 2020-10-12 21:58:28 +0200 | [diff] [blame] | 19 | |
| 20 | foreach ($entries as &$entry) { |
| 21 | if (!$isSignedIn) |
| 22 | $entry['user_subject_id'] = null; |
| 23 | |
| 24 | $entry['user_selected'] = $entry['user_subject_id'] !== null; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public static function getStartupSubjects() { |
| 29 | global $con; |
| 30 | $isSignedIn = Users::isSignedIn(); |
| 31 | |
| 32 | $query = $con->prepare('SELECT s.id, s.friendly_name'.($isSignedIn ? ', us.id user_subject_id' : '').' |
| 33 | FROM subjects s'.($isSignedIn ? ' |
| 34 | LEFT JOIN user_subjects us |
| 35 | ON s.id = us.subject_id |
| 36 | WHERE |
| 37 | us.user_id = :user_id OR |
| 38 | us.user_id IS NULL' : '').' |
| 39 | ORDER BY s.friendly_name ASC'); |
| 40 | |
| 41 | $query_params = [ |
| 42 | "user_id" => Users::getUserId() |
| 43 | ]; |
| 44 | |
| 45 | if (!$query->execute($query_params)) return false; |
| 46 | $subjects = $query->fetchAll(\PDO::FETCH_ASSOC); |
| 47 | |
| 48 | self::addIsUserSelected($subjects, false); |
| 49 | return $subjects; |
| 50 | } |
| 51 | |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 52 | public static function exists(int $subject): bool { |
| 53 | global $con; |
| 54 | $query = $con->prepare('SELECT id FROM subjects WHERE id = ?'); |
| 55 | if (!$query->execute([$subject])) |
| 56 | return false; |
| 57 | |
| 58 | return $query->rowCount() > 0; |
| 59 | } |
| 60 | |
| 61 | public static function isNewUserSubject(int $subjectId): bool { |
| 62 | global $con; |
| 63 | $query = $con->prepare('SELECT id FROM user_subjects WHERE user_id = :user_id AND subject_id = :subject_id'); |
| 64 | |
| 65 | $userId = Users::getUserId(); |
| 66 | if ($userId == -1 || !$query->execute([ |
| 67 | 'user_id' => $userId, |
| 68 | 'subject_id' => $subjectId |
| 69 | ])) |
| 70 | return false; |
| 71 | |
| 72 | return $query->rowCount() == 0; |
| 73 | } |
| 74 | |
| 75 | public static function getUserSubjects() { |
| 76 | global $con; |
| 77 | $query = $con->prepare('SELECT |
| 78 | us.id, us.subject_id, s.friendly_name, s.calendar_name |
| 79 | FROM user_subjects us |
| 80 | INNER JOIN subjects s |
| 81 | ON us.subject_id = s.id |
| 82 | WHERE us.user_id = ?'); |
| 83 | |
| 84 | $userId = Users::getUserId(); |
| 85 | if ($userId == -1 || !$query->execute([$userId])) |
| 86 | return false; |
| 87 | |
| 88 | return $query->fetchAll(\PDO::FETCH_ASSOC); |
| 89 | } |
| 90 | |
| 91 | public static function addUserSubject(int $subjectId): bool { |
| 92 | global $con; |
| 93 | |
| 94 | if (!self::exists($subjectId) || !self::isNewUserSubject($subjectId)) |
| 95 | return false; |
| 96 | |
| 97 | $query = $con->prepare('INSERT INTO user_subjects (user_id, subject_id) VALUES (:user_id, :subject_id)'); |
| 98 | |
| 99 | $userId = Users::getUserId(); |
| 100 | |
| 101 | return $userId != -1 && $query->execute([ |
| 102 | 'user_id' => $userId, |
| 103 | 'subject_id' => $subjectId |
| 104 | ]); |
| 105 | } |
| 106 | |
| 107 | public static function removeUserSubject(int $subjectId): bool { |
| 108 | global $con; |
| 109 | |
| 110 | $query = $con->prepare('DELETE FROM user_subjects WHERE user_id = :user_id AND subject_id = :subject_id LIMIT 1'); |
| 111 | |
| 112 | $userId = Users::getUserId(); |
| 113 | return $userId != -1 && $query->execute([ |
| 114 | 'user_id' => $userId, |
| 115 | 'subject_id' => $subjectId |
| 116 | ]); |
| 117 | } |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 118 | } |