MDL-10107 Fixed the definition of message providers ... it now works through files...
[moodle.git] / message / edit.php
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
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  */
35 require_once('../config.php');
37 require_once($CFG->dirroot.'/message/edit_form.php');
38 require_once($CFG->libdir.'/messagelib.php');
41 httpsrequired();
43 $userid = optional_param('id', $USER->id, PARAM_INT);    // user id
44 $course = optional_param('course', SITEID, PARAM_INT);   // course id (defaults to Site)
46 if (!$course = $DB->get_record('course', array('id' => $course))) {
47     error('Course ID was incorrect');
48 }
50 if ($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 }
61 if (isguestuser()) {
62     print_error('guestnoeditmessage', 'message');
63 }
65 if (!$user = $DB->get_record('user', array('id' => $userid))) {
66     error('User ID was incorrect');
67 }
69 $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
70 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
71 $coursecontext   = get_context_instance(CONTEXT_COURSE, $course->id);
74 // check access control
75 if ($user->id == $USER->id) {
76     //editing own message profile
77     require_capability('moodle/user:editownmessageprofile', $systemcontext);
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 }
94 //retrieve preferences from db
95 $preferences = new object();
96 $preferences->id = $user->id;
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);
104 // Get all the known providers
105 $providers = message_get_my_providers();
107 foreach ( $providers as $providerid => $provider){
108     foreach (array('loggedin', 'loggedoff') as $state){
109         $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$state, '', $user->id);
110         if ($linepref == ''){ 
111             continue;
112         }
113         $lineprefarray = explode(',', $linepref);
114         $preferences->{$provider->component.'_'.$state} = array();
115         foreach ($lineprefarray as $pref){
116             $preferences->{$provider->component.'_'.$state}[$provider->component.'_'.$state.'_'.$pref] = 1;
117         }
118     }
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');
123 foreach ( $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     }
138 //create form
139 $userform = new user_edit_form();
140 $userform->set_data($preferences);
142 if ($messageconf = $userform->get_data()) {
144     add_to_log($course->id, 'message', 'update', "edit.php?id=$user->id&course=$course->id", '');
146     $preferences = array();
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;
154     //get a listing of all the message providers and process the form
155     foreach ( $providers as $providerid => $provider){
156         foreach (array('loggedin', 'loggedoff') as $state){
157             $linepref = '';
158             foreach ($messageconf->{$provider->component.'_'.$state} as $process=>$one){
159                 $parray = explode( '_', $process);
160                 if ($linepref == ''){ 
161                     $linepref = $parray[2];
162                 } else { 
163                     $linepref .= ','.$parray[2];
164                 }
165             }
166             $preferences[ 'message_provider_'.$provider->component.'_'.$state  ] = $linepref;
167         }
168     }
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     }
187     //set the user preferences
188     if (!set_user_preferences( $preferences, $user->id ) ){
189         error('Error updating user message preferences');
190     }
192     redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
196 /// Display page header
197 $streditmymessage = get_string('editmymessage', 'message');
198 $strparticipants  = get_string('participants');
199 $userfullname     = fullname($user, true);
201 $navlinks = array();
202 if (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');
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);
212 if ($course->id != SITEID) {
213     print_header("$course->shortname: $streditmymessage", "$course->fullname: $streditmymessage", $navigation);
214 } else {
215     print_header("$course->shortname: $streditmymessage", $course->fullname, $navigation);
217 /// Print tabs at the top
218 $showroles = 1;
219 $currenttab = 'editmessage';
220 require('../user/tabs.php');
222 notify('WARNING: This interface is still under construction!');
224 /// Finally display THE form
225 $userform->display();
227 /// and proper footer
228 print_footer($course);
230 ?>