Skip to content
Browse files

MDL-27171 messages: upgrade user profile messaging preferences interface

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...
1 parent 1d72e9d commit 814e373576249f1f4e05d4af0556917dc43129c7 Ruslan Kabalin committed May 17, 2011
Showing with 212 additions and 144 deletions.
  1. +3 −2 lang/en/message.php
  2. +1 −1 message/defaultoutputs.php
  3. +30 −121 message/edit.php
  4. +24 −11 message/lib.php
  5. +149 −9 message/renderer.php
  6. +5 −0 theme/standard/style/core.css
View
5 lang/en/message.php
@@ -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';
@@ -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';
View
2 message/defaultoutputs.php
@@ -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
View
151 message/edit.php
@@ -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)
@@ -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');
}
@@ -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
@@ -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();
View
35 message/lib.php
@@ -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;
@@ -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;
@@ -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;
+}
View
158 message/renderer.php
@@ -38,7 +38,7 @@ class core_message_renderer extends plugin_renderer_base {
/**
* Display the interface to manage message outputs
*
- * @param array $processors The list of message processors
+ * @param mixed $processors array of objects containing message processors
* @return string The text to render
*/
public function manage_messageoutputs($processors) {
@@ -106,10 +106,10 @@ public function manage_messageoutputs($processors) {
/**
* Display the interface to manage default message outputs
*
- * @param array $processors The list of message processors
- * @param array $providers The list of message providers
- * @param array $preferences The list of current preferences
- * @return string The text to render
+ * @param mixed $processors array of objects containing message processors
+ * @param mixed $providers array of objects containing message providers
+ * @param mixed $preferences array of objects containing current preferences
+ * @return string The text to render
*/
public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
global $CFG;
@@ -161,14 +161,14 @@ public function manage_defaultmessageoutputs($processors, $providers, $preferenc
if ($setting == 'permitted') {
$label = get_string('sendingvia', 'message', $labelparams);
// determine the current setting or use default
- $select = MESSAGE_DEFAULT_PERMITTED_VALUE;
+ $select = MESSAGE_DEFAULT_PERMITTED;
$preference = $processor->name.'_provider_'.$preferencebase;
if (array_key_exists($preference, $preferences)) {
$select = $preferences->{$preference};
}
// dropdown menu
- $cellcontent = html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
- $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
+ $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
+ $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
$cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
} else {
$label = get_string('sendingviawhen', 'message', $labelparams);
@@ -184,8 +184,8 @@ public function manage_defaultmessageoutputs($processors, $providers, $preferenc
}
// generate content
$cellcontent .= html_writer::start_tag('div');
- $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
$cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
+ $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
$cellcontent .= $statename;
$cellcontent .= html_writer::end_tag('div');
}
@@ -203,4 +203,144 @@ public function manage_defaultmessageoutputs($processors, $providers, $preferenc
return $output;
}
+ /**
+ * Display the interface for messaging options
+ *
+ * @param mixed $processors array of objects containing message processors
+ * @param mixed $providers array of objects containing message providers
+ * @param mixed $preferences array of objects containing current preferences
+ * @param mixed $defaultpreferences array of objects containing site default preferences
+ * @return string The text to render
+ */
+ public function manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences) {
+ // 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()));
+
+ /// 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'));
+
+ // Display the messging options table
+ $table = new html_table();
+ $table->attributes['class'] = 'generaltable';
+ $table->data = array();
+ $table->head = array('');
+
+ foreach ($readyprocessors as $processor) {
+ $table->head[] = get_string('pluginname', 'message_'.$processor->name);
+ }
+
+ $number_procs = count($processors);
+ // Populate the table with rows
+ foreach ( $providers as $provider) {
+ $preferencebase = $provider->component.'_'.$provider->name;
+
+ $headerrow = new html_table_row();
+ $providername = get_string('messageprovider:'.$provider->name, $provider->component);
+ $providercell = new html_table_cell($providername);
+ $providercell->header = true;
+ $providercell->colspan = $number_procs + 1;
+ $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 (array_key_exists($defaultpreference, $defaultpreferences)) {
+ $permitted = $defaultpreferences->{$defaultpreference};
+ }
+ // If settings are disallowed, just display the message that
+ // the setting is not permitted, if not use user settings or
+ // force them.
+ if ($permitted == 'disallowed') {
+ if ($state == 'loggedoff') {
+ // skip if we are rendering the second line
+ continue;
+ }
+ $cellcontent = html_writer::nonempty_tag('div', get_string('notpermitted', 'message'), array('class' => 'dimmed_text'));
+ $optioncell = new html_table_cell($cellcontent);
+ $optioncell->rowspan = 2;
+ $optioncell->attributes['class'] = 'disallowed';
+ } else {
+ // determine user preferences and use then unless we force
+ // the preferences.
+ $disabled = array();
+ if ($permitted == 'forced') {
+ $checked = true;
+ $disabled['disabled'] = 1;
+ } else {
+ $checked = false;
+ // See if hser 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 (array_key_exists($defaultpreference, $defaultpreferences)) {
+ $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), $disabled));
+ $optioncell = new html_table_cell($cellcontent);
+ $optioncell->attributes['class'] = 'mdl-align';
+ }
+ $optionrow->cells[] = $optioncell;
+ }
+ $table->data[] = $optionrow;
+ }
+ }
+ $output .= html_writer::start_tag('div');
+ $output .= html_writer::table($table);
+ $output .= html_writer::end_tag('div');
+ $output .= html_writer::end_tag('fieldset');
+
+ 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 .= get_string('blocknoncontacts', 'message').': ';
+ $output .= html_writer::checkbox('blocknoncontacts', 1, $preferences->blocknoncontacts, '');
+ $output .= html_writer::end_tag('div');
+ $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('updatemyprofile'), 'class' => 'form-submit'));
+ $output .= html_writer::end_tag('div');
+ $output .= html_writer::end_tag('form');
+ return $output;
+ }
+
}
View
5 theme/standard/style/core.css
@@ -444,3 +444,8 @@ table#tag-management-list {margin: 10px auto;width: 80%;}
/* Portfolio */
.path-portfolio .exportsummary {margin:0px auto 1em auto;}
.portfolio-add-icon {margin-left:5px;}
+
+/* Messaging options */
+#page-message-edit table.generaltable th.c0 {text-align: left;}
+#page-message-edit table.generaltable td.c0 {text-align: right;}
+#page-message-edit table.generaltable td.disallowed {text-align: center;vertical-align:middle;}

0 comments on commit 814e373

Please sign in to comment.
Something went wrong with that request. Please try again.