diff options
Diffstat (limited to 'modules-available/exams/page.inc.php')
-rw-r--r-- | modules-available/exams/page.inc.php | 72 |
1 files changed, 50 insertions, 22 deletions
diff --git a/modules-available/exams/page.inc.php b/modules-available/exams/page.inc.php index 23a5bc39..d229b883 100644 --- a/modules-available/exams/page.inc.php +++ b/modules-available/exams/page.inc.php @@ -23,7 +23,7 @@ class Page_Exams extends Page } else { $tmp = Database::simpleQuery("SELECT locationid FROM exams_x_location WHERE examid= :examid", array('examid' => $examidOrLocations)); $active = array(); - while ($row = $tmp->fetch(PDO::FETCH_ASSOC)) { + foreach ($tmp as $row) { $active[] = (int)$row['locationid']; } } @@ -40,7 +40,7 @@ class Page_Exams extends Page . "GROUP BY examid " . "ORDER BY examid ASC"); - while ($exam = $tmp->fetch(PDO::FETCH_ASSOC)) { + foreach ($tmp as $exam) { $view = $edit = false; // User has permission for all locations if (in_array(0, $this->userViewLocations)) { @@ -79,7 +79,7 @@ class Page_Exams extends Page protected function readLectures() { $tmp = Database::simpleQuery( - "SELECT lectureid, Group_Concat(locationid) as lids, displayname, starttime, endtime, isenabled, firstname, lastname, email " . + "SELECT lectureid, Group_Concat(locationid) as lids, islocationprivate, displayname, starttime, endtime, isenabled, firstname, lastname, email " . "FROM sat.lecture " . "INNER JOIN sat.user ON (user.userid = lecture.ownerid) " . "NATURAL LEFT JOIN sat.lecture_x_location " . @@ -87,7 +87,7 @@ class Page_Exams extends Page "GROUP BY lectureid " . "ORDER BY starttime ASC, displayname ASC", ['rangeMax' => $this->rangeMax, 'rangeMin' => $this->rangeMin]); - while ($lecture = $tmp->fetch(PDO::FETCH_ASSOC)) { + foreach ($tmp as $lecture) { $this->lectures[] = $lecture; } } @@ -103,7 +103,7 @@ class Page_Exams extends Page } // returns true if user is allowed to edit the exam - protected function userCanEditExam($examid = NULL) + protected function userCanEditExam(string $examid = NULL): bool { if (in_array(0, $this->userEditLocations)) // Trivial case -- don't query if global perms return true; @@ -111,16 +111,19 @@ class Page_Exams extends Page return User::hasPermission('exams.edit'); // Check locations of existing exam $res = Database::simpleQuery("SELECT locationid FROM exams_x_location WHERE examid= :examid", array('examid' => $examid)); - while ($locId = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $locId) { if (!in_array($locId['locationid'], $this->userEditLocations)) return false; } return true; } - // checks if user is allowed to save an exam with all the locations - // needs information if it's add (second para = true) or edit (second para = false) - protected function userCanEditLocation($locationids) { + /** + * checks if user is allowed to save an exam with all the locations + * needs information if it's add (second para = true) or edit (second para = false) + */ + protected function userCanEditLocation(array $locationids): bool + { return empty(array_diff($locationids, $this->userEditLocations)); } @@ -213,10 +216,16 @@ class Page_Exams extends Page return json_encode($out); } - protected function makeExamsForTemplate() + /** + * @return array{exams: array, decollapse: bool} + */ + protected function makeExamsForTemplate(): array { $out = []; $now = time(); + $cutoff = strtotime('-90 days'); + $foundActive = false; + $hasCollapsed = false; if (is_array($this->exams)) { foreach ($this->exams as $exam) { if ($exam['endtime'] < $now) { @@ -225,16 +234,27 @@ class Page_Exams extends Page $exam['liesInPast'] = true; } else { $exam['btnClass'] = 'btn-danger'; + if ($exam['starttime'] < $now) { + $exam['rowClass'] = 'slx-bold'; + } + } + if (!$foundActive) { + if ($exam['endtime'] > $cutoff) { + $foundActive = true; + } else { + $exam['rowClass'] .= ' collapse'; + $hasCollapsed = true; + } } $exam['starttime_s'] = date('Y-m-d H:i', $exam['starttime']); $exam['endtime_s'] = date('Y-m-d H:i', $exam['endtime']); $out[] = $exam; } } - return $out; + return ['exams' => $out, 'decollapse' => $hasCollapsed]; } - protected function makeLectureExamList() + protected function makeLectureExamList(): array { $out = []; $now = time(); @@ -274,15 +294,15 @@ class Page_Exams extends Page ] + $source; } - private function isDateSane($time) + private function isDateSane(int $time): bool { - return ($time >= $this->rangeMin && $time <= $this->rangeMax); + return ($time >= strtotime('-10 years') && $time <= strtotime('+10 years')); } private function saveExam() { if (!Request::isPost()) { - Util::traceError('Is not post'); + ErrorHandler::traceError('Is not post'); } /* process form-data */ $locationids = Request::post('locations', [], "ARRAY"); @@ -439,11 +459,17 @@ class Page_Exams extends Page } elseif ($this->action === false) { - Util::traceError("action not implemented"); + ErrorHandler::traceError("action not implemented"); } } + private function getLocationLookupJson() + { + $locs = Location::getLocationsAssoc(); // Add key x so we get an object, not array + return json_encode(['x' => 0] + array_map(function ($item) { return $item['children']; }, $locs)); + } + protected function doRender() { if ($this->action === "show") { @@ -452,7 +478,7 @@ class Page_Exams extends Page // General title and description Render::addTemplate('page-main-heading'); // List of defined exam periods - $params = ['exams' => $this->makeExamsForTemplate()]; + $params = $this->makeExamsForTemplate(); Permission::addGlobalTags($params['perms'], NULL, ['exams.edit']); Render::addTemplate('page-exams', $params); // List of upcoming lectures marked as exam @@ -481,7 +507,7 @@ class Page_Exams extends Page } elseif ($this->action === "add") { Render::setTitle(Dictionary::translate('title_add-exam')); - $data = []; + $data = ['locmap' => $this->getLocationLookupJson()]; $baseLecture = Request::any('lectureid', false, 'string'); $locations = null; if ($baseLecture !== false) { @@ -519,10 +545,12 @@ class Page_Exams extends Page } } - $data = []; - $data['exam'] = $exam; - $data['locations'] = $this->locations; - $data['lectures'] = $this->lectures; + $data = [ + 'locmap' => $this->getLocationLookupJson(), + 'exam' => $exam, + 'locations' => $this->locations, + 'lectures' => $this->lectures, + ]; // if user has no permission to edit for this location, disable the location in the select foreach ($data['locations'] as &$loc) { |