MDL-54928 notifications: Add username to emailto users
[moodle.git] / user / messageselect.php
CommitLineData
ce221eb5 1<?php
ce221eb5 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 * This file is part of the User section Moodle
19 *
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a2ed6e69 22 * @package core_user
ce221eb5 23 */
24
25require_once('../config.php');
26require_once($CFG->dirroot.'/message/lib.php');
27
a2ed6e69
SH
28$id = required_param('id', PARAM_INT);
29$messagebody = optional_param('messagebody', '', PARAM_CLEANHTML);
30$send = optional_param('send', '', PARAM_BOOL);
31$preview = optional_param('preview', '', PARAM_BOOL);
32$edit = optional_param('edit', '', PARAM_BOOL);
33$returnto = optional_param('returnto', '', PARAM_LOCALURL);
34$format = optional_param('format', FORMAT_MOODLE, PARAM_INT);
35$deluser = optional_param('deluser', 0, PARAM_INT);
ce221eb5 36
a2ed6e69 37$url = new moodle_url('/user/messageselect.php', array('id' => $id));
ce221eb5 38if ($messagebody !== '') {
39 $url->param('messagebody', $messagebody);
40}
41if ($send !== '') {
42 $url->param('send', $send);
43}
44if ($preview !== '') {
45 $url->param('preview', $preview);
46}
47if ($edit !== '') {
48 $url->param('edit', $edit);
49}
50if ($returnto !== '') {
51 $url->param('returnto', $returnto);
52}
53if ($format !== FORMAT_MOODLE) {
54 $url->param('format', $format);
55}
56if ($deluser !== 0) {
57 $url->param('deluser', $deluser);
58}
59$PAGE->set_url($url);
60
a2ed6e69 61if (!$course = $DB->get_record('course', array('id' => $id))) {
ce221eb5 62 print_error('invalidcourseid');
63}
64
b469e62a 65require_login($course);
ce221eb5 66
a2ed6e69
SH
67$coursecontext = context_course::instance($id); // Course context.
68$systemcontext = context_system::instance(); // SYSTEM context.
ce221eb5 69require_capability('moodle/course:bulkmessaging', $coursecontext);
70
71if (empty($SESSION->emailto)) {
72 $SESSION->emailto = array();
73}
a2ed6e69 74if (!array_key_exists($id, $SESSION->emailto)) {
ce221eb5 75 $SESSION->emailto[$id] = array();
76}
77
78if ($deluser) {
a2ed6e69 79 if (array_key_exists($id, $SESSION->emailto) && array_key_exists($deluser, $SESSION->emailto[$id])) {
ce221eb5 80 unset($SESSION->emailto[$id][$deluser]);
f19570d0 81 }
ce221eb5 82}
f19570d0 83
ce221eb5 84if (empty($SESSION->emailselect[$id]) || $messagebody) {
85 $SESSION->emailselect[$id] = array('messagebody' => $messagebody);
86}
0be6f678 87
ce221eb5 88$messagebody = $SESSION->emailselect[$id]['messagebody'];
f19570d0 89
ce221eb5 90$count = 0;
f19570d0 91
632f3eff
ARN
92if ($data = data_submitted()) {
93 require_sesskey();
a327f25e 94 $namefields = get_all_user_name_fields(true);
78fcdb5f 95 foreach ($data as $k => $v) {
a2ed6e69
SH
96 if (preg_match('/^(user|teacher)(\d+)$/', $k, $m)) {
97 if (!array_key_exists($m[2], $SESSION->emailto[$id])) {
0b47bbfa
MG
98 if ($user = $DB->get_record_select('user', "id = ?", array($m[2]), 'id, '.
99 $namefields . ', idnumber, email, mailformat, lastaccess, lang, '.
6f2815fa 100 'maildisplay, auth, suspended, deleted, emailstop, username')) {
78fcdb5f
PS
101 $SESSION->emailto[$id][$m[2]] = $user;
102 $count++;
103 }
f19570d0 104 }
105 }
106 }
ce221eb5 107}
108
f624f569
AD
109if ($course->id == SITEID) {
110 $strtitle = get_string('sitemessage');
111 $PAGE->set_pagelayout('admin');
112} else {
113 $strtitle = get_string('coursemessage');
114 $PAGE->set_pagelayout('incourse');
115}
ce221eb5 116
117$link = null;
a2ed6e69
SH
118if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
119 has_capability('moodle/site:viewparticipants', $systemcontext)) {
120 $link = new moodle_url("/user/index.php", array('id' => $course->id));
ce221eb5 121}
122$PAGE->navbar->add(get_string('participants'), $link);
123$PAGE->navbar->add($strtitle);
124$PAGE->set_title($strtitle);
125$PAGE->set_heading($strtitle);
ce221eb5 126echo $OUTPUT->header();
a2ed6e69 127// If messaging is disabled on site, we can still allow users with capabilities to send emails instead.
ce221eb5 128if (empty($CFG->messaging)) {
a2ed6e69 129 echo $OUTPUT->notification(get_string('messagingdisabled', 'message'));
ce221eb5 130}
131
132if ($count) {
133 if ($count == 1) {
a2ed6e69 134 $heading = get_string('addedrecip', 'moodle', $count);
ce221eb5 135 } else {
a2ed6e69 136 $heading = get_string('addedrecips', 'moodle', $count);
f19570d0 137 }
ce221eb5 138 echo $OUTPUT->heading($heading);
139}
f19570d0 140
ce221eb5 141if (!empty($messagebody) && !$edit && !$deluser && ($preview || $send)) {
632f3eff 142 require_sesskey();
ce221eb5 143 if (count($SESSION->emailto[$id])) {
144 if (!empty($preview)) {
145 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
f09002ca 146<input type="hidden" name="returnto" value="'.s($returnto).'" />
60af2703 147<input type="hidden" name="id" value="'.$id.'" />
148<input type="hidden" name="format" value="'.$format.'" />
60fb0ef8 149<input type="hidden" name="sesskey" value="' . sesskey() . '" />
18c3baa2 150';
a2ed6e69
SH
151 echo "<h3>".get_string('previewhtml')."</h3>";
152 echo "<div class=\"messagepreview\">\n".format_text($messagebody, $format)."\n</div>\n";
ce221eb5 153 echo '<p align="center"><input type="submit" name="send" value="'.get_string('sendmessage', 'message').'" />'."\n";
154 echo '<input type="submit" name="edit" value="'.get_string('update').'" /></p>';
155 echo "\n</form>";
632f3eff 156 } else if (!empty($send)) {
77acc5d7 157 $fails = array();
ce221eb5 158 foreach ($SESSION->emailto[$id] as $user) {
77acc5d7
DNA
159 if (!message_post_message($USER, $user, $messagebody, $format)) {
160 $user->fullname = fullname($user);
161 $fails[] = get_string('messagedselecteduserfailed', 'moodle', $user);
162 };
ce221eb5 163 }
77acc5d7 164 if (empty($fails)) {
ce221eb5 165 echo $OUTPUT->heading(get_string('messagedselectedusers'));
166 unset($SESSION->emailto[$id]);
167 unset($SESSION->emailselect[$id]);
168 } else {
77acc5d7
DNA
169 echo $OUTPUT->heading(get_string('messagedselectedcountusersfailed', 'moodle', count($fails)));
170 echo '<ul>';
171 foreach ($fails as $f) {
172 echo '<li>', $f, '</li>';
173 }
174 echo '</ul>';
f19570d0 175 }
ce221eb5 176 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
f19570d0 177 }
ce221eb5 178 echo $OUTPUT->footer();
179 exit;
180 } else {
181 echo $OUTPUT->notification(get_string('nousersyet'));
f19570d0 182 }
ce221eb5 183}
f19570d0 184
a2ed6e69
SH
185echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.
186 ((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
f19570d0 187
ce221eb5 188if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
189 echo $OUTPUT->notification(get_string('allfieldsrequired'));
190}
f19570d0 191
ce221eb5 192if (count($SESSION->emailto[$id])) {
632f3eff 193 require_sesskey();
ce221eb5 194 require("message.html");
195}
f19570d0 196
ea3aa9f9
CB
197$PAGE->requires->yui_module('moodle-core-formchangechecker',
198 'M.core_formchangechecker.init',
199 array(array(
200 'formid' => 'theform'
201 ))
202);
203$PAGE->requires->string_for_js('changesmadereallygoaway', 'moodle');
f09002ca 204
ea3aa9f9 205echo $OUTPUT->footer();