Merge branch 'wip-mdl-50259' of https://github.com/rajeshtaneja/moodle
[moodle.git] / message / edit.php
blob54381dcd4d94d9bb3bc6180e04a59b08b34d5f9b
1 <?php
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.
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/>.
17 /**
18 * Edit user message preferences
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
25 require_once(dirname(__FILE__) . '/../config.php');
26 require_once($CFG->dirroot . '/message/lib.php');
27 require_once($CFG->dirroot . '/user/lib.php');
29 $userid = optional_param('id', 0, PARAM_INT); // User id.
30 $disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
32 if (!$userid) {
33 $userid = $USER->id;
36 $url = new moodle_url('/message/edit.php');
37 $url->param('id', $userid);
39 $PAGE->set_url($url);
40 $PAGE->set_popup_notification_allowed(false); // We are within the messaging system so don't show message popups
42 require_login();
44 if (isguestuser()) {
45 print_error('guestnoeditmessage', 'message');
48 if (!$user = $DB->get_record('user', array('id' => $userid))) {
49 print_error('invaliduserid');
52 $systemcontext = context_system::instance();
53 $personalcontext = context_user::instance($user->id);
55 $PAGE->set_context($personalcontext);
56 $PAGE->set_pagelayout('admin');
57 $PAGE->requires->js_init_call('M.core_message.init_editsettings');
59 // check access control
60 if ($user->id == $USER->id) {
61 //editing own message profile
62 require_capability('moodle/user:editownmessageprofile', $systemcontext);
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');
70 // no editing of admins by non admins!
71 if (is_siteadmin($user) and !is_siteadmin($USER)) {
72 print_error('useradmineditadmin');
74 $PAGE->navbar->includesettingsbase = true;
75 $PAGE->navigation->extend_for_user($user);
78 // Fetch message providers
79 $providers = message_get_providers_for_user($user->id);
81 /// Save new preferences if data was submitted
83 if (($form = data_submitted()) && confirm_sesskey()) {
84 $preferences = array();
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));
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;
110 if (empty($linepref)) {
111 $linepref = 'none';
113 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
118 /// Set all the processor options as well
119 $processors = get_message_processors(true);
120 foreach ($processors as $processor) {
121 $processor->object->process_form($form, $preferences);
124 //process general messaging preferences
125 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
126 $preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
128 // Save all the new preferences to the database
129 if (!set_user_preferences($preferences, $user->id)) {
130 print_error('cannotupdateusermsgpref');
133 if (isset($form->mailformat)) {
134 $user->mailformat = clean_param($form->mailformat, PARAM_INT);
136 user_update_user($user, false, false);
138 $redirect = new moodle_url("/user/preferences.php", array('userid' => $userid));
139 redirect($redirect);
142 /// Load preferences
143 $preferences = new stdClass();
144 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
146 /// Get providers preferences
147 foreach ($providers as $provider) {
148 foreach (array('loggedin', 'loggedoff') as $state) {
149 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
150 if ($linepref == ''){
151 continue;
153 $lineprefarray = explode(',', $linepref);
154 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
155 foreach ($lineprefarray as $pref) {
156 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
161 // Load all processors
162 $processors = get_message_processors();
163 /// For every processors put its options on the form (need to get function from processor's lib.php)
164 foreach ($processors as $processor) {
165 $processor->object->load_data($preferences, $user->id);
168 //load general messaging preferences
169 $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
170 $preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
171 $preferences->mailformat = $user->mailformat;
172 $preferences->mailcharset = get_user_preferences( 'mailcharset', '', $user->id);
174 /// Display page header
175 $strmessaging = get_string('messaging', 'message');
176 $PAGE->set_title($strmessaging);
177 $PAGE->set_heading(fullname($user));
179 // Grab the renderer
180 $renderer = $PAGE->get_renderer('core', 'message');
181 // Fetch default (site) preferences
182 $defaultpreferences = get_message_output_default_preferences();
184 $messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences,
185 $user->emailstop, $user->id);
187 echo $OUTPUT->header();
188 echo $messagingoptions;
189 echo $OUTPUT->footer();