Skip to content

Send notifications (via Moodle notifications processors) #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all 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.

9 changes: 6 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 @@ -392,4 +395,4 @@ define(['jquery'], function($) {
});
}
};
});
});
15 changes: 14 additions & 1 deletion classes/base_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct($uniqueid) {
'global',
'aicon',
'dismissible',
'sendnotifications',
'times',
'date_from',
'date_to',
Expand All @@ -80,6 +81,7 @@ public function __construct($uniqueid) {
get_string('advnotifications_field_global', 'block_advnotifications'), // Global: Yes.
get_string('advnotifications_field_aicon', 'block_advnotifications'), // AIcon: Yes.
get_string('advnotifications_field_dismissible', 'block_advnotifications'), // Dismissible: Yes.
get_string('advnotifications_field_sendnotifications', 'block_advnotifications'), // Send notifications: Yes.
get_string('advnotifications_field_times', 'block_advnotifications'), // Times: 10.
get_string('advnotifications_field_date_from', 'block_advnotifications'), // Date From: dd/mm/yyyy.
get_string('advnotifications_field_date_to', 'block_advnotifications'), // Date To: dd/mm/yyyy.
Expand Down Expand Up @@ -172,6 +174,17 @@ public function col_dismissible($values) {
return ($values->dismissible == 1 ? $this->yes : $this->no);
}

/**
* This function is called for each data row to allow processing of the
* sendnotifications value.
*
* @param object $values Contains object with all the values of record.
* @return $string Return whether notification is sent via moodle notifications system or not.
*/
public function col_sendnotifications($values) {
return ($values->sendnotifications == 1 ? $this->yes : $this->no);
}

/**
* This function is called for each data row to allow processing of the
* times value.
Expand Down Expand Up @@ -223,4 +236,4 @@ public function print_nothing_to_display() {

echo '<p class="notifications--empty">' . get_string('advnotifications_table_empty', 'block_advnotifications') . '</p>';
}
}
}
86 changes: 86 additions & 0 deletions classes/task/sendnotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 <[email protected]>
* @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 <[email protected]>
* @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;
}

if (!empty($notification->blockid)) {
$bcontext = \context_block::instance($notification->blockid);
}
if (isset($bcontext) && $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);
}
}
}
Loading