From fdf92b3b805585a8682f742919b0c64270fed1ed Mon Sep 17 00:00:00 2001 From: Stuart Lamour Date: Fri, 13 Dec 2024 19:26:00 +0000 Subject: [PATCH 1/8] CTP-4184 Enable staff only alerts --- block_alerts.php | 29 ++++++++++++++++++++++++++++- lang/en/block_alerts.php | 1 + settings.php | 13 ++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/block_alerts.php b/block_alerts.php index b5c327e..aa7b196 100644 --- a/block_alerts.php +++ b/block_alerts.php @@ -77,19 +77,46 @@ public function get_content(): stdClass { return $this->content; } + /** + * Return if user has archetype editingteacher. + * + */ + public static function is_teacher(): bool { + global $DB, $USER; + // Get id's from role where archetype is editingteacher. + $roles = $DB->get_fieldset('role', 'id', ['archetype' => 'editingteacher']); + + // Check if user has editingteacher role on any courses. + list($roles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED); + $params['userid'] = $USER->id; + $sql = "SELECT id + FROM {role_assignments} + WHERE userid = :userid + AND roleid $roles"; + return $DB->record_exists_sql($sql, $params); + } + /** * Get the alerts. * * @return array alerts items. */ public function fetch_alert(): array { + // Staff only check. + if (get_config('block_alerts', 'staffonly')) { + if (!self::is_teacher()) { + return []; // Don't ouput for learners. + } + } + // Template data for mustache. $template = new stdClass(); // Get alert content. $alert = new stdClass(); - $alert->description = get_config('block_alerts', 'description'); + $alert->staffonly = get_config('block_alerts', 'staffonly'); $alert->title = get_config('block_alerts', 'title'); + $alert->description = get_config('block_alerts', 'description'); $alert->link = get_config('block_alerts', 'link'); $alert->linktext = get_config('block_alerts', 'linktext'); diff --git a/lang/en/block_alerts.php b/lang/en/block_alerts.php index 89ce66b..680ee64 100644 --- a/lang/en/block_alerts.php +++ b/lang/en/block_alerts.php @@ -34,4 +34,5 @@ $string['linktext_help'] = 'A meaningful link title. Do not use inaccessible link text such as "click here" or "read more".'; $string['pluginname'] = 'Alerts'; $string['privacy:metadata'] = 'Alerts does not store any personal data.'; +$string['staffonly'] = "Staff only"; $string['title'] = 'Title'; diff --git a/settings.php b/settings.php index cef57f5..8b5d783 100644 --- a/settings.php +++ b/settings.php @@ -29,12 +29,18 @@ $default = ''; + // Staff only. + $setting = new admin_setting_configcheckbox('block_alerts/staffonly', + get_string('staffonly', 'block_alerts'), '', 0); + $settings->add($setting); + // Title. $setting = new admin_setting_configtext('block_alerts/title', get_string('title', 'block_alerts'), '', $default, - PARAM_RAW + PARAM_RAW, + '140' ); $settings->add($setting); @@ -43,7 +49,8 @@ get_string('description', 'block_alerts'), get_string('description_help', 'block_alerts'), $default, - PARAM_RAW + PARAM_RAW, + '140' ); $settings->add($setting); @@ -52,7 +59,7 @@ get_string('link', 'block_alerts'), get_string('link_help', 'block_alerts'), $default, - PARAM_RAW + PARAM_RAW, ); $settings->add($setting); From eb3f1c5232d90017070c9e2d67c6e33e1596fd50 Mon Sep 17 00:00:00 2001 From: Stuart Lamour Date: Fri, 13 Dec 2024 19:45:29 +0000 Subject: [PATCH 2/8] CI indents --- block_alerts.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/block_alerts.php b/block_alerts.php index aa7b196..ca7431e 100644 --- a/block_alerts.php +++ b/block_alerts.php @@ -77,7 +77,7 @@ public function get_content(): stdClass { return $this->content; } - /** + /** * Return if user has archetype editingteacher. * */ @@ -104,9 +104,9 @@ public static function is_teacher(): bool { public function fetch_alert(): array { // Staff only check. if (get_config('block_alerts', 'staffonly')) { - if (!self::is_teacher()) { - return []; // Don't ouput for learners. - } + if (!self::is_teacher()) { + return []; // Don't ouput for learners. + } } // Template data for mustache. From fe23d3c6287d10b824044237437e495fe0983d4c Mon Sep 17 00:00:00 2001 From: Stuart Lamour Date: Sat, 14 Dec 2024 01:05:31 +0000 Subject: [PATCH 3/8] cr update --- block_alerts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/block_alerts.php b/block_alerts.php index ca7431e..61db0c8 100644 --- a/block_alerts.php +++ b/block_alerts.php @@ -114,7 +114,6 @@ public function fetch_alert(): array { // Get alert content. $alert = new stdClass(); - $alert->staffonly = get_config('block_alerts', 'staffonly'); $alert->title = get_config('block_alerts', 'title'); $alert->description = get_config('block_alerts', 'description'); $alert->link = get_config('block_alerts', 'link'); From f217617348f426cb6ef2d1000d79a49fd3f18314 Mon Sep 17 00:00:00 2001 From: Stuart Lamour Date: Wed, 8 Jan 2025 12:04:00 +0000 Subject: [PATCH 4/8] Add system level roles, improve form --- block_alerts.php | 17 +++++++++++++---- settings.php | 8 +++++--- templates/content.mustache | 2 +- version.php | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/block_alerts.php b/block_alerts.php index 61db0c8..e546e77 100644 --- a/block_alerts.php +++ b/block_alerts.php @@ -83,6 +83,17 @@ public function get_content(): stdClass { */ public static function is_teacher(): bool { global $DB, $USER; + + // System level roles. + $context = context_system::instance(); + $roles = get_user_roles($context, $USER->id); + // Check for manager or coursecreator role. + foreach ($roles as $role) { + if ($role->shortname === 'manager' || $role->shortname === 'coursecreator') { + return true; + } + } + // Get id's from role where archetype is editingteacher. $roles = $DB->get_fieldset('role', 'id', ['archetype' => 'editingteacher']); @@ -103,10 +114,8 @@ public static function is_teacher(): bool { */ public function fetch_alert(): array { // Staff only check. - if (get_config('block_alerts', 'staffonly')) { - if (!self::is_teacher()) { - return []; // Don't ouput for learners. - } + if (get_config('block_alerts', 'staffonly') && !self::is_teacher()) { + return []; } // Template data for mustache. diff --git a/settings.php b/settings.php index 8b5d783..985b06a 100644 --- a/settings.php +++ b/settings.php @@ -40,7 +40,7 @@ '', $default, PARAM_RAW, - '140' + '60' ); $settings->add($setting); @@ -50,7 +50,7 @@ get_string('description_help', 'block_alerts'), $default, PARAM_RAW, - '140' + '60' ); $settings->add($setting); @@ -60,6 +60,7 @@ get_string('link_help', 'block_alerts'), $default, PARAM_RAW, + '60' ); $settings->add($setting); @@ -68,7 +69,8 @@ get_string('linktext', 'block_alerts'), get_string('linktext_help', 'block_alerts'), $default, - PARAM_RAW + PARAM_RAW, + '60' ); $settings->add($setting); diff --git a/templates/content.mustache b/templates/content.mustache index 43cbfa0..72d5220 100644 --- a/templates/content.mustache +++ b/templates/content.mustache @@ -28,7 +28,7 @@ }}