Bumping version for 1.8.10 release (note new convention for numbering)
[moodle.git] / user / messageselect.php
blob2c45440d50c2a2d9fb1fec817c520427a655a7a4
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->dirroot.'/message/lib.php');
6 $id = required_param('id',PARAM_INT);
7 $messagebody = optional_param('messagebody','',PARAM_CLEANHTML);
8 $send = optional_param('send','',PARAM_RAW); // Content is actually treated as boolean
9 $preview = optional_param('preview','',PARAM_RAW); // Content is actually treated as boolean
10 $edit = optional_param('edit','',PARAM_RAW); // Content is actually treated as boolean
11 $returnto = optional_param('returnto','',PARAM_LOCALURL);
12 $format = optional_param('format',FORMAT_MOODLE,PARAM_INT);
13 $deluser = optional_param('deluser',0,PARAM_INT);
15 if (!$course = get_record('course','id',$id)) {
16 error("Invalid course id");
19 require_login();
20 require_capability('moodle/course:bulkmessaging', get_context_instance(CONTEXT_COURSE, $id));
22 if (empty($SESSION->emailto)) {
23 $SESSION->emailto = array();
25 if (!array_key_exists($id,$SESSION->emailto)) {
26 $SESSION->emailto[$id] = array();
29 if ($deluser) {
30 if (array_key_exists($id,$SESSION->emailto) && array_key_exists($deluser,$SESSION->emailto[$id])) {
31 unset($SESSION->emailto[$id][$deluser]);
35 if (empty($SESSION->emailselect[$id]) || $messagebody) {
36 $SESSION->emailselect[$id] = array('messagebody' => $messagebody);
39 $messagebody = $SESSION->emailselect[$id]['messagebody'];
41 $count = 0;
43 foreach ($_POST as $k => $v) {
44 if (preg_match('/^(user|teacher)(\d+)$/',$k,$m)) {
45 if (!array_key_exists($m[2],$SESSION->emailto[$id])) {
46 if ($user = get_record_select('user','id = '.$m[2],'id,firstname,lastname,idnumber,email,emailstop,mailformat,lastaccess')) {
47 $SESSION->emailto[$id][$m[2]] = $user;
48 $SESSION->emailto[$id][$m[2]]->teacher = ($m[1] == 'teacher');
49 $count++;
55 $strtitle = get_string('coursemessage');
57 if (empty($messagebody)) {
58 $formstart = "theform.messagebody";
59 } else {
60 $formstart = "";
63 print_header($strtitle,$strtitle,"<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> <a href=\"index.php?id=$course->id\">".get_string("participants")."</a> -> ".$strtitle,$formstart);
65 // if messaging is disabled on site, we can still allow users with capabilities to send emails instead
66 if (empty($CFG->messaging)) {
67 notify(get_string('messagingdisabled','message'));
70 if ($count) {
71 if ($count == 1) {
72 $heading = get_string('addedrecip','moodle',$count);
73 } else {
74 $heading = get_string('addedrecips','moodle',$count);
76 print_heading($heading);
79 if (!empty($messagebody) && !$edit && !$deluser && ($preview || $send)) {
80 if (count($SESSION->emailto[$id])) {
81 if (!empty($preview)) {
82 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
83 <input type="hidden" name="returnto" value="'.s($returnto).'" />
84 <input type="hidden" name="id" value="'.$id.'" />
85 <input type="hidden" name="format" value="'.$format.'" />
87 echo "<h3>".get_string('previewhtml')."</h3><div class=\"messagepreview\">\n".format_text(stripslashes($messagebody),$format)."\n</div>";
88 echo "\n<p align=\"center\"><input type=\"submit\" name=\"send\" value=\"".get_string('sendmessage', 'message')."\" /> <input type=\"submit\" name=\"edit\" value=\"".get_string('update')."\" /></p>\n</form>";
90 } else if (!empty($send)) {
91 $good = 1;
92 $teachers = array();
93 foreach ($SESSION->emailto[$id] as $user) {
94 $good = $good && message_post_message($USER,$user,addslashes($messagebody),$format,'direct');
95 if ($user->teacher) {
96 $teachers[] = $user->id;
99 if (!empty($good)) {
100 print_heading(get_string('messagedselectedusers'));
101 unset($SESSION->emailto[$id]);
102 unset($SESSION->emailselect[$id]);
103 } else {
104 print_heading(get_string('messagedselectedusersfailed'));
106 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
108 print_footer();
109 exit;
110 } else {
111 notify(get_string('nousersyet'));
115 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
117 if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
118 notify(get_string('allfieldsrequired'));
121 if (count($SESSION->emailto[$id])) {
122 $usehtmleditor = can_use_richtext_editor();
123 require("message.html");
124 if ($usehtmleditor) {
125 use_html_editor("messagebody");
129 print_footer();