3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file is part of the User section Moodle
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once($CFG->dirroot
.'/message/lib.php');
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
);
38 $url = new moodle_url('/user/messageselect.php', array('id'=>$id));
39 if ($messagebody !== '') {
40 $url->param('messagebody', $messagebody);
43 $url->param('send', $send);
45 if ($preview !== '') {
46 $url->param('preview', $preview);
49 $url->param('edit', $edit);
51 if ($returnto !== '') {
52 $url->param('returnto', $returnto);
54 if ($format !== FORMAT_MOODLE
) {
55 $url->param('format', $format);
58 $url->param('deluser', $deluser);
61 $PAGE->set_context(context_system
::instance());
63 if (!$course = $DB->get_record('course', array('id'=>$id))) {
64 print_error('invalidcourseid');
69 $coursecontext = context_course
::instance($id); // Course context
70 $systemcontext = context_system
::instance(); // SYSTEM context
71 require_capability('moodle/course:bulkmessaging', $coursecontext);
73 if (empty($SESSION->emailto
)) {
74 $SESSION->emailto
= array();
76 if (!array_key_exists($id,$SESSION->emailto
)) {
77 $SESSION->emailto
[$id] = array();
81 if (array_key_exists($id,$SESSION->emailto
) && array_key_exists($deluser,$SESSION->emailto
[$id])) {
82 unset($SESSION->emailto
[$id][$deluser]);
86 if (empty($SESSION->emailselect
[$id]) ||
$messagebody) {
87 $SESSION->emailselect
[$id] = array('messagebody' => $messagebody);
90 $messagebody = $SESSION->emailselect
[$id]['messagebody'];
94 if ($data = data_submitted()) {
96 foreach ($data as $k => $v) {
97 if (preg_match('/^(user|teacher)(\d+)$/',$k,$m)) {
98 if (!array_key_exists($m[2],$SESSION->emailto
[$id])) {
99 if ($user = $DB->get_record_select('user', "id = ?", array($m[2]), 'id,firstname,lastname,idnumber,email,mailformat,lastaccess, lang, maildisplay')) {
100 $SESSION->emailto
[$id][$m[2]] = $user;
108 $strtitle = get_string('coursemessage');
111 if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
has_capability('moodle/site:viewparticipants', $systemcontext)) {
112 $link = new moodle_url("/user/index.php", array('id'=>$course->id
));
114 $PAGE->navbar
->add(get_string('participants'), $link);
115 $PAGE->navbar
->add($strtitle);
116 $PAGE->set_title($strtitle);
117 $PAGE->set_heading($strtitle);
118 echo $OUTPUT->header();
119 // if messaging is disabled on site, we can still allow users with capabilities to send emails instead
120 if (empty($CFG->messaging
)) {
121 echo $OUTPUT->notification(get_string('messagingdisabled','message'));
126 $heading = get_string('addedrecip','moodle',$count);
128 $heading = get_string('addedrecips','moodle',$count);
130 echo $OUTPUT->heading($heading);
133 if (!empty($messagebody) && !$edit && !$deluser && ($preview ||
$send)) {
135 if (count($SESSION->emailto
[$id])) {
136 if (!empty($preview)) {
137 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
138 <input type="hidden" name="returnto" value="'.s($returnto).'" />
139 <input type="hidden" name="id" value="'.$id.'" />
140 <input type="hidden" name="format" value="'.$format.'" />
141 <input type="hidden" name="sesskey" value="' . sesskey() . '" />
143 echo "<h3>".get_string('previewhtml')."</h3><div class=\"messagepreview\">\n".format_text($messagebody,$format)."\n</div>\n";
144 echo '<p align="center"><input type="submit" name="send" value="'.get_string('sendmessage', 'message').'" />'."\n";
145 echo '<input type="submit" name="edit" value="'.get_string('update').'" /></p>';
147 } else if (!empty($send)) {
149 foreach ($SESSION->emailto
[$id] as $user) {
150 $good = $good && message_post_message($USER,$user,$messagebody,$format);
153 echo $OUTPUT->heading(get_string('messagedselectedusers'));
154 unset($SESSION->emailto
[$id]);
155 unset($SESSION->emailselect
[$id]);
157 echo $OUTPUT->heading(get_string('messagedselectedusersfailed'));
159 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
161 echo $OUTPUT->footer();
164 echo $OUTPUT->notification(get_string('nousersyet'));
168 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.((count($SESSION->emailto
[$id])) ?
', '.get_string('usemessageform') : '').'</p>';
170 if ((!empty($send) ||
!empty($preview) ||
!empty($edit)) && (empty($messagebody))) {
171 echo $OUTPUT->notification(get_string('allfieldsrequired'));
174 if (count($SESSION->emailto
[$id])) {
176 $usehtmleditor = can_use_html_editor();
177 require("message.html");
180 echo $OUTPUT->footer();