Skip to content

Commit

Permalink
Merge branch 'MDL-73546-master' of https://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jan 12, 2022
2 parents 58bcc49 + 65d56fa commit 7be7bb5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
50 changes: 37 additions & 13 deletions admin/tool/admin_presets/classes/local/action/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,37 @@ public function execute(): void {
* the preset available settings.
*/
public function show(): void {
$this->display_preset(true);
}

/**
* Displays a preset information (name, description, settings different from the current configuration...).
*/
public function preview(): void {
$this->display_preset(false, false);
}

/**
* Method to prepare the information to preview/load the preset.
*
* @param bool $displayform Whether the form should be displayed in the page or not.
* @param bool $raiseexception Whether the exception should be raised or not when the preset doesn't exist. When it's set
* to false, a message is displayed, instead of raising the exception.
*/
protected function display_preset(bool $displayform = true, bool $raiseexception = true) {
global $DB, $OUTPUT;

$data = new stdClass();
$data->id = $this->id;

// Preset data.
if (!$preset = $DB->get_record('adminpresets', ['id' => $data->id])) {
throw new moodle_exception('errornopreset', 'core_adminpresets');
if ($raiseexception) {
throw new moodle_exception('errornopreset', 'core_adminpresets');
} else {
$this->outputs = get_string('errornopreset', 'core_adminpresets');
return;
}
}

// Print preset basic data.
Expand All @@ -125,22 +148,23 @@ public function show(): void {
$applieddata->settings = $applied;
$applieddata->beforeapplying = true;
$application->appliedchanges = $applieddata;
if (empty($applied)) {
// Display a warning when no settings will be applied.
$applieddata->message = get_string('nosettingswillbeapplied', 'tool_admin_presets');
if ($displayform) {
if (empty($applied)) {
// Display a warning when no settings will be applied.
$applieddata->message = get_string('nosettingswillbeapplied', 'tool_admin_presets');

// Only display the Continue button.
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
$this->moodleform = new continue_form($url);
} else {
// Display the form to apply the preset.
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
$this->moodleform = new load_form($url);
$this->moodleform->set_data($data);
// Only display the Continue button.
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
$this->moodleform = new continue_form($url);
} else {
// Display the form to apply the preset.
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
$this->moodleform = new load_form($url);
$this->moodleform->set_data($data);
}
}

$this->outputs .= $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);

}

protected function get_explanatory_description(): ?string {
Expand Down
27 changes: 27 additions & 0 deletions admin/tool/admin_presets/tests/local/action/load_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ public function test_load_show_unexisting_preset(): void {
$action->show();
}


/**
* Test the behaviour of preview() method when the preset id doesn't exist.
*
* @covers ::preview
*/
public function test_load_preview_unexisting_preset(): void {

$this->resetAfterTest();
$this->setAdminUser();

// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();

// Initialise the parameters and create the load class.
$_POST['action'] = 'load';
$_POST['mode'] = 'preview';
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.

$action = new load();
$action->preview();
$outputs = $generator->access_protected($action, 'outputs');
// In that case, no exception should be raised and the text of no preset found should be displayed.
$this->assertEquals(get_string('errornopreset', 'core_adminpresets'), $outputs);
}

/**
* Test the behaviour of execute() method.
*
Expand Down
2 changes: 1 addition & 1 deletion lang/en/adminpresets.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$string['enabled'] = 'Enabled';
$string['errordeleting'] = 'Error deleting from database.';
$string['errorinserting'] = 'Error inserting into database.';
$string['errornopreset'] = 'It doesn\'t exists a preset with that name.';
$string['errornopreset'] = 'It doesn\'t exist a preset with that name.';
$string['fullpreset'] = 'Full';
$string['fullpresetdescription'] = 'All the Starter features plus External (LTI) tool, SCORM, Workshop, Analytics, Badges, Competencies, Learning plans and lots more.';
$string['markedasadvanced'] = 'marked as advanced';
Expand Down

0 comments on commit 7be7bb5

Please sign in to comment.