-
Notifications
You must be signed in to change notification settings - Fork 7
/
resourcenotif.php
112 lines (90 loc) · 3.6 KB
/
resourcenotif.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* @package local_resourcenotif
* @copyright 2012-2021 Silecs {@link http://www.silecs.info/societe}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \local_resourcenotif\notification;
use \local_resourcenotif\notifstudents;
use \local_resourcenotif\resourcenotif_form;
require_once("../../config.php");
$id = required_param('id', PARAM_INT);
if (! $cm = get_coursemodule_from_id('', $id)) {
print_error('invalidcoursemodule');
}
if (! $moduletype = $DB->get_field('modules', 'name', ['id' => $cm->module], MUST_EXIST)) {
print_error('invalidmodule');
}
if (! $course = $DB->get_record('course', ['id' => $cm->course])) {
print_error('coursemisconf');
}
if (! $module = $DB->get_record($moduletype, ['id' => $cm->instance])) {
print_error('invalidcoursemodule');
}
require_login($course, false, $cm);
$modcontext = context_module::instance($cm->id);
require_capability('moodle/course:manageactivities', $modcontext);
$url = new moodle_url('/local/resourcenotif/resourcenotif.php', ['id' => $id]);
$PAGE->set_url($url);
$msgresult = '';
$urlcourse = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
$notificationprocess = new notification($course, $cm, $moduletype);
$notificationprocess->set_message_body_info();
$PAGE->set_heading(format_string($course->fullname));
$PAGE->set_title(format_string($module->name));
$PAGE->requires->css(new moodle_url('/local/resourcenotif/resourcenotif.css'));
$notifrecipients = new notifstudents($course->id, $cm);
$notifiablestudents = $notifrecipients->get_users_from_course('student');
$formcustomdata = $notificationprocess->get_form_customdata($notifiablestudents);
$mform = new resourcenotif_form(null, $formcustomdata);
$newformdata = ['id' => $id, 'mod' => $moduletype, 'courseid' => $course->id];
$mform->set_data($newformdata);
$formdata = $mform->get_data();
if ($mform->is_cancelled()) {
redirect($urlcourse);
}
if ($formdata) {
$notificationprocess->set_notification_message($formdata->complement);
switch ($formdata->send) {
case 'all':
if ($notifiablestudents) {
$msgresult = $notificationprocess->send_notifications($notifiablestudents);
}
break;
case 'selection':
$groups = [];
if (isset($formdata->groups) && count($formdata->groups)) {
$groups = $formdata->groups;
}
$groupings = [];
if (isset($formdata->groupings) && count($formdata->groupings)) {
$groupings = $formdata->groupings;
}
$grpNotifiedStudents = notifstudents::get_users_recipients($groups, $groupings);
if (count($grpNotifiedStudents)) {
$msgresult = $notificationprocess->send_notifications($grpNotifiedStudents);
}
break;
case 'selectionstudents':
$listidstudents = $formdata->students;
if (count($listidstudents)) {
$notifiedS = [];
foreach ($listidstudents as $id) {
$notifiedS[$id] = $notifiablestudents[$id];
}
$msgresult = $notificationprocess->send_notifications($notifiedS);
}
break;
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('sendnotification', 'local_resourcenotif'));
if ($msgresult != '') {
echo $OUTPUT->box_start('info');
echo $msgresult;
echo html_writer::tag('p', html_writer::link($urlcourse, get_string('returncourse', 'local_resourcenotif')));
echo $OUTPUT->box_end();
} else {
$mform->display();
}
echo $OUTPUT->footer();