Merge branch 'wip-mdl-30375-m20' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / message / edit.php
blob6f418850754f455700e6d421590250bdc409d0cd
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)
30 $disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
32 $url = new moodle_url('/message/edit.php');
33 if ($userid !== $USER->id) {
34 $url->param('id', $userid);
36 if ($course != SITEID) {
37 $url->param('course', $course);
39 $PAGE->set_url($url);
41 if (!$course = $DB->get_record('course', array('id' => $course))) {
42 print_error('invalidcourseid');
45 if ($course->id != SITEID) {
46 require_login($course);
48 } else {
49 if (!isloggedin()) {
50 if (empty($SESSION->wantsurl)) {
51 $SESSION->wantsurl = $CFG->httpswwwroot.'/message/edit.php';
53 redirect(get_login_url());
57 if (isguestuser()) {
58 print_error('guestnoeditmessage', 'message');
61 if (!$user = $DB->get_record('user', array('id' => $userid))) {
62 print_error('invaliduserid');
65 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
66 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
67 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
69 $PAGE->set_context($personalcontext);
70 $PAGE->set_pagelayout('course');
71 $PAGE->requires->js_init_call('M.core_message.init_editsettings');
73 // check access control
74 if ($user->id == $USER->id) {
75 //editing own message profile
76 require_capability('moodle/user:editownmessageprofile', $systemcontext);
77 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
78 $node->make_active();
79 $PAGE->navbar->includesettingsbase = true;
81 } else {
82 // teachers, parents, etc.
83 require_capability('moodle/user:editmessageprofile', $personalcontext);
84 // no editing of guest user account
85 if (isguestuser($user->id)) {
86 print_error('guestnoeditmessageother', 'message');
88 // no editing of admins by non admins!
89 if (is_siteadmin($user) and !is_siteadmin($USER)) {
90 print_error('useradmineditadmin');
92 $PAGE->navigation->extend_for_user($user);
95 // Fetch message providers
96 $providers = message_get_providers_for_user($user->id);
98 /// Save new preferences if data was submitted
100 if (($form = data_submitted()) && confirm_sesskey()) {
101 //only update the user's "emailstop" if its actually changed
102 if ( $user->emailstop != $disableall ) {
103 $user->emailstop = $disableall;
104 $DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
107 $preferences = array();
109 $possiblestates = array('loggedin', 'loggedoff');
110 foreach ( $providers as $providerid => $provider){
111 foreach ($possiblestates as $state){
112 $linepref = '';
113 $componentproviderstate = $provider->component.'_'.$provider->name.'_'.$state;
114 if (array_key_exists($componentproviderstate, $form)) {
115 foreach ($form->{$componentproviderstate} as $process=>$one){
116 if ($linepref == ''){
117 $linepref = $process;
118 } else {
119 $linepref .= ','.$process;
123 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
126 foreach ( $providers as $providerid => $provider){
127 foreach ($possiblestates as $state){
128 $preferencekey = 'message_provider_'.$provider->component.'_'.$provider->name.'_'.$state;
129 if (empty($preferences[$preferencekey])) {
130 $preferences[$preferencekey] = 'none';
135 /// Set all the processor options as well
136 $processors = $DB->get_records('message_processors');
137 foreach ( $processors as $processorid => $processor){
138 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
139 if ( is_readable($processorfile) ) {
140 include_once( $processorfile );
142 $processclass = 'message_output_' . $processor->name;
143 if ( class_exists($processclass) ){
144 $pclass = new $processclass();
145 $pclass->process_form($form, $preferences);
146 } else{
147 print_error('errorcallingprocessor', 'message');
152 //process general messaging preferences
153 $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
154 //$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
156 // Save all the new preferences to the database
157 if (!set_user_preferences( $preferences, $user->id ) ){
158 print_error('cannotupdateusermsgpref');
161 redirect("$CFG->wwwroot/message/edit.php?id=$user->id&course=$course->id");
164 /// Load preferences
165 $preferences = new stdClass();
166 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
168 foreach ( $providers as $providerid => $provider){
169 foreach (array('loggedin', 'loggedoff') as $state){
170 $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
171 if ($linepref == ''){
172 continue;
174 $lineprefarray = explode(',', $linepref);
175 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
176 foreach ($lineprefarray as $pref){
177 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
182 /// For every processors put its options on the form (need to get function from processor's lib.php)
183 $processors = $DB->get_records('message_processors');
184 foreach ( $processors as $processorid => $processor){
185 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
186 if ( is_readable($processorfile) ) {
187 include_once( $processorfile );
188 $processclass = 'message_output_' . $processor->name;
189 if ( class_exists($processclass) ){
190 $pclass = new $processclass();
191 $pclass->load_data($preferences, $user->id);
192 } else{
193 print_error('errorcallingprocessor', 'message');
198 //load general messaging preferences
199 $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
200 //$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
202 /// Display page header
203 $streditmymessage = get_string('editmymessage', 'message');
204 $strparticipants = get_string('participants');
205 $userfullname = fullname($user, true);
207 $PAGE->set_title("$course->shortname: $streditmymessage");
208 if ($course->id != SITEID) {
209 $PAGE->set_heading("$course->fullname: $streditmymessage");
210 } else {
211 $PAGE->set_heading($course->fullname);
213 echo $OUTPUT->header();
215 // Start the form. We're not using mform here because of our special formatting needs ...
216 echo '<form class="mform" method="post" action="'.$PAGE->url.'">';
218 /// Settings table...
219 echo '<fieldset id="providers" class="clearfix">';
220 echo '<legend class="ftoggler">'.get_string('providers_config', 'message').'</legend>';
221 $processors = $DB->get_records('message_processors', null, 'name DESC');
222 $number_procs = count($processors);
223 echo '<table cellpadding="2"><tr><td>&nbsp;</td>'."\n";
224 foreach ( $processors as $processorid => $processor){
225 echo '<th align="center">'.get_string('pluginname', 'message_'.$processor->name).'</th>';
227 echo '</tr>';
229 foreach ( $providers as $providerid => $provider){
230 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
232 echo '<tr><th align="right">'.$providername.'</th><td colspan="'.$number_procs.'"></td></tr>'."\n";
233 $disabled = '';
234 if ($user->emailstop) {
235 $disabled = 'disabled="disabled"';
237 foreach (array('loggedin', 'loggedoff') as $state){
238 $state_res = get_string($state.'description', 'message');
239 echo '<tr><td align="right">'.$state_res.'</td>'."\n";
240 foreach ( $processors as $processorid => $processor) {
241 if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state})) {
242 $checked = '';
243 } else if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name])) {
244 $checked = '';
245 } else {
246 $checked = $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name]==1?" checked=\"checked\"":"";
248 echo '<td align="center"><input type="checkbox" '.$disabled.' class="notificationpreference" name="'.$provider->component.'_'.$provider->name.'_'.$state.'['.$processor->name.']" '.$checked.' /></td>'."\n";
250 echo '</tr>'."\n";
253 echo '</table>';
255 $disableallcheckbox = $OUTPUT->help_icon('disableall', 'message') . get_string('disableall', 'message') . html_writer::checkbox('disableall', 1, $user->emailstop, '', array('class'=>'disableallcheckbox'));
256 echo html_writer::nonempty_tag('div', $disableallcheckbox, array('class'=>'disableall'));
258 echo '</fieldset>';
260 /// Show all the message processors
261 $processors = $DB->get_records('message_processors');
263 $processorconfigform = null;
264 foreach ($processors as $processorid => $processor) {
265 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
266 if (is_readable($processorfile)) {
267 include_once($processorfile);
268 $processclass = 'message_output_' . $processor->name;
270 if (class_exists($processclass)) {
271 $pclass = new $processclass();
272 $processorconfigform = $pclass->config_form($preferences);
274 if (!empty($processorconfigform)) {
275 echo '<fieldset id="messageprocessor_'.$processor->name.'" class="clearfix">';
276 echo '<legend class="ftoggler">'.get_string('pluginname', 'message_'.$processor->name).'</legend>';
278 echo $processorconfigform;
280 echo '</fieldset>';
282 } else{
283 print_error('errorcallingprocessor', 'message');
288 echo '<fieldset id="messageprocessor_general" class="clearfix">';
289 echo '<legend class="ftoggler">'.get_string('generalsettings','admin').'</legend>';
290 echo get_string('blocknoncontacts', 'message').': <input type="checkbox" name="blocknoncontacts" '.($preferences->blocknoncontacts==1?' checked="checked"':'');
291 //get_string('beepnewmessage', 'message').': <input type="checkbox" name="beepnewmessage" '.($preferences->beepnewmessage==1?" checked=\"checked\"":"").' />';
292 echo '</fieldset>';
294 echo '<div><input type="hidden" name="sesskey" value="'.sesskey().'" /></div>';
295 echo '<div style="text-align:center"><input name="submit" value="'. get_string('updatemyprofile') .'" type="submit" /></div>';
297 echo "</form>";
299 echo $OUTPUT->footer();