MDL-27171 messages: add default message outputs management interface
[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
eb5334ff 26require_once('../config.php');
3b120e46 27
eb5334ff 28$userid = optional_param('id', $USER->id, PARAM_INT); // user id
29$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
3b120e46 30
a6855934 31$url = new moodle_url('/message/edit.php');
eb5334ff 32if ($userid !== $USER->id) {
33 $url->param('id', $userid);
34}
7b2e259c 35if ($course != SITEID) {
eb5334ff 36 $url->param('course', $course);
37}
38$PAGE->set_url($url);
3b120e46 39
eb5334ff 40if (!$course = $DB->get_record('course', array('id' => $course))) {
41 print_error('invalidcourseid');
42}
3b120e46 43
eb5334ff 44if ($course->id != SITEID) {
45 require_login($course);
3b120e46 46
1d422980 47} else {
eb5334ff 48 if (!isloggedin()) {
49 if (empty($SESSION->wantsurl)) {
50 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
3b120e46 51 }
eb5334ff 52 redirect(get_login_url());
3b120e46 53 }
eb5334ff 54}
3b120e46 55
eb5334ff 56if (isguestuser()) {
57 print_error('guestnoeditmessage', 'message');
58}
3b120e46 59
eb5334ff 60if (!$user = $DB->get_record('user', array('id' => $userid))) {
61 print_error('invaliduserid');
62}
3b120e46 63
eb5334ff 64$systemcontext = get_context_instance(CONTEXT_SYSTEM);
65$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
66$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
3b120e46 67
5ac851fb 68$PAGE->set_context($personalcontext);
c8621a02 69$PAGE->set_pagelayout('course');
3b120e46 70
eb5334ff 71// check access control
72if ($user->id == $USER->id) {
73 //editing own message profile
74 require_capability('moodle/user:editownmessageprofile', $systemcontext);
5ac851fb
SH
75 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
76 $node->make_active();
77 $PAGE->navbar->includesettingsbase = true;
78 }
eb5334ff 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');
85 }
4f622c38 86 // no editing of admins by non admins!
1e725535 87 if (is_siteadmin($user) and !is_siteadmin($USER)) {
4f622c38 88 print_error('useradmineditadmin');
3b120e46 89 }
5ac851fb 90 $PAGE->navigation->extend_for_user($user);
eb5334ff 91}
3b120e46 92
71666cf3 93/// Save new preferences if data was submitted
305a014f 94
eb5334ff 95if (($form = data_submitted()) && confirm_sesskey()) {
96 $preferences = array();
305a014f 97
eb5334ff 98/// Set all the preferences for all the message providers
d18b1bbd 99 $providers = message_get_my_providers();
a813a748 100 $possiblestates = array('loggedin', 'loggedoff');
d18b1bbd 101 foreach ( $providers as $providerid => $provider){
a813a748 102 foreach ($possiblestates as $state){
eb5334ff 103 $linepref = '';
c8621a02
AD
104 $componentproviderstate = $provider->component.'_'.$provider->name.'_'.$state;
105 if (array_key_exists($componentproviderstate, $form)) {
106 foreach ($form->{$componentproviderstate} as $process=>$one){
107 if ($linepref == ''){
108 $linepref = $process;
109 } else {
110 $linepref .= ','.$process;
111 }
eb5334ff 112 }
d18b1bbd 113 }
eb5334ff 114 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
d18b1bbd 115 }
116 }
a813a748
AD
117 foreach ( $providers as $providerid => $provider){
118 foreach ($possiblestates as $state){
119 $preferencekey = 'message_provider_'.$provider->component.'_'.$provider->name.'_'.$state;
120 if (empty($preferences[$preferencekey])) {
121 $preferences[$preferencekey] = 'none';
122 }
123 }
124 }
f3d095f8 125
eb5334ff 126/// Set all the processor options as well
d18b1bbd 127 $processors = $DB->get_records('message_processors');
eb5334ff 128 foreach ( $processors as $processorid => $processor){
d18b1bbd 129 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
130 if ( is_readable($processorfile) ) {
eb5334ff 131 include_once( $processorfile );
132
133 $processclass = 'message_output_' . $processor->name;
134 if ( class_exists($processclass) ){
d18b1bbd 135 $pclass = new $processclass();
eb5334ff 136 $pclass->process_form($form, $preferences);
137 } else{
53e242f9 138 print_error('errorcallingprocessor', 'message');
d18b1bbd 139 }
140 }
141 }
305a014f 142
2509c0e9
AD
143 //process general messaging preferences
144 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
145 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
146
fa8f03ef 147 // Save all the new preferences to the database
eb5334ff 148 if (!set_user_preferences( $preferences, $user->id ) ){
149 print_error('cannotupdateusermsgpref');
150 }
151
152 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
153}
154
1d422980 155/// Load preferences
8e803c3f 156$preferences = new stdClass();
fa8f03ef 157$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
305a014f 158
eb5334ff 159/// Get providers preferences
160$providers = message_get_my_providers();
161foreach ( $providers as $providerid => $provider){
162 foreach (array('loggedin', 'loggedoff') as $state){
163 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
164 if ($linepref == ''){
165 continue;
166 }
167 $lineprefarray = explode(',', $linepref);
168 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
169 foreach ($lineprefarray as $pref){
170 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
171 }
3b120e46 172 }
eb5334ff 173}
174
175/// For every processors put its options on the form (need to get function from processor's lib.php)
176$processors = $DB->get_records('message_processors');
1d422980 177foreach ( $processors as $processorid => $processor){
eb5334ff 178 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
179 if ( is_readable($processorfile) ) {
180 include_once( $processorfile );
181 $processclass = 'message_output_' . $processor->name;
182 if ( class_exists($processclass) ){
183 $pclass = new $processclass();
184 $pclass->load_data($preferences, $user->id);
185 } else{
186 print_error('errorcallingprocessor', 'message');
187 }
3b120e46 188 }
eb5334ff 189}
190
2509c0e9
AD
191//load general messaging preferences
192$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
193//$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
194
eb5334ff 195/// Display page header
196$streditmymessage = get_string('editmymessage', 'message');
197$strparticipants = get_string('participants');
198$userfullname = fullname($user, true);
199
eb5334ff 200$PAGE->set_title("$course->shortname: $streditmymessage");
201if ($course->id != SITEID) {
202 $PAGE->set_heading("$course->fullname: $streditmymessage");
203} else {
204 $PAGE->set_heading($course->fullname);
205}
206echo $OUTPUT->header();
3b120e46 207
fa8f03ef
AD
208// Start the form. We're not using mform here because of our special formatting needs ...
209echo '<form class="mform" method="post" action="'.$PAGE->url.'">';
305a014f 210
211/// Settings table...
eb5334ff 212echo '<fieldset id="providers" class="clearfix">';
213echo '<legend class="ftoggler">'.get_string('providers_config', 'message').'</legend>';
214$providers = message_get_my_providers();
a813a748 215$processors = $DB->get_records('message_processors', null, 'name DESC');
eb5334ff 216$number_procs = count($processors);
217echo '<table cellpadding="2"><tr><td>&nbsp;</td>'."\n";
218foreach ( $processors as $processorid => $processor){
be8e0d8a 219 echo '<th align="center">'.get_string('pluginname', 'message_'.$processor->name).'</th>';
eb5334ff 220}
221echo '</tr>';
222
223foreach ( $providers as $providerid => $provider){
224 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
225
cb616be8 226 echo '<tr><th align="right">'.$providername.'</th><td colspan="'.$number_procs.'"></td></tr>'."\n";
eb5334ff 227 foreach (array('loggedin', 'loggedoff') as $state){
a93cdf85 228 $state_res = get_string($state.'description', 'message');
eb5334ff 229 echo '<tr><td align="right">'.$state_res.'</td>'."\n";
230 foreach ( $processors as $processorid => $processor) {
231 if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state})) {
232 $checked = '';
233 } else if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name])) {
234 $checked = '';
235 } else {
236 $checked = $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name]==1?" checked=\"checked\"":"";
838a8eb1 237 }
eb5334ff 238 echo '<td align="center"><input type="checkbox" name="'.$provider->component.'_'.$provider->name.'_'.$state.'['.$processor->name.']" '.$checked.' /></td>'."\n";
d18b1bbd 239 }
eb5334ff 240 echo '</tr>'."\n";
d18b1bbd 241 }
eb5334ff 242}
243echo '</table>';
244echo '</fieldset>';
305a014f 245
246/// Show all the message processors
eb5334ff 247$processors = $DB->get_records('message_processors');
305a014f 248
a813a748 249$processorconfigform = null;
eb5334ff 250foreach ($processors as $processorid => $processor) {
251 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
252 if (is_readable($processorfile)) {
253 include_once($processorfile);
254 $processclass = 'message_output_' . $processor->name;
305a014f 255
eb5334ff 256 if (class_exists($processclass)) {
257 $pclass = new $processclass();
a813a748
AD
258 $processorconfigform = $pclass->config_form($preferences);
259
260 if (!empty($processorconfigform)) {
eb5334ff 261 echo '<fieldset id="messageprocessor_'.$processor->name.'" class="clearfix">';
be8e0d8a 262 echo '<legend class="ftoggler">'.get_string('pluginname', 'message_'.$processor->name).'</legend>';
305a014f 263
a813a748 264 echo $processorconfigform;
305a014f 265
eb5334ff 266 echo '</fieldset>';
a813a748 267 }
eb5334ff 268 } else{
269 print_error('errorcallingprocessor', 'message');
d18b1bbd 270 }
271 }
eb5334ff 272}
14d70d26 273
a813a748
AD
274echo '<fieldset id="messageprocessor_general" class="clearfix">';
275echo '<legend class="ftoggler">'.get_string('generalsettings','admin').'</legend>';
7610341a
AD
276echo get_string('blocknoncontacts', 'message').': <input type="checkbox" name="blocknoncontacts" '.($preferences->blocknoncontacts==1?' checked="checked"':'');
277//get_string('beepnewmessage', 'message').': <input type="checkbox" name="beepnewmessage" '.($preferences->beepnewmessage==1?" checked=\"checked\"":"").' />';
a813a748
AD
278echo '</fieldset>';
279
eb5334ff 280echo '<div><input type="hidden" name="sesskey" value="'.sesskey().'" /></div>';
281echo '<div style="text-align:center"><input name="submit" value="'. get_string('updatemyprofile') .'" type="submit" /></div>';
305a014f 282
eb5334ff 283echo "</form>";
d18b1bbd 284
eb5334ff 285echo $OUTPUT->footer();
3b120e46 286