Skip to content

Commit

Permalink
MDL-27171 messages: upgrade user profile messaging preferences interface
Browse files Browse the repository at this point in the history
Existing user messaging preferences should accomodate default settings and
permissions.  This also includes some changes to default outputs preferences.

Signed-off-by: Ruslan Kabalin <ruslan.kabalin@luns.net.uk>
  • Loading branch information
Ruslan Kabalin committed May 27, 2011
1 parent 1d72e9d commit 814e373
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 144 deletions.
5 changes: 3 additions & 2 deletions lang/en/message.php
Expand Up @@ -89,6 +89,7 @@
$string['nomessagesfound'] = 'No messages were found';
$string['noreply'] = 'Do not reply to this message';
$string['nosearchresults'] = 'There were no results from your search';
$string['notpermitted'] = 'Not permitted';
$string['offline'] = 'Offline';
$string['offlinecontacts'] = 'Offline contacts ({$a})';
$string['online'] = 'Online';
Expand All @@ -115,8 +116,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['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
2 changes: 1 addition & 1 deletion message/defaultoutputs.php
Expand Up @@ -105,7 +105,7 @@
$renderer = $PAGE->get_renderer('core', 'message');

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

// Display the page
Expand Down
151 changes: 30 additions & 121 deletions message/edit.php
Expand Up @@ -23,7 +23,8 @@
* @package message
*/

require_once('../config.php');
require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once(dirname(dirname(__FILE__)) . '/message/lib.php');

$userid = optional_param('id', $USER->id, PARAM_INT); // user id
$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
Expand Down Expand Up @@ -97,55 +98,39 @@

/// Set all the preferences for all the message providers
$providers = message_get_my_providers();
$possiblestates = array('loggedin', 'loggedoff');
foreach ( $providers as $providerid => $provider){
foreach ($possiblestates as $state){
foreach ( $providers as $provider) {
$componentproviderbase = $provider->component.'_'.$provider->name;
foreach (array('loggedin', 'loggedoff') as $state) {
$linepref = '';
$componentproviderstate = $provider->component.'_'.$provider->name.'_'.$state;
$componentproviderstate = $componentproviderbase.'_'.$state;
if (array_key_exists($componentproviderstate, $form)) {
foreach ($form->{$componentproviderstate} as $process=>$one){
foreach (array_keys($form->{$componentproviderstate}) as $process){
if ($linepref == ''){
$linepref = $process;
} else {
$linepref .= ','.$process;
}
}
}
$preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
}
}
foreach ( $providers as $providerid => $provider){
foreach ($possiblestates as $state){
$preferencekey = 'message_provider_'.$provider->component.'_'.$provider->name.'_'.$state;
if (empty($preferences[$preferencekey])) {
$preferences[$preferencekey] = 'none';
if (empty($linepref)) {
$linepref = 'none';
}
$preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
}
}

/// Set all the processor options as well
$processors = $DB->get_records('message_processors');
foreach ( $processors as $processorid => $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();
$pclass->process_form($form, $preferences);
} else{
print_error('errorcallingprocessor', 'message');
}
}
$processors = get_message_processors(true);
foreach ($processors as $processor) {
$processor->object->process_form($form, $preferences);
}

//process general messaging preferences
$preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
$preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0;
//$preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0;

// Save all the new preferences to the database
if (!set_user_preferences( $preferences, $user->id ) ){
if (!set_user_preferences($preferences, $user->id)) {
print_error('cannotupdateusermsgpref');
}

Expand All @@ -158,34 +143,25 @@

/// Get providers preferences
$providers = message_get_my_providers();
foreach ( $providers as $providerid => $provider){
foreach (array('loggedin', 'loggedoff') as $state){
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){
foreach ($lineprefarray as $pref) {
$preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
}
}
}

// Load all processors
$processors = get_message_processors();
/// For every processors put its options on the form (need to get function from processor's lib.php)
$processors = $DB->get_records('message_processors');
foreach ( $processors as $processorid => $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();
$pclass->load_data($preferences, $user->id);
} else{
print_error('errorcallingprocessor', 'message');
}
}
foreach ($processors as $processor) {
$processor->object->load_data($preferences, $user->id);
}

//load general messaging preferences
Expand All @@ -203,84 +179,17 @@
} else {
$PAGE->set_heading($course->fullname);
}
echo $OUTPUT->header();

// Start the form. We're not using mform here because of our special formatting needs ...
echo '<form class="mform" method="post" action="'.$PAGE->url.'">';

/// Settings table...
echo '<fieldset id="providers" class="clearfix">';
echo '<legend class="ftoggler">'.get_string('providers_config', 'message').'</legend>';
// Grab the renderer
$renderer = $PAGE->get_renderer('core', 'message');
// Fetch message providers
$providers = message_get_my_providers();
$processors = $DB->get_records('message_processors', null, 'name DESC');
$number_procs = count($processors);
echo '<table cellpadding="2"><tr><td>&nbsp;</td>'."\n";
foreach ( $processors as $processorid => $processor){
echo '<th align="center">'.get_string('pluginname', 'message_'.$processor->name).'</th>';
}
echo '</tr>';
// Fetch default (site) preferences
$defaultpreferences = get_message_output_default_preferences();

foreach ( $providers as $providerid => $provider){
$providername = get_string('messageprovider:'.$provider->name, $provider->component);

echo '<tr><th align="right">'.$providername.'</th><td colspan="'.$number_procs.'"></td></tr>'."\n";
foreach (array('loggedin', 'loggedoff') as $state){
$state_res = get_string($state.'description', 'message');
echo '<tr><td align="right">'.$state_res.'</td>'."\n";
foreach ( $processors as $processorid => $processor) {
if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state})) {
$checked = '';
} else if (!isset($preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name])) {
$checked = '';
} else {
$checked = $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$processor->name]==1?" checked=\"checked\"":"";
}
echo '<td align="center"><input type="checkbox" name="'.$provider->component.'_'.$provider->name.'_'.$state.'['.$processor->name.']" '.$checked.' /></td>'."\n";
}
echo '</tr>'."\n";
}
}
echo '</table>';
echo '</fieldset>';

/// Show all the message processors
$processors = $DB->get_records('message_processors');

$processorconfigform = null;
foreach ($processors as $processorid => $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();
$processorconfigform = $pclass->config_form($preferences);

if (!empty($processorconfigform)) {
echo '<fieldset id="messageprocessor_'.$processor->name.'" class="clearfix">';
echo '<legend class="ftoggler">'.get_string('pluginname', 'message_'.$processor->name).'</legend>';

echo $processorconfigform;

echo '</fieldset>';
}
} else{
print_error('errorcallingprocessor', 'message');
}
}
}

echo '<fieldset id="messageprocessor_general" class="clearfix">';
echo '<legend class="ftoggler">'.get_string('generalsettings','admin').'</legend>';
echo get_string('blocknoncontacts', 'message').': <input type="checkbox" name="blocknoncontacts" '.($preferences->blocknoncontacts==1?' checked="checked"':'');
//get_string('beepnewmessage', 'message').': <input type="checkbox" name="beepnewmessage" '.($preferences->beepnewmessage==1?" checked=\"checked\"":"").' />';
echo '</fieldset>';

echo '<div><input type="hidden" name="sesskey" value="'.sesskey().'" /></div>';
echo '<div style="text-align:center"><input name="submit" value="'. get_string('updatemyprofile') .'" type="submit" /></div>';

echo "</form>";
$messagingoptions = $renderer->manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences);

echo $OUTPUT->header();
echo $messagingoptions;
echo $OUTPUT->footer();

35 changes: 24 additions & 11 deletions message/lib.php
Expand Up @@ -56,7 +56,7 @@
/**
* Set default value for default outputs permitted setting
*/
define('MESSAGE_DEFAULT_PERMITTED_VALUE', 'disallowed');
define('MESSAGE_DEFAULT_PERMITTED', 'permitted');

if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds
$CFG->message_contacts_refresh = 60;
Expand Down Expand Up @@ -2174,25 +2174,27 @@ 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
* Get all message processors, validate corresponding plugin existance and
* system configuration
* @param bool $ready only return ready-to-use processors
* @return mixed $processors array of objects containing information on message processors
*/
function get_message_processors($enabled = false) {
function get_message_processors($ready = false) {
global $DB, $CFG;

static $processors;

if (empty($processors)) {
$processors = $DB->get_records('message_processors', null, 'name');
// Get all processors, ensure the name column is the first so it will be the array key
$processors = $DB->get_records('message_processors', null, 'name DESC', 'name, id, enabled');
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->object = $pclass;
$processor->configured = 0;
if ($pclass->is_system_configured()) {
$processor->configured = 1;
Expand All @@ -2210,11 +2212,22 @@ function get_message_processors($enabled = false) {
}
}
}
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;'));
if ($ready) {
// Filter out enabled, available and system_configured processors only.
$processors = array_filter($processors, create_function('$a', 'return $a->enabled && $a->configured;'));
}

return $processors;
}

/**
* Get messaging outputs default (site) preferences
* @return object $processors object containing information on message processors
*/
function get_message_output_default_preferences() {
$preferences = get_config('message');
if (!$preferences) {
$preferences = (object) array();
}
return $preferences;
}

0 comments on commit 814e373

Please sign in to comment.