+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$messageproviders = [
+ // Send notifications to users.
+ 'sendadvnotifications' => [
+ 'defaults' => [
+ 'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN,
+ 'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF
+ ],
+ ],
+];
diff --git a/db/upgrade.php b/db/upgrade.php
index 171043f..3258b0c 100644
--- a/db/upgrade.php
+++ b/db/upgrade.php
@@ -102,7 +102,22 @@ function xmldb_block_advnotifications_upgrade($oldversion) {
upgrade_block_savepoint(true, 2021010616, 'advnotifications');
}
+ if ($oldversion < 2021092000) {
+
+ // Define field id to be added to block_advnotifications.
+ $table = new xmldb_table('block_advnotifications');
+ $field = new xmldb_field('sendnotifications', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'aicon');
+
+ // Conditionally launch add field id.
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ // Advnotifications savepoint reached.
+ upgrade_block_savepoint(true, 2021092000, 'advnotifications');
+ }
+
// Add future upgrade points here.
return true;
-}
\ No newline at end of file
+}
diff --git a/lang/en/block_advnotifications.php b/lang/en/block_advnotifications.php
index 6196f0d..6519acc 100644
--- a/lang/en/block_advnotifications.php
+++ b/lang/en/block_advnotifications.php
@@ -43,6 +43,7 @@
$string['advnotifications_field_global'] = 'Global';
$string['advnotifications_field_aicon'] = 'Icon';
$string['advnotifications_field_dismissible'] = 'Dismissible';
+$string['advnotifications_field_sendnotifications'] = 'Send notifications';
$string['advnotifications_field_times'] = 'View Times';
$string['advnotifications_field_date_from'] = 'From';
$string['advnotifications_field_date_to'] = 'To';
@@ -85,6 +86,7 @@
$string['advnotifications_cancel'] = 'Cancel';
$string['advnotifications_req'] = 'Required...';
$string['advnotifications_preview'] = 'Preview';
+$string['advnotifications_sendnotifications'] = 'Send notitications via site notification system (usually will appear on the bell icon on top)';
// Renderer.
$string['advnotifications_add_heading'] = 'New notification';
@@ -151,6 +153,8 @@
// Misc.
$string['advnotifications_join'] = ' & ';
+$string['messageprovider:sendadvnotifications'] = 'Send advanced notifications to users';
+
// Privacy API.
$string['privacy:metadata:block_advnotifications'] = 'Information about notifications the user has been exposed to and recorded interactions.';
$string['privacy:metadata:block_advnotifications:title'] = 'The title of the notification.';
diff --git a/pages/process.php b/pages/process.php
index 77ee9a1..90764f3 100644
--- a/pages/process.php
+++ b/pages/process.php
@@ -61,6 +61,7 @@
$type = optional_param('type', null, PARAM_TEXT);
$times = optional_param('times', null, PARAM_INT);
$aicon = optional_param('aicon', null, PARAM_TEXT);
+$sendnotifications = optional_param('sendnotifications', null, PARAM_TEXT);
$dismissible = optional_param('dismissible', null, PARAM_TEXT);
$datefrom = optional_param('date_from', null, PARAM_TEXT);
$dateto = optional_param('date_to', null, PARAM_TEXT);
@@ -101,6 +102,11 @@
} else {
$dismissible = 0;
}
+if ($sendnotifications == 'on' || $sendnotifications == '1') {
+ $sendnotifications = 1;
+} else {
+ $sendnotifications = 0;
+}
// TODO: Check if successful?
// Convert dates to epoch for DB. If empty, set to 0 (forever) by default.
@@ -246,6 +252,7 @@
$urow->message = $message;
$urow->type = $type;
$urow->aicon = $aicon;
+ $urow->sendnotifications = $sendnotifications;
$urow->enabled = $enabled;
$urow->global = $global;
$urow->blockid = $blockinstance;
@@ -256,6 +263,15 @@
$DB->update_record('block_advnotifications', $urow);
+ if ($urow->sendnotifications) {
+ $task = new block_advnotifications\task\sendnotifications();
+ $task->set_custom_data(['notificationid' => $id]);
+ if ($datefrom > time()) {
+ $task->set_next_run_time($datefrom);
+ }
+ \core\task\manager::reschedule_or_queue_adhoc_task($task);
+ }
+
if ($ajax) {
echo json_encode(array("updated" => $title));
exit();
@@ -311,6 +327,7 @@
$row->message = $message;
$row->type = $type;
$row->aicon = $aicon;
+ $row->sendnotifications = $sendnotifications;
$row->enabled = $enabled;
$row->global = $global;
$row->blockid = $blockinstance;
@@ -323,7 +340,16 @@
$row->deleted_by = -1;
$row->created_by = $USER->id;
- $DB->insert_record('block_advnotifications', $row);
+ $id = $DB->insert_record('block_advnotifications', $row);
+
+ if ($row->sendnotifications) {
+ $task = new block_advnotifications\task\sendnotifications();
+ $task->set_custom_data(['notificationid' => $id]);
+ if ($datefrom > time()) {
+ $task->set_next_run_time($datefrom);
+ }
+ \core\task\manager::queue_adhoc_task($task);
+ }
// Send JSON response if AJAX call was made, otherwise simply redirect to origin page.
if ($ajax) {
@@ -333,4 +359,4 @@
} else {
redirect(new moodle_url('/blocks/advnotifications/pages/notifications.php', $params));
}
-}
\ No newline at end of file
+}
diff --git a/renderer.php b/renderer.php
index 1647aec..91b5ae4 100644
--- a/renderer.php
+++ b/renderer.php
@@ -189,6 +189,11 @@ class="form-check-input"
+
+
+
+