2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
30 $disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
32 $url = new moodle_url('/message/edit.php');
33 if ($userid !== $USER->id) {
34 $url->param('id', $userid);
36 if ($course != SITEID) {
37 $url->param('course', $course);
41 if (!$course = $DB->get_record('course', array('id' => $course))) {
42 print_error('invalidcourseid');
45 if ($course->id != SITEID) {
46 require_login($course);
50 if (empty($SESSION->wantsurl)) {
51 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
53 redirect(get_login_url());
58 print_error('guestnoeditmessage', 'message');
61 if (!$user = $DB->get_record('user', array('id' => $userid))) {
62 print_error('invaliduserid');
65 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
66 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
67 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
69 $PAGE->set_context($personalcontext);
70 $PAGE->set_pagelayout('course');
71 $PAGE->requires->js_init_call('M.core_message.init_editsettings');
73 // check access control
74 if ($user->id == $USER->id) {
75 //editing own message profile
76 require_capability('moodle/user:editownmessageprofile', $systemcontext);
77 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
79 $PAGE->navbar->includesettingsbase = true;
82 // teachers, parents, etc.
83 require_capability('moodle/user:editmessageprofile', $personalcontext);
84 // no editing of guest user account
85 if (isguestuser($user->id)) {
86 print_error('guestnoeditmessageother', 'message');
88 // no editing of admins by non admins!
89 if (is_siteadmin($user) and !is_siteadmin($USER)) {
90 print_error('useradmineditadmin');
92 $PAGE->navigation->extend_for_user($user);
95 // Fetch message providers
96 $providers = message_get_providers_for_user($user->id);
98 /// Save new preferences if data was submitted
100 if (($form = data_submitted()) && confirm_sesskey()) {
101 $preferences = array();
103 //only update the user's "emailstop" if its actually changed
104 if ( $user->emailstop != $disableall ) {
105 $user->emailstop = $disableall;
106 $DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
109 // Turning on emailstop disables the preference checkboxes in the browser.
110 // Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked.
111 // Only alter the messaging preferences if emailstop is turned off
112 if (!$user->emailstop) {
113 foreach ($providers as $provider) {
114 $componentproviderbase = $provider->component.'_'.$provider->name;
115 foreach (array('loggedin', 'loggedoff') as $state) {
117 $componentproviderstate = $componentproviderbase.'_'.$state;
118 if (array_key_exists($componentproviderstate, $form)) {
119 foreach (array_keys($form->{$componentproviderstate}) as $process) {
120 if ($linepref == ''){
121 $linepref = $process;
123 $linepref .= ','.$process;
127 if (empty($linepref)) {
130 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
135 /// Set all the processor options as well
136 $processors = get_message_processors(true);
137 foreach ($processors as $processor) {
138 $processor->object->process_form($form, $preferences);
141 //process general messaging preferences
142 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
143 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
145 // Save all the new preferences to the database
146 if (!set_user_preferences($preferences, $user->id)) {
147 print_error('cannotupdateusermsgpref');
150 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
154 $preferences = new stdClass();
155 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
157 /// Get providers preferences
158 foreach ($providers as $provider) {
159 foreach (array('loggedin', 'loggedoff') as $state) {
160 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
161 if ($linepref == ''){
164 $lineprefarray = explode(',', $linepref);
165 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
166 foreach ($lineprefarray as $pref) {
167 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
172 // Load all processors
173 $processors = get_message_processors();
174 /// For every processors put its options on the form (need to get function from processor's lib.php)
175 foreach ($processors as $processor) {
176 $processor->object->load_data($preferences, $user->id);
179 //load general messaging preferences
180 $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
181 //$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
183 /// Display page header
184 $streditmymessage = get_string('editmymessage', 'message');
185 $strparticipants = get_string('participants');
186 $userfullname = fullname($user, true);
188 $PAGE->set_title("$course->shortname: $streditmymessage");
189 if ($course->id != SITEID) {
190 $PAGE->set_heading("$course->fullname: $streditmymessage");
192 $PAGE->set_heading($course->fullname);
196 $renderer = $PAGE->get_renderer('core', 'message');
197 // Fetch default (site) preferences
198 $defaultpreferences = get_message_output_default_preferences();
200 $messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $user->emailstop);
202 echo $OUTPUT->header();
203 echo $messagingoptions;
204 echo $OUTPUT->footer();