MDL-49412 navigation: Isset check on email setting.
[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');
479fa47d 27require_once($CFG->dirroot . '/user/lib.php');
3b120e46 28
59d52284 29$userid = optional_param('id', 0, PARAM_INT); // User id.
bb3546f3 30$disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
3b120e46 31
59d52284
DC
32if (!$userid) {
33 $userid = $USER->id;
34}
35
a6855934 36$url = new moodle_url('/message/edit.php');
ff038e4b 37$url->param('id', $userid);
ff038e4b 38
eb5334ff 39$PAGE->set_url($url);
26e02d75 40$PAGE->set_popup_notification_allowed(false); // We are within the messaging system so don't show message popups
3b120e46 41
5e008d36 42require_login();
3b120e46 43
eb5334ff 44if (isguestuser()) {
45 print_error('guestnoeditmessage', 'message');
46}
3b120e46 47
eb5334ff 48if (!$user = $DB->get_record('user', array('id' => $userid))) {
49 print_error('invaliduserid');
50}
3b120e46 51
bf0f06b1
AA
52$systemcontext = context_system::instance();
53$personalcontext = context_user::instance($user->id);
3b120e46 54
5ac851fb 55$PAGE->set_context($personalcontext);
d5814f4e 56$PAGE->set_pagelayout('admin');
bb3546f3 57$PAGE->requires->js_init_call('M.core_message.init_editsettings');
3b120e46 58
eb5334ff 59// check access control
60if ($user->id == $USER->id) {
61 //editing own message profile
62 require_capability('moodle/user:editownmessageprofile', $systemcontext);
eb5334ff 63} else {
64 // teachers, parents, etc.
65 require_capability('moodle/user:editmessageprofile', $personalcontext);
66 // no editing of guest user account
67 if (isguestuser($user->id)) {
68 print_error('guestnoeditmessageother', 'message');
69 }
4f622c38 70 // no editing of admins by non admins!
1e725535 71 if (is_siteadmin($user) and !is_siteadmin($USER)) {
4f622c38 72 print_error('useradmineditadmin');
3b120e46 73 }
f495187d 74 $PAGE->navbar->includesettingsbase = true;
5ac851fb 75 $PAGE->navigation->extend_for_user($user);
eb5334ff 76}
3b120e46 77
a8134ff6
DP
78// Fetch message providers
79$providers = message_get_providers_for_user($user->id);
80
71666cf3 81/// Save new preferences if data was submitted
305a014f 82
eb5334ff 83if (($form = data_submitted()) && confirm_sesskey()) {
84 $preferences = array();
305a014f 85
bb3546f3
AD
86 //only update the user's "emailstop" if its actually changed
87 if ( $user->emailstop != $disableall ) {
88 $user->emailstop = $disableall;
89 $DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
90 }
91
f6ce2eb2
AD
92 // Turning on emailstop disables the preference checkboxes in the browser.
93 // Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked.
94 // Only alter the messaging preferences if emailstop is turned off
95 if (!$user->emailstop) {
96 foreach ($providers as $provider) {
97 $componentproviderbase = $provider->component.'_'.$provider->name;
98 foreach (array('loggedin', 'loggedoff') as $state) {
99 $linepref = '';
100 $componentproviderstate = $componentproviderbase.'_'.$state;
101 if (array_key_exists($componentproviderstate, $form)) {
102 foreach (array_keys($form->{$componentproviderstate}) as $process) {
103 if ($linepref == ''){
104 $linepref = $process;
105 } else {
106 $linepref .= ','.$process;
107 }
c8621a02 108 }
eb5334ff 109 }
f6ce2eb2
AD
110 if (empty($linepref)) {
111 $linepref = 'none';
112 }
113 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
d18b1bbd 114 }
a813a748
AD
115 }
116 }
f3d095f8 117
eb5334ff 118/// Set all the processor options as well
814e3735
RK
119 $processors = get_message_processors(true);
120 foreach ($processors as $processor) {
121 $processor->object->process_form($form, $preferences);
d18b1bbd 122 }
305a014f 123
2509c0e9 124 //process general messaging preferences
814e3735 125 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
9279cae6 126 $preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
2509c0e9 127
fa8f03ef 128 // Save all the new preferences to the database
814e3735 129 if (!set_user_preferences($preferences, $user->id)) {
eb5334ff 130 print_error('cannotupdateusermsgpref');
131 }
132
06347532
AG
133 if (isset($form->mailformat)) {
134 $user->mailformat = clean_param($form->mailformat, PARAM_INT);
135 }
479fa47d
DW
136 user_update_user($user, false, false);
137
5e008d36 138 redirect("$CFG->wwwroot/message/edit.php?id=$user->id");
eb5334ff 139}
140
1d422980 141/// Load preferences
8e803c3f 142$preferences = new stdClass();
fa8f03ef 143$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
305a014f 144
eb5334ff 145/// Get providers preferences
814e3735
RK
146foreach ($providers as $provider) {
147 foreach (array('loggedin', 'loggedoff') as $state) {
eb5334ff 148 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
149 if ($linepref == ''){
150 continue;
151 }
152 $lineprefarray = explode(',', $linepref);
153 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
814e3735 154 foreach ($lineprefarray as $pref) {
eb5334ff 155 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
156 }
3b120e46 157 }
eb5334ff 158}
159
814e3735
RK
160// Load all processors
161$processors = get_message_processors();
eb5334ff 162/// For every processors put its options on the form (need to get function from processor's lib.php)
814e3735
RK
163foreach ($processors as $processor) {
164 $processor->object->load_data($preferences, $user->id);
eb5334ff 165}
166
2509c0e9
AD
167//load general messaging preferences
168$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
9279cae6 169$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
479fa47d
DW
170$preferences->mailformat = $user->mailformat;
171$preferences->mailcharset = get_user_preferences( 'mailcharset', '', $user->id);
2509c0e9 172
eb5334ff 173/// Display page header
058dadb0
AD
174$strmessaging = get_string('messaging', 'message');
175$PAGE->set_title($strmessaging);
479fa47d 176$PAGE->set_heading(fullname($user));
3b120e46 177
814e3735
RK
178// Grab the renderer
179$renderer = $PAGE->get_renderer('core', 'message');
814e3735
RK
180// Fetch default (site) preferences
181$defaultpreferences = get_message_output_default_preferences();
eb5334ff 182
bb3546f3 183$messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $user->emailstop);
d18b1bbd 184
814e3735
RK
185echo $OUTPUT->header();
186echo $messagingoptions;
eb5334ff 187echo $OUTPUT->footer();
3b120e46 188