MDL-27171 messages: implement new syntax in messages.php and update processing
[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
814e3735
RK
26require_once(dirname(dirname(__FILE__)) . '/config.php');
27require_once(dirname(dirname(__FILE__)) . '/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)
3b120e46 31
a6855934 32$url = new moodle_url('/message/edit.php');
eb5334ff 33if ($userid !== $USER->id) {
34 $url->param('id', $userid);
35}
7b2e259c 36if ($course != SITEID) {
eb5334ff 37 $url->param('course', $course);
38}
39$PAGE->set_url($url);
3b120e46 40
eb5334ff 41if (!$course = $DB->get_record('course', array('id' => $course))) {
42 print_error('invalidcourseid');
43}
3b120e46 44
eb5334ff 45if ($course->id != SITEID) {
46 require_login($course);
3b120e46 47
1d422980 48} else {
eb5334ff 49 if (!isloggedin()) {
50 if (empty($SESSION->wantsurl)) {
51 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
3b120e46 52 }
eb5334ff 53 redirect(get_login_url());
3b120e46 54 }
eb5334ff 55}
3b120e46 56
eb5334ff 57if (isguestuser()) {
58 print_error('guestnoeditmessage', 'message');
59}
3b120e46 60
eb5334ff 61if (!$user = $DB->get_record('user', array('id' => $userid))) {
62 print_error('invaliduserid');
63}
3b120e46 64
eb5334ff 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);
3b120e46 68
5ac851fb 69$PAGE->set_context($personalcontext);
c8621a02 70$PAGE->set_pagelayout('course');
3b120e46 71
eb5334ff 72// check access control
73if ($user->id == $USER->id) {
74 //editing own message profile
75 require_capability('moodle/user:editownmessageprofile', $systemcontext);
5ac851fb
SH
76 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
77 $node->make_active();
78 $PAGE->navbar->includesettingsbase = true;
79 }
eb5334ff 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');
86 }
4f622c38 87 // no editing of admins by non admins!
1e725535 88 if (is_siteadmin($user) and !is_siteadmin($USER)) {
4f622c38 89 print_error('useradmineditadmin');
3b120e46 90 }
5ac851fb 91 $PAGE->navigation->extend_for_user($user);
eb5334ff 92}
3b120e46 93
71666cf3 94/// Save new preferences if data was submitted
305a014f 95
eb5334ff 96if (($form = data_submitted()) && confirm_sesskey()) {
97 $preferences = array();
305a014f 98
eb5334ff 99/// Set all the preferences for all the message providers
d18b1bbd 100 $providers = message_get_my_providers();
7a04c476 101 foreach ($providers as $provider) {
814e3735
RK
102 $componentproviderbase = $provider->component.'_'.$provider->name;
103 foreach (array('loggedin', 'loggedoff') as $state) {
eb5334ff 104 $linepref = '';
814e3735 105 $componentproviderstate = $componentproviderbase.'_'.$state;
c8621a02 106 if (array_key_exists($componentproviderstate, $form)) {
814e3735 107 foreach (array_keys($form->{$componentproviderstate}) as $process){
c8621a02
AD
108 if ($linepref == ''){
109 $linepref = $process;
110 } else {
111 $linepref .= ','.$process;
112 }
eb5334ff 113 }
d18b1bbd 114 }
814e3735
RK
115 if (empty($linepref)) {
116 $linepref = 'none';
a813a748 117 }
814e3735 118 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
a813a748
AD
119 }
120 }
f3d095f8 121
eb5334ff 122/// Set all the processor options as well
814e3735
RK
123 $processors = get_message_processors(true);
124 foreach ($processors as $processor) {
125 $processor->object->process_form($form, $preferences);
d18b1bbd 126 }
305a014f 127
2509c0e9 128 //process general messaging preferences
814e3735 129 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
2509c0e9
AD
130 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
131
fa8f03ef 132 // Save all the new preferences to the database
814e3735 133 if (!set_user_preferences($preferences, $user->id)) {
eb5334ff 134 print_error('cannotupdateusermsgpref');
135 }
136
137 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
138}
139
1d422980 140/// Load preferences
8e803c3f 141$preferences = new stdClass();
fa8f03ef 142$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
305a014f 143
eb5334ff 144/// Get providers preferences
145$providers = message_get_my_providers();
814e3735
RK
146foreach ($providers as $provider) {
147 foreach (array('loggedin', 'loggedoff') as $state) {
eb5334ff 148 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
149 if ($linepref == ''){
150 continue;
151 }
152 $lineprefarray = explode(',', $linepref);
153 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
814e3735 154 foreach ($lineprefarray as $pref) {
eb5334ff 155 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
156 }
3b120e46 157 }
eb5334ff 158}
159
814e3735
RK
160// Load all processors
161$processors = get_message_processors();
eb5334ff 162/// For every processors put its options on the form (need to get function from processor's lib.php)
814e3735
RK
163foreach ($processors as $processor) {
164 $processor->object->load_data($preferences, $user->id);
eb5334ff 165}
166
2509c0e9
AD
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);
170
eb5334ff 171/// Display page header
172$streditmymessage = get_string('editmymessage', 'message');
173$strparticipants = get_string('participants');
174$userfullname = fullname($user, true);
175
eb5334ff 176$PAGE->set_title("$course->shortname: $streditmymessage");
177if ($course->id != SITEID) {
178 $PAGE->set_heading("$course->fullname: $streditmymessage");
179} else {
180 $PAGE->set_heading($course->fullname);
181}
3b120e46 182
814e3735
RK
183// Grab the renderer
184$renderer = $PAGE->get_renderer('core', 'message');
185// Fetch message providers
eb5334ff 186$providers = message_get_my_providers();
814e3735
RK
187// Fetch default (site) preferences
188$defaultpreferences = get_message_output_default_preferences();
eb5334ff 189
814e3735 190$messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences);
d18b1bbd 191
814e3735
RK
192echo $OUTPUT->header();
193echo $messagingoptions;
eb5334ff 194echo $OUTPUT->footer();
3b120e46 195