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);