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