MDL-10107 - First check-in of Messaging 2.0 code from Luis Rodrigues, GSOC student.
[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');
38
39
40httpsrequired();
41
42$userid = optional_param('id', $USER->id, PARAM_INT); // user id
43$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
44
45if (!$course = $DB->get_record('course', array('id' => $course))) {
46 error('Course ID was incorrect');
47}
48
49if ($course->id != SITEID) {
50 require_login($course);
51} else {
52 if (!isloggedin()) {
53 if (empty($SESSION->wantsurl)) {
54 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
55 }
56 redirect($CFG->httpswwwroot.'/login/index.php');
57 }
58}
59
60if (isguestuser()) {
61 print_error('guestnoeditmessage', 'message');
62}
63
64if (!$user = $DB->get_record('user', array('id' => $userid))) {
65 error('User ID was incorrect');
66}
67
68$systemcontext = get_context_instance(CONTEXT_SYSTEM);
69$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
70
71
72// check access control
73if ($user->id == $USER->id) {
74 //editing own message profile
75 require_capability('moodle/user:editownmessageprofile', $systemcontext);
76
77} else {
78 // teachers, parents, etc.
79 require_capability('moodle/user:editmessageprofile', $personalcontext);
80 // no editing of guest user account
81 if (isguestuser($user->id)) {
82 print_error('guestnoeditmessageother', 'message');
83 }
84 // no editing of primary admin!
85 $mainadmin = get_admin();
86 if ($user->id == $mainadmin->id) {
87 print_error('adminprimarynoedit');
88 }
89}
90
91
92//retrieve preferences from db
93$preferences = new object();
94$preferences->id = $user->id;
95
96//get the message general preferences
97$preferences->showmessagewindow = get_user_preferences( 'message_showmessagewindow', '', $user->id);
98$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
99$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
100$preferences->noframesjs = get_user_preferences( 'message_noframesjs', '', $user->id);
101
102//for every message provider get preferences for the form
103$providers = $DB->get_records('message_providers');
104foreach ( $providers as $providerid => $provider){
105 foreach (array('loggedin', 'loggedoff') as $state){
106 $linepref = get_user_preferences('message_provider_'.$provider->modulename.'_'.$state, '', $user->id);
107 if ($linepref == ''){
108 continue;
109 }
110 $lineprefarray = explode(',', $linepref);
111 $preferences->{$provider->modulename.'_'.$state} = array();
112 foreach ($lineprefarray as $pref){
113 $preferences->{$provider->modulename.'_'.$state}[$provider->modulename.'_'.$state.'_'.$pref] = 1;
114 }
115 }
116}
117
118//for every processors put its options on the form (need to get funcion from processor's lib.php)
119$processors = $DB->get_records('message_processors');
120foreach ( $processors as $processorid => $processor){
121 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
122 if ( is_readable($processorfile) ) {
123 include_once( $processorfile );
124 $processclass = 'message_output_' . $processor->name;
125 if ( class_exists($processclass) ){
126 $pclass = new $processclass();
127 $pclass->load_data($preferences, $user->id);
128 } else{
129 error('Error calling defined processor');
130 }
131 }
132}
133
134
135//create form
136$userform = new user_edit_form();
137$userform->set_data($preferences);
138
139if ($messageconf = $userform->get_data()) {
140
141 add_to_log($course->id, 'message', 'update', "edit.php?id=$user->id&course=$course->id", '');
142
143 $preferences = array();
144
145 //get the list of normal preferences
146 $preferences['message_showmessagewindow'] = $messageconf->showmessagewindow?1:0;
147 $preferences['message_blocknoncontacts'] = $messageconf->blocknoncontacts?1:0;
148 $preferences['message_beepnewmessage'] = $messageconf->beepnewmessage?1:0;
149 $preferences['message_noframesjs'] = $messageconf->noframesjs?1:0;
150
151 //get a listing of all the message processors and process the form
152 $providers = $DB->get_records('message_providers');
153 foreach ( $providers as $providerid => $provider){
154 foreach (array('loggedin', 'loggedoff') as $state){
155 $linepref = '';
156 foreach ($messageconf->{$provider->modulename.'_'.$state} as $process=>$one){
157 $parray = explode( '_', $process);
158 if ($linepref == ''){
159 $linepref = $parray[2];
160 } else {
161 $linepref .= ','.$parray[2];
162 }
163 }
164 $preferences[ 'message_provider_'.$provider->modulename.'_'.$state ] = $linepref;
165 }
166 }
167
168 //list all the processors options (need to get funcion from processor's lib.php)
169 $processors = $DB->get_records('message_processors');
170 foreach ( $processors as $processorid => $processor){
171 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/lib.php';
172 if ( is_readable($processorfile) ) {
173 include_once( $processorfile );
174
175 $processclass = 'message_output_' . $processor->name;
176 if ( class_exists($processclass) ){
177 $pclass = new $processclass();
178 $pclass->process_form($messageconf, $preferences);
179 } else{
180 error('Error calling defined processor');
181 }
182 }
183 }
184
185 //set the user preferences
186 if (!set_user_preferences( $preferences, $user->id ) ){
187 error('Error updating user message preferences');
188 }
189
190 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
191}
192
193
194/// Display page header
195$streditmymessage = get_string('editmymessage', 'message');
196$strparticipants = get_string('participants');
197$userfullname = fullname($user, true);
198if ($course->id != SITEID) {
199 print_header("$course->shortname: $streditmymessage", "$course->fullname: $streditmessage",
200 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
201 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
202 -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
203 -> $streditmymessage", "");
204} else {
205 print_header("$course->shortname: $streditmymessage", $course->fullname,
206 "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
207 -> $streditmymessage", "");
208}
209/// Print tabs at the top
210$showroles = 1;
211$currenttab = 'editmessage';
212require('../user/tabs.php');
213
214notify('WARNING: This interface is still under construction!');
215
216/// Finally display THE form
217$userform->display();
218
219/// and proper footer
220print_footer($course);
221
222?>