Skip to content

Commit

Permalink
Merge branch 'MDL-75148-master' of https://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov committed Aug 2, 2022
2 parents 3dff2b6 + 599aca6 commit 5c1df4a
Show file tree
Hide file tree
Showing 22 changed files with 825 additions and 137 deletions.
5 changes: 3 additions & 2 deletions mod/data/amd/build/selectpreset.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mod/data/amd/build/selectpreset.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 37 additions & 20 deletions mod/data/amd/src/selectpreset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import Notification from 'core/notification';
import {get_string as getString} from 'core/str';

const selectors = {
presetRadioButton: 'input[name="fullname"]',
selectPresetButton: 'input[name="selectpreset"]',
selectedPresetRadioButton: 'input[name="fullname"]:checked',
};
Expand All @@ -33,22 +31,41 @@ const selectors = {
* Initialize module.
*/
export const init = () => {
const selectPresetButton = document.querySelector(selectors.selectPresetButton);

selectPresetButton.addEventListener('click', event => {
event.preventDefault();
// Validate whether there is a selected preset before submitting the form.
if (document.querySelectorAll(selectors.selectedPresetRadioButton).length > 0) {
const presetsForm = event.target.closest('form');
presetsForm.submit();
} else {
// No selected presets. Display an error message to user.
getString('presetnotselected', 'mod_data').then((str) => {
return Notification.addNotification({
type: 'error',
message: str
});
}).catch(Notification.exception);
}
const radioButton = document.querySelectorAll(selectors.presetRadioButton);

// Initialize the "Use preset" button properly.
disableUsePresetButton();

radioButton.forEach((elem) => {
elem.addEventListener('change', function(event) {
event.preventDefault();
// Enable the "Use preset" button when any of the radio buttons in the presets list is checked.
disableUsePresetButton();
});
});

};

/**
* Decide whether to disable or not the "Use preset" button.
* When there is no preset selected, the button should be displayed disabled; otherwise, it will appear enabled as a primary button.
*
* @method
* @private
*/
const disableUsePresetButton = () => {
let selectPresetButton = document.querySelector(selectors.selectPresetButton);
const selectedRadioButton = document.querySelectorAll(selectors.selectedPresetRadioButton);

if (selectedRadioButton.length > 0) {
// There is one preset selected, so the button should be enabled.
selectPresetButton.removeAttribute('disabled');
selectPresetButton.classList.remove('btn-secondary');
selectPresetButton.classList.add('btn-primary');
} else {
// There is no any preset selected, so the button should be disabled.
selectPresetButton.setAttribute('disabled', true);
selectPresetButton.classList.remove('btn-primary');
selectPresetButton.classList.add('btn-secondary');
}
};
10 changes: 7 additions & 3 deletions mod/data/classes/form/save_as_preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use moodle_exception;
use moodle_url;
use core_form\dynamic_form;
use mod_data\manager;

/**
* Save database as preset form.
Expand Down Expand Up @@ -72,9 +73,11 @@ public function validation($formdata, $files): array {

$errors = parent::validation($formdata, $files);
$context = $this->get_context_for_dynamic_submission();
$cm = get_coursemodule_from_id('', $context->instanceid, 0, false, MUST_EXIST);
$manager = manager::create_from_coursemodule($cm);

if (!empty($formdata['overwrite'])) {
$presets = data_get_available_presets($context);
$presets = $manager->get_available_presets();
$selectedpreset = new \stdClass();
foreach ($presets as $preset) {
if ($preset->name == $formdata['name']) {
Expand All @@ -87,7 +90,7 @@ public function validation($formdata, $files): array {
}
} else {
// If the preset exists now then we need to throw an error.
$sitepresets = data_get_available_site_presets($context);
$sitepresets = $manager->get_available_saved_presets();
foreach ($sitepresets as $preset) {
if ($formdata['name'] == $preset->name) {
$errors['name'] = get_string('errorpresetexists', 'data');
Expand Down Expand Up @@ -137,7 +140,8 @@ public function process_dynamic_submission(): array {

try {
if (!empty($this->get_data()->overwrite)) {
$presets = data_get_available_presets($context);
$manager = manager::create_from_coursemodule($cm);
$presets = $manager->get_available_presets();
$selectedpreset = new \stdClass();
foreach ($presets as $preset) {
if ($preset->name == $this->get_data()->name) {
Expand Down
78 changes: 78 additions & 0 deletions mod/data/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use mod_data\event\course_module_viewed;
use mod_data\event\template_viewed;
use mod_data\event\template_updated;
use core_component;
use stdClass;

/**
Expand Down Expand Up @@ -300,4 +301,81 @@ public function update_templates(stdClass $newtemplates): bool {

return true;
}

/**
* Returns an array of all the available presets.
*
* @return array A list with the datapreset plugins and the presets saved by users.
*/
public function get_available_presets(): array {
// First load the datapreset plugins that exist within the modules preset dir.
$pluginpresets = static::get_available_plugin_presets();

// Then find the presets that people have saved.
$savedpresets = static::get_available_saved_presets();

return array_merge($pluginpresets, $savedpresets);
}

/**
* Returns an array of all the presets that users have saved to the site.
*
* @return array A list with the preset saved by the users.
*/
public function get_available_saved_presets(): array {
global $USER;

$presets = [];

$fs = get_file_storage();
$files = $fs->get_area_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA);
if (empty($files)) {
return $presets;
}
$canviewall = has_capability('mod/data:viewalluserpresets', $this->get_context());
foreach ($files as $file) {
$isnotdirectory = ($file->is_directory() && $file->get_filepath() == '/') || !$file->is_directory();
$userid = $file->get_userid();
$cannotviewfile = !$canviewall && $userid != $USER->id;
if ($isnotdirectory || $cannotviewfile) {
continue;
}

$preset = new stdClass();
$preset->isplugin = false;
$preset->path = $file->get_filepath();
$preset->name = trim($preset->path, '/');
$preset->shortname = $preset->name;
$preset->userid = $userid;
$preset->id = $file->get_id();
$preset->storedfile = $file;
$presets[] = $preset;
}

return $presets;
}

/**
* Returns an array of all the available plugin presets.
*
* @return array A list with the datapreset plugins.
*/
public static function get_available_plugin_presets(): array {
$presets = [];

$dirs = core_component::get_plugin_list('datapreset');
foreach ($dirs as $dir => $fulldir) {
if (preset::is_directory_a_preset($fulldir)) {
$preset = new stdClass();
$preset->isplugin = true;
$preset->path = $fulldir;
$preset->userid = 0;
$preset->shortname = $dir;
$preset->name = preset::get_name_from_plugin($dir);
$presets[] = $preset;
}
}

return $presets;
}
}
Loading

0 comments on commit 5c1df4a

Please sign in to comment.