MDL-64786 analytics: Allow renaming models via inplace editable element

This commit is contained in:
David Mudrák 2019-04-03 18:37:54 +02:00
parent 6482734681
commit 066a4d6493
10 changed files with 170 additions and 10 deletions

View file

@ -48,7 +48,7 @@ class clihelper {
foreach ($models as $model) {
$modelid = $model->get_id();
$isenabled = $model->is_enabled() ? get_string('enabled', 'tool_analytics') : get_string('disabled', 'tool_analytics');
$name = $model->get_target()->get_name();
$name = $model->get_name();
echo str_pad($modelid, 15, ' ') . ' ' . str_pad($name, 50, ' ') . ' ' . str_pad($isenabled, 15, ' ') . "\n";
}
}

View file

@ -125,7 +125,7 @@ class invalid_analysables implements \renderable, \templatable {
// Prepare the context object.
$data = new \stdClass();
$data->modelname = $this->model->get_target()->get_name();
$data->modelname = $this->model->get_name();
if ($this->page > 0) {
$prev = clone $PAGE->url;

View file

@ -65,7 +65,7 @@ class predict_models extends \core\task\scheduled_task {
\tool_analytics\output\helper::reset_page();
if ($result) {
echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_target()->get_name()));
echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_name()));
$renderer = $PAGE->get_renderer('tool_analytics');
echo $renderer->render_get_predictions_results(false, array(), $result, $model->get_analyser()->get_logs());
}

View file

@ -76,7 +76,7 @@ class train_models extends \core\task\scheduled_task {
\tool_analytics\output\helper::reset_page();
if ($result) {
echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_target()->get_name()));
echo $OUTPUT->heading(get_string('modelresults', 'tool_analytics', $model->get_name()));
$renderer = $PAGE->get_renderer('tool_analytics');
echo $renderer->render_get_predictions_results($result, $model->get_analyser()->get_logs());

View file

@ -96,6 +96,7 @@ $string['loginfo'] = 'Log extra info';
$string['missingmoodleversion'] = 'Imported file does not define a moodle version number';
$string['modelid'] = 'Model ID';
$string['modelinvalidanalysables'] = 'Invalid analysable elements for "{$a}" model';
$string['modelname'] = 'Model name';
$string['modelresults'] = '{$a} results';
$string['modeltimesplitting'] = 'Time splitting';
$string['nextpage'] = 'Next page';

View file

@ -40,7 +40,7 @@ $url = new \moodle_url('/admin/tool/analytics/model.php', $params);
switch ($action) {
case 'edit':
$title = get_string('editmodel', 'tool_analytics', $model->get_target()->get_name());
$title = get_string('editmodel', 'tool_analytics', $model->get_name());
break;
case 'evaluate':
$title = get_string('evaluatemodel', 'tool_analytics');

View file

@ -118,7 +118,7 @@
<caption>{{#str}}analyticmodels, tool_analytics{{/str}}</caption>
<thead>
<tr>
<th scope="col">{{#str}}target, tool_analytics{{/str}}</th>
<th scope="col">{{#str}}modelname, tool_analytics{{/str}}</th>
<th scope="col">{{#str}}enabled, tool_analytics{{/str}}</th>
<th scope="col">{{#str}}indicators, tool_analytics{{/str}}</th>
<th scope="col">{{#str}}modeltimesplitting, tool_analytics{{/str}}</th>
@ -130,10 +130,15 @@
{{#models}}
<tr>
<td>
<span class="target-name">{{target}}</span>
{{#name}}
<span class="model-name">{{>core/inplace_editable}}</span>
{{/name}}
<div>
<small class="target-class">{{targetclass}}</small>
{{#targethelp}}
{{>core/help_icon}}
{{/targethelp}}
</div>
</td>
<td>
{{#enabled}}

View file

@ -1446,7 +1446,10 @@ class model {
\core_analytics\manager::check_can_manage_models();
$data = clone $this->model;
$data->name = $this->inplace_editable_name()->export_for_template($output);
$data->target = $this->get_target()->get_name();
$data->targetclass = $this->get_target()->get_id();
if ($timesplitting = $this->get_time_splitting()) {
$data->timesplitting = $timesplitting->get_name();
@ -1691,6 +1694,54 @@ class model {
$DB->update_record('analytics_models', $this->model);
}
/**
* Returns the name of the model.
*
* By default, models use their target's name as their own name. They can have their explicit name, too. In which
* case, the explicit name is used instead of the default one.
*
* @return string|lang_string
*/
public function get_name() {
if (trim($this->model->name) === '') {
return $this->get_target()->get_name();
} else {
return $this->model->name;
}
}
/**
* Renames the model to the given name.
*
* When given an empty string, the model falls back to using the associated target's name as its name.
*
* @param string $name The new name for the model, empty string for using the default name.
*/
public function rename(string $name) {
global $DB, $USER;
$this->model->name = $name;
$this->model->timemodified = time();
$this->model->usermodified = $USER->id;
$DB->update_record('analytics_models', $this->model);
}
/**
* Returns an inplace editable element with the model's name.
*
* @return \core\output\inplace_editable
*/
public function inplace_editable_name() {
$displayname = format_string($this->get_name());
return new \core\output\inplace_editable('core_analytics', 'modelname', $this->model->id,
has_capability('moodle/analytics:managemodels', \context_system::instance()), $displayname, $this->model->name);
}
/**
* Adds the id from {analytics_predictions} db table to the prediction \stdClass objects.
*

46
analytics/lib.php Normal file
View file

@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - https://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/>.
/**
* The interface library between the core and the subsystem.
*
* @package core_analytics
* @copyright 2019 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Implements the inplace editable feature.
*
* @param string $itemtype Type if the inplace editable element
* @param int $itemid Identifier of the element
* @param string $newvalue New value for the element
* @return \core\output\inplace_editable
*/
function core_analytics_inplace_editable($itemtype, $itemid, $newvalue) {
if ($itemtype === 'modelname') {
\external_api::validate_context(context_system::instance());
require_capability('moodle/analytics:managemodels', \context_system::instance());
$model = new \core_analytics\model($itemid);
$model->rename(clean_param($newvalue, PARAM_NOTAGS));
return $model->inplace_editable_name();
}
}

View file

@ -437,6 +437,63 @@ class analytics_model_testcase extends advanced_testcase {
$this->assertCount(1, $modeldata->indicators);
}
/**
* Test the implementation of {@link \core_analytics\model::inplace_editable_name()}.
*/
public function test_inplace_editable_name() {
global $PAGE;
$this->resetAfterTest();
$output = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
// Check as a user with permission to edit the name.
$this->setAdminUser();
$ie = $this->model->inplace_editable_name();
$this->assertInstanceOf(\core\output\inplace_editable::class, $ie);
$data = $ie->export_for_template($output);
$this->assertEquals('core_analytics', $data['component']);
$this->assertEquals('modelname', $data['itemtype']);
// Check as a user without permission to edit the name.
$this->setGuestUser();
$ie = $this->model->inplace_editable_name();
$this->assertInstanceOf(\core\output\inplace_editable::class, $ie);
$data = $ie->export_for_template($output);
$this->assertArrayHasKey('displayvalue', $data);
}
/**
* Test how the models present themselves in the UI and that they can be renamed.
*/
public function test_get_name_and_rename() {
global $PAGE;
$this->resetAfterTest();
$output = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
// By default, the model exported for template uses its target's name in the name inplace editable element.
$this->assertEquals($this->model->get_name(), $this->model->get_target()->get_name());
$data = $this->model->export($output);
$this->assertEquals($data->name['displayvalue'], $this->model->get_target()->get_name());
$this->assertEquals($data->name['value'], '');
// Rename the model.
$this->model->rename('Nějaký pokusný model');
$this->assertEquals($this->model->get_name(), 'Nějaký pokusný model');
$data = $this->model->export($output);
$this->assertEquals($data->name['displayvalue'], 'Nějaký pokusný model');
$this->assertEquals($data->name['value'], 'Nějaký pokusný model');
// Undo the renaming.
$this->model->rename('');
$this->assertEquals($this->model->get_name(), $this->model->get_target()->get_name());
$data = $this->model->export($output);
$this->assertEquals($data->name['displayvalue'], $this->model->get_target()->get_name());
$this->assertEquals($data->name['value'], '');
}
/**
* Generates a model log record.
*/