Bumping to 1.9.1
[moodle.git] / user / messageselect.php
blob059598e32ef96034e6038adb9792822b85b33e83
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();
21 $coursecontext = get_context_instance(CONTEXT_COURSE, $id); // Course context
22 $systemcontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
23 require_capability('moodle/course:bulkmessaging', $coursecontext);
25 if (empty($SESSION->emailto)) {
26 $SESSION->emailto = array();
28 if (!array_key_exists($id,$SESSION->emailto)) {
29 $SESSION->emailto[$id] = array();
32 if ($deluser) {
33 if (array_key_exists($id,$SESSION->emailto) && array_key_exists($deluser,$SESSION->emailto[$id])) {
34 unset($SESSION->emailto[$id][$deluser]);
38 if (empty($SESSION->emailselect[$id]) || $messagebody) {
39 $SESSION->emailselect[$id] = array('messagebody' => $messagebody);
42 $messagebody = $SESSION->emailselect[$id]['messagebody'];
44 $count = 0;
46 foreach ($_POST as $k => $v) {
47 if (preg_match('/^(user|teacher)(\d+)$/',$k,$m)) {
48 if (!array_key_exists($m[2],$SESSION->emailto[$id])) {
49 if ($user = get_record_select('user','id = '.$m[2],'id,firstname,lastname,idnumber,email,emailstop,mailformat,lastaccess')) {
50 $SESSION->emailto[$id][$m[2]] = $user;
51 $SESSION->emailto[$id][$m[2]]->teacher = ($m[1] == 'teacher');
52 $count++;
58 $strtitle = get_string('coursemessage');
60 if (empty($messagebody)) {
61 $formstart = "theform.messagebody";
62 } else {
63 $formstart = "";
66 $navlinks = array();
67 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
68 $navlinks[] = array('name' => get_string('participants'), 'link' => "index.php?id=$course->id", 'type' => 'misc');
70 $navlinks[] = array('name' => $strtitle, 'link' => null, 'type' => 'misc');
71 $navigation = build_navigation($navlinks);
73 print_header($strtitle,$strtitle,$navigation,$formstart);
75 // if messaging is disabled on site, we can still allow users with capabilities to send emails instead
76 if (empty($CFG->messaging)) {
77 notify(get_string('messagingdisabled','message'));
80 if ($count) {
81 if ($count == 1) {
82 $heading = get_string('addedrecip','moodle',$count);
83 } else {
84 $heading = get_string('addedrecips','moodle',$count);
86 print_heading($heading);
89 if (!empty($messagebody) && !$edit && !$deluser && ($preview || $send)) {
90 if (count($SESSION->emailto[$id])) {
91 if (!empty($preview)) {
92 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
93 <input type="hidden" name="returnto" value="'.s($returnto).'" />
94 <input type="hidden" name="id" value="'.$id.'" />
95 <input type="hidden" name="format" value="'.$format.'" />
97 echo "<h3>".get_string('previewhtml')."</h3><div class=\"messagepreview\">\n".format_text(stripslashes($messagebody),$format)."\n</div>\n";
98 echo '<p align="center"><input type="submit" name="send" value="'.get_string('sendmessage', 'message').'" />'."\n";
99 echo '<input type="submit" name="edit" value="'.get_string('update').'" /></p>';
100 echo "\n</form>";
101 } else if (!empty($send)) {
102 $good = 1;
103 $teachers = array();
104 foreach ($SESSION->emailto[$id] as $user) {
105 $good = $good && message_post_message($USER,$user,addslashes($messagebody),$format,'direct');
106 if ($user->teacher) {
107 $teachers[] = $user->id;
110 if (!empty($good)) {
111 print_heading(get_string('messagedselectedusers'));
112 unset($SESSION->emailto[$id]);
113 unset($SESSION->emailselect[$id]);
114 } else {
115 print_heading(get_string('messagedselectedusersfailed'));
117 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
119 print_footer();
120 exit;
121 } else {
122 notify(get_string('nousersyet'));
126 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
128 if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
129 notify(get_string('allfieldsrequired'));
132 if (count($SESSION->emailto[$id])) {
133 $usehtmleditor = can_use_richtext_editor();
134 require("message.html");
135 if ($usehtmleditor) {
136 use_html_editor("messagebody");
140 print_footer();