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