2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Copied from Moodle 3.3 messageselect.php - allows sending messages to multiple users.
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
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);
39 $url->param('send', $send);
41 if ($preview !== '') {
42 $url->param('preview', $preview);
45 $url->param('edit', $edit);
47 if ($returnto !== '') {
48 $url->param('returnto', $returnto);
50 if ($format !== FORMAT_MOODLE
) {
51 $url->param('format', $format);
54 $url->param('deluser', $deluser);
57 if (!$course = $DB->get_record('course', array('id' => $id))) {
58 print_error('invalidcourseid');
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();
67 if (!array_key_exists($id, $SESSION->emailto
)) {
68 $SESSION->emailto
[$id] = array();
71 if (array_key_exists($id, $SESSION->emailto
) && array_key_exists($deluser, $SESSION->emailto
[$id])) {
72 unset($SESSION->emailto
[$id][$deluser]);
75 if (empty($SESSION->emailselect
[$id]) ||
$messagebody) {
76 $SESSION->emailselect
[$id] = array('messagebody' => $messagebody);
78 $messagebody = $SESSION->emailselect
[$id]['messagebody'];
80 if ($data = data_submitted()) {
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;
96 if ($course->id
== SITEID
) {
97 $strtitle = get_string('sitemessage');
98 $PAGE->set_pagelayout('admin');
100 $strtitle = get_string('coursemessage');
101 $PAGE->set_pagelayout('incourse');
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
));
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();
116 $heading = get_string('addedrecip', 'moodle', $count);
118 $heading = get_string('addedrecips', 'moodle', $count);
120 echo $OUTPUT->heading($heading);
122 if (!empty($messagebody) && !$edit && !$deluser && ($preview ||
$send)) {
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() . '" />
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>';
137 } else if (!empty($send)) {
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);
146 echo $OUTPUT->heading(get_string('messagedselectedusers'));
147 unset($SESSION->emailto
[$id]);
148 unset($SESSION->emailselect
[$id]);
150 echo $OUTPUT->heading(get_string('messagedselectedcountusersfailed', 'moodle', count($fails)));
152 foreach ($fails as $f) {
153 echo '<li>', $f, '</li>';
157 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
159 echo $OUTPUT->footer();
162 echo $OUTPUT->notification(get_string('nousersyet'));
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'));
170 if (count($SESSION->emailto
[$id])) {
172 require("message.html");
174 $PAGE->requires
->yui_module('moodle-core-formchangechecker',
175 'M.core_formchangechecker.init',
177 'formid' => 'theform'
180 $PAGE->requires
->string_for_js('changesmadereallygoaway', 'moodle');
181 echo $OUTPUT->footer();