MDL-64739 core_analytics: New db field for model contexts

This commit is contained in:
Mitxel Moriana 2019-09-16 10:30:00 +08:00 committed by David Monllaó
parent 86a092b61d
commit c345aa721c
5 changed files with 68 additions and 1 deletions

View File

@ -89,6 +89,18 @@ class edit_model extends \moodleform {
$mform->addHelpButton('indicators', 'indicators', 'tool_analytics');
}
// Contexts restriction.
$contexts = array();
if ($this->_customdata['contexts']) {
$contexts = \tool_analytics\output\helper::contexts_to_options($this->_customdata['contexts']);
}
$options = array(
'multiple' => true
);
$mform->addElement('autocomplete', 'contexts', get_string('contexts', 'tool_analytics'), $contexts, $options);
$mform->setType('contexts', PARAM_ALPHANUMEXT);
$mform->addHelpButton('contexts', 'indicators', 'tool_analytics');
// Time-splitting methods.
if (!empty($this->_customdata['invalidcurrenttimesplitting'])) {
$mform->addElement('html', $OUTPUT->notification(

View File

@ -151,4 +151,39 @@ class helper {
$singleselect = new \single_select($url, 'contextid', $contexts, $selected, $nothing);
return $singleselect->export_for_template($output);
}
/**
* Converts a list of contexts to an array of options that can be used in a autocomplete moodleform field.
*
* @param array $contexts Array of context ids.
* @param bool $includeall Wether to include the "all" context option (the system context).
* @param bool $shortentext Wether to shorten the context names.
* @return array Associative array with context ids as keys and context names as values.
*/
public static function contexts_to_options(array $contexts, ?bool $includeall = false, ?bool $shortentext = true): array {
foreach ($contexts as $contextid) {
$context = \context::instance_by_id($contextid);
// Special name for system level predictions as showing "System is not visually nice".
if ($contextid == SYSCONTEXTID) {
$contextname = get_string('allpredictions', 'tool_analytics');
} else {
if ($shortentext) {
$contextname = shorten_text($context->get_context_name(false, true), 40);
} else {
$contextname = $context->get_context_name(false, true);
}
}
$contexts[$contextid] = $contextname;
}
if ($includeall) {
$contexts[0] = get_string('all');
}
\core_collator::asort($contexts);
return $contexts;
}
}

View File

@ -3844,6 +3844,7 @@
<FIELD NAME="timesplitting" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="predictionsprocessor" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="version" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="contextids" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The model will be restricted to this contexts"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>

View File

@ -3619,5 +3619,24 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2019101600.01);
}
if ($oldversion < 2019101800.12) {
// Get the table by its previous name.
$table = new xmldb_table('analytics_models');
if ($dbman->table_exists($table)) {
// Define field contextids to be added to analytics_models.
$field = new xmldb_field('contextids', XMLDB_TYPE_TEXT, null, null, null, null, null, 'version');
// Conditionally launch add field contextids.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019101800.12);
}
return true;
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2019101800.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2019101800.12; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.