MDL-54698 message: split message preference page into renderables

This commit is contained in:
Ryan Wyllie 2016-07-19 05:22:48 +00:00 committed by Mark Nelson
parent 39986f5044
commit 5ee31614c7
6 changed files with 602 additions and 242 deletions

View File

@ -0,0 +1,70 @@
<?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/>.
/**
* Contains general_settings class for displaying on message preferences
* page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\preferences;
use renderable;
use templatable;
/**
* Class to create context for the general settings on the message
* preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class general_settings implements templatable, renderable {
/**
* A list of message preferences.
*/
protected $preferences;
/**
* A user.
*/
protected $user;
/**
* Constructor.
*
* @param stdClass $preferences
* @param stdClass $user
*/
public function __construct($preferences, $user) {
$this->preferences = $preferences;
$this->user = $user;
}
public function export_for_template(\renderer_base $output) {
return [
'userid' => $this->user->id,
'blocknoncontacts' => $this->preferences->blocknoncontacts,
'disableall' => $this->user->emailstop,
'disableallhelpicon' => $output->help_icon('disableall', 'message'),
];
}
}

View File

@ -0,0 +1,106 @@
<?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/>.
/**
* Contains notification_list class for displaying on message preferences
* page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\preferences;
use renderable;
use templatable;
/**
* Class to create context for the list of notifications on the message
* preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class notification_list implements templatable, renderable {
/**
* A list of message processors.
*/
protected $processors;
/**
* A list of message providers.
*/
protected $providers;
/**
* A list of message preferences.
*/
protected $preferences;
/**
* A user.
*/
protected $user;
/**
* Constructor.
*
* @param array $processors
* @param array $providers
* @param stdClass $preferences
* @param stdClass $user
*/
public function __construct($processors, $providers, $preferences, $user) {
$this->processors = $processors;
$this->providers = $providers;
$this->preferences = $preferences;
$this->user = $user;
}
public function export_for_template(\renderer_base $output) {
$processors = $this->processors;
$providers = $this->providers;
$preferences = $this->preferences;
$user = $this->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);
$context = [];
foreach ($components as $component) {
$notificationcomponent = new \core_message\output\preferences\notification_list_component($component, $processors, $providers, $preferences);
$context['components'][] = $notificationcomponent->export_for_template($output);
}
$context['userid'] = $user->id;
$context['disableall'] = $user->emailstop;
return $context;
}
}

View File

@ -0,0 +1,145 @@
<?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/>.
/**
* Contains notification_list_component class for displaying on message preferences
* page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\preferences;
require_once($CFG->dirroot . '/message/lib.php');
use renderable;
use templatable;
/**
* Class to create context for a notification component on the message
* preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class notification_list_component implements templatable, renderable {
/**
* A list of message processors.
*/
protected $processors;
/**
* A list of message providers.
*/
protected $providers;
/**
* A list of message preferences.
*/
protected $preferences;
/**
* The component name.
*/
protected $component;
/**
* Constructor.
*
* @param string $component
* @param array $processors
* @param array $providers
* @param stdClass $preferences
*/
public function __construct($component, $processors, $providers, $preferences) {
$this->processors = $processors;
$this->providers = $providers;
$this->preferences = $preferences;
$this->component = $component;
}
/**
* Get the base key prefix for the given provider.
*
* @param stdClass message provider
* @return string
*/
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);
}
public function export_for_template(\renderer_base $output) {
$processors = $this->processors;
$providers = $this->providers;
$preferences = $this->preferences;
$component = $this->component;
$defaultpreferences = get_message_output_default_preferences();
if ($component != 'moodle') {
$componentname = get_string('pluginname', $component);
} else {
$componentname = get_string('coresystem');
}
$context = [
'displayname' => $componentname,
'processornames' => [],
'notifications' => [],
];
foreach ($processors as $processor) {
$context['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) {
$notificationprocessor = new \core_message\output\preferences\notification_list_processor($processor, $provider, $preferences);
$notificationcontext['processors'][] = $notificationprocessor->export_for_template($output);
}
$context['notifications'][] = $notificationcontext;
}
return $context;
}
}

View File

@ -0,0 +1,178 @@
<?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/>.
/**
* Contains notification_list_processor class for displaying on message preferences
* page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\preferences;
require_once($CFG->dirroot . '/message/lib.php');
use renderable;
use templatable;
/**
* Class to create context for a notification component on the message
* preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class notification_list_processor implements templatable, renderable {
/**
* A notification processor.
*/
protected $processor;
/**
* A notification provider.
*/
protected $provider;
/**
* A list of message preferences.
*/
protected $preferences;
/**
* Constructor.
*
* @param stdClass $processor
* @param stdClass $provider
* @param stdClass $preferences
*/
public function __construct($processor, $provider, $preferences) {
$this->processor = $processor;
$this->provider = $provider;
$this->preferences = $preferences;
}
/**
* Get the base key prefix for the given provider.
*
* @return string
*/
private function get_preference_base() {
return $this->provider->component . '_' . $this->provider->name;
}
/**
* 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 = $this->processor;
$preferences = $this->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;
}
public function export_for_template(\renderer_base $output) {
$processor = $this->processor;
$provider = $this->provider;
$preferences = $this->preferences;
$context = [
'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') {
$context['locked'] = true;
$context['lockedmessage'] = get_string('disallowed', 'message');
} else if ($permitted == 'forced') {
$context['locked'] = true;
$context['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' => $output->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' => $output->pix_url('i/completion-auto-n')->out(),
],
'both' => [
'name' => 'both',
'displayname' => get_string('always'),
'checked' => false,
'iconurl' => $output->pix_url('i/completion-auto-pass')->out(),
],
'none' => [
'name' => 'none',
'displayname' => get_string('never'),
'checked' => false,
'iconurl' => $output->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;
}
$context['states'] = array_values($statescontext);
}
return $context;
}
}

View File

@ -0,0 +1,91 @@
<?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/>.
/**
* Contains processors class for displaying on message preferences
* page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_message\output\preferences;
use renderable;
use templatable;
/**
* Class to create context for each of the message processors settings
* on the message preferences page.
*
* @package core_message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class processors implements templatable, renderable {
/**
* A list of message processors.
*/
protected $processors;
/**
* A list of message preferences.
*/
protected $preferences;
/**
* A user.
*/
protected $user;
/**
* Constructor.
*
* @param array $processors
* @param stdClass $preferences
* @param stdClass $user
*/
public function __construct($processors, $preferences, $user) {
$this->processors = $processors;
$this->preferences = $preferences;
$this->user = $user;
}
public function export_for_template(\renderer_base $output) {
$context = [
'userid' => $this->user->id,
'processors' => [],
];
foreach ($this->processors as $processor) {
$formhtml = $processor->object->config_form($this->preferences);
if (!$formhtml) {
continue;
}
$context['processors'][] = [
'displayname' => get_string('pluginname', 'message_'.$processor->name),
'name' => $processor->name,
'formhtml' => $formhtml,
];
}
return $context;
}
}

View File

@ -215,26 +215,6 @@ class core_message_renderer extends plugin_renderer_base {
return $output;
}
/**
* Get the base key prefix for the given provider.
*
* @param stdClass message provider
* @return string
*/
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.
*
@ -268,199 +248,13 @@ class core_message_renderer extends plugin_renderer_base {
}
//load general messaging preferences
$preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $user->id);
$preferences->mailformat = $user->mailformat;
$preferences->mailcharset = get_user_preferences( 'mailcharset', '', $user->id);
$preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $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);
$context = [];
foreach ($components as $component) {
$context['components'][] = $this->get_component_context($component, $processors, $providers, $preferences);
}
$context['userid'] = $user->id;
$context['disableall'] = $user->emailstop;
return $context;
}
/**
* Display the interface for messaging options
*
@ -469,43 +263,19 @@ class core_message_renderer extends plugin_renderer_base {
*/
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();'));
$readyprocessors = array_filter(get_message_processors(), function($processor) {
return $processor->enabled && $processor->configured && $processor->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);
$notificationlistoutput = new \core_message\output\preferences\notification_list($readyprocessors, $providers, $preferences, $user);
$processorsoutput = new \core_message\output\preferences\processors($readyprocessors, $preferences, $user);
$generalsettingsoutput = new \core_message\output\preferences\general_settings($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,
'blocknoncontacts' => $preferences->blocknoncontacts,
'disableall' => $user->emailstop,
'disableallhelpicon' => $this->output->help_icon('disableall', 'message'),
];
$output .= $this->render_from_template('message/preferences_general_settings', $generalsettingscontext);
$output = $this->render_from_template('message/preferences_notifications_list', $notificationlistoutput->export_for_template($this));
$output .= $this->render_from_template('message/preferences_processors', $processorsoutput->export_for_template($this));
$output .= $this->render_from_template('message/preferences_general_settings', $generalsettingsoutput->export_for_template($this));
return $output;
}