MDL-54698 message: make processor settings dialogue

This commit is contained in:
Ryan Wyllie 2016-07-25 03:17:00 +00:00 committed by Mark Nelson
parent e9cce46cf0
commit 643b015db2
14 changed files with 259 additions and 175 deletions

View File

@ -135,6 +135,7 @@ $string['pagerefreshes'] = 'This page refreshes automatically every {$a} seconds
$string['permitted'] = 'Permitted';
$string['page-message-x'] = 'Any message pages';
$string['private_config'] = 'Popup message window';
$string['processorsettings'] = 'Processor settings';
$string['processortag'] = 'Destination';
$string['providers_config'] = 'Configure notification methods for incoming messages';
$string['providerstag'] = 'Source';

View File

@ -0,0 +1,60 @@
// 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/>.
/**
* Load the settings for a message processor.
*
* @module core_message/notification_processor_settings
* @class notification_processor_settings
* @package message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.2
*/
define(['jquery', 'core/fragment', 'core/templates', 'core/str', 'tool_lp/dialogue'], function($, Fragment, Templates, Str, Dialogue) {
/**
* Constructor for the notification processor settings.
*
* @param element jQuery object root element of the processor
* @return object NotificationProcessorSettings
*/
var NotificationProcessorSettings = function(element) {
this.root = $(element);
this.name = this.root.attr('data-name');
this.userId = this.root.attr('data-user-id');
this.contextId = this.root.attr('data-context-id');
};
/**
* Show the notification processor settings dialogue.
*
* @method show
*/
NotificationProcessorSettings.prototype.show = function() {
Fragment.loadFragment('message', 'processor_settings', this.contextId, {
userid: this.userId,
type: this.name,
})
.done(function(html, js) {
Str.get_string('processorsettings', 'message').done(function(string) {
new Dialogue(string, html, function() {
Templates.runTemplateJS(js);
});
});
});
}
return NotificationProcessorSettings;
});

View File

@ -24,11 +24,15 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.2
*/
define(['jquery', 'core_message/notification_preference'], function($, NotificationPreference) {
define(['jquery', 'core/custom_interaction_events', 'core_message/notification_preference',
'core_message/notification_processor_settings'],
function($, CustomEvents, NotificationPreference, NotificationProcessorSettings) {
var SELECTORS = {
PREFERENCE: '[data-state]',
PREFERENCE_ROW: '.preference-row',
PREFERENCE_INPUT: '[data-state] input',
PROCESSOR_SETTING: '[data-processor-setting]',
};
/**
@ -41,26 +45,7 @@ define(['jquery', 'core_message/notification_preference'], function($, Notificat
this.root = $(element);
this.userId = this.root.attr('data-user-id');
this.root.on('change', function(e) {
if (!this.isDisabled()) {
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
var preferenceRow = $(e.target).closest(SELECTORS.PREFERENCE_ROW);
var preference = new NotificationPreference(preferenceRow, this.userId);
preferenceElement.addClass('loading');
preference.save().always(function() {
preferenceElement.removeClass('loading');
});
}
}.bind(this));
$(document).on('messageprefs:disableall', function() {
this.setDisabled();
}.bind(this));
$(document).on('messageprefs:enableall', function() {
this.setEnabled();
}.bind(this));
this.registerEventListeners();
};
/**
@ -93,5 +78,38 @@ define(['jquery', 'core_message/notification_preference'], function($, Notificat
this.root.find(SELECTORS.PREFERENCE_INPUT).prop('disabled', false);
};
PreferencesController.prototype.registerEventListeners = function() {
CustomEvents.define(this.root, [
CustomEvents.events.activate,
]);
this.root.on('change', function(e) {
if (!this.isDisabled()) {
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
var preferenceRow = $(e.target).closest(SELECTORS.PREFERENCE_ROW);
var preference = new NotificationPreference(preferenceRow, this.userId);
preferenceElement.addClass('loading');
preference.save().always(function() {
preferenceElement.removeClass('loading');
});
}
}.bind(this));
this.root.on(CustomEvents.events.activate, SELECTORS.PROCESSOR_SETTING, function(e) {
var element = $(e.target).closest(SELECTORS.PROCESSOR_SETTING);
var processorSettings = new NotificationProcessorSettings(element);
processorSettings.show();
});
$(document).on('messageprefs:disableall', function() {
this.setDisabled();
}.bind(this));
$(document).on('messageprefs:enableall', function() {
this.setEnabled();
}.bind(this));
}
return PreferencesController;
});

View File

@ -16,8 +16,8 @@
/**
* Manages the processor form on the message preferences page.
*
* @module core_message/processor_form
* @class processor_form
* @module core_message/preferences_processor_form
* @class preferences_processor_form
* @package message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -28,13 +28,16 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notificat
* Constructor for the ProcessorForm.
*
* @param element jQuery object root element of the preference
* @param int the current user id
* @return object ProcessorForm
*/
var ProcessorForm = function(element, userId) {
var ProcessorForm = function(element) {
this.root = $(element);
this.userId = userId;
this.userId = this.root.attr('data-user-id');
this.name = this.root.attr('data-processor-name');
this.root.on('change', function(e) {
this.save();
}.bind(this));
};
/**

View File

@ -1,49 +0,0 @@
// 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/>.
/**
* Controls the processors config section on the message preference page
*
* @module core_message/preferences_processors_controller
* @class preferences_processors_controller
* @package message
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.2
*/
define(['jquery', 'core/ajax', 'core/notification', 'core_message/processor_form'], function($, ajax, notification, ProcessorForm) {
var SELECTORS = {
PROCESSOR: '[data-processor-name]',
};
/**
* Constructor for the ProcessorsController.
*
* @param element jQuery object root element of the preference
* @return object ProcessorsController
*/
var ProcessorsController = function(element) {
this.root = $(element);
this.userId = this.root.attr('data-user-id');
this.root.on('change', function(e) {
var element = $(e.target).closest(SELECTORS.PROCESSOR);
var processor = new ProcessorForm(element, this.userId);
processor.save();
}.bind(this));
};
return ProcessorsController;
});

View File

@ -94,7 +94,8 @@ class notification_list implements templatable, renderable {
$context = [];
foreach ($components as $component) {
$notificationcomponent = new \core_message\output\preferences\notification_list_component($component, $processors, $providers, $preferences);
$notificationcomponent = new \core_message\output\preferences\notification_list_component(
$component, $processors, $providers, $preferences, $user);
$context['components'][] = $notificationcomponent->export_for_template($output);
}

View File

@ -29,6 +29,7 @@ require_once($CFG->dirroot . '/message/lib.php');
use renderable;
use templatable;
use context_user;
/**
* Class to create context for a notification component on the message
@ -60,6 +61,11 @@ class notification_list_component implements templatable, renderable {
*/
protected $component;
/**
* A user.
*/
protected $user;
/**
* Constructor.
*
@ -68,11 +74,12 @@ class notification_list_component implements templatable, renderable {
* @param array $providers
* @param stdClass $preferences
*/
public function __construct($component, $processors, $providers, $preferences) {
public function __construct($component, $processors, $providers, $preferences, $user) {
$this->processors = $processors;
$this->providers = $providers;
$this->preferences = $preferences;
$this->component = $component;
$this->user = $user;
}
/**
@ -101,6 +108,7 @@ class notification_list_component implements templatable, renderable {
$preferences = $this->preferences;
$component = $this->component;
$defaultpreferences = get_message_output_default_preferences();
$usercontext = context_user::instance($this->user->id);
if ($component != 'moodle') {
$componentname = get_string('pluginname', $component);
@ -110,12 +118,17 @@ class notification_list_component implements templatable, renderable {
$context = [
'displayname' => $componentname,
'processornames' => [],
'processors' => [],
'notifications' => [],
];
foreach ($processors as $processor) {
$context['processornames'][] = get_string('pluginname', 'message_'.$processor->name);
$context['processors'][] = [
'displayname' => get_string('pluginname', 'message_'.$processor->name),
'name' => $processor->name,
'hassettings' => !empty($processor->object->config_form($preferences)),
'contextid' => $usercontext->id,
];
}
foreach ($providers as $provider) {

View File

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains processors class for displaying on message preferences
* Contains processor class for displaying on message preferences
* page.
*
* @package core_message
@ -29,19 +29,19 @@ use renderable;
use templatable;
/**
* Class to create context for each of the message processors settings
* Class to create context for one 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 {
class processor implements templatable, renderable {
/**
* A list of message processors.
* The message processor.
*/
protected $processors;
protected $processor;
/**
* A list of message preferences.
@ -53,39 +53,31 @@ class processors implements templatable, renderable {
*/
protected $user;
/**
* The processor type.
*/
protected $type;
/**
* Constructor.
*
* @param array $processors
* @param stdClass $processor
* @param stdClass $preferences
* @param stdClass $user
*/
public function __construct($processors, $preferences, $user) {
$this->processors = $processors;
public function __construct($processor, $preferences, $user, $type) {
$this->processor = $processor;
$this->preferences = $preferences;
$this->user = $user;
$this->type = $type;
}
public function export_for_template(\renderer_base $output) {
$context = [
return [
'userid' => $this->user->id,
'processors' => [],
'displayname' => get_string('pluginname', 'message_'.$this->type),
'name' => $this->type,
'formhtml' => $this->processor->config_form($this->preferences),
];
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

@ -2115,3 +2115,51 @@ function message_is_user_blocked($recipient, $sender = null) {
return false;
}
function get_providers_preferences($providers, $userid) {
$preferences = new stdClass();
/// Get providers preferences
foreach ($providers as $provider) {
foreach (array('loggedin', 'loggedoff') as $state) {
$linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $userid);
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;
}
}
}
return $preferences;
}
function message_output_fragment_processor_settings($args = []) {
global $PAGE;
if (!isset($args['type'])) {
throw new moodle_exception('Must provide a processor type');
}
if (!isset($args['userid'])) {
throw new moodle_exception('Must provide a userid');
}
$type = $args['type'];
$userid = $args['userid'];
$user = core_user::get_user($userid, '*', MUST_EXIST);
$processor = get_message_processor($type);
$providers = message_get_providers_for_user($userid);
$preferences = get_providers_preferences($providers, $userid);
$processor->load_data($preferences, $userid);
$processoroutput = new \core_message\output\preferences\processor($processor, $preferences, $user, $type);
$renderer = $PAGE->get_renderer('core', 'message');
return $renderer->render_from_template('core_message/preferences_processor', $processoroutput->export_for_template($renderer));
}

View File

@ -224,24 +224,9 @@ class core_message_renderer extends plugin_renderer_base {
* @return stdClass
*/
private function get_all_preferences($processors, $providers, $user) {
$preferences = new stdClass();
$preferences = get_providers_preferences($providers, $user->id);
$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;
}
}
}
/// 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);
@ -270,11 +255,9 @@ class core_message_renderer extends plugin_renderer_base {
$providers = message_get_providers_for_user($user->id);
$preferences = $this->get_all_preferences($readyprocessors, $providers, $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', $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;

View File

@ -37,7 +37,15 @@
"components": [
{
"displayname": "System",
"processornames": ["Popup notification"],
"processors": [
{
displayname: "Popup notification",
name: "popup",
userid: 1,
contextid: 18,
hassettings: true
}
],
"notifications": [
{
"displayname": "Notices about minor problems",
@ -74,9 +82,22 @@
<thead>
<tr>
<th>{{displayname}}</th>
{{#processornames}}
<th>{{.}}</th>
{{/processornames}}
{{#processors}}
<th>
{{#hassettings}}
<a href="#"
data-processor-setting
data-user-id="{{userid}}"
data-context-id="{{contextid}}"
data-name="{{name}}">
{{displayname}} {{#pix}} i/settings {{/pix}}
</a>
{{/hassettings}}
{{^hassettings}}
{{displayname}}
{{/hassettings}}
</th>
{{/processors}}
</tr>
</thead>
<tbody>

View File

@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core_message/preferences_processors
@template core_message/preferences_processor
The message processor configuration block for the preferences page
@ -32,32 +32,27 @@
Example context (json):
{
"userid": 1,
"processors": [
{
"displayname": "Email",
"name": "email",
"formhtml": "<input name='test' type='text' />"
}
]
"displayname": "Email",
"name": "email",
"formhtml": "<input name='test' type='text' />"
}
}}
<h2 class="title-case">{{#str}} messageprocessors, message {{/str}}</h2>
<div class="processors-container" data-user-id="{{userid}}">
{{#processors}}
<div class="processor-container" data-processor-name="{{name}}">
<div class="loading-container">
<div class="vertical-align"></div>
{{> core/loading }}
</div>
<h3>{{displayname}}</h3>
<form>
{{{formhtml}}}
</form>
</div>
{{/processors}}
<h4 class="title-case">{{displayname}}</h4>
<div class="processor-container"
data-user-id="{{userid}}"
data-processor-name="{{name}}"
data-processor-id="{{uniqid}}">
<div class="loading-container">
<div class="vertical-align"></div>
{{> core/loading }}
</div>
<form>
{{{formhtml}}}
</form>
</div>
{{#js}}
require(['jquery', 'core_message/preferences_processors_controller'], function($, controller) {
new controller($('.processors-container'));
require(['jquery', 'core_message/preferences_processor_form'], function($, ProcessorForm) {
new ProcessorForm($('[data-processor-id="{{uniqid}}"]'));
});
{{/js}}

View File

@ -373,30 +373,28 @@
display: inline-block;
}
}
.processors-container {
.processor-container {
position: relative;
.processor-container {
position: relative;
.loading-container {
display: none;
position: absolute;
width: 100%;
.loading-container {
display: none;
position: absolute;
width: 100%;
height: 100%;
text-align: center;
background-color: rgba(255, 255, 255, 0.5);
.vertical-align {
height: 100%;
text-align: center;
background-color: rgba(255, 255, 255, 0.5);
.vertical-align {
height: 100%;
width: 0%;
display: inline-block;
vertical-align: middle;
}
width: 0%;
display: inline-block;
vertical-align: middle;
}
}
&.loading {
.loading-container {
display: block;
}
&.loading {
.loading-container {
display: block;
}
}
}

View File

@ -6061,10 +6061,10 @@ a.ygtvspacer:hover {
.general-settings-container label {
display: inline-block;
}
.processors-container .processor-container {
.processor-container {
position: relative;
}
.processors-container .processor-container .loading-container {
.processor-container .loading-container {
display: none;
position: absolute;
width: 100%;
@ -6072,13 +6072,13 @@ a.ygtvspacer:hover {
text-align: center;
background-color: rgba(255, 255, 255, 0.5);
}
.processors-container .processor-container .loading-container .vertical-align {
.processor-container .loading-container .vertical-align {
height: 100%;
width: 0%;
display: inline-block;
vertical-align: middle;
}
.processors-container .processor-container.loading .loading-container {
.processor-container.loading .loading-container {
display: block;
}
.title-case {