MDL-54698 message: make preferences page ajax save

This commit is contained in:
Ryan Wyllie
2016-10-07 16:26:40 +08:00
committed by Mark Nelson
parent a0e358a64a
commit a0eabdd3c8
15 changed files with 1618 additions and 262 deletions
+272 -159
View File
@@ -216,186 +216,299 @@ class core_message_renderer extends plugin_renderer_base {
}
/**
* Display the interface for messaging options
* Get the base key prefix for the given provider.
*
* @param array $processors Array of objects containing message processors
* @param array $providers Array of objects containing message providers
* @param array $preferences Array of objects containing current preferences
* @param array $defaultpreferences Array of objects containing site default preferences
* @param bool $notificationsdisabled Indicate if the user's "emailstop" flag is set (shouldn't receive any non-forced notifications)
* @param null|int $userid User id, or null if current user.
* @return string The text to render
* @param stdClass message provider
* @return string
*/
public function manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences,
$notificationsdisabled = false, $userid = null) {
global $USER;
if (empty($userid)) {
$userid = $USER->id;
private function get_preference_base($provider) {
return $provider->component.'_'.$provider->name;
}
/**
* Get the display name for the given provider.
*
* @param stdClass $provider message provider
* @return string
*/
private function get_provider_display_name($provider) {
return get_string('messageprovider:'.$provider->name, $provider->component);
}
/**
* Get the preferences for the given user.
*
* @param array $processors list of message processors
* @param array $providers list of message providers
* @param stdClass $user user
* @return stdClass
*/
private function get_all_preferences($processors, $providers, $user) {
$preferences = new stdClass();
$preferences->userdefaultemail = $user->email;//may be displayed by the email processor
/// Get providers preferences
foreach ($providers as $provider) {
foreach (array('loggedin', 'loggedoff') as $state) {
$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();
foreach ($lineprefarray as $pref) {
$preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
}
}
}
// Filter out enabled, available system_configured and user_configured processors only.
$readyprocessors = array_filter($processors, create_function('$a', 'return $a->enabled && $a->configured && $a->object->is_user_configured();'));
// Start the form. We're not using mform here because of our special formatting needs ...
$output = html_writer::start_tag('form', array('method'=>'post', 'class' => 'mform'));
$output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
/// For every processors put its options on the form (need to get function from processor's lib.php)
foreach ($processors as $processor) {
$processor->object->load_data($preferences, $user->id);
}
/// Settings table...
$output .= html_writer::start_tag('fieldset', array('id' => 'providers', 'class' => 'clearfix'));
$output .= html_writer::nonempty_tag('legend', get_string('providers_config', 'message'), array('class' => 'ftoggler'));
//load general messaging preferences
$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
$preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $user->id);
$preferences->mailformat = $user->mailformat;
$preferences->mailcharset = get_user_preferences( 'mailcharset', '', $user->id);
return $preferences;
}
/**
* Check if the given preference is enabled or not.
*
* @param string $name preference name
* @param stdClass $processor the processors for the preference
* @param stdClass $preferences the preferences config
* @return bool
*/
private function is_preference_enabled($name, $processor, $preferences) {
$defaultpreferences = get_message_output_default_preferences();
$checked = false;
// See if user has touched this preference
if (isset($preferences->{$name})) {
// User have some preferneces for this state in the database, use them
$checked = isset($preferences->{$name}[$processor->name]);
} else {
// User has not set this preference yet, using site default preferences set by admin
$defaultpreference = 'message_provider_'.$name;
if (isset($defaultpreferences->{$defaultpreference})) {
$checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
}
}
return $checked;
}
/**
* Build the template context for the given processor.
*
* @param stdClass $processor
* @param stdClass $provider
* @param stdClass $preferences the preferences config
* @return array
*/
private function get_processor_context($processor, $provider, $preferences) {
$processorcontext = [
'displayname' => get_string('pluginname', 'message_'.$processor->name),
'name' => $processor->name,
'locked' => false,
'radioname' => strtolower(str_replace(" ", "-", $processor->name)),
'states' => []
];
// determine the default setting
$preferencebase = $this->get_preference_base($provider);
$permitted = MESSAGE_DEFAULT_PERMITTED;
$defaultpreferences = get_message_output_default_preferences();
$defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
if (isset($defaultpreferences->{$defaultpreference})) {
$permitted = $defaultpreferences->{$defaultpreference};
}
// If settings are disallowed or forced, just display the
// corresponding message, if not use user settings.
if ($permitted == 'disallowed') {
$processorcontext['locked'] = true;
$processorcontext['lockedmessage'] = get_string('disallowed', 'message');
} else if ($permitted == 'forced') {
$processorcontext['locked'] = true;
$processorcontext['lockedmessage'] = get_string('forced', 'message');
} else {
$statescontext = [
'loggedin' => [
'name' => 'loggedin',
'displayname' => get_string('loggedindescription', 'message'),
'checked' => $this->is_preference_enabled($preferencebase.'_loggedin', $processor, $preferences),
'iconurl' => $this->pix_url('i/completion-auto-y')->out(),
],
'loggedoff' => [
'name' => 'loggedoff',
'displayname' => get_string('loggedoffdescription', 'message'),
'checked' => $this->is_preference_enabled($preferencebase.'_loggedoff', $processor, $preferences),
'iconurl' => $this->pix_url('i/completion-auto-n')->out(),
],
'both' => [
'name' => 'both',
'displayname' => get_string('always'),
'checked' => false,
'iconurl' => $this->pix_url('i/completion-auto-pass')->out(),
],
'none' => [
'name' => 'none',
'displayname' => get_string('never'),
'checked' => false,
'iconurl' => $this->pix_url('i/completion-auto-fail')->out(),
],
];
if ($statescontext['loggedin']['checked'] && $statescontext['loggedoff']['checked']) {
$statescontext['both']['checked'] = true;
$statescontext['loggedin']['checked'] = false;
$statescontext['loggedoff']['checked'] = false;
} else if (!$statescontext['loggedin']['checked'] && !$statescontext['loggedoff']['checked']) {
$statescontext['none']['checked'] = true;
}
$processorcontext['states'] = array_values($statescontext);
}
return $processorcontext;
}
/**
* Build the template context for the given component.
*
* @param string $component the component name
* @param stdClass $processors an array of processors
* @param stdClass $providers and array of providers
* @param stdClass $preferences the preferences config
* @return array
*/
private function get_component_context($component, $processors, $providers, $preferences) {
$defaultpreferences = get_message_output_default_preferences();
if ($component != 'moodle') {
$componentname = get_string('pluginname', $component);
} else {
$componentname = get_string('coresystem');
}
$componentcontext = [
'displayname' => $componentname,
'processornames' => [],
'notifications' => [],
];
foreach ($processors as $processor) {
$componentcontext['processornames'][] = get_string('pluginname', 'message_'.$processor->name);
}
foreach ($providers as $provider) {
$preferencebase = $this->get_preference_base($provider);
// If provider component is not same or provider disabled then don't show.
if (($provider->component != $component) ||
(!empty($defaultpreferences->{$preferencebase.'_disable'}))) {
continue;
}
$notificationcontext = [
'displayname' => $this->get_provider_display_name($provider),
'preferencekey' => 'message_provider_'.$preferencebase,
'processors' => [],
];
foreach ($processors as $processor) {
$notificationcontext['processors'][] = $this->get_processor_context($processor, $provider, $preferences);
}
$componentcontext['notifications'][] = $notificationcontext;
}
return $componentcontext;
}
/**
* Build the template context for the message preferences page.
*
* @param stdClass $processors an array of processors
* @param stdClass $providers and array of providers
* @param stdClass $preferences the preferences config
* @param stdClass $user the current user
* @return array
*/
private function get_preferences_context($processors, $providers, $preferences, $user) {
foreach($providers as $provider) {
if($provider->component != 'moodle') {
$components[] = $provider->component;
}
}
// Lets arrange by components so that core settings (moodle) appear as the first table.
$components = array_unique($components);
asort($components);
array_unshift($components, 'moodle'); // pop it in front! phew!
asort($providers);
$numprocs = count($processors);
// Display the messaging options table(s)
$context = [];
foreach ($components as $component) {
$provideradded = false;
$table = new html_table();
$table->attributes['class'] = 'generaltable';
$table->data = array();
if ($component != 'moodle') {
$componentname = get_string('pluginname', $component);
} else {
$componentname = get_string('coresystem');
}
$table->head = array($componentname);
foreach ($readyprocessors as $processor) {
$table->head[] = get_string('pluginname', 'message_'.$processor->name);
}
// Populate the table with rows
foreach ($providers as $provider) {
$preferencebase = $provider->component.'_'.$provider->name;
// If provider component is not same or provider disabled then don't show.
if (($provider->component != $component) ||
(!empty($defaultpreferences->{$preferencebase.'_disable'}))) {
continue;
}
$provideradded = true;
$headerrow = new html_table_row();
$providername = get_string('messageprovider:'.$provider->name, $provider->component);
$providercell = new html_table_cell($providername);
$providercell->header = true;
$providercell->colspan = $numprocs;
$providercell->attributes['class'] = 'c0';
$headerrow->cells = array($providercell);
$table->data[] = $headerrow;
foreach (array('loggedin', 'loggedoff') as $state) {
$optionrow = new html_table_row();
$optionname = new html_table_cell(get_string($state.'description', 'message'));
$optionname->attributes['class'] = 'c0';
$optionrow->cells = array($optionname);
foreach ($readyprocessors as $processor) {
// determine the default setting
$permitted = MESSAGE_DEFAULT_PERMITTED;
$defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
if (isset($defaultpreferences->{$defaultpreference})) {
$permitted = $defaultpreferences->{$defaultpreference};
}
// If settings are disallowed or forced, just display the
// corresponding message, if not use user settings.
if (in_array($permitted, array('disallowed', 'forced'))) {
if ($state == 'loggedoff') {
// skip if we are rendering the second line
continue;
}
$cellcontent = html_writer::nonempty_tag('div', get_string($permitted, 'message'), array('class' => 'dimmed_text'));
$optioncell = new html_table_cell($cellcontent);
$optioncell->rowspan = 2;
$optioncell->attributes['class'] = 'disallowed';
} else {
// determine user preferences and use them.
$disabled = array();
$checked = false;
if ($notificationsdisabled) {
$disabled['disabled'] = 1;
}
// See if user has touched this preference
if (isset($preferences->{$preferencebase.'_'.$state})) {
// User have some preferneces for this state in the database, use them
$checked = isset($preferences->{$preferencebase.'_'.$state}[$processor->name]);
} else {
// User has not set this preference yet, using site default preferences set by admin
$defaultpreference = 'message_provider_'.$preferencebase.'_'.$state;
if (isset($defaultpreferences->{$defaultpreference})) {
$checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
}
}
$elementname = $preferencebase.'_'.$state.'['.$processor->name.']';
// prepare language bits
$processorname = get_string('pluginname', 'message_'.$processor->name);
$statename = get_string($state, 'message');
$labelparams = array(
'provider' => $providername,
'processor' => $processorname,
'state' => $statename
);
$label = get_string('sendingviawhen', 'message', $labelparams);
$cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
$cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array_merge(array('id' => $elementname, 'class' => 'notificationpreference'), $disabled));
$optioncell = new html_table_cell($cellcontent);
$optioncell->attributes['class'] = 'mdl-align';
}
$optionrow->cells[] = $optioncell;
}
$table->data[] = $optionrow;
}
}
// Add settings only if provider added for component.
if ($provideradded) {
$output .= html_writer::start_tag('div', array('class' => 'messagesettingcomponent'));
$output .= html_writer::table($table);
$output .= html_writer::end_tag('div');
}
$context['components'][] = $this->get_component_context($component, $processors, $providers, $preferences);
}
$output .= html_writer::end_tag('fieldset');
$context['userid'] = $user->id;
$context['disableall'] = $user->emailstop;
foreach ($processors as $processor) {
if (($processorconfigform = $processor->object->config_form($preferences)) && $processor->enabled) {
$output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_'.$processor->name, 'class' => 'clearfix'));
$output .= html_writer::nonempty_tag('legend', get_string('pluginname', 'message_'.$processor->name), array('class' => 'ftoggler'));
$output .= html_writer::start_tag('div');
$output .= $processorconfigform;
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('fieldset');
}
}
$output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_general', 'class' => 'clearfix'));
$output .= html_writer::nonempty_tag('legend', get_string('generalsettings','admin'), array('class' => 'ftoggler'));
$output .= html_writer::start_tag('div');
$output .= html_writer::checkbox('beepnewmessage', 1, $preferences->beepnewmessage, get_string('beepnewmessage', 'message'));
$output .= html_writer::end_tag('div');
$output .= html_writer::start_tag('div');
$output .= html_writer::checkbox('blocknoncontacts', 1, $preferences->blocknoncontacts, get_string('blocknoncontacts', 'message'));
$output .= html_writer::end_tag('div');
$disableallcheckbox = html_writer::checkbox('disableall', 1, $notificationsdisabled, get_string('disableall', 'message'), array('class'=>'disableallcheckbox'));
$disableallcheckbox .= $this->output->help_icon('disableall', 'message');
$output .= html_writer::nonempty_tag('div', $disableallcheckbox, array('class'=>'disableall'));
$redirect = new moodle_url("/user/preferences.php", array('userid' => $userid));
$output .= html_writer::end_tag('fieldset');
$output .= html_writer::start_tag('div', array('class' => 'mdl-align'));
$output .= html_writer::empty_tag('input', array('type' => 'submit',
'value' => get_string('savechanges'), 'class' => 'form-submit'));
$output .= html_writer::link($redirect, html_writer::empty_tag('input', array('type' => 'button',
'value' => get_string('cancel'), 'class' => 'btn-cancel')));
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
return $output;
return $context;
}
/**
* Display the interface for messaging options
*
* @param object $user instance of a user
* @return string The text to render
*/
public function render_user_preferences($user) {
// Filter out enabled, available system_configured and user_configured processors only.
$readyprocessors = array_filter(get_message_processors(), create_function('$a', 'return $a->enabled && $a->configured && $a->object->is_user_configured();'));
$providers = message_get_providers_for_user($user->id);
$preferences = $this->get_all_preferences($readyprocessors, $providers, $user);
$preferencescontext = $this->get_preferences_context($readyprocessors, $providers, $preferences, $user);
$output = $this->render_from_template('message/preferences_notifications_list', $preferencescontext);
$processorscontext = [
'userid' => $user->id,
'processors' => [],
];
foreach ($readyprocessors as $processor) {
$formhtml = $processor->object->config_form($preferences);
if (!$formhtml) {
continue;
}
$processorscontext['processors'][] = [
'displayname' => get_string('pluginname', 'message_'.$processor->name),
'name' => $processor->name,
'formhtml' => $formhtml,
];
}
$output .= $this->render_from_template('message/preferences_processors', $processorscontext);
$generalsettingscontext = [
'userid' => $user->id,
'beepnewmessage' => $preferences->beepnewmessage,
'blocknoncontacts' => $preferences->blocknoncontacts,
'disableall' => $user->emailstop,
'disableallhelpicon' => $this->output->help_icon('disableall', 'message'),
];
$output .= $this->render_from_template('message/preferences_general_settings', $generalsettingscontext);
return $output;
}
}