3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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 if ($userid !== $USER->id
) {
35 $url->param('id', $userid);
37 if ($course != SITEID
) {
38 $url->param('course', $course);
42 if (!$course = $DB->get_record('course', array('id' => $course))) {
43 print_error('invalidcourseid');
46 if ($course->id
!= SITEID
) {
47 require_login($course);
51 if (empty($SESSION->wantsurl
)) {
52 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/message/edit.php';
54 redirect(get_login_url());
59 print_error('guestnoeditmessage', 'message');
62 if (!$user = $DB->get_record('user', array('id' => $userid))) {
63 print_error('invaliduserid');
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
);
70 $PAGE->set_context($personalcontext);
71 $PAGE->set_pagelayout('course');
72 $PAGE->requires
->js_init_call('M.core_message.init_editsettings');
74 // check access control
75 if ($user->id
== $USER->id
) {
76 //editing own message profile
77 require_capability('moodle/user:editownmessageprofile', $systemcontext);
78 if ($course->id
!= SITEID
&& $node = $PAGE->navigation
->find($course->id
, navigation_node
::TYPE_COURSE
)) {
80 $PAGE->navbar
->includesettingsbase
= true;
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');
89 // no editing of admins by non admins!
90 if (is_siteadmin($user) and !is_siteadmin($USER)) {
91 print_error('useradmineditadmin');
93 $PAGE->navigation
->extend_for_user($user);
96 // Fetch message providers
97 $providers = message_get_providers_for_user($user->id
);
99 /// Save new preferences if data was submitted
101 if (($form = data_submitted()) && confirm_sesskey()) {
102 $preferences = array();
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
));
111 foreach ($providers as $provider) {
112 $componentproviderbase = $provider->component
.'_'.$provider->name
;
113 foreach (array('loggedin', 'loggedoff') as $state) {
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;
121 $linepref .= ','.$process;
125 if (empty($linepref)) {
128 $preferences['message_provider_'.$provider->component
.'_'.$provider->name
.'_'.$state] = $linepref;
132 /// Set all the processor options as well
133 $processors = get_message_processors(true);
134 foreach ($processors as $processor) {
135 $processor->object->process_form($form, $preferences);
138 //process general messaging preferences
139 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts
)?
1:0;
140 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
142 // Save all the new preferences to the database
143 if (!set_user_preferences($preferences, $user->id
)) {
144 print_error('cannotupdateusermsgpref');
147 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
151 $preferences = new stdClass();
152 $preferences->userdefaultemail
= $user->email
;//may be displayed by the email processor
154 /// Get providers preferences
155 foreach ($providers as $provider) {
156 foreach (array('loggedin', 'loggedoff') as $state) {
157 $linepref = get_user_preferences('message_provider_'.$provider->component
.'_'.$provider->name
.'_'.$state, '', $user->id
);
158 if ($linepref == ''){
161 $lineprefarray = explode(',', $linepref);
162 $preferences->{$provider->component
.'_'.$provider->name
.'_'.$state} = array();
163 foreach ($lineprefarray as $pref) {
164 $preferences->{$provider->component
.'_'.$provider->name
.'_'.$state}[$pref] = 1;
169 // Load all processors
170 $processors = get_message_processors();
171 /// For every processors put its options on the form (need to get function from processor's lib.php)
172 foreach ($processors as $processor) {
173 $processor->object->load_data($preferences, $user->id
);
176 //load general messaging preferences
177 $preferences->blocknoncontacts
= get_user_preferences( 'message_blocknoncontacts', '', $user->id
);
178 //$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
180 /// Display page header
181 $streditmymessage = get_string('editmymessage', 'message');
182 $strparticipants = get_string('participants');
183 $userfullname = fullname($user, true);
185 $PAGE->set_title("$course->shortname: $streditmymessage");
186 if ($course->id
!= SITEID
) {
187 $PAGE->set_heading("$course->fullname: $streditmymessage");
189 $PAGE->set_heading($course->fullname
);
193 $renderer = $PAGE->get_renderer('core', 'message');
194 // Fetch default (site) preferences
195 $defaultpreferences = get_message_output_default_preferences();
197 $messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $user->emailstop
);
199 echo $OUTPUT->header();
200 echo $messagingoptions;
201 echo $OUTPUT->footer();