Skip to content

Commit

Permalink
MDL-65492 cache admin: make the code easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Jun 12, 2019
1 parent f350727 commit 1baf3af
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 62 deletions.
46 changes: 23 additions & 23 deletions cache/admin.php
Expand Up @@ -44,9 +44,9 @@
admin_externalpage_setup('cacheconfig');
$context = context_system::instance();

$stores = cache_administration_helper::get_store_instance_summaries();
$plugins = cache_administration_helper::get_store_plugin_summaries();
$definitions = cache_administration_helper::get_definition_summaries();
$storeinstancesummaries = cache_administration_helper::get_store_instance_summaries();
$storepluginsummaries = cache_administration_helper::get_store_plugin_summaries();
$definitionsummaries = cache_administration_helper::get_definition_summaries();
$defaultmodestores = cache_administration_helper::get_default_mode_stores();
$locks = cache_administration_helper::get_lock_summaries();

Expand All @@ -63,11 +63,11 @@
break;
case 'addstore' : // Add the requested store.
$plugin = required_param('plugin', PARAM_PLUGIN);
if (!$plugins[$plugin]['canaddinstance']) {
if (!$storepluginsummaries[$plugin]['canaddinstance']) {
print_error('ex_unmetstorerequirements', 'cache');
}
$mform = cache_administration_helper::get_add_store_form($plugin);
$title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
$title = get_string('addstore', 'cache', $storepluginsummaries[$plugin]['name']);
if ($mform->is_cancelled()) {
redirect($PAGE->url);
} else if ($data = $mform->get_data()) {
Expand All @@ -80,14 +80,14 @@
}
}
$writer->add_store_instance($data->name, $data->plugin, $config);
redirect($PAGE->url, get_string('addstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
redirect($PAGE->url, get_string('addstoresuccess', 'cache', $storepluginsummaries[$plugin]['name']), 5);
}
break;
case 'editstore' : // Edit the requested store.
$plugin = required_param('plugin', PARAM_PLUGIN);
$store = required_param('store', PARAM_TEXT);
$mform = cache_administration_helper::get_edit_store_form($plugin, $store);
$title = get_string('addstore', 'cache', $plugins[$plugin]['name']);
$title = get_string('addstore', 'cache', $storepluginsummaries[$plugin]['name']);
if ($mform->is_cancelled()) {
redirect($PAGE->url);
} else if ($data = $mform->get_data()) {
Expand All @@ -101,17 +101,17 @@
}
}
$writer->edit_store_instance($data->name, $data->plugin, $config);
redirect($PAGE->url, get_string('editstoresuccess', 'cache', $plugins[$plugin]['name']), 5);
redirect($PAGE->url, get_string('editstoresuccess', 'cache', $storepluginsummaries[$plugin]['name']), 5);
}
break;
case 'deletestore' : // Delete a given store.
$store = required_param('store', PARAM_TEXT);
$confirm = optional_param('confirm', false, PARAM_BOOL);

if (!array_key_exists($store, $stores)) {
if (!array_key_exists($store, $storeinstancesummaries)) {
$notifysuccess = false;
$notifications[] = array(get_string('invalidstore', 'cache'), false);
} else if ($stores[$store]['mappings'] > 0) {
} else if ($storeinstancesummaries[$store]['mappings'] > 0) {
$notifysuccess = false;
$notifications[] = array(get_string('deletestorehasmappings', 'cache'), false);
}
Expand All @@ -127,7 +127,7 @@
$PAGE->set_heading($SITE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$confirmation = get_string('deletestoreconfirmation', 'cache', $stores[$store]['name']);
$confirmation = get_string('deletestoreconfirmation', 'cache', $storeinstancesummaries[$store]['name']);
echo $OUTPUT->confirm($confirmation, $button, $PAGE->url);
echo $OUTPUT->footer();
exit;
Expand All @@ -140,7 +140,7 @@
break;
case 'editdefinitionmapping' : // Edit definition mappings.
$definition = required_param('definition', PARAM_SAFEPATH);
if (!array_key_exists($definition, $definitions)) {
if (!array_key_exists($definition, $definitionsummaries)) {
throw new cache_exception('Invalid cache definition requested');
}
$title = get_string('editdefinitionmappings', 'cache', $definition);
Expand All @@ -161,22 +161,22 @@
break;
case 'editdefinitionsharing' :
$definition = required_param('definition', PARAM_SAFEPATH);
if (!array_key_exists($definition, $definitions)) {
if (!array_key_exists($definition, $definitionsummaries)) {
throw new cache_exception('Invalid cache definition requested');
}
$title = get_string('editdefinitionsharing', 'cache', $definition);
$sharingoptions = $definitions[$definition]['sharingoptions'];
$sharingoptions = $definitionsummaries[$definition]['sharingoptions'];
$customdata = array('definition' => $definition, 'sharingoptions' => $sharingoptions);
$mform = new cache_definition_sharing_form($PAGE->url, $customdata);
$mform->set_data(array(
'sharing' => $definitions[$definition]['selectedsharingoption'],
'userinputsharingkey' => $definitions[$definition]['userinputsharingkey']
'sharing' => $definitionsummaries[$definition]['selectedsharingoption'],
'userinputsharingkey' => $definitionsummaries[$definition]['userinputsharingkey']
));
if ($mform->is_cancelled()) {
redirect($PAGE->url);
} else if ($data = $mform->get_data()) {
$component = $definitions[$definition]['component'];
$area = $definitions[$definition]['area'];
$component = $definitionsummaries[$definition]['component'];
$area = $definitionsummaries[$definition]['area'];
// Purge the stores removing stale data before we alter the sharing option.
cache_helper::purge_stores_used_by_definition($component, $area);
$writer = cache_config_writer::instance();
Expand All @@ -187,7 +187,7 @@
}
break;
case 'editmodemappings': // Edit default mode mappings.
$mform = new cache_mode_mappings_form(null, $stores);
$mform = new cache_mode_mappings_form(null, $storeinstancesummaries);
$mform->set_data(array(
'mode_'.cache_store::MODE_APPLICATION => key($defaultmodestores[cache_store::MODE_APPLICATION]),
'mode_'.cache_store::MODE_SESSION => key($defaultmodestores[cache_store::MODE_SESSION]),
Expand Down Expand Up @@ -282,7 +282,7 @@

// Add cache store warnings to the list of notifications.
// Obviously as these are warnings they are show as failures.
foreach (cache_helper::warnings($stores) as $warning) {
foreach (cache_helper::warnings($storeinstancesummaries) as $warning) {
$notifications[] = array($warning, false);
}

Expand All @@ -298,9 +298,9 @@
if ($mform instanceof moodleform) {
$mform->display();
} else {
echo $renderer->store_plugin_summaries($plugins);
echo $renderer->store_instance_summariers($stores, $plugins);
echo $renderer->definition_summaries($definitions, $context);
echo $renderer->store_plugin_summaries($storepluginsummaries);
echo $renderer->store_instance_summariers($storeinstancesummaries, $storepluginsummaries);
echo $renderer->definition_summaries($definitionsummaries, $context);
echo $renderer->lock_summaries($locks);

$applicationstore = join(', ', $defaultmodestores[cache_store::MODE_APPLICATION]);
Expand Down
28 changes: 19 additions & 9 deletions cache/locallib.php
Expand Up @@ -738,7 +738,9 @@ public static function get_store_instance_summaries() {

/**
* Returns an array of information about plugins, everything a renderer needs.
* @return array
*
* @return array for each store, an array containing various information about each store.
* See the code below for details
*/
public static function get_store_plugin_summaries() {
$return = array();
Expand Down Expand Up @@ -779,7 +781,9 @@ public static function get_store_plugin_summaries() {

/**
* Returns an array about the definitions. All the information a renderer needs.
* @return array
*
* @return array for each store, an array containing various information about each store.
* See the code below for details
*/
public static function get_definition_summaries() {
$factory = cache_factory::instance();
Expand Down Expand Up @@ -845,10 +849,14 @@ public static function get_definition_sharing_options($sharingoption, $isselecte

/**
* Returns all of the actions that can be performed on a definition.
* @param context $context
* @return array
*
* @param context $context the system context.
* @param array $definitionsummary information about this cache, from the array returned by
* cache_administration_helper::get_definition_summaries(). Currently only 'sharingoptions'
* element is used.
* @return array of actions. Each action is an array with two elements, 'text' and 'url'.
*/
public static function get_definition_actions(context $context, array $definition) {
public static function get_definition_actions(context $context, array $definitionsummary) {
if (has_capability('moodle/site:config', $context)) {
$actions = array();
// Edit mappings.
Expand All @@ -857,7 +865,7 @@ public static function get_definition_actions(context $context, array $definitio
'url' => new moodle_url('/cache/admin.php', array('action' => 'editdefinitionmapping', 'sesskey' => sesskey()))
);
// Edit sharing.
if (count($definition['sharingoptions']) > 1) {
if (count($definitionsummary['sharingoptions']) > 1) {
$actions[] = array(
'text' => get_string('editsharing', 'cache'),
'url' => new moodle_url('/cache/admin.php', array('action' => 'editdefinitionsharing', 'sesskey' => sesskey()))
Expand All @@ -877,8 +885,9 @@ public static function get_definition_actions(context $context, array $definitio
* Returns all of the actions that can be performed on a store.
*
* @param string $name The name of the store
* @param array $storedetails
* @return array
* @param array $storedetails information about this store, from the array returned by
* cache_administration_helper::get_store_instance_summaries().
* @return array of actions. Each action is an array with two elements, 'text' and 'url'.
*/
public static function get_store_instance_actions($name, array $storedetails) {
$actions = array();
Expand All @@ -902,11 +911,12 @@ public static function get_store_instance_actions($name, array $storedetails) {
return $actions;
}


/**
* Returns all of the actions that can be performed on a plugin.
*
* @param string $name The name of the plugin
* @param array $plugindetails information about this store, from the array returned by
* cache_administration_helper::get_store_plugin_summaries().
* @param array $plugindetails
* @return array
*/
Expand Down
66 changes: 36 additions & 30 deletions cache/renderer.php
Expand Up @@ -40,11 +40,13 @@ class core_cache_renderer extends plugin_renderer_base {
/**
* Displays store summaries.
*
* @param array $stores
* @param array $plugins
* @param array $storeinstancesummaries information about each store instance,
* as returned by cache_administration_helper::get_store_instance_summaries().
* @param array $storepluginsummaries information about each store plugin as
* returned by cache_administration_helper::get_store_plugin_summaries().
* @return string HTML
*/
public function store_instance_summariers(array $stores, array $plugins) {
public function store_instance_summariers(array $storeinstancesummaries, array $storepluginsummaries) {
$table = new html_table();
$table->head = array(
get_string('storename', 'cache'),
Expand All @@ -70,63 +72,63 @@ public function store_instance_summariers(array $stores, array $plugins) {

$defaultstoreactions = get_string('defaultstoreactions', 'cache');

foreach ($stores as $name => $store) {
$actions = cache_administration_helper::get_store_instance_actions($name, $store);
foreach ($storeinstancesummaries as $name => $storesummary) {
$actions = cache_administration_helper::get_store_instance_actions($name, $storesummary);
$modes = array();
foreach ($store['modes'] as $mode => $enabled) {
foreach ($storesummary['modes'] as $mode => $enabled) {
if ($enabled) {
$modes[] = get_string('mode_'.$mode, 'cache');
}
}

$supports = array();
foreach ($store['supports'] as $support => $enabled) {
foreach ($storesummary['supports'] as $support => $enabled) {
if ($enabled) {
$supports[] = get_string('supports_'.$support, 'cache');
}
}

$info = '';
if (!empty($store['default'])) {
if (!empty($storesummary['default'])) {
$info = $this->output->pix_icon('i/info', $defaultstoreactions, '', array('class' => 'icon'));
}
$htmlactions = array();
foreach ($actions as $action) {
$htmlactions[] = $this->output->action_link($action['url'], $action['text']);
}

$isready = $store['isready'] && $store['requirementsmet'];
$isready = $storesummary['isready'] && $storesummary['requirementsmet'];
$readycell = new html_table_cell;
if ($isready) {
$readycell->text = $this->output->pix_icon('i/valid', '1');
}

$storename = $store['name'];
if (!empty($store['default'])) {
$storename = get_string('store_'.$store['name'], 'cache');
$storename = $storesummary['name'];
if (!empty($storesummary['default'])) {
$storename = get_string('store_'.$storesummary['name'], 'cache');
}
if (!$isready && (int)$store['mappings'] > 0) {
if (!$isready && (int)$storesummary['mappings'] > 0) {
$readycell->text = $this->output->help_icon('storerequiresattention', 'cache');
$readycell->attributes['class'] = 'store-requires-attention';
}

$lock = $store['lock']['name'];
if (!empty($store['lock']['default'])) {
$lock = get_string($store['lock']['name'], 'cache');
$lock = $storesummary['lock']['name'];
if (!empty($storesummary['lock']['default'])) {
$lock = get_string($storesummary['lock']['name'], 'cache');
}

$row = new html_table_row(array(
$storename,
get_string('pluginname', 'cachestore_'.$store['plugin']),
get_string('pluginname', 'cachestore_'.$storesummary['plugin']),
$readycell,
$store['mappings'],
$storesummary['mappings'],
join(', ', $modes),
join(', ', $supports),
$lock,
$info.join(', ', $htmlactions)
));
$row->attributes['class'] = 'store-'.$name;
if ($store['default']) {
if ($storesummary['default']) {
$row->attributes['class'] .= ' default-store';
}
$table->data[] = $row;
Expand All @@ -140,12 +142,13 @@ public function store_instance_summariers(array $stores, array $plugins) {
}

/**
* Displays plugin summaries
* Displays plugin summaries.
*
* @param array $plugins
* @param array $storepluginsummaries information about each store plugin as
* returned by cache_administration_helper::get_store_plugin_summaries().
* @return string HTML
*/
public function store_plugin_summaries(array $plugins) {
public function store_plugin_summaries(array $storepluginsummaries) {
$table = new html_table();
$table->head = array(
get_string('plugin', 'cache'),
Expand All @@ -165,7 +168,7 @@ public function store_plugin_summaries(array $plugins) {
);
$table->data = array();

foreach ($plugins as $name => $plugin) {
foreach ($storepluginsummaries as $name => $plugin) {
$actions = cache_administration_helper::get_store_plugin_actions($name, $plugin);

$modes = array();
Expand Down Expand Up @@ -208,12 +211,15 @@ public function store_plugin_summaries(array $plugins) {
}

/**
* Displays definition summaries
* Displays definition summaries.
*
* @param array $definitions
* @return string HTML
* @param array $definitionsummaries information about each definition, as returned by
* cache_administration_helper::get_definition_summaries().
* @param context $context the system context.
*
* @return string HTML.
*/
public function definition_summaries(array $definitions, context $context) {
public function definition_summaries(array $definitionsummaries, context $context) {
$table = new html_table();
$table->head = array(
get_string('definition', 'cache'),
Expand All @@ -235,10 +241,10 @@ public function definition_summaries(array $definitions, context $context) {
);
$table->data = array();

core_collator::asort_array_of_arrays_by_key($definitions, 'name');
core_collator::asort_array_of_arrays_by_key($definitionsummaries, 'name');

$none = new lang_string('none', 'cache');
foreach ($definitions as $id => $definition) {
foreach ($definitionsummaries as $id => $definition) {
$actions = cache_administration_helper::get_definition_actions($context, $definition);
$htmlactions = array();
foreach ($actions as $action) {
Expand Down Expand Up @@ -400,4 +406,4 @@ public function notifications(array $notifications = array()) {
$html .= html_writer::end_div();
return $html;
}
}
}

0 comments on commit 1baf3af

Please sign in to comment.