Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions amd/build/custom.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/custom.min.js.map

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions amd/src/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ define(['jquery'], function($) {
i === 'enabled' ||
i === 'global' ||
i === 'dismissible' ||
i === 'aicon'
i === 'aicon' ||
i === 'sendnotifications'
) && data[i] == 1) {
affectelement.prop('checked', true);
} else if (
(i === 'enabled' ||
i === 'global' ||
i === 'dismissible' ||
i === 'aicon') && data[i] == 0) {
i === 'aicon' ||
i === 'sendnotifications'
) && data[i] == 0) {
affectelement.prop('checked', false);
} else {
affectelement.val(data[i]);
Expand Down Expand Up @@ -240,6 +243,11 @@ define(['jquery'], function($) {
reloadPreview();
});

$('#add_notification_sendnotifications').on('change', function() {
// Checking specifically whether ticked/checked or not to ensure it's displayed correctly (not toggling).
reloadPreview();
});

Comment thread
danielneis marked this conversation as resolved.
Outdated
// Check if preview is displaying correct (Update it).
var reloadPreview = function() {
// Update title.
Expand Down Expand Up @@ -392,4 +400,4 @@ define(['jquery'], function($) {
});
}
};
});
});
84 changes: 84 additions & 0 deletions classes/task/sendnotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// 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/>.

/**
* Ad hoc task to send notifications.
*
* @package block_advnotifications
* @copyright 2021 Daniel Neis Araujo <daniel@adapta.online>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_advnotifications\task;

defined('MOODLE_INTERNAL') || die();

/**
* Ad hoc task to send notifications class.
*
* @package block_advnotifications
* @copyright 2021 Daniel Neis Araujo <daniel@adapta.online>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sendnotifications extends \core\task\adhoc_task {

/**
* Return the task's name as shown in admin screens.
*
* @return string
*/
public function get_name() {
return get_string('sendnotificationstask', 'block-advnotifications');
}

public function execute() {
global $DB;

if (!get_config('block_advnotifications', 'enable')) {
return;
}

$from = \core_user::get_noreply_user();

$notification = $DB->get_record('block_advnotifications', ['id' => $this->get_custom_data()->notificationid]);

if (!$notification->enabled) {
return;
}

$bcontext = \context_block::instance($notification->blockid);
if ($ccontext = $bcontext->get_course_context(false)) {
$users = get_enrolled_users($ccontext, '', 0 , 'u.id');
} else {
$users = $DB->get_records('user', ['deleted' => 0, 'suspended' => 0], '', 'id');
}

foreach ($users as $u) {
$eventdata = new \core\message\message();
$eventdata->component = 'block_advnotifications';
$eventdata->name = 'sendadvnotifications';
$eventdata->userfrom = $from;
$eventdata->userto = $u->id;
$eventdata->subject = $notification->title;
$eventdata->fullmessage = format_text($notification->message, FORMAT_MOODLE);
$eventdata->fullmessageformat = FORMAT_MOODLE;
$eventdata->fullmessagehtml = format_text($notification->message, FORMAT_MOODLE);
$eventdata->smallmessage = '';
$eventdata->notification = true;
message_send($eventdata);
}
}
}
8 changes: 6 additions & 2 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PATH="blocks/advnotifications/db" VERSION="20160704" COMMENT="XMLDB file for Moodle blocks/advnotifications" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<XMLDB PATH="blocks/advnotifications/db" VERSION="20210920" COMMENT="XMLDB file for Moodle blocks/advnotifications"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="block_advnotifications" COMMENT="A record of all advanced notifications">
<FIELDS>
Expand All @@ -8,6 +11,7 @@
<FIELD NAME="message" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="To be used as notification's message"/>
<FIELD NAME="type" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="info" SEQUENCE="false" COMMENT="What type of notification to be displayed"/>
<FIELD NAME="aicon" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" COMMENT="Toggle whether an icon should be displayed or not"/>
<FIELD NAME="sendnotifications" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" COMMENT="Toggle whether notification is enabled/displayed or not"/>
<FIELD NAME="global" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" COMMENT="Toggles whether notification is a global or instance-based"/>
<FIELD NAME="blockid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Toggles whether notification is a global or instance-based"/>
Expand Down Expand Up @@ -37,4 +41,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
Loading