Skip to content

Commit

Permalink
MDL-27171 messages: add default message outputs management interface
Browse files Browse the repository at this point in the history
This introduces the new page where admin may change the permissions and
defaults for particular combination of message processors and providers.

Signed-off-by: Ruslan Kabalin <ruslan.kabalin@luns.net.uk>
  • Loading branch information
Ruslan Kabalin committed May 27, 2011
1 parent e710658 commit 1d72e9d
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 23 deletions.
5 changes: 3 additions & 2 deletions admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@
// message outputs
$ADMIN->add('modules', new admin_category('messageoutputs', get_string('messageoutputs', 'message')));
$ADMIN->add('messageoutputs', new admin_page_managemessageoutputs());
$ADMIN->add('messageoutputs', new admin_page_defaultmessageoutputs());
require_once($CFG->dirroot.'/message/lib.php');
$processors = get_message_processors();
foreach ($processors as $processor) {
$processorname = $processor->name;
if (!$processor->available) {
continue;
}
$strprocessorname = get_string('pluginname', 'message_'.$processorname);
if (file_exists($CFG->dirroot.'/message/output/'.$processor->name.'/settings.php')) {
if ($processor->hassettings) {
$strprocessorname = get_string('pluginname', 'message_'.$processorname);
$settings = new admin_settingpage('messagesetting'.$processorname, $strprocessorname, 'moodle/site:config', !$processor->enabled);
include($CFG->dirroot.'/message/output/'.$processor->name.'/settings.php');
if ($settings) {
Expand Down
7 changes: 7 additions & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@
$string['contacts'] = 'Contacts';
$string['context'] = 'context';
$string['couldnotfindpreference'] = 'Could not load preference {$a}. Does the component and name you supplied to message_send() match a row in message_providers? Message providers must appear in the database so users can configure how they will be notified when they receive messages.';
$string['defaultmessageoutputs'] = 'Default message outputs';
$string['defaults'] = 'Defaults';
$string['deletemessagesdays'] = 'Number of days before old messages are automatically deleted';
$string['disabled'] = 'Messaging is disabled on this site';
$string['disallowed'] = 'Disallowed';
$string['discussion'] = 'Discussion';
$string['editmymessage'] = 'Messaging';
$string['emailmessages'] = 'Email messages when I am offline';
$string['emailtagline'] = 'This is a copy of a message sent to you at "{$a->sitename}". Go to {$a->url} to reply.';
$string['emptysearchstring'] = 'You must search for something';
$string['errorcallingprocessor'] = 'Error calling defined processor';
$string['forced'] = 'Forced';
$string['formorethan'] = 'For more than';
$string['guestnoeditmessage'] = 'Guest user can not edit messaging options';
$string['guestnoeditmessageother'] = 'Guest user can not edit other user messaging options';
Expand Down Expand Up @@ -98,6 +102,7 @@
$string['outputnotavailable'] = 'Not available';
$string['outputnotconfigured'] = 'Not configured';
$string['pagerefreshes'] = 'This page refreshes automatically every {$a} seconds';
$string['permitted'] = 'Permitted';
$string['private_config'] = 'Popup message window';
$string['processortag'] = 'Destination';
$string['providers_config'] = 'Configure notification methods for incoming messages';
Expand All @@ -110,6 +115,8 @@
$string['searchforperson'] = 'Search for a person';
$string['searchmessages'] = 'Search messages';
$string['searchcombined'] = 'Search people and messages';
$string['sendingvia'] = 'Sending {$a->provider} via {$a->processor}';
$string['sendingviawhen'] = 'Sending {$a->provider} via {$a->processor} when {$a->state}';
$string['sendmessage'] = 'Send message';
$string['sendmessageto'] = 'Send message to {$a}';
$string['sendmessagetopopup'] = 'Send message to {$a} - new window';
Expand Down
15 changes: 15 additions & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4965,6 +4965,21 @@ public function search($query) {
}
}

/**
* Default message outputs configuration
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_page_defaultmessageoutputs extends admin_page_managemessageoutputs {
/**
* Calls parent::__construct with specific arguments
*/
public function __construct() {
global $CFG;
admin_externalpage::__construct('defaultmessageoutputs', get_string('defaultmessageoutputs', 'message'), "$CFG->wwwroot/message/defaultoutputs.php");
}
}

/**
* Question type manage page
*
Expand Down
1 change: 1 addition & 0 deletions lib/outputrequirementslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ protected function find_module($component) {
break;
case 'core_message':
$module = array('name' => 'core_message',
'requires' => array('base', 'node', 'event', 'node-event-simulate'),
'fullpath' => '/message/module.js');
break;
case 'core_flashdetect':
Expand Down
115 changes: 115 additions & 0 deletions message/defaultoutputs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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/>.

/**
* Default message outputs configuration page
*
* @package message
* @copyright 2011 Lancaster University Network Services Limited
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once(dirname(dirname(__FILE__)) . '/message/lib.php');
require_once($CFG->libdir.'/adminlib.php');

// This is an admin page
admin_externalpage_setup('defaultmessageoutputs');

// Require site configuration capability
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));

// Fetch processors
$processors = get_message_processors(true);
// Fetch message providers
$providers = $DB->get_records('message_providers', null, 'name');

if (($form = data_submitted()) && confirm_sesskey()) {
$preferences = array();
// Prepare default message outputs settings
foreach ( $providers as $provider) {
$componentproviderbase = $provider->component.'_'.$provider->name;
foreach (array('permitted', 'loggedin', 'loggedoff') as $setting){
$value = null;
$componentprovidersetting = $componentproviderbase.'_'.$setting;
if ($setting == 'permitted') {
// if we deal with permitted select element, we need to create individual
// setting for each possible processor. Note that this block will
// always be processed first after entring parental foreach iteration
// so we can change form values on this stage.
foreach($processors as $processor) {
$value = '';
if (isset($form->{$componentprovidersetting}[$processor->name])) {
$value = $form->{$componentprovidersetting}[$processor->name];
}
// Ensure that loggedin loggedoff options are set correctly
// for this permission
if ($value == 'forced') {
$form->{$componentproviderbase.'_loggedin'}[$processor->name] = 1;
$form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 1;
} else if ($value == 'disallowed') {
// It might be better to unset them, but I can't figure out why that cause error
$form->{$componentproviderbase.'_loggedin'}[$processor->name] = 0;
$form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 0;
}
// record the site preference
$preferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
}
} else if (array_key_exists($componentprovidersetting, $form)) {
// we must be processing loggedin or loggedoff checkboxes. Store
// defained comma-separated processors as setting value.
// Using array_filter eliminates elements set to 0 above
$value = join(',', array_keys(array_filter($form->{$componentprovidersetting})));
if (empty($value)) {
$value = null;
}
}
if ($setting != 'permitted') {
// we have already recoded site preferences for 'permitted' type
$preferences['message_provider_'.$componentprovidersetting] = $value;
}
}
}

// Update database
$transaction = $DB->start_delegated_transaction();
foreach ($preferences as $name => $value) {
set_config($name, $value, 'message');
}
$transaction->allow_commit();

// Redirect
$url = new moodle_url('defaultoutputs.php');
redirect($url);
}



// Page settings
$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
$PAGE->requires->js_init_call('M.core_message.init_defaultoutputs');

// Grab the renderer
$renderer = $PAGE->get_renderer('core', 'message');

// Display the manage message outputs interface
$preferences = get_config('message');
$messageoutputs = $renderer->manage_defaultmessageoutputs($processors, $providers, $preferences);

// Display the page
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('defaultmessageoutputs', 'message'));
echo $messageoutputs;
echo $OUTPUT->footer();
52 changes: 36 additions & 16 deletions message/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
define('MESSAGE_CONTACTS_PER_PAGE',10);
define('MESSAGE_MAX_COURSE_NAME_LENGTH', 30);

/**
* Set default value for default outputs permitted setting
*/
define('MESSAGE_DEFAULT_PERMITTED_VALUE', 'disallowed');

if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds
$CFG->message_contacts_refresh = 60;
}
Expand Down Expand Up @@ -2171,30 +2176,45 @@ function message_print_heading($title, $colspan=3) {
/**
* Get all message processors and validate corresponding plugin existance and
* configuration
* @param bool $enabled only return enabled processors
* @return array $processors array of objects containing information on message processors
*/
function get_message_processors() {
function get_message_processors($enabled = false) {
global $DB, $CFG;

$processors = $DB->get_records('message_processors', null, 'name');
foreach ($processors as &$processor){
$processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
if (is_readable($processorfile)) {
include_once($processorfile);
$processclass = 'message_output_' . $processor->name;
if (class_exists($processclass)) {
$pclass = new $processclass();
$processor->configured = 0;
if ($pclass->is_system_configured()) {
$processor->configured = 1;
static $processors;

if (empty($processors)) {
$processors = $DB->get_records('message_processors', null, 'name');
foreach ($processors as &$processor){
$processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
if (is_readable($processorfile)) {
include_once($processorfile);
$processclass = 'message_output_' . $processor->name;
if (class_exists($processclass)) {
$pclass = new $processclass();
$processor->configured = 0;
if ($pclass->is_system_configured()) {
$processor->configured = 1;
}
$processor->hassettings = 0;
if (is_readable($CFG->dirroot.'/message/output/'.$processor->name.'/settings.php')) {
$processor->hassettings = 1;
}
$processor->available = 1;
} else {
print_error('errorcallingprocessor', 'message');
}
$processor->available = 1;
} else {
print_error('errorcallingprocessor', 'message');
$processor->available = 0;
}
} else {
$processor->available = 0;
}
}
if ($enabled) {
// Filter out enabled processors only, the reason of not doing this in
// database request is caching the result.
$processors = array_filter($processors, create_function('$a', 'return $a->enabled;'));
}

return $processors;
}
47 changes: 46 additions & 1 deletion message/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,49 @@ M.core_message.init_notification = function(Y, title, content, url) {
return false;
}, o);
});
};
};

M.core_message.init_defaultoutputs = function(Y) {
var defaultoutputs = {

init : function() {
Y.all('#defaultmessageoutputs select').each(function(node) {
// attach event listener
node.on('change', defaultoutputs.changeState);
// set initial layout
node.simulate("change");
}, this);
},

changeState : function(e) {
var value = e.target._node.options[e.target.get('selectedIndex')].value;
var parentnode = e.target.ancestor('td');
switch (value) {
case 'forced':
defaultoutputs.updateCheckboxes(parentnode, 1, 1);
break;
case 'disallowed':
defaultoutputs.updateCheckboxes(parentnode, 1, 0);
break;
case 'permitted':
defaultoutputs.updateCheckboxes(parentnode, 0, 0);
break;
}
},

updateCheckboxes : function(blocknode, disabled, checked) {
blocknode.all('input[type=checkbox]').each(function(node) {
node.removeAttribute('disabled');
if (disabled) {
node.setAttribute('disabled', 1)
node.removeAttribute('checked');
}
if (checked) {
node.setAttribute('checked', 1)
}
}, this);
}
}

defaultoutputs.init();
}
Loading

0 comments on commit 1d72e9d

Please sign in to comment.