+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once('../../../config.php');
+
+$id = required_param('id', PARAM_INT);
+
+$notification = $DB->get_record('block_advnotifications', ['id' => $id]);
+
+if ($notification->blockid > 0) {
+ $bcontext = context_block::instance($notification->blockid);
+ $ctx = $bcontext->get_course_context(false);
+}
+if (empty($ctx)) {
+ $ctx = context_system::instance();
+}
+
+require_login();
+
+$url = new moodle_url('/blocks/advnotifications/pages/audience.php', ['id' => $id]);
+
+$str = get_string('editing_audiences', 'block_advnotifications');
+
+$manageurl = new moodle_url('/blocks/advnotifications/pages/notifications.php');
+
+$PAGE->set_context($ctx);
+$PAGE->set_pagelayout('standard');
+$PAGE->set_url($url);
+$PAGE->set_title($str);
+$PAGE->set_heading($str);
+
+$PAGE->navbar->add(get_string('blocks'));
+$PAGE->navbar->add(get_string('pluginname', 'block_advnotifications'));
+$PAGE->navbar->add(get_string('advnotifications_table_title_short', 'block_advnotifications'), $manageurl);
+$PAGE->navbar->add($str);
+
+$output = $PAGE->get_renderer('block_advnotifications');
+
+$userfieldfilters = $DB->get_records('block_advnotifications_field', ['notificationid' => $id]);
+$notification->userfieldfilters = [];
+foreach ($userfieldfilters as $f) {
+ $notification->userfieldfilters[] = [
+ 'userfield' => $f->userfield,
+ 'operator' => $f->operator,
+ 'fieldvalue' => $f->fieldvalue,
+ ];
+}
+if ($userfieldfilters) {
+ $filterscount = count($userfieldfilters);
+} else {
+ $filterscount = 1;
+}
+$form = new \block_advnotifications\output\form\audience($url->out(false),
+ ['notificationid' => $id, 'filterscount' => $filterscount]);
+
+if ($form->is_cancelled()) {
+ redirect($manageurl);
+} else if ($data = $form->get_data()) {
+ $DB->delete_records('block_advnotifications_coh', ['notificationid' => $id]);
+ $DB->delete_records('block_advnotifications_field', ['notificationid' => $id]);
+ $DB->delete_records('block_advnotifications_roles', ['notificationid' => $id]);
+ $coh = (object)['notificationid' => $id];
+ foreach ($data->cohorts as $c) {
+ $coh->cohortid = $c;
+ $DB->insert_record_raw('block_advnotifications_coh', $coh);
+ }
+ $field = (object)['notificationid' => $id];
+ if (isset($data->userfieldfilters)) {
+ foreach ($data->userfieldfilters as $f) {
+ if (!empty($f['userfield']) && !empty($f['operator']) && !empty($f['fieldvalue'])) {
+ $field->userfield = $f['userfield'];
+ $field->operator = $f['operator'];
+ $field->fieldvalue = $f['fieldvalue'];
+ $DB->insert_record_raw('block_advnotifications_field', $field);
+ }
+ }
+ }
+ $role = (object)['notificationid' => $id];
+ foreach ($data->roles as $r) {
+ $role->roleid = $r;
+ $DB->insert_record_raw('block_advnotifications_roles', $role);
+ }
+ redirect($url, get_string('audiencesaved', 'block_advnotifications'));
+}
+$form->set_data($notification);
+
+echo $output->header(),
+ $output->heading($notification->title),
+ $form->render(),
+ $output->footer();
diff --git a/pages/process.php b/pages/process.php
index 28010d4..c8aa64e 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.
@@ -265,6 +271,7 @@
$urow->message = $message;
$urow->type = $type;
$urow->aicon = $aicon;
+ $urow->sendnotifications = $sendnotifications;
$urow->enabled = $enabled;
$urow->global = $global;
$urow->blockid = $blockinstance;
@@ -292,6 +299,15 @@
$event = \block_advnotifications\event\notification_updated::create($params);
$event->trigger();
+ 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();
@@ -347,6 +363,7 @@
$row->message = $message;
$row->type = $type;
$row->aicon = $aicon;
+ $row->sendnotifications = $sendnotifications;
$row->enabled = $enabled;
$row->global = $global;
$row->blockid = $blockinstance;
@@ -370,6 +387,15 @@
$event = \block_advnotifications\event\notification_created::create($params);
$event->trigger();
+ 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) {
// Return Successful.
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"
+
+
+
+