Merge branch 'MDL-26724' of git://github.com/timhunt/moodle
[moodle.git] / message / edit.php
blobca66db93562dd2e9551843084f4ff137473e445b
1 <?php
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.
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/>.
18 /**
19 * Edit user message preferences
21 * @author Luis Rodrigues and Martin Dougiamas
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package message
26 require_once('../config.php');
28 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
29 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
31 $url = new moodle_url('/message/edit.php');
32 if ($userid !== $USER->id) {
33 $url->param('id', $userid);
35 if ($course != SITEID) {
36 $url->param('course', $course);
38 $PAGE->set_url($url);
40 if (!$course = $DB->get_record('course', array('id' => $course))) {
41 print_error('invalidcourseid');
44 if ($course->id != SITEID) {
45 require_login($course);
47 } else {
48 if (!isloggedin()) {
49 if (empty($SESSION->wantsurl)) {
50 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
52 redirect(get_login_url());
56 if (isguestuser()) {
57 print_error('guestnoeditmessage', 'message');
60 if (!$user = $DB->get_record('user', array('id' => $userid))) {
61 print_error('invaliduserid');
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);
68 $PAGE->set_context($personalcontext);
69 $PAGE->set_pagelayout('course');
71 // check access control
72 if ($user->id == $USER->id) {
73 //editing own message profile
74 require_capability('moodle/user:editownmessageprofile', $systemcontext);
75 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
76 $node->make_active();
77 $PAGE->navbar->includesettingsbase = true;
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');
86 // no editing of admins by non admins!
87 if (is_siteadmin($user) and !is_siteadmin($USER)) {
88 print_error('useradmineditadmin');
90 $PAGE->navigation->extend_for_user($user);
93 /// Save new preferences if data was submitted
95 if (($form = data_submitted()) && confirm_sesskey()) {
96 $preferences = array();
98 /// Set all the preferences for all the message providers
99 $providers = message_get_my_providers();
100 $possiblestates = array('loggedin', 'loggedoff');
101 foreach ( $providers as $providerid => $provider){
102 foreach ($possiblestates as $state){
103 $linepref = '';
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;
114 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
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';
126 /// Set all the processor options as well
127 $processors = $DB->get_records('message_processors');
128 foreach ( $processors as $processorid => $processor){
129 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
130 if ( is_readable($processorfile) ) {
131 include_once( $processorfile );
133 $processclass = 'message_output_' . $processor->name;
134 if ( class_exists($processclass) ){
135 $pclass = new $processclass();
136 $pclass->process_form($form, $preferences);
137 } else{
138 print_error('errorcallingprocessor', 'message');
143 //process general messaging preferences
144 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
145 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
147 // Save all the new preferences to the database
148 if (!set_user_preferences( $preferences, $user->id ) ){
149 print_error('cannotupdateusermsgpref');
152 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
155 /// Load preferences
156 $preferences = new stdClass();
157 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
159 /// Get providers preferences
160 $providers = message_get_my_providers();
161 foreach ( $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;
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;
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');
177 foreach ( $processors as $processorid => $processor){
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');
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);
195 /// Display page header
196 $streditmymessage = get_string('editmymessage', 'message');
197 $strparticipants = get_string('participants');
198 $userfullname = fullname($user, true);
200 $PAGE->set_title("$course->shortname: $streditmymessage");
201 if ($course->id != SITEID) {
202 $PAGE->set_heading("$course->fullname: $streditmymessage");
203 } else {
204 $PAGE->set_heading($course->fullname);
206 echo $OUTPUT->header();
208 // Start the form. We're not using mform here because of our special formatting needs ...
209 echo '<form class="mform" method="post" action="'.$PAGE->url.'">';
211 /// Settings table...
212 echo '<fieldset id="providers" class="clearfix">';
213 echo '<legend class="ftoggler">'.get_string('providers_config', 'message').'</legend>';
214 $providers = message_get_my_providers();
215 $processors = $DB->get_records('message_processors', null, 'name DESC');
216 $number_procs = count($processors);
217 echo '<table cellpadding="2"><tr><td>&nbsp;</td>'."\n";
218 foreach ( $processors as $processorid => $processor){
219 echo '<th align="center">'.get_string('pluginname', 'message_'.$processor->name).'</th>';
221 echo '</tr>';
223 foreach ( $providers as $providerid => $provider){
224 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
226 echo '<tr><th align="right">'.$providername.'</th><td colspan="'.$number_procs.'"></td></tr>'."\n";
227 foreach (array('loggedin', 'loggedoff') as $state){
228 $state_res = get_string($state.'description', 'message');
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\"":"";
238 echo '<td align="center"><input type="checkbox" name="'.$provider->component.'_'.$provider->name.'_'.$state.'['.$processor->name.']" '.$checked.' /></td>'."\n";
240 echo '</tr>'."\n";
243 echo '</table>';
244 echo '</fieldset>';
246 /// Show all the message processors
247 $processors = $DB->get_records('message_processors');
249 $processorconfigform = null;
250 foreach ($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;
256 if (class_exists($processclass)) {
257 $pclass = new $processclass();
258 $processorconfigform = $pclass->config_form($preferences);
260 if (!empty($processorconfigform)) {
261 echo '<fieldset id="messageprocessor_'.$processor->name.'" class="clearfix">';
262 echo '<legend class="ftoggler">'.get_string('pluginname', 'message_'.$processor->name).'</legend>';
264 echo $processorconfigform;
266 echo '</fieldset>';
268 } else{
269 print_error('errorcallingprocessor', 'message');
274 echo '<fieldset id="messageprocessor_general" class="clearfix">';
275 echo '<legend class="ftoggler">'.get_string('generalsettings','admin').'</legend>';
276 echo 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\"":"").' />';
278 echo '</fieldset>';
280 echo '<div><input type="hidden" name="sesskey" value="'.sesskey().'" /></div>';
281 echo '<div style="text-align:center"><input name="submit" value="'. get_string('updatemyprofile') .'" type="submit" /></div>';
283 echo "</form>";
285 echo $OUTPUT->footer();