From c2668bf721d22fe58663c8588b07f324b884bc5f Mon Sep 17 00:00:00 2001 From: Thomas Hackl Date: Mon, 25 Apr 2022 16:11:49 +0200 Subject: restructure db query for all courses and corresponding listing --- app/controllers/admin/autoinsert.php | 95 ++++++++++++++++++++++---- app/views/admin/autoinsert/_degrees.php | 10 +-- app/views/admin/autoinsert/_domains.php | 20 +++--- app/views/admin/autoinsert/_institutes.php | 14 ++-- app/views/admin/autoinsert/_semesters.php | 12 ++++ app/views/admin/autoinsert/_subjects.php | 10 +-- app/views/admin/autoinsert/index.php | 89 ++++++++++++++---------- lib/classes/AutoInsert.class.php | 74 +++++++++++++------- resources/assets/javascripts/lib/autoinsert.js | 10 +-- 9 files changed, 228 insertions(+), 106 deletions(-) create mode 100644 app/views/admin/autoinsert/_semesters.php diff --git a/app/controllers/admin/autoinsert.php b/app/controllers/admin/autoinsert.php index 5e80e58..c1efb00 100644 --- a/app/controllers/admin/autoinsert.php +++ b/app/controllers/admin/autoinsert.php @@ -34,7 +34,7 @@ class Admin_AutoinsertController extends AuthenticatedController * Maintenance view for the auto insert parameters * */ - public function index_action() + public function index_action($group = 'by_course') { // search seminars if (Request::submitted('suchen')) { @@ -53,8 +53,20 @@ class Admin_AutoinsertController extends AuthenticatedController PageLayout::postError(_('Bitte geben Sie einen Suchparameter ein.')); } } - $seminare = AutoInsert::getAllSeminars(); - $this->auto_sems = $seminare; + $entries = AutoInsert::getAllSeminars(); + + $this->auto_sems = []; + if ($group == 'by_course') { + foreach ($entries as $entry) { + $this->auto_sems[$entry['seminar_id']][$entry['range_type']][] = $entry; + } + } else if ($group == 'by_type') { + } + + PageLayout::postInfo('
' . print_r($this->auto_sems, 1) . '
'); + + $this->range_types = AutoInsert::getRangeTypes(); + $this->grouping = $group; $domains = []; $domains[] = [ @@ -70,13 +82,24 @@ class Admin_AutoinsertController extends AuthenticatedController $this->degrees = Degree::findBySQL("1 ORDER BY `name`"); $this->subjects = Fach::findBySQL("1 ORDER BY `name`"); + $views = new ViewsWidget(); + $views->addLink( + _('nach Veranstaltung'), + $this->link_for('admin/autoinsert/index/by_course') + )->setActive($group == 'by_course'); + $views->addLink( + _('nach Art der Zuordnung'), + $this->link_for('admin/autoinsert/index/by_type') + )->setActive($group == 'by_type'); + Sidebar::get()->addWidget($views); + $links = new ActionsWidget(); $links->addLink( _('Benutzergruppen manuell eintragen'), $this->manualURL(), Icon::create('visibility-visible') ); - Sidebar::Get()->addWidget($links); + Sidebar::get()->addWidget($links); } /** @@ -85,19 +108,61 @@ class Admin_AutoinsertController extends AuthenticatedController public function new_action() { if (Request::submitted('anlegen')) { + PageLayout::postInfo('
' . print_r(Request::getInstance(), 1) . '
'); $sem_id = Request::option('sem_id'); - $domains = Request::getArray('rechte'); - if (empty($domains)) { - PageLayout::postError(_('Mindestens ein Status sollte selektiert werden!')); + + $entries = Request::getArray('rechte'); + if (empty($entries)) { + PageLayout::postError(_('Mindestens ein Eintrag sollte selektiert werden!')); } else { - foreach ($domains as $id => $rechte) { - if ($id === 'keine') - $id = ''; - if (!AutoInsert::checkSeminar($sem_id, $id)) { - AutoInsert::saveSeminar($sem_id, $rechte, $id); - PageLayout::postSuccess(_('Die Zuordnung wurde erfolgreich gespeichert!')); - } else { - PageLayout::postError(_('Das Seminar wird bereits zu diesem Zweck verwendet!')); + foreach ($entries as $type => $assignments) { + switch ($type) { + // Old behaviour: domain assignments + case 'domain': + foreach ($assignments as $id => $rechte) { + if ($id === 'keine') + $id = ''; + if (!AutoInsert::checkSeminar($sem_id, $id)) { + AutoInsert::saveSeminar($sem_id, $rechte, $id, $type); + PageLayout::postSuccess(_('Die Zuordnung wurde erfolgreich gespeichert!')); + } else { + PageLayout::postError(_('Das Seminar wird bereits zu diesem Zweck verwendet!')); + } + } + break; + // Institute assignments with permission level + case 'institute': + foreach ($assignments as $rechte) { + $id = Request::option('institute_id'); + if (!AutoInsert::checkSeminar($sem_id, $id, $type)) { + AutoInsert::saveSeminar($sem_id, $rechte, $id, $type); + PageLayout::postSuccess(_('Die Zuordnung wurde erfolgreich gespeichert!')); + } else { + PageLayout::postError(_('Das Seminar wird bereits zu diesem Zweck verwendet!')); + } + } + break; + // Degree and subject assignments + case 'degree': + case 'subject': + foreach ($assignments as $id) { + if (!AutoInsert::checkSeminar($sem_id, $id, $type)) { + AutoInsert::saveSeminar($sem_id, '', $id, $type); + PageLayout::postSuccess(_('Die Zuordnung wurde erfolgreich gespeichert!')); + } else { + PageLayout::postError(_('Das Seminar wird bereits zu diesem Zweck verwendet!')); + } + } + break; + // Semester of study assignments + case 'semester': + if (!AutoInsert::checkSeminar($sem_id, $assignments, $type)) { + AutoInsert::saveSeminar($sem_id, '', $assignments, $type); + PageLayout::postSuccess(_('Die Zuordnung wurde erfolgreich gespeichert!')); + } else { + PageLayout::postError(_('Das Seminar wird bereits zu diesem Zweck verwendet!')); + } + break; } } } diff --git a/app/views/admin/autoinsert/_degrees.php b/app/views/admin/autoinsert/_degrees.php index a9dda00..751169d 100644 --- a/app/views/admin/autoinsert/_degrees.php +++ b/app/views/admin/autoinsert/_degrees.php @@ -1,14 +1,14 @@ -
-

+
+ -

+
-
+ diff --git a/app/views/admin/autoinsert/_domains.php b/app/views/admin/autoinsert/_domains.php index a056db3..7690eb5 100644 --- a/app/views/admin/autoinsert/_domains.php +++ b/app/views/admin/autoinsert/_domains.php @@ -1,25 +1,25 @@ -
-

- -

+
+ + + -

+

-

+
-
+ diff --git a/app/views/admin/autoinsert/_institutes.php b/app/views/admin/autoinsert/_institutes.php index f264b25..8cbf67f 100644 --- a/app/views/admin/autoinsert/_institutes.php +++ b/app/views/admin/autoinsert/_institutes.php @@ -1,22 +1,22 @@ -
-

+
+ -

+ withButton() ?> -
+ diff --git a/app/views/admin/autoinsert/_semesters.php b/app/views/admin/autoinsert/_semesters.php new file mode 100644 index 0000000..87a3538 --- /dev/null +++ b/app/views/admin/autoinsert/_semesters.php @@ -0,0 +1,12 @@ +
+ + + + +
+ +
+
diff --git a/app/views/admin/autoinsert/_subjects.php b/app/views/admin/autoinsert/_subjects.php index 970af88..1a9d053 100644 --- a/app/views/admin/autoinsert/_subjects.php +++ b/app/views/admin/autoinsert/_subjects.php @@ -1,14 +1,14 @@ -
-

+
+ -

+
-
+ diff --git a/app/views/admin/autoinsert/index.php b/app/views/admin/autoinsert/index.php index 49e3e0e..1d2abf0 100644 --- a/app/views/admin/autoinsert/index.php +++ b/app/views/admin/autoinsert/index.php @@ -41,19 +41,23 @@ use Studip\Button, Studip\LinkButton;
+
@@ -76,40 +80,55 @@ use Studip\Button, Studip\LinkButton; - - - - - - - + + + + + + + - $courses): ?> - $auto_sem): ?> - - + + $types): ?> + + + + + + + + + + + - render_partial("admin/autoinsert/_status.php", ['status' => 'dozent', 'auto_sem' => $auto_sem, 'domains' => $userdomains]) ?> - render_partial("admin/autoinsert/_status.php", ['status' => 'tutor', 'auto_sem' => $auto_sem, 'domains' => $userdomains]) ?> - render_partial("admin/autoinsert/_status.php", ['status' => 'autor', 'auto_sem' => $auto_sem, 'domains' => $userdomains]) ?> - - - - - + + + + + + + +
- - - -
+ + + + + + + + + + - - _('Veranstaltung entfernen'), 'class' => 'text-top'] - ) ?> - -
+ + _('Veranstaltung entfernen'), 'class' => 'text-top'] + ) ?> + +
diff --git a/lib/classes/AutoInsert.class.php b/lib/classes/AutoInsert.class.php index 4c6069d..c7bfed9 100644 --- a/lib/classes/AutoInsert.class.php +++ b/lib/classes/AutoInsert.class.php @@ -193,17 +193,17 @@ class AutoInsert * @param string $seminar_id Id of the seminar * @return bool Indicating whether the seminar already has an autoinsert record */ - public static function checkSeminar($seminar_id, $domain_id = false) + public static function checkSeminar($seminar_id, $range_id = false, $range_type = 'domain') { $cached = self::getSeminarCache(); if (!isset($cached[$seminar_id])) { - $query = "SELECT domain_id, 1 + $query = "SELECT range_id, 1 FROM auto_insert_sem - WHERE seminar_id = ?"; + WHERE seminar_id = ? AND range_type = ?"; $cached[$seminar_id] = DBManager::get()->fetchGroupedPairs( $query, - [$seminar_id], + [$seminar_id, $range_type], function ($value) { return (bool) $value; } @@ -222,13 +222,13 @@ class AutoInsert * containing the status(ses) to enable for * autoinsertion */ - public static function saveSeminar($seminar_id, $status, $domain_id) + public static function saveSeminar($seminar_id, $status, $range_id, $range_type = 'domain') { - $query = "INSERT INTO auto_insert_sem (seminar_id, status,domain_id) VALUES (?, ?,?)"; + $query = "INSERT INTO auto_insert_sem (seminar_id, status, range_id, range_type) VALUES (?, ?, ?, ?)"; $statement = DBManager::get()->prepare($query); foreach ((array)$status as $s) { - $statement->execute([$seminar_id, $s, $domain_id]); + $statement->execute([$seminar_id, $s, $range_id, $range_type]); } } @@ -279,26 +279,39 @@ class AutoInsert $results = $statement->fetchAll(PDO::FETCH_COLUMN); } else { $results = []; + foreach (words('degree domain institute semester subject') as $type) { - $query = "SELECT a.seminar_id, GROUP_CONCAT(a.status,IF(LENGTH(a.range_id)=0,':keine',CONCAT(':',a.range_id))) AS range_status, s.Name, s.Schreibzugriff, s.start_time "; - $query .= "FROM auto_insert_sem a "; - $query .= "JOIN seminare AS s USING (Seminar_id) WHERE a.`range_type` = :type "; - - $query .= "GROUP BY s.seminar_id "; - $query .= "ORDER BY s.Name"; - - $data = DBManager::get()->fetchAll($query, ['type' => $type]); - if (count($data) > 0) { - $results[$type] = []; - - foreach ($data as $index => $result) { - $entries = explode(',', $result['range_status']); - foreach ($entries as $entry) { - $array = explode(':', $entry); - $results[$type][$index]['status'][$array[1]][] = $array[0]; - } - } + $select = "SELECT a.`seminar_id`, s.`Name`, a.`range_type`, a.`range_id`, + GROUP_CONCAT(a.`status`) AS status, s.`Schreibzugriff`, s.`start_time`"; + $from = " FROM `auto_insert_sem` a "; + $join = [ + "JOIN `seminare` s ON (s.`Seminar_id` = a.`seminar_id`)" + ]; + $where = " WHERE a.`range_type` = :type "; + $order = "GROUP BY a.`range_type`, a.`range_id` ORDER BY s.`start_time` DESC, s.`Name`, range_name"; + + switch ($type) { + case 'domain': + $select .= ", IF(LENGTH(`range_id`) = 0, 'keine', a.`range_id`) AS range_name"; + break; + case 'institute': + $select .= ", i.`Name` AS range_name "; + $join[] = "JOIN `Institute` i ON (i.`Institut_id` = a.`range_id`)"; + break; + case 'degree': + $select .= ", d.`name` AS range_name"; + $join[] = "JOIN `abschluss` d ON (d.`abschluss_id` = a.`range_id`)"; + break; + case 'subject': + $select .= ", f.`name` AS range_name"; + $join[] = "JOIN `fach` f ON (f.`fach_id` = a.`range_id`)"; + break; + case 'semester': + $select .= ", a.`range_id` AS range_name"; } + + $query = $select.$from.implode(' ', $join).$where.$order; + $results += array_merge($results, DBManager::get()->fetchAll($query, ['type' => $type])); } } @@ -369,4 +382,15 @@ class AutoInsert } return self::$seminar_cache; } + + public static function getRangeTypes() + { + return [ + 'degree' => _('Abschluss'), + 'domain' => _('Domäne'), + 'institute' => _('Einrichtung'), + 'semester' => _('Fachsemester'), + 'subject' => _('Fach') + ]; + } } diff --git a/resources/assets/javascripts/lib/autoinsert.js b/resources/assets/javascripts/lib/autoinsert.js index 66ffafa..b231d27 100644 --- a/resources/assets/javascripts/lib/autoinsert.js +++ b/resources/assets/javascripts/lib/autoinsert.js @@ -1,9 +1,11 @@ const Autoinsert = { init: function() { - $('input[name="autoinsert_type"][type="radio"]').on('change', function(event) { - const selected = event.target.value - $('.autoinsert-selection').addClass('hidden-js') - $('#autoinsert-' + selected).removeClass('hidden-js') + $('input[name="autoinsert_type"][type="checkbox"]').on('change', function(event) { + if (this.checked) { + $('#autoinsert-' + this.value).removeClass('hidden-js') + } else { + $('#autoinsert-' + this.value).addClass('hidden-js') + } }) } } -- cgit v1.0