Automatic installer.php lang files by installer_builder (20070224)
[moodle.git] / user / messageselect.php
blob58a97627c8cea2789e19456feb164ea2bf4acbf8
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 if (!isteacher($course->id)) {
20 error("Only teachers can use this page");
23 if (empty($SESSION->emailto)) {
24 $SESSION->emailto = array();
26 if (!array_key_exists($id,$SESSION->emailto)) {
27 $SESSION->emailto[$id] = array();
30 if ($deluser) {
31 if (array_key_exists($id,$SESSION->emailto) && array_key_exists($deluser,$SESSION->emailto[$id])) {
32 unset($SESSION->emailto[$id][$deluser]);
36 if (empty($SESSION->emailselect[$id]) || $messagebody) {
37 $SESSION->emailselect[$id] = array('messagebody' => $messagebody);
40 $messagebody = $SESSION->emailselect[$id]['messagebody'];
42 $count = 0;
44 foreach ($_POST as $k => $v) {
45 if (preg_match('/^(user|teacher)(\d+)$/',$k,$m)) {
46 if (!array_key_exists($m[2],$SESSION->emailto[$id])) {
47 if ($user = get_record_select('user','id = '.$m[2],'id,firstname,lastname,idnumber,email,emailstop,mailformat,lastaccess')) {
48 $SESSION->emailto[$id][$m[2]] = $user;
49 $SESSION->emailto[$id][$m[2]]->teacher = ($m[1] == 'teacher');
50 $count++;
56 $strtitle = get_string('coursemessage');
58 if (empty($messagebody)) {
59 $formstart = "theform.messagebody";
60 } else {
61 $formstart = "";
64 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);
67 if ($count) {
68 if ($count == 1) {
69 $heading = get_string('addedrecip','moodle',$count);
70 } else {
71 $heading = get_string('addedrecips','moodle',$count);
73 print_heading($heading);
76 if (!empty($messagebody) && !$edit && !$deluser && ($preview || $send)) {
77 if (count($SESSION->emailto[$id])) {
78 if ($preview) {
79 echo '<form method="post" action="messageselect.php" style="margin: 0 20px;">
80 <input type="hidden" name="returnto" value="'.stripslashes($returnto).'">
81 <input type="hidden" name="id" value="'.$id.'">
82 <input type="hidden" name="format" value="'.$format.'">
84 echo "<h3>".get_string('previewhtml')."</h3><div class=\"messagepreview\">\n".format_text(stripslashes($messagebody),$format)."\n</div>";
85 echo "\n<p align=\"center\"><input type=\"submit\" name=\"send\" value=\"Send\" /> <input type=\"submit\" name=\"edit\" value=\"Edit\" /></p>\n</form>";
86 } elseif ($send) {
87 $good = 1;
88 $teachers = array();
89 foreach ($SESSION->emailto[$id] as $user) {
90 $good = $good && message_post_message($USER,$user,addslashes($messagebody),$format,'direct');
91 if ($user->teacher) {
92 $teachers[] = $user->id;
95 if ($good) {
96 print_heading(get_string('messagedselectedusers'));
97 unset($SESSION->emailto[$id]);
98 unset($SESSION->emailselect[$id]);
99 } else {
100 print_heading(get_string('messagedselectedusersfailed'));
102 echo '<p align="center"><a href="index.php?id='.$id.'">'.get_string('backtoparticipants').'</a></p>';
104 print_footer();
105 exit;
106 } else {
107 notify(get_string('nousersyet'));
111 echo '<p align="center"><a href="'.$returnto.'">'.get_string("keepsearching").'</a>'.((count($SESSION->emailto[$id])) ? ', '.get_string('usemessageform') : '').'</p>';
113 if ((!empty($send) || !empty($preview) || !empty($edit)) && (empty($messagebody))) {
114 notify(get_string('allfieldsrequired'));
117 if (count($SESSION->emailto[$id])) {
118 $usehtmleditor = can_use_richtext_editor();
119 require("message.html");
120 if ($usehtmleditor) {
121 use_html_editor("messagebody");
125 print_footer();