mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-64786 analytics: Allow renaming models via inplace editable element
This commit is contained in:
parent
6482734681
commit
066a4d6493
10 changed files with 170 additions and 10 deletions
|
@ -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
46
analytics/lib.php
Normal 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();
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue