Implement data privacy provider.
[moodle-mod_attendance.git] / messageselect.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17 /**
18 * Copied from Moodle 3.3 messageselect.php - allows sending messages to multiple users.
19 *
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package mod_attendance
23 */
24 require_once('../../config.php');
25 require_once($CFG->dirroot.'/message/lib.php');
26 $id = required_param('id', PARAM_INT);
27 $messagebody = optional_param('messagebody', '', PARAM_CLEANHTML);
28 $send = optional_param('send', '', PARAM_BOOL);
29 $preview = optional_param('preview', '', PARAM_BOOL);
30 $edit = optional_param('edit', '', PARAM_BOOL);
31 $returnto = optional_param('returnto', '', PARAM_LOCALURL);
32 $format = optional_param('format', FORMAT_MOODLE, PARAM_INT);
33 $deluser = optional_param('deluser', 0, PARAM_INT);
34 $url = new moodle_url('/user/messageselect.php', array('id' => $id));
35 if ($messagebody !== '') {
36 $url->param('messagebody', $messagebody);
37 }
38 if ($send !== '') {
39 $url->param('send', $send);
40 }
41 if ($preview !== '') {
42 $url->param('preview', $preview);
43 }
44 if ($edit !== '') {
45 $url->param('edit', $edit);
46 }
47 if ($returnto !== '') {
48 $url->param('returnto', $returnto);
49 }
50 if ($format !== FORMAT_MOODLE) {
51 $url->param('format', $format);
52 }
53 if ($deluser !== 0) {
54 $url->param('deluser', $deluser);
55 }
56 $PAGE->set_url($url);
57 if (!$course = $DB->get_record('course', array('id' => $id))) {
58 print_error('invalidcourseid');
59 }
60 require_login($course);
61 $coursecontext = context_course::instance($id); // Course context.
62 $systemcontext = context_system::instance(); // SYSTEM context.
63 require_capability('moodle/course:bulkmessaging', $coursecontext);
64 if (empty($SESSION->emailto)) {
65 $SESSION->emailto = array();
66 }
67 if (!array_key_exists($id, $SESSION->emailto)) {
68 $SESSION->emailto[$id] = array();
69 }
70 if ($deluser) {
71 if (array_key_exists($id, $SESSION->emailto) && array_key_exists($deluser, $SESSION->emailto[$id])) {
72 unset($SESSION->emailto[$id][$deluser]);
73 }
74 }
75 if (empty($SESSION->emailselect[$id]) || $messagebody) {
76 $SESSION->emailselect[$id] = array('messagebody' => $messagebody);
77 }
78 $messagebody = $SESSION->emailselect[$id]['messagebody'];
79 $count = 0;
80 if ($data = data_submitted()) {
81 require_sesskey();
82 $namefields = get_all_user_name_fields(true);
83 foreach ($data as $k => $v) {
84 if (preg_match('/^(user|teacher)(\d+)$/', $k, $m)) {
85 if (!array_key_exists($m[2], $SESSION->emailto[$id])) {
86 if ($user = $DB->get_record_select('user', "id = ?", array($m[2]), 'id, '.
87 $namefields . ', idnumber, email, mailformat, lastaccess, lang, '.
88 'maildisplay, auth, suspended, deleted, emailstop, username')) {
89 $SESSION->emailto[$id][$m[2]] = $user;
90 $count++;
91 }
92 }
93 }
94 }
95 }
96 if ($course->id == SITEID) {
97 $strtitle = get_string('sitemessage');
98 $PAGE->set_pagelayout('admin');
99 } else {
100 $strtitle = get_string('coursemessage');
101 $PAGE->set_pagelayout('incourse');
102 }
103 $link = null;
104 if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
105 has_capability('moodle/site:viewparticipants', $systemcontext)) {
106 $link = new moodle_url("/user/index.php", array('id' => $course->id));
107 }
108 $PAGE->navbar->add(get_string('participants'), $link);
109 $PAGE->navbar->add($strtitle);
110 $PAGE->set_title($strtitle);
111 $PAGE->set_heading($strtitle);
112 echo $OUTPUT->header();
113
114 if ($count) {
115 if ($count == 1) {
116 $heading = get_string('addedrecip', 'moodle', $count);
117 } else {
118 $heading = get_string('addedrecips', 'moodle', $count);
119 }
120 echo $OUTPUT->heading($heading);
121 }
122 if (!empty($messagebody) && !$edit && !$deluser && ($preview || $send)) {
123 require_sesskey();
124 if (count($SESSION->emailto[$id])) {
125 if (!empty($preview)) {
126 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
127 <input type="hidden" name="returnto" value="'.s($returnto).'" />
128 <input type="hidden" name="id" value="'.$id.'" />
129 <input type="hidden" name="format" value="'.$format.'" />
130 <input type="hidden" name="sesskey" value="' . sesskey() . '" />
131 ';
132 echo "<h3>".get_string('previewhtml')."</h3>";
133 echo "<div class=\"messagepreview\">\n".format_text($messagebody, $format)."\n</div>\n";
134 echo '<p align="center"><input type="submit" name="send" value="'.get_string('sendmessage', 'message').'" />'."\n";
135 echo '<input type="submit" name="edit" value="'.get_string('update').'" /></p>';
136 echo "\n</form>";
137 } else if (!empty($send)) {
138 $fails = array();
139 foreach ($SESSION->emailto[$id] as $user) {
140 if (!message_post_message($USER, $user, $messagebody, $format)) {
141 $user->fullname = fullname($user);
142 $fails[] = get_string('messagedselecteduserfailed', 'moodle', $user);
143 };
144 }
145 if (empty($fails)) {
146 echo $OUTPUT->heading(get_string('messagedselectedusers'));
147 unset($SESSION->emailto[$id]);
148 unset($SESSION->emailselect[$id]);
149 } else {
150 echo $OUTPUT->heading(get_string('messagedselectedcountusersfailed', 'moodle', count($fails)));
151 echo '<ul>';
152 foreach ($fails as $f) {
153 echo '<li>', $f, '</li>';
154 }
155 echo '</ul>';
156 }
157 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
158 }
159 echo $OUTPUT->footer();
160 exit;
161 } else {
162 echo $OUTPUT->notification(get_string('nousersyet'));
163 }
164 }
165 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.
166 ((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
167 if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
168 echo $OUTPUT->notification(get_string('allfieldsrequired'));
169 }
170 if (count($SESSION->emailto[$id])) {
171 require_sesskey();
172 require("message.html");
173 }
174 $PAGE->requires->yui_module('moodle-core-formchangechecker',
175 'M.core_formchangechecker.init',
176 array(array(
177 'formid' => 'theform'
178 ))
179 );
180 $PAGE->requires->string_for_js('changesmadereallygoaway', 'moodle');
181 echo $OUTPUT->footer();