MDL-28187 message: fixed up $PAGE->set_url() in /message/index.php so the nav highlig...
[moodle.git] / message / edit.php
CommitLineData
eb5334ff 1<?php
eb5334ff 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
3b120e46 16
17/**
18 * Edit user message preferences
19 *
6fbd60ef
AD
20 * @package core_message
21 * @copyright 2008 Luis Rodrigues and Martin Dougiamas
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3b120e46 23 */
24
60bbe768
RK
25require_once(dirname(__FILE__) . '/../config.php');
26require_once($CFG->dirroot . '/message/lib.php');
3b120e46 27
eb5334ff 28$userid = optional_param('id', $USER->id, PARAM_INT); // user id
29$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
bb3546f3 30$disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
3b120e46 31
a6855934 32$url = new moodle_url('/message/edit.php');
ff038e4b
AD
33$url->param('id', $userid);
34$url->param('course', $course);
35
eb5334ff 36$PAGE->set_url($url);
3b120e46 37
eb5334ff 38if (!$course = $DB->get_record('course', array('id' => $course))) {
39 print_error('invalidcourseid');
40}
3b120e46 41
eb5334ff 42if ($course->id != SITEID) {
43 require_login($course);
3b120e46 44
1d422980 45} else {
eb5334ff 46 if (!isloggedin()) {
47 if (empty($SESSION->wantsurl)) {
48 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
3b120e46 49 }
eb5334ff 50 redirect(get_login_url());
3b120e46 51 }
eb5334ff 52}
3b120e46 53
eb5334ff 54if (isguestuser()) {
55 print_error('guestnoeditmessage', 'message');
56}
3b120e46 57
eb5334ff 58if (!$user = $DB->get_record('user', array('id' => $userid))) {
59 print_error('invaliduserid');
60}
3b120e46 61
eb5334ff 62$systemcontext = get_context_instance(CONTEXT_SYSTEM);
63$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
64$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
3b120e46 65
5ac851fb 66$PAGE->set_context($personalcontext);
c8621a02 67$PAGE->set_pagelayout('course');
bb3546f3 68$PAGE->requires->js_init_call('M.core_message.init_editsettings');
3b120e46 69
eb5334ff 70// check access control
71if ($user->id == $USER->id) {
72 //editing own message profile
73 require_capability('moodle/user:editownmessageprofile', $systemcontext);
5ac851fb
SH
74 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
75 $node->make_active();
76 $PAGE->navbar->includesettingsbase = true;
77 }
eb5334ff 78} else {
79 // teachers, parents, etc.
80 require_capability('moodle/user:editmessageprofile', $personalcontext);
81 // no editing of guest user account
82 if (isguestuser($user->id)) {
83 print_error('guestnoeditmessageother', 'message');
84 }
4f622c38 85 // no editing of admins by non admins!
1e725535 86 if (is_siteadmin($user) and !is_siteadmin($USER)) {
4f622c38 87 print_error('useradmineditadmin');
3b120e46 88 }
5ac851fb 89 $PAGE->navigation->extend_for_user($user);
eb5334ff 90}
3b120e46 91
a8134ff6
DP
92// Fetch message providers
93$providers = message_get_providers_for_user($user->id);
94
71666cf3 95/// Save new preferences if data was submitted
305a014f 96
eb5334ff 97if (($form = data_submitted()) && confirm_sesskey()) {
98 $preferences = array();
305a014f 99
bb3546f3
AD
100 //only update the user's "emailstop" if its actually changed
101 if ( $user->emailstop != $disableall ) {
102 $user->emailstop = $disableall;
103 $DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
104 }
105
f6ce2eb2
AD
106 // Turning on emailstop disables the preference checkboxes in the browser.
107 // Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked.
108 // Only alter the messaging preferences if emailstop is turned off
109 if (!$user->emailstop) {
110 foreach ($providers as $provider) {
111 $componentproviderbase = $provider->component.'_'.$provider->name;
112 foreach (array('loggedin', 'loggedoff') as $state) {
113 $linepref = '';
114 $componentproviderstate = $componentproviderbase.'_'.$state;
115 if (array_key_exists($componentproviderstate, $form)) {
116 foreach (array_keys($form->{$componentproviderstate}) as $process) {
117 if ($linepref == ''){
118 $linepref = $process;
119 } else {
120 $linepref .= ','.$process;
121 }
c8621a02 122 }
eb5334ff 123 }
f6ce2eb2
AD
124 if (empty($linepref)) {
125 $linepref = 'none';
126 }
127 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
d18b1bbd 128 }
a813a748
AD
129 }
130 }
f3d095f8 131
eb5334ff 132/// Set all the processor options as well
814e3735
RK
133 $processors = get_message_processors(true);
134 foreach ($processors as $processor) {
135 $processor->object->process_form($form, $preferences);
d18b1bbd 136 }
305a014f 137
2509c0e9 138 //process general messaging preferences
814e3735 139 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
2509c0e9
AD
140 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
141
fa8f03ef 142 // Save all the new preferences to the database
814e3735 143 if (!set_user_preferences($preferences, $user->id)) {
eb5334ff 144 print_error('cannotupdateusermsgpref');
145 }
146
147 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
148}
149
1d422980 150/// Load preferences
8e803c3f 151$preferences = new stdClass();
fa8f03ef 152$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
305a014f 153
eb5334ff 154/// Get providers preferences
814e3735
RK
155foreach ($providers as $provider) {
156 foreach (array('loggedin', 'loggedoff') as $state) {
eb5334ff 157 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
158 if ($linepref == ''){
159 continue;
160 }
161 $lineprefarray = explode(',', $linepref);
162 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
814e3735 163 foreach ($lineprefarray as $pref) {
eb5334ff 164 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
165 }
3b120e46 166 }
eb5334ff 167}
168
814e3735
RK
169// Load all processors
170$processors = get_message_processors();
eb5334ff 171/// For every processors put its options on the form (need to get function from processor's lib.php)
814e3735
RK
172foreach ($processors as $processor) {
173 $processor->object->load_data($preferences, $user->id);
eb5334ff 174}
175
2509c0e9
AD
176//load general messaging preferences
177$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
178//$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
179
eb5334ff 180/// Display page header
181$streditmymessage = get_string('editmymessage', 'message');
182$strparticipants = get_string('participants');
183$userfullname = fullname($user, true);
184
eb5334ff 185$PAGE->set_title("$course->shortname: $streditmymessage");
186if ($course->id != SITEID) {
187 $PAGE->set_heading("$course->fullname: $streditmymessage");
188} else {
189 $PAGE->set_heading($course->fullname);
190}
3b120e46 191
814e3735
RK
192// Grab the renderer
193$renderer = $PAGE->get_renderer('core', 'message');
814e3735
RK
194// Fetch default (site) preferences
195$defaultpreferences = get_message_output_default_preferences();
eb5334ff 196
bb3546f3 197$messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $user->emailstop);
d18b1bbd 198
814e3735
RK
199echo $OUTPUT->header();
200echo $messagingoptions;
eb5334ff 201echo $OUTPUT->footer();
3b120e46 202