2009-09-29 03:54:14 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2008-07-24 08:38:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit user message preferences
|
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @package core_message
|
|
|
|
* @copyright 2008 Luis Rodrigues and Martin Dougiamas
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-07-24 08:38:03 +00:00
|
|
|
*/
|
|
|
|
|
2011-05-31 15:16:17 +01:00
|
|
|
require_once(dirname(__FILE__) . '/../config.php');
|
|
|
|
require_once($CFG->dirroot . '/message/lib.php');
|
2015-04-08 13:07:46 +08:00
|
|
|
require_once($CFG->dirroot . '/user/lib.php');
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2015-01-19 13:16:48 +08:00
|
|
|
$userid = optional_param('id', 0, PARAM_INT); // User id.
|
2011-08-16 14:06:11 +08:00
|
|
|
$disableall = optional_param('disableall', 0, PARAM_BOOL); //disable all of this user's notifications
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2015-01-19 13:16:48 +08:00
|
|
|
if (!$userid) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/message/edit.php');
|
2012-03-08 14:45:45 +07:00
|
|
|
$url->param('id', $userid);
|
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
$PAGE->set_url($url);
|
2012-10-01 11:00:48 +08:00
|
|
|
$PAGE->set_popup_notification_allowed(false); // We are within the messaging system so don't show message popups
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2012-10-22 16:46:39 +08:00
|
|
|
require_login();
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
if (isguestuser()) {
|
|
|
|
print_error('guestnoeditmessage', 'message');
|
|
|
|
}
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
if (!$user = $DB->get_record('user', array('id' => $userid))) {
|
|
|
|
print_error('invaliduserid');
|
|
|
|
}
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2012-08-02 11:20:48 +08:00
|
|
|
$systemcontext = context_system::instance();
|
|
|
|
$personalcontext = context_user::instance($user->id);
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2010-07-13 02:35:31 +00:00
|
|
|
$PAGE->set_context($personalcontext);
|
2013-10-19 12:22:12 +02:00
|
|
|
$PAGE->set_pagelayout('admin');
|
2011-08-16 14:06:11 +08:00
|
|
|
$PAGE->requires->js_init_call('M.core_message.init_editsettings');
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
// check access control
|
|
|
|
if ($user->id == $USER->id) {
|
|
|
|
//editing own message profile
|
|
|
|
require_capability('moodle/user:editownmessageprofile', $systemcontext);
|
|
|
|
} else {
|
|
|
|
// teachers, parents, etc.
|
|
|
|
require_capability('moodle/user:editmessageprofile', $personalcontext);
|
|
|
|
// no editing of guest user account
|
|
|
|
if (isguestuser($user->id)) {
|
|
|
|
print_error('guestnoeditmessageother', 'message');
|
|
|
|
}
|
2010-08-12 09:44:28 +00:00
|
|
|
// no editing of admins by non admins!
|
2010-09-17 19:35:21 +00:00
|
|
|
if (is_siteadmin($user) and !is_siteadmin($USER)) {
|
2010-08-12 09:44:28 +00:00
|
|
|
print_error('useradmineditadmin');
|
2008-07-24 08:38:03 +00:00
|
|
|
}
|
2015-03-19 10:59:27 +08:00
|
|
|
$PAGE->navbar->includesettingsbase = true;
|
2010-07-13 02:35:31 +00:00
|
|
|
$PAGE->navigation->extend_for_user($user);
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2011-05-28 08:46:47 +01:00
|
|
|
// Fetch message providers
|
|
|
|
$providers = message_get_providers_for_user($user->id);
|
|
|
|
|
2010-09-17 19:41:02 +00:00
|
|
|
/// Save new preferences if data was submitted
|
2008-09-26 02:43:39 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
if (($form = data_submitted()) && confirm_sesskey()) {
|
|
|
|
$preferences = array();
|
2008-09-26 02:43:39 +00:00
|
|
|
|
2011-08-16 14:06:11 +08:00
|
|
|
//only update the user's "emailstop" if its actually changed
|
|
|
|
if ( $user->emailstop != $disableall ) {
|
|
|
|
$user->emailstop = $disableall;
|
|
|
|
$DB->set_field('user', 'emailstop', $user->emailstop, array("id"=>$user->id));
|
|
|
|
}
|
|
|
|
|
2011-12-01 16:54:48 +08:00
|
|
|
// Turning on emailstop disables the preference checkboxes in the browser.
|
|
|
|
// Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked.
|
|
|
|
// Only alter the messaging preferences if emailstop is turned off
|
|
|
|
if (!$user->emailstop) {
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
$componentproviderbase = $provider->component.'_'.$provider->name;
|
|
|
|
foreach (array('loggedin', 'loggedoff') as $state) {
|
|
|
|
$linepref = '';
|
|
|
|
$componentproviderstate = $componentproviderbase.'_'.$state;
|
|
|
|
if (array_key_exists($componentproviderstate, $form)) {
|
|
|
|
foreach (array_keys($form->{$componentproviderstate}) as $process) {
|
|
|
|
if ($linepref == ''){
|
|
|
|
$linepref = $process;
|
|
|
|
} else {
|
|
|
|
$linepref .= ','.$process;
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
2011-12-01 16:54:48 +08:00
|
|
|
if (empty($linepref)) {
|
|
|
|
$linepref = 'none';
|
|
|
|
}
|
|
|
|
$preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
|
2008-08-29 23:19:54 +00:00
|
|
|
}
|
2010-10-25 09:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 03:05:44 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
/// Set all the processor options as well
|
2011-05-17 10:47:21 +01:00
|
|
|
$processors = get_message_processors(true);
|
|
|
|
foreach ($processors as $processor) {
|
|
|
|
$processor->object->process_form($form, $preferences);
|
2008-08-29 23:19:54 +00:00
|
|
|
}
|
2008-09-26 02:43:39 +00:00
|
|
|
|
2010-10-26 02:39:23 +00:00
|
|
|
//process general messaging preferences
|
2011-05-17 10:47:21 +01:00
|
|
|
$preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
|
2013-02-22 10:37:57 +08:00
|
|
|
$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;
|
2010-10-26 02:39:23 +00:00
|
|
|
|
2010-11-18 05:28:51 +00:00
|
|
|
// Save all the new preferences to the database
|
2011-05-17 10:47:21 +01:00
|
|
|
if (!set_user_preferences($preferences, $user->id)) {
|
2009-09-29 03:54:14 +00:00
|
|
|
print_error('cannotupdateusermsgpref');
|
|
|
|
}
|
|
|
|
|
2015-04-09 10:24:32 +08:00
|
|
|
if (isset($form->mailformat)) {
|
|
|
|
$user->mailformat = clean_param($form->mailformat, PARAM_INT);
|
|
|
|
}
|
2015-04-08 13:07:46 +08:00
|
|
|
user_update_user($user, false, false);
|
|
|
|
|
2015-04-22 09:53:35 +05:30
|
|
|
$redirect = new moodle_url("/user/preferences.php", array('userid' => $userid));
|
|
|
|
redirect($redirect);
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
|
|
|
|
2009-11-01 12:22:45 +00:00
|
|
|
/// Load preferences
|
2010-09-21 08:18:23 +00:00
|
|
|
$preferences = new stdClass();
|
2010-11-18 05:28:51 +00:00
|
|
|
$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
|
2008-09-26 02:43:39 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
/// Get providers preferences
|
2011-05-17 10:47:21 +01:00
|
|
|
foreach ($providers as $provider) {
|
|
|
|
foreach (array('loggedin', 'loggedoff') as $state) {
|
2009-09-29 03:54:14 +00:00
|
|
|
$linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $user->id);
|
|
|
|
if ($linepref == ''){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$lineprefarray = explode(',', $linepref);
|
|
|
|
$preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
|
2011-05-17 10:47:21 +01:00
|
|
|
foreach ($lineprefarray as $pref) {
|
2009-09-29 03:54:14 +00:00
|
|
|
$preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
|
|
|
|
}
|
2008-07-24 08:38:03 +00:00
|
|
|
}
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
|
|
|
|
2011-05-17 10:47:21 +01:00
|
|
|
// Load all processors
|
|
|
|
$processors = get_message_processors();
|
2009-09-29 03:54:14 +00:00
|
|
|
/// For every processors put its options on the form (need to get function from processor's lib.php)
|
2011-05-17 10:47:21 +01:00
|
|
|
foreach ($processors as $processor) {
|
|
|
|
$processor->object->load_data($preferences, $user->id);
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
|
|
|
|
2010-10-26 02:39:23 +00:00
|
|
|
//load general messaging preferences
|
|
|
|
$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
|
2013-02-22 10:37:57 +08:00
|
|
|
$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
|
2015-04-08 13:07:46 +08:00
|
|
|
$preferences->mailformat = $user->mailformat;
|
|
|
|
$preferences->mailcharset = get_user_preferences( 'mailcharset', '', $user->id);
|
2010-10-26 02:39:23 +00:00
|
|
|
|
2009-09-29 03:54:14 +00:00
|
|
|
/// Display page header
|
2012-11-01 09:42:11 +08:00
|
|
|
$strmessaging = get_string('messaging', 'message');
|
|
|
|
$PAGE->set_title($strmessaging);
|
2015-04-08 13:07:46 +08:00
|
|
|
$PAGE->set_heading(fullname($user));
|
2008-07-24 08:38:03 +00:00
|
|
|
|
2011-05-17 10:47:21 +01:00
|
|
|
// Grab the renderer
|
|
|
|
$renderer = $PAGE->get_renderer('core', 'message');
|
|
|
|
// Fetch default (site) preferences
|
|
|
|
$defaultpreferences = get_message_output_default_preferences();
|
2009-09-29 03:54:14 +00:00
|
|
|
|
2015-04-22 09:53:35 +05:30
|
|
|
$messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences,
|
|
|
|
$user->emailstop, $user->id);
|
2008-08-29 23:19:54 +00:00
|
|
|
|
2011-05-17 10:47:21 +01:00
|
|
|
echo $OUTPUT->header();
|
|
|
|
echo $messagingoptions;
|
2009-09-29 03:54:14 +00:00
|
|
|
echo $OUTPUT->footer();
|
2008-07-24 08:38:03 +00:00
|
|
|
|