Skip to content

Commit

Permalink
Code checker changes
Browse files Browse the repository at this point in the history
N.B. Doing this manually while setting automation up locally.
  • Loading branch information
stuartlamour committed May 10, 2023
1 parent 50aa956 commit 273e987
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 71 deletions.
63 changes: 37 additions & 26 deletions block_alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;

}
}

Expand All @@ -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,
Expand All @@ -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;
}
}
}

19 changes: 14 additions & 5 deletions classes/admin_setting_date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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,
'<div class="form-text defaultsnext">
<input type="date"
<input type="date"
id="'.$this->get_id().'"
class="form-control"
size="10"
name="'.$this->get_full_name().'"
name="'.$this->get_full_name().'"
value="'.s($data).'" /></div>',
$this->description, true, '', $default, $query);
}
}
}

7 changes: 5 additions & 2 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -41,4 +43,5 @@
],
'clonepermissionsfrom' => 'moodle/site:manageblocks'
],
];
];

18 changes: 9 additions & 9 deletions edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', '<p><a href="'.$href.'" >'.$text.'</a></p>');

// TODO - Carousel or grid layout option?
}

function set_data($defaults) {
parent::set_data($defaults);
}
}
}

12 changes: 7 additions & 5 deletions lang/en/block_alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -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.';
$string['description_help'] = 'Short text, no more that 140 characters.';

17 changes: 14 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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);
}
}
}


57 changes: 39 additions & 18 deletions templates/content.mustache
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
<div class="alert alert-warning px-2 border-warning d-flex d-flex align-items-center justify-content-center"
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}

{{!
@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
}
]

}
}}

<div class="alert alert-warning px-2 border-warning d-flex d-flex align-items-center justify-content-center"
style="border: none; border-left: .25rem solid;">
<!-- Alerts icon. -->
<div>
<div class="text-warning d-flex d-flex align-items-center justify-content-center font-weight-bold rounded-circle"
<div class="text-warning d-flex d-flex align-items-center justify-content-center font-weight-bold rounded-circle"
style="height: 30px; width: 30px; border: .25rem solid;">
!
</div>
Expand All @@ -15,8 +50,8 @@

{{#alerts}}
<div class="carousel-item {{#active}} active {{/active}}">
<div><small class="font-weight-normal">{{displaydate}}</small></div>
<div> {{description}} </div>
<strong>{{title}}</strong>
<div>{{description}}</div>
</div>
{{/alerts}}

Expand All @@ -35,17 +70,3 @@
{{/nav}}

</div>

<!-- We use an inline style tag because moodle cannot process @ symbol in css files. -->
<style>
.block-alert-carousel .carousel-item {
transition: all .5s ease-in-out;
opacity: 0;
}
.block-alert-carousel .carousel-item.active {
opacity: 1;
}
</style>
Loading

0 comments on commit 273e987

Please sign in to comment.