MDL-10107 Fixed the definition of message providers ... it now works through files...
[moodle.git] / message / edit.php
CommitLineData
3b120e46 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * Edit user message preferences
28 *
29 * @author Luis Rodrigues
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package
33 */
34
35require_once('../config.php');
36
37require_once($CFG->dirroot.'/message/edit_form.php');
120b3758 38require_once($CFG->libdir.'/messagelib.php');
3b120e46 39
40
41httpsrequired();
42
43$userid = optional_param('id', $USER->id, PARAM_INT); // user id
44$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
45
46if (!$course = $DB->get_record('course', array('id' => $course))) {
47 error('Course ID was incorrect');
48}
49
50if ($course->id != SITEID) {
51 require_login($course);
52} else {
53 if (!isloggedin()) {
54 if (empty($SESSION->wantsurl)) {
55 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
56 }
57 redirect($CFG->httpswwwroot.'/login/index.php');
58 }
59}
60
61if (isguestuser()) {
62 print_error('guestnoeditmessage', 'message');
63}
64
65if (!$user = $DB->get_record('user', array('id' => $userid))) {
66 error('User ID was incorrect');
67}
68
69$systemcontext = get_context_instance(CONTEXT_SYSTEM);
70$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
539dba3b 71$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
3b120e46 72
73
74// check access control
75if ($user->id == $USER->id) {
76 //editing own message profile
77 require_capability('moodle/user:editownmessageprofile', $systemcontext);
78
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 }
86 // no editing of primary admin!
87 $mainadmin = get_admin();
88 if ($user->id == $mainadmin->id) {
89 print_error('adminprimarynoedit');
90 }
91}
92
93
94//retrieve preferences from db
95$preferences = new object();
96$preferences->id = $user->id;
97
98//get the message general preferences
99$preferences->showmessagewindow = get_user_preferences( 'message_showmessagewindow', '', $user->id);
100$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
101$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
102$preferences->noframesjs = get_user_preferences( 'message_noframesjs', '', $user->id);
103
120b3758 104// Get all the known providers
105$providers = message_get_my_providers();
106
3b120e46 107foreach ( $providers as $providerid => $provider){
108 foreach (array('loggedin', 'loggedoff') as $state){
120b3758 109 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$state, '', $user->id);
3b120e46 110 if ($linepref == ''){
111 continue;
112 }
113 $lineprefarray = explode(',', $linepref);
120b3758 114 $preferences->{$provider->component.'_'.$state} = array();
3b120e46 115 foreach ($lineprefarray as $pref){
120b3758 116 $preferences->{$provider->component.'_'.$state}[$provider->component.'_'.$state.'_'.$pref] = 1;
3b120e46 117 }
118 }
119}
120
121//for every processors put its options on the form (need to get funcion from processor's lib.php)
122$processors = $DB->get_records('message_processors');
123foreach ( $processors as $processorid => $processor){
124 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
125 if ( is_readable($processorfile) ) {
126 include_once( $processorfile );
127 $processclass = 'message_output_' . $processor->name;
128 if ( class_exists($processclass) ){
129 $pclass = new $processclass();
130 $pclass->load_data($preferences, $user->id);
131 } else{
132 error('Error calling defined processor');
133 }
134 }
135}
136
137
138//create form
139$userform = new user_edit_form();
140$userform->set_data($preferences);
141
142if ($messageconf = $userform->get_data()) {
143
144 add_to_log($course->id, 'message', 'update', "edit.php?id=$user->id&course=$course->id", '');
145
146 $preferences = array();
147
148 //get the list of normal preferences
149 $preferences['message_showmessagewindow'] = $messageconf->showmessagewindow?1:0;
150 $preferences['message_blocknoncontacts'] = $messageconf->blocknoncontacts?1:0;
151 $preferences['message_beepnewmessage'] = $messageconf->beepnewmessage?1:0;
152 $preferences['message_noframesjs'] = $messageconf->noframesjs?1:0;
153
120b3758 154 //get a listing of all the message providers and process the form
3b120e46 155 foreach ( $providers as $providerid => $provider){
156 foreach (array('loggedin', 'loggedoff') as $state){
157 $linepref = '';
120b3758 158 foreach ($messageconf->{$provider->component.'_'.$state} as $process=>$one){
3b120e46 159 $parray = explode( '_', $process);
160 if ($linepref == ''){
161 $linepref = $parray[2];
162 } else {
163 $linepref .= ','.$parray[2];
164 }
165 }
120b3758 166 $preferences[ 'message_provider_'.$provider->component.'_'.$state ] = $linepref;
3b120e46 167 }
168 }
169
170 //list all the processors options (need to get funcion from processor's lib.php)
171 $processors = $DB->get_records('message_processors');
172 foreach ( $processors as $processorid => $processor){
173 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/lib.php';
174 if ( is_readable($processorfile) ) {
175 include_once( $processorfile );
176
177 $processclass = 'message_output_' . $processor->name;
178 if ( class_exists($processclass) ){
179 $pclass = new $processclass();
180 $pclass->process_form($messageconf, $preferences);
181 } else{
182 error('Error calling defined processor');
183 }
184 }
185 }
186
187 //set the user preferences
188 if (!set_user_preferences( $preferences, $user->id ) ){
189 error('Error updating user message preferences');
190 }
191
192 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
193}
194
195
196/// Display page header
197$streditmymessage = get_string('editmymessage', 'message');
198$strparticipants = get_string('participants');
199$userfullname = fullname($user, true);
539dba3b 200
201$navlinks = array();
202if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
203 has_capability('moodle/site:viewparticipants', $systemcontext)) {
204 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
205}
206$navlinks[] = array('name' => $userfullname,
207 'link' => "view.php?id=$user->id&amp;course=$course->id",
208 'type' => 'misc');
209$navlinks[] = array('name' => $streditmymessage, 'link' => null, 'type' => 'misc');
210$navigation = build_navigation($navlinks);
211
3b120e46 212if ($course->id != SITEID) {
120b3758 213 print_header("$course->shortname: $streditmymessage", "$course->fullname: $streditmymessage", $navigation);
3b120e46 214} else {
539dba3b 215 print_header("$course->shortname: $streditmymessage", $course->fullname, $navigation);
3b120e46 216}
217/// Print tabs at the top
218$showroles = 1;
219$currenttab = 'editmessage';
220require('../user/tabs.php');
221
222notify('WARNING: This interface is still under construction!');
223
224/// Finally display THE form
225$userform->display();
226
227/// and proper footer
228print_footer($course);
229
230?>