From 273e987c9806ef6f9b9d603ce80b5dd344c6068b Mon Sep 17 00:00:00 2001 From: Stuart Lamour Date: Wed, 10 May 2023 22:55:37 +0100 Subject: [PATCH] Code checker changes N.B. Doing this manually while setting automation up locally. --- block_alerts.php | 63 ++++++++++++++++++++-------------- classes/admin_setting_date.php | 19 +++++++--- db/access.php | 7 ++-- edit_form.php | 18 +++++----- lang/en/block_alerts.php | 12 ++++--- settings.php | 17 +++++++-- templates/content.mustache | 57 ++++++++++++++++++++---------- version.php | 7 ++-- 8 files changed, 129 insertions(+), 71 deletions(-) diff --git a/block_alerts.php b/block_alerts.php index f372a0f..b16a841 100644 --- a/block_alerts.php +++ b/block_alerts.php @@ -18,10 +18,9 @@ * Block definition class for the block_alerts plugin. * * @package block_alerts - * @author 2023 Stuart Lamour + * @copyright 2023 Stuart Lamour * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - class block_alerts extends block_base { /** @@ -34,7 +33,11 @@ public function init() { $this->title = get_string('pluginname', 'block_alerts'); } - function specialization() { + /** + * Gets the block settings. + * + */ + public function specialization() { if (isset($this->config->title)) { $this->title = $this->title = format_string($this->config->title, true, ['context' => $this->context]); } else { @@ -45,9 +48,9 @@ function specialization() { /** * Gets the block contents. * - * @return string The block HTML. + * @return stdClass - the block content. */ - public function get_content() { + public function get_content() : stdClass { global $OUTPUT; if ($this->content !== null) { @@ -60,10 +63,7 @@ public function get_content() { $template = new stdClass(); $template->alerts = $this->fetch_alerts(); $itemcount = count($template->alerts); - - if ($itemcount) { - $template->hasalerts = true; - } + if ($itemcount > 1) { $template->nav = true; } @@ -73,32 +73,33 @@ public function get_content() { return $this->content; } - /** + /** * Get the alerts. - * + * * @return array alerts items. */ public function fetch_alerts() : array { // Template data for mustache. $template = new stdClass(); - + // Get alerts items. - for ($i = 1 ; $i < 4; $i++) { + for ($i = 1; $i < 4; $i++) { $alerts = new stdClass(); $alerts->description = get_config('block_alerts', 'description'.$i); + $alerts->title = get_config('block_alerts', 'title'.$i); $alerts->date = get_config('block_alerts', 'date'.$i); - + // Check alerts is populated. - if ($alerts->description) { + if ($alerts->title) { // Format the date for display. - if($alerts->date) { - $alerts->displaydate = date_format(date_create($alerts->date),"jS M Y"); + if ($alerts->date) { + $alerts->displaydate = date_format(date_create($alerts->date), "jS M Y"); } // Make a temp key value array to sort. // NOTE - index added to make keys unique. $template->tempalerts[$alerts->date.'-'.$i] = $alerts; - + } } @@ -114,19 +115,19 @@ public function fetch_alerts() : array { foreach ($template->tempalerts as $alerts) { $template->alerts[] = $alerts; } - + // Set first element as active for carosel version. $template->alerts[0]->active = true; - return $template->alerts; + return $template->alerts; } /** - * Defines in which pages this block can be added. + * Defines on which pages this block can be added. * * @return array of the pages where the block can be added. */ - public function applicable_formats() { + public function applicable_formats() : array { return [ 'admin' => false, 'site-index' => true, @@ -136,11 +137,21 @@ public function applicable_formats() { ]; } - public function instance_allow_multiple() { + /** + * Defines if the block can be added multiple times. + * + * @return bool. + */ + public function instance_allow_multiple() : bool { return true; } - - function has_config() { + /** + * Defines if the has config. + * + * @return bool. + */ + public function has_config() : bool { return true; } -} \ No newline at end of file +} + diff --git a/classes/admin_setting_date.php b/classes/admin_setting_date.php index 04f78a3..9a15170 100644 --- a/classes/admin_setting_date.php +++ b/classes/admin_setting_date.php @@ -19,10 +19,11 @@ /** * Class to render input of type url in settings pages. * @package block_alerts - * @author 2023 Stuart Lamour + * @copyright 2023 Stuart Lamour * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class admin_setting_date extends \admin_setting_configtext { + /** * Config url constructor * @@ -36,16 +37,24 @@ public function __construct($name, $visiblename, $description, $defaultsetting) parent::__construct($name, $visiblename, $description, $defaultsetting, PARAM_RAW, 50); } - public function output_html($data, $query='') { + /** + * Generate the HTML output. + * + * @param array $data An array of checked values + * @param string $query + * @return string HMTL field + */ + public function output_html($data, $query='') : string { $default = $this->get_defaultsetting(); return format_admin_setting($this, $this->visiblename, '
-
', $this->description, true, '', $default, $query); } -} \ No newline at end of file +} + diff --git a/db/access.php b/db/access.php index 99ab262..8c921ab 100644 --- a/db/access.php +++ b/db/access.php @@ -18,10 +18,12 @@ * Plugin capabilities for the block_alerts plugin. * * @package block_alerts - * @author 2023 Stuart Lamour + * @copyright 2023 Stuart Lamour * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +defined('MOODLE_INTERNAL') || die(); + $capabilities = [ 'block/alerts:myaddinstance' => [ 'captype' => 'write', @@ -41,4 +43,5 @@ ], 'clonepermissionsfrom' => 'moodle/site:manageblocks' ], -]; \ No newline at end of file +]; + diff --git a/edit_form.php b/edit_form.php index 0008ce9..aae0e86 100644 --- a/edit_form.php +++ b/edit_form.php @@ -18,17 +18,21 @@ * Block definition class for the block_alerts plugin. * * @package block_alerts - * @author 2023 Stuart Lamour + * @copyright 2023 Stuart Lamour * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - class block_alerts_edit_form extends block_edit_form { + /** + * Edit form. + * + * @param \MoodleQuickForm $mform the form being built. + */ protected function specific_definition($mform) { // Fieldset. $mform->addElement('header', 'configheader', get_string('blocksettings', 'block')); - + // Block title. $mform->addElement('text', 'config_title', get_string('blocktitle', 'block_alerts')); $mform->setType('config_title', PARAM_TEXT); @@ -37,11 +41,7 @@ protected function specific_definition($mform) { $href = new moodle_url('/admin/settings.php?section=blocksettingalerts'); $text = get_string('configurealerts', 'block_alerts'); $mform->addElement('html', '

'.$text.'

'); - - // TODO - Carousel or grid layout option? } - function set_data($defaults) { - parent::set_data($defaults); - } -} \ No newline at end of file +} + diff --git a/lang/en/block_alerts.php b/lang/en/block_alerts.php index d070144..3dedcb3 100644 --- a/lang/en/block_alerts.php +++ b/lang/en/block_alerts.php @@ -18,7 +18,7 @@ * Languages configuration for the block_alerts plugin. * * @package block_alerts - * @author 2023 Stuart Lamour + * @copyright 2023 Stuart Lamour * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -27,9 +27,11 @@ $string['alerts:myaddinstance'] = 'Add alerts block'; $string['blocktitle'] = 'Alerts block title'; -$string['configurealerts'] = "Edit global alerts items"; -$string['alertsitem'] = 'Alerts item'; +$string['configurealerts'] = "Edit global alert posts"; $string['title'] = 'Title'; -$string['date'] = 'Date'; +$string['alertsitem'] = 'Alerts post'; +$string['date'] = 'Post date'; +$string['date_help'] = 'Post date sets the order of display, not the date it becomes visible.'; $string['description'] = 'Description'; -$string['description_help'] = 'Short text, no more that 140 characters.'; \ No newline at end of file +$string['description_help'] = 'Short text, no more that 140 characters.'; + diff --git a/settings.php b/settings.php index 4dd11f7..5351ee4 100644 --- a/settings.php +++ b/settings.php @@ -32,7 +32,7 @@ $default = ''; // Add 3 slots for alerts. - for ($i = 1 ; $i < 4; $i++) { + for ($i = 1; $i < 4; $i++) { // Heading. $setting = new admin_setting_heading('h'.$i, get_string('alertsitem', 'block_alerts'), @@ -43,19 +43,30 @@ // Date. $setting = new admin_setting_date('block_alerts/date'.$i, get_string('date', 'block_alerts'), - '', + get_string('date_help', 'block_alerts'), $default ); $settings->add($setting); + // Title. + $setting = new admin_setting_configtext('block_alerts/title'.$i, + get_string('title', 'block_alerts'), + '', + $default, + PARAM_RAW + ); + $settings->add($setting); + // Description. $setting = new admin_setting_configtext('block_alerts/description'.$i, get_string('description', 'block_alerts'), get_string('description_help', 'block_alerts'), - $default, + $default, PARAM_RAW ); $settings->add($setting); } } } + + diff --git a/templates/content.mustache b/templates/content.mustache index 16051ad..0122bab 100644 --- a/templates/content.mustache +++ b/templates/content.mustache @@ -1,8 +1,43 @@ -
. +}} + +{{! + @template blocks_alerts/content + Example context (json): + { + "nav": 1, + "alerts": [ + { + "title": "This is the alert title", + "description": "This is the news description", + "active": 1 + }, + { + "title": "This is the alert title", + "description": "This is the news description", + "active": 0 + } + ] + + } +}} + +
-
!
@@ -15,8 +50,8 @@ {{#alerts}} {{/alerts}} @@ -35,17 +70,3 @@ {{/nav}}
- - - diff --git a/version.php b/version.php index ec9b1c0..03323c5 100644 --- a/version.php +++ b/version.php @@ -24,6 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023050300; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2019051100; // Requires this Moodle version -$plugin->component = 'block_alerts'; // Full name of the plugin (used for diagnostics) \ No newline at end of file +$plugin->version = 2023050300; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2019051100; // Requires this Moodle version. +$plugin->component = 'block_alerts'; // Full name of the plugin (used for diagnostics). +