Initial checkin of new feature to select users from participants page for a recipient...
[moodle.git] / user / messageselect.php
CommitLineData
f19570d0 1<?php
2{
3 require_once('../config.php');
4 require_once($CFG->dirroot.'/message/lib.php');
5
6 $id = required_param('id');
7 $messagebody = optional_param('messagebody',null,false);
8 $send = optional_param('send',null);
9 $returnto = optional_param('returnto',null);
10 $preview = optional_param('preview',null);
11 $format = optional_param('format',FORMAT_MOODLE);
12 $edit = optional_param('edit',null);
13 $deluser = optional_param('deluser',null);
14
15 if (!$course = get_record('course','id',$id)) {
16 error("Invalid course id");
17 }
18
19 if (!isteacher($course->id)) {
20 error("Only teachers can use this page");
21 }
22
23 if (empty($SESSION->emailto)) {
24 $SESSION->emailto = array();
25 }
26 if (!array_key_exists($id,$SESSION->emailto)) {
27 $SESSION->emailto[$id] = array();
28 }
29
30 if ($deluser) {
31 if (array_key_exists($id,$SESSION->emailto) && array_key_exists($deluser,$SESSION->emailto[$id])) {
32 unset($SESSION->emailto[$id][$deluser]);
33 }
34 }
35
36 $count = 0;
37
38 foreach ($_POST as $k => $v) {
39 if (preg_match('/email(\d+)/',$k,$m)) {
40 if (!array_key_exists($m[1],$SESSION->emailto[$id])) {
41 if ($user = get_record_select('user','id = '.$m[1],'id,firstname,lastname,idnumber,email,emailstop,mailformat')) {
42 $SESSION->emailto[$id][$m[1]] = $user;
43 $count++;
44 }
45 }
46 }
47 }
48
49 $strtitle = get_string('coursemessage');
50
51 if (empty($messagebody)) {
52 $formstart = "theform.messagebody";
53 } else {
54 $formstart = "";
55 }
56
57 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);
58
59
60 if ($count) {
61 if ($count == 1) {
62 $heading = get_string('addedrecip','moodle',$count);
63 } else {
64 $heading = get_string('addedrecips','moodle',$count);
65 }
66 print_heading($heading);
67 }
68
69 if (!empty($messagebody) && !$edit && !$deluser) {
70 if (count($SESSION->emailto[$id])) {
71 if ($preview) {
72 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
73<input type="hidden" name="returnto" value="'.stripslashes($returnto).'">
74<input type="hidden" name="id" value="'.$id.'">
75<input type="hidden" name="format" value="'.$format.'">
76<input type="hidden" name="messagebody" value="'.htmlentities(stripslashes($messagebody)).'">';
77 echo "<br/><h3>".get_string('previewhtml')."</h3><div class=\"messagepreview\">\n".format_text(stripslashes($messagebody),$format)."\n</div>";
78 echo "\n<p align=\"center\"><input type=\"submit\" name=\"send\" value=\"Send\" /> <input type=\"submit\" name=\"edit\" value=\"Edit\" /></p>\n</form>";
79 } elseif ($send) {
80 $good = 1;
81 foreach ($SESSION->emailto[$id] as $user) {
82 $good = $good && message_post_message($USER,$user,$messagebody,$format,'direct');
83 }
84 if ($good) {
85 print_heading(get_string('messagedselectedusers'));
86 unset($SESSION->emailto[$id]);
87 } else {
88 print_heading(get_string('messagedselectedusersfailed'));
89 }
90 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
91 }
92 print_footer();
93 exit;
94 } else {
95 notify(get_string('nousersyet'));
96 }
97 }
98
99 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
100
101 if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
102 notify(get_string('allfieldsrequired'));
103 }
104
105 if (count($SESSION->emailto[$id])) {
106 $usehtmleditor = can_use_richtext_editor();
107 require("message.html");
108 if ($usehtmleditor) {
109 use_html_editor("messagebody");
110 }
111 }
112
113 print_footer();
114
115}
116?>