Merge branch 'MDL-32249-21' of github.com:srynot4sale/moodle into MOODLE_21_STABLE
[moodle.git] / message / edit.php
bloba231b25553bd33943a97f51910ab1cc05b8d737d
1 <?php
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.
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/>.
18 /**
19 * Edit user message preferences
21 * @author Luis Rodrigues and Martin Dougiamas
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package message
26 require_once(dirname(__FILE__) . '/../config.php');
27 require_once($CFG->dirroot . '/message/lib.php');
29 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
30 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
31 $disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
33 $url = new moodle_url('/message/edit.php');
34 $url->param('id', $userid);
35 $url->param('course', $course);
37 $PAGE->set_url($url);
39 if (!$course = $DB->get_record('course', array('id' => $course))) {
40 print_error('invalidcourseid');
43 if ($course->id != SITEID) {
44 require_login($course);
46 } else {
47 if (!isloggedin()) {
48 if (empty($SESSION->wantsurl)) {
49 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
51 redirect(get_login_url());
55 if (isguestuser()) {
56 print_error('guestnoeditmessage', 'message');
59 if (!$user = $DB->get_record('user', array('id' => $userid))) {
60 print_error('invaliduserid');
63 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
64 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
65 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
67 $PAGE->set_context($personalcontext);
68 $PAGE->set_pagelayout('course');
69 $PAGE->requires->js_init_call('M.core_message.init_editsettings');
71 // check access control
72 if ($user->id == $USER->id) {
73 //editing own message profile
74 require_capability('moodle/user:editownmessageprofile', $systemcontext);
75 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
76 $node->make_active();
77 $PAGE->navbar->includesettingsbase = true;
79 } else {
80 // teachers, parents, etc.
81 require_capability('moodle/user:editmessageprofile', $personalcontext);
82 // no editing of guest user account
83 if (isguestuser($user->id)) {
84 print_error('guestnoeditmessageother', 'message');
86 // no editing of admins by non admins!
87 if (is_siteadmin($user) and !is_siteadmin($USER)) {
88 print_error('useradmineditadmin');
90 $PAGE->navigation->extend_for_user($user);
93 // Fetch message providers
94 $providers = message_get_providers_for_user($user->id);
96 /// Save new preferences if data was submitted
98 if (($form = data_submitted()) && confirm_sesskey()) {
99 $preferences = array();
101 //only update the user's "emailstop" if its actually changed
102 if ( $user->emailstop != $disableall ) {
103 $user->emailstop = $disableall;
104 $DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
107 // Turning on emailstop disables the preference checkboxes in the browser.
108 // Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked.
109 // Only alter the messaging preferences if emailstop is turned off
110 if (!$user->emailstop) {
111 foreach ($providers as $provider) {
112 $componentproviderbase = $provider->component.'_'.$provider->name;
113 foreach (array('loggedin', 'loggedoff') as $state) {
114 $linepref = '';
115 $componentproviderstate = $componentproviderbase.'_'.$state;
116 if (array_key_exists($componentproviderstate, $form)) {
117 foreach (array_keys($form->{$componentproviderstate}) as $process) {
118 if ($linepref == ''){
119 $linepref = $process;
120 } else {
121 $linepref .= ','.$process;
125 if (empty($linepref)) {
126 $linepref = 'none';
128 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
133 /// Set all the processor options as well
134 $processors = get_message_processors(true);
135 foreach ($processors as $processor) {
136 $processor->object->process_form($form, $preferences);
139 //process general messaging preferences
140 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
141 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
143 // Save all the new preferences to the database
144 if (!set_user_preferences($preferences, $user->id)) {
145 print_error('cannotupdateusermsgpref');
148 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
151 /// Load preferences
152 $preferences = new stdClass();
153 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
155 /// Get providers preferences
156 foreach ($providers as $provider) {
157 foreach (array('loggedin', 'loggedoff') as $state) {
158 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
159 if ($linepref == ''){
160 continue;
162 $lineprefarray = explode(',', $linepref);
163 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
164 foreach ($lineprefarray as $pref) {
165 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
170 // Load all processors
171 $processors = get_message_processors();
172 /// For every processors put its options on the form (need to get function from processor's lib.php)
173 foreach ($processors as $processor) {
174 $processor->object->load_data($preferences, $user->id);
177 //load general messaging preferences
178 $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
179 //$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
181 /// Display page header
182 $streditmymessage = get_string('editmymessage', 'message');
183 $strparticipants = get_string('participants');
184 $userfullname = fullname($user, true);
186 $PAGE->set_title("$course->shortname: $streditmymessage");
187 if ($course->id != SITEID) {
188 $PAGE->set_heading("$course->fullname: $streditmymessage");
189 } else {
190 $PAGE->set_heading($course->fullname);
193 // Grab the renderer
194 $renderer = $PAGE->get_renderer('core', 'message');
195 // Fetch default (site) preferences
196 $defaultpreferences = get_message_output_default_preferences();
198 $messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $user->emailstop);
200 echo $OUTPUT->header();
201 echo $messagingoptions;
202 echo $OUTPUT->footer();