Merge branch 'm21_MDL-28017' of git://github.com/danmarsden/moodle into MOODLE_21_STABLE
[moodle.git] / message / edit.php
blob479d876a4d7555a78f838719fd05a33e7c1d235a
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)
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);
39 $PAGE->set_url($url);
41 if (!$course = $DB->get_record('course', array('id' => $course))) {
42 print_error('invalidcourseid');
45 if ($course->id != SITEID) {
46 require_login($course);
48 } else {
49 if (!isloggedin()) {
50 if (empty($SESSION->wantsurl)) {
51 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
53 redirect(get_login_url());
57 if (isguestuser()) {
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');
72 // check access control
73 if ($user->id == $USER->id) {
74 //editing own message profile
75 require_capability('moodle/user:editownmessageprofile', $systemcontext);
76 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
77 $node->make_active();
78 $PAGE->navbar->includesettingsbase = true;
80 } else {
81 // teachers, parents, etc.
82 require_capability('moodle/user:editmessageprofile', $personalcontext);
83 // no editing of guest user account
84 if (isguestuser($user->id)) {
85 print_error('guestnoeditmessageother', 'message');
87 // no editing of admins by non admins!
88 if (is_siteadmin($user) and !is_siteadmin($USER)) {
89 print_error('useradmineditadmin');
91 $PAGE->navigation->extend_for_user($user);
94 // Fetch message providers
95 $providers = message_get_providers_for_user($user->id);
97 /// Save new preferences if data was submitted
99 if (($form = data_submitted()) && confirm_sesskey()) {
100 $preferences = array();
102 foreach ($providers as $provider) {
103 $componentproviderbase = $provider->component.'_'.$provider->name;
104 foreach (array('loggedin', 'loggedoff') as $state) {
105 $linepref = '';
106 $componentproviderstate = $componentproviderbase.'_'.$state;
107 if (array_key_exists($componentproviderstate, $form)) {
108 foreach (array_keys($form->{$componentproviderstate}) as $process){
109 if ($linepref == ''){
110 $linepref = $process;
111 } else {
112 $linepref .= ','.$process;
116 if (empty($linepref)) {
117 $linepref = 'none';
119 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
123 /// Set all the processor options as well
124 $processors = get_message_processors(true);
125 foreach ($processors as $processor) {
126 $processor->object->process_form($form, $preferences);
129 //process general messaging preferences
130 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
131 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
133 // Save all the new preferences to the database
134 if (!set_user_preferences($preferences, $user->id)) {
135 print_error('cannotupdateusermsgpref');
138 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
141 /// Load preferences
142 $preferences = new stdClass();
143 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
145 /// Get providers preferences
146 foreach ($providers as $provider) {
147 foreach (array('loggedin', 'loggedoff') as $state) {
148 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
149 if ($linepref == ''){
150 continue;
152 $lineprefarray = explode(',', $linepref);
153 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
154 foreach ($lineprefarray as $pref) {
155 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
160 // Load all processors
161 $processors = get_message_processors();
162 /// For every processors put its options on the form (need to get function from processor's lib.php)
163 foreach ($processors as $processor) {
164 $processor->object->load_data($preferences, $user->id);
167 //load general messaging preferences
168 $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
169 //$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
171 /// Display page header
172 $streditmymessage = get_string('editmymessage', 'message');
173 $strparticipants = get_string('participants');
174 $userfullname = fullname($user, true);
176 $PAGE->set_title("$course->shortname: $streditmymessage");
177 if ($course->id != SITEID) {
178 $PAGE->set_heading("$course->fullname: $streditmymessage");
179 } else {
180 $PAGE->set_heading($course->fullname);
183 // Grab the renderer
184 $renderer = $PAGE->get_renderer('core', 'message');
185 // Fetch default (site) preferences
186 $defaultpreferences = get_message_output_default_preferences();
188 $messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences);
190 echo $OUTPUT->header();
191 echo $messagingoptions;
192 echo $OUTPUT->footer();