MDL-11104
[moodle-linuxchix.git] / user / messageselect.php
blob49ee850d73b62d520e1c106932a0a687f26b3574
1 <?php
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_ALPHA);
9 $returnto = optional_param('returnto','',PARAM_LOCALURL);
10 $preview = optional_param('preview','',PARAM_ALPHA);
11 $format = optional_param('format',FORMAT_MOODLE,PARAM_INT);
12 $edit = optional_param('edit','',PARAM_ALPHA);
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/site:readallmessages', get_context_instance(CONTEXT_COURSE, $id));
22 // fix for MDL-10112
23 if (empty($CFG->messaging)) {
24 error("Messaging is disabled on this site");
27 if (empty($SESSION->emailto)) {
28 $SESSION->emailto = array();
30 if (!array_key_exists($id,$SESSION->emailto)) {
31 $SESSION->emailto[$id] = array();
34 if ($deluser) {
35 if (array_key_exists($id,$SESSION->emailto) && array_key_exists($deluser,$SESSION->emailto[$id])) {
36 unset($SESSION->emailto[$id][$deluser]);
40 if (empty($SESSION->emailselect[$id]) || $messagebody) {
41 $SESSION->emailselect[$id] = array('messagebody' => $messagebody);
44 $messagebody = $SESSION->emailselect[$id]['messagebody'];
46 $count = 0;
48 foreach ($_POST as $k => $v) {
49 if (preg_match('/^(user|teacher)(\d+)$/',$k,$m)) {
50 if (!array_key_exists($m[2],$SESSION->emailto[$id])) {
51 if ($user = get_record_select('user','id = '.$m[2],'id,firstname,lastname,idnumber,email,emailstop,mailformat,lastaccess')) {
52 $SESSION->emailto[$id][$m[2]] = $user;
53 $SESSION->emailto[$id][$m[2]]->teacher = ($m[1] == 'teacher');
54 $count++;
60 $strtitle = get_string('coursemessage');
62 if (empty($messagebody)) {
63 $formstart = "theform.messagebody";
64 } else {
65 $formstart = "";
68 $navlinks = array();
69 $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);
76 if ($count) {
77 if ($count == 1) {
78 $heading = get_string('addedrecip','moodle',$count);
79 } else {
80 $heading = get_string('addedrecips','moodle',$count);
82 print_heading($heading);
85 if (!empty($messagebody) && !$edit && !$deluser && ($preview || $send)) {
86 if (count($SESSION->emailto[$id])) {
87 if ($preview) {
88 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
89 <input type="hidden" name="returnto" value="'.stripslashes($returnto).'" />
90 <input type="hidden" name="id" value="'.$id.'" />
91 <input type="hidden" name="format" value="'.$format.'" />
93 echo "<h3>".get_string('previewhtml')."</h3><div class=\"messagepreview\">\n".format_text(stripslashes($messagebody),$format)."\n</div>";
94 echo "\n<p align=\"center\"><input type=\"submit\" name=\"send\" value=\"Send\" /> <input type=\"submit\" name=\"edit\" value=\"Edit\" /></p>\n</form>";
95 } elseif ($send) {
96 $good = 1;
97 $teachers = array();
98 foreach ($SESSION->emailto[$id] as $user) {
99 $good = $good && message_post_message($USER,$user,addslashes($messagebody),$format,'direct');
100 if ($user->teacher) {
101 $teachers[] = $user->id;
104 if ($good) {
105 print_heading(get_string('messagedselectedusers'));
106 unset($SESSION->emailto[$id]);
107 unset($SESSION->emailselect[$id]);
108 } else {
109 print_heading(get_string('messagedselectedusersfailed'));
111 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
113 print_footer();
114 exit;
115 } else {
116 notify(get_string('nousersyet'));
120 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
122 if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
123 notify(get_string('allfieldsrequired'));
126 if (count($SESSION->emailto[$id])) {
127 $usehtmleditor = can_use_richtext_editor();
128 require("message.html");
129 if ($usehtmleditor) {
130 use_html_editor("messagebody");
134 print_footer();