Merge branch 'MDL-48035-26-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_26_...
[moodle.git] / message / edit.php
blob8a498629229b739234b67e107eb55205c5f4e066
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');
28 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
29 $disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
31 $url = new moodle_url('/message/edit.php');
32 $url->param('id', $userid);
34 $PAGE->set_url($url);
35 $PAGE->set_popup_notification_allowed(false); // We are within the messaging system so don't show message popups
37 require_login();
39 if (isguestuser()) {
40 print_error('guestnoeditmessage', 'message');
43 if (!$user = $DB->get_record('user', array('id' => $userid))) {
44 print_error('invaliduserid');
47 $systemcontext = context_system::instance();
48 $personalcontext = context_user::instance($user->id);
50 $PAGE->set_context($personalcontext);
51 $PAGE->set_pagelayout('admin');
52 $PAGE->requires->js_init_call('M.core_message.init_editsettings');
54 // check access control
55 if ($user->id == $USER->id) {
56 //editing own message profile
57 require_capability('moodle/user:editownmessageprofile', $systemcontext);
58 } else {
59 // teachers, parents, etc.
60 require_capability('moodle/user:editmessageprofile', $personalcontext);
61 // no editing of guest user account
62 if (isguestuser($user->id)) {
63 print_error('guestnoeditmessageother', 'message');
65 // no editing of admins by non admins!
66 if (is_siteadmin($user) and !is_siteadmin($USER)) {
67 print_error('useradmineditadmin');
69 $PAGE->navigation->extend_for_user($user);
72 // Fetch message providers
73 $providers = message_get_providers_for_user($user->id);
75 /// Save new preferences if data was submitted
77 if (($form = data_submitted()) && confirm_sesskey()) {
78 $preferences = array();
80 //only update the user's "emailstop" if its actually changed
81 if ( $user->emailstop != $disableall ) {
82 $user->emailstop = $disableall;
83 $DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
86 // Turning on emailstop disables the preference checkboxes in the browser.
87 // Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked.
88 // Only alter the messaging preferences if emailstop is turned off
89 if (!$user->emailstop) {
90 foreach ($providers as $provider) {
91 $componentproviderbase = $provider->component.'_'.$provider->name;
92 foreach (array('loggedin', 'loggedoff') as $state) {
93 $linepref = '';
94 $componentproviderstate = $componentproviderbase.'_'.$state;
95 if (array_key_exists($componentproviderstate, $form)) {
96 foreach (array_keys($form->{$componentproviderstate}) as $process) {
97 if ($linepref == ''){
98 $linepref = $process;
99 } else {
100 $linepref .= ','.$process;
104 if (empty($linepref)) {
105 $linepref = 'none';
107 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
112 /// Set all the processor options as well
113 $processors = get_message_processors(true);
114 foreach ($processors as $processor) {
115 $processor->object->process_form($form, $preferences);
118 //process general messaging preferences
119 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
120 $preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
122 // Save all the new preferences to the database
123 if (!set_user_preferences($preferences, $user->id)) {
124 print_error('cannotupdateusermsgpref');
127 redirect("$CFG->wwwroot/message/edit.php?id=$user->id");
130 /// Load preferences
131 $preferences = new stdClass();
132 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
134 /// Get providers preferences
135 foreach ($providers as $provider) {
136 foreach (array('loggedin', 'loggedoff') as $state) {
137 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
138 if ($linepref == ''){
139 continue;
141 $lineprefarray = explode(',', $linepref);
142 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
143 foreach ($lineprefarray as $pref) {
144 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
149 // Load all processors
150 $processors = get_message_processors();
151 /// For every processors put its options on the form (need to get function from processor's lib.php)
152 foreach ($processors as $processor) {
153 $processor->object->load_data($preferences, $user->id);
156 //load general messaging preferences
157 $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
158 $preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
160 /// Display page header
161 $strmessaging = get_string('messaging', 'message');
162 $PAGE->set_title($strmessaging);
163 $PAGE->set_heading($strmessaging);
165 // Grab the renderer
166 $renderer = $PAGE->get_renderer('core', 'message');
167 // Fetch default (site) preferences
168 $defaultpreferences = get_message_output_default_preferences();
170 $messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $user->emailstop);
172 echo $OUTPUT->header();
173 echo $messagingoptions;
174 echo $OUTPUT->footer();