notes MDL-19818 Upgrade deprecated calls and added set_url calls
[moodle.git] / message / edit.php
CommitLineData
d18b1bbd 1<?php // $Id$
3b120e46 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * Edit user message preferences
28 *
305a014f 29 * @author Luis Rodrigues and Martin Dougiamas
3b120e46 30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package
33 */
34
3b120e46 35
305a014f 36 require_once('../config.php');
37 require_once($CFG->libdir.'/messagelib.php');
3b120e46 38
305a014f 39 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
40 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
3b120e46 41
305a014f 42 if (!$course = $DB->get_record('course', array('id' => $course))) {
43 print_error('invalidcourseid');
44 }
3b120e46 45
305a014f 46 if ($course->id != SITEID) {
47 require_login($course);
3b120e46 48
305a014f 49 } else {
50 if (!isloggedin()) {
51 if (empty($SESSION->wantsurl)) {
52 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
53 }
93f66983 54 redirect(get_login_url());
3b120e46 55 }
3b120e46 56 }
3b120e46 57
305a014f 58 if (isguestuser()) {
59 print_error('guestnoeditmessage', 'message');
60 }
3b120e46 61
305a014f 62 if (!$user = $DB->get_record('user', array('id' => $userid))) {
63 print_error('invaliduserid');
64 }
3b120e46 65
305a014f 66 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
67 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
68 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
3b120e46 69
70
305a014f 71 // check access control
72 if ($user->id == $USER->id) {
73 //editing own message profile
74 require_capability('moodle/user:editownmessageprofile', $systemcontext);
3b120e46 75
305a014f 76 } else {
77 // teachers, parents, etc.
78 require_capability('moodle/user:editmessageprofile', $personalcontext);
79 // no editing of guest user account
80 if (isguestuser($user->id)) {
81 print_error('guestnoeditmessageother', 'message');
82 }
83 // no editing of primary admin!
84 $mainadmin = get_admin();
85 if ($user->id == $mainadmin->id) {
86 print_error('adminprimarynoedit');
87 }
3b120e46 88 }
3b120e46 89
305a014f 90/// Save new preferences if data was submited
91
92 if (($form = data_submitted()) && confirm_sesskey()) {
93 $preferences = array();
d18b1bbd 94
d18b1bbd 95 /// Set all the preferences for all the message providers
305a014f 96 $providers = message_get_my_providers();
97 foreach ( $providers as $providerid => $provider){
98 foreach (array('loggedin', 'loggedoff') as $state){
99 $linepref = '';
100 foreach ($form->{$provider->component.'_'.$provider->name.'_'.$state} as $process=>$one){
101 if ($linepref == ''){
102 $linepref = $process;
103 } else {
104 $linepref .= ','.$process;
105 }
106 }
107 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
108 }
109 }
110
111 /// Set all the processor options as well
112 $processors = $DB->get_records('message_processors');
113 foreach ( $processors as $processorid => $processor){
114 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
115 if ( is_readable($processorfile) ) {
116 include_once( $processorfile );
117
118 $processclass = 'message_output_' . $processor->name;
119 if ( class_exists($processclass) ){
120 $pclass = new $processclass();
121 $pclass->process_form($form, $preferences);
122 } else{
123 print_error('errorcallingprocessor', 'message');
124 }
125 }
126 }
127
128 /// Save all the new preferences to the database
129 if (!set_user_preferences( $preferences, $user->id ) ){
130 print_error('cannotupdateusermsgpref');
131 }
132
133 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
134 }
135
136/// Load preferences
137 $preferences = new object();
138
139/// Get providers preferences
d18b1bbd 140 $providers = message_get_my_providers();
141 foreach ( $providers as $providerid => $provider){
142 foreach (array('loggedin', 'loggedoff') as $state){
305a014f 143 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
144 if ($linepref == ''){
145 continue;
146 }
147 $lineprefarray = explode(',', $linepref);
148 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
149 foreach ($lineprefarray as $pref){
150 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
d18b1bbd 151 }
d18b1bbd 152 }
153 }
f3d095f8 154
305a014f 155/// For every processors put its options on the form (need to get function from processor's lib.php)
d18b1bbd 156 $processors = $DB->get_records('message_processors');
305a014f 157 foreach ( $processors as $processorid => $processor){
d18b1bbd 158 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
159 if ( is_readable($processorfile) ) {
305a014f 160 include_once( $processorfile );
d18b1bbd 161 $processclass = 'message_output_' . $processor->name;
162 if ( class_exists($processclass) ){
163 $pclass = new $processclass();
305a014f 164 $pclass->load_data($preferences, $user->id);
d18b1bbd 165 } else{
53e242f9 166 print_error('errorcallingprocessor', 'message');
d18b1bbd 167 }
168 }
169 }
305a014f 170
171/// Display page header
172 $streditmymessage = get_string('editmymessage', 'message');
173 $strparticipants = get_string('participants');
174 $userfullname = fullname($user, true);
175
305a014f 176 if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
177 has_capability('moodle/site:viewparticipants', $systemcontext)) {
91152a35 178 $PAGE->navbar->add($strparticipants, new moodle_url($CFG->wwwroot.'/message/index.php', array('id'=>$course->id)));
3b120e46 179 }
91152a35 180 $PAGE->navbar->add($userfullname, new moodle_url($CFG->wwwroot.'/message/view.php', array('id'=>$user->id, 'course'=>$course->id)));
5a294042 181 $PAGE->navbar->add($streditmymessage);
182 $PAGE->set_title("$course->shortname: $streditmymessage");
305a014f 183 if ($course->id != SITEID) {
5a294042 184 $PAGE->set_heading("$course->fullname: $streditmymessage");
305a014f 185 } else {
5a294042 186 $PAGE->set_heading($course->fullname);
3b120e46 187 }
5a294042 188 echo $OUTPUT->header();
3b120e46 189
3b120e46 190/// Print tabs at the top
305a014f 191 $showroles = 1;
192 $currenttab = 'editmessage';
193 require('../user/tabs.php');
194
195/// Start the form. We're not using mform here because of our special formatting needs ...
196 echo '<form class="mform" method="post" action="'.$CFG->wwwroot.'/message/edit.php">';
197
198/// Settings table...
199 echo '<fieldset id="providers" class="clearfix">';
200 echo '<legend class="ftoggler">'.get_string('providers_config', 'message').'</legend>';
201 $providers = message_get_my_providers();
202 $processors = $DB->get_records('message_processors');
203 $number_procs = count($processors);
204 echo '<table cellpadding="2"><tr><td>&nbsp;</td>'."\n";
205 foreach ( $processors as $processorid => $processor){
206 echo '<th align="center">'.get_string($processor->name, 'messageprocessor_'.$processor->name).'</th>';
207 }
208 echo '</tr>';
209
210 foreach ( $providers as $providerid => $provider){
211 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
97844c1d 212
213 /// TODO XXX: This is only a quick hack ... helpfile locations should be provided as part of the provider definition
214 if ($provider->component == 'moodle') {
aa9a6867 215 $helpbtn = $OUTPUT->help_icon(moodle_help_icon::make('moodle_'.$provider->name, $providername, 'message'));
97844c1d 216 } else {
aa9a6867 217 $helpbtn = $OUTPUT->help_icon(moodle_help_icon::make('message_'.$provider->name, $providername, basename($provider->component)));
97844c1d 218 }
219
305a014f 220 echo '<tr><th align="right">'.$providername.$helpbtn.'</th><td colspan="'.$number_procs.'"></td></tr>'."\n";
221 foreach (array('loggedin', 'loggedoff') as $state){
222 $state_res = get_string($state, 'message');
223 echo '<tr><td align="right">'.$state_res.'</td>'."\n";
224 foreach ( $processors as $processorid => $processor) {
225 if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state})) {
226 $checked = '';
227 } else if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name])) {
228 $checked = '';
229 } else {
230 $checked = $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name]==1?" checked=\"checked\"":"";
231 }
232 echo '<td align="center"><input type="checkbox" name="'.$provider->component.'_'.$provider->name.'_'.$state.'['.$processor->name.']" '.$checked.' /></td>'."\n";
838a8eb1 233 }
305a014f 234 echo '</tr>'."\n";
d18b1bbd 235 }
d18b1bbd 236 }
305a014f 237 echo '</table>';
238 echo '</fieldset>';
239
240/// Show all the message processors
241 $processors = $DB->get_records('message_processors');
242
243 foreach ($processors as $processorid => $processor) {
244 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
245 if (is_readable($processorfile)) {
246 include_once($processorfile);
247 $processclass = 'message_output_' . $processor->name;
248
249 if (class_exists($processclass)) {
250 $pclass = new $processclass();
251 echo '<fieldset id="messageprocessor_'.$processor->name.'" class="clearfix">';
252 echo '<legend class="ftoggler">'.get_string($processor->name, 'messageprocessor_'.$processor->name).'</legend>';
253
254 echo $pclass->config_form($preferences);
255
256 echo '</fieldset>';
257
258 } else{
259 print_error('errorcallingprocessor', 'message');
260 }
d18b1bbd 261 }
262 }
14d70d26 263
305a014f 264 echo '<div><input type="hidden" name="sesskey" value="'.sesskey().'" /></div>';
265 echo '<div style="text-align:center"><input name="submit" value="'. get_string('updatemyprofile') .'" type="submit" /></div>';
266
267 echo "</form>";
d18b1bbd 268
8f3b787b 269 echo $OUTPUT->footer();
3b120e46 270
271?>