Skip to content

Commit

Permalink
MDL-75188 mod_data: Move import preset for to a modal
Browse files Browse the repository at this point in the history
 * Use a modal instead of a standard page to select the preset file
   and import it.
 * Change the zero state import button to a modal dialog so it uses the same
   workflow as on the preset page
  • Loading branch information
Laurent David authored and laurentdavid committed Oct 27, 2022
1 parent 5a9a6dd commit d67fbbb
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 159 deletions.
10 changes: 10 additions & 0 deletions mod/data/amd/build/importpresets.min.js

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

1 change: 1 addition & 0 deletions mod/data/amd/build/importpresets.min.js.map

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

62 changes: 62 additions & 0 deletions mod/data/amd/src/importpresets.js
@@ -0,0 +1,62 @@
// This file is part of Moodle - http://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/>.

/**
* Javascript module for importing presets.
*
* @module mod_data/importpreset
* @copyright 2022 Laurent David <laurent.david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

const selectors = {
importPresetButton: '[data-action="importpresets"]',
};

/**
* Initialize module
*/
export const init = () => {
const importPresetButton = document.querySelector(selectors.importPresetButton);

importPresetButton.addEventListener('click', event => {
event.preventDefault();

const modalForm = new ModalForm({
modalConfig: {
title: getString('importpreset', 'mod_data'),
},
formClass: 'mod_data\\form\\import_presets',
args: {d: importPresetButton.getAttribute('data-dataid')},
saveButtonText: getString('importandapply', 'mod_data'),
});

modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
if (event.detail.result) {
window.location.assign(event.detail.url);
} else {
Notification.addNotification({
type: 'error',
message: event.detail.errors.join('<br>')
});
}
});
modalForm.show();
});
};
119 changes: 119 additions & 0 deletions mod/data/classes/form/import_presets.php
@@ -0,0 +1,119 @@
<?php
// This file is part of Moodle - http://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/>.

namespace mod_data\form;

use context;
use moodle_exception;
use moodle_url;
use core_form\dynamic_form;

/**
* Import presets form.
*
* @package mod_data
* @copyright 2022 Laurent David <laurent.david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_presets extends dynamic_form {

/**
* Process the form submission
*
* @return array
* @throws moodle_exception
*/
public function process_dynamic_submission(): array {
global $CFG;
$filepath = $this->save_temp_file('importfile');
$context = $this->get_context_for_dynamic_submission();
$cm = get_coursemodule_from_id('data', $context->instanceid);
$returnurl = new moodle_url('/mod/data/preset.php', [
'd' => $cm->instance,
'action' => 'importzip',
'filepath' => str_replace($CFG->tempdir, '', $filepath)
]);
return [
'result' => true,
'url' => $returnurl->out(false),
];
}

/**
* Get context
*
* @return context
*/
protected function get_context_for_dynamic_submission(): context {
global $DB;
$d = $this->optional_param('d', null, PARAM_INT);
$data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $data->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);

return \context_module::instance($cm->id);
}

/**
* Set data
*
* @return void
*/
public function set_data_for_dynamic_submission(): void {
$data = (object) [
'd' => $this->optional_param('d', 0, PARAM_INT),
];
$this->set_data($data);
}

/**
* Has access ?
*
* @return void
* @throws moodle_exception
*/
protected function check_access_for_dynamic_submission(): void {
if (!has_capability('mod/data:managetemplates', $this->get_context_for_dynamic_submission())) {
throw new moodle_exception('importpresetmissingcapability', 'data');
}
}

/**
* Get page URL
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
$d = $this->optional_param('d', null, PARAM_INT);
return new moodle_url('/mod/data/preset.php', ['d' => $d]);
}

/**
* Form definition
*
* @return void
*/
protected function definition() {
$mform = $this->_form;
$mform->addElement('html', \html_writer::div(get_string('importpreset_desc', 'data'), 'py-3'));
$mform->addElement('hidden', 'd');
$mform->setType('d', PARAM_INT);

$mform->addElement('filepicker', 'importfile', get_string('choosepreset', 'data'), null,
['accepted_types' => '.zip']);
$mform->addRule('importfile', null, 'required');
}
}
6 changes: 3 additions & 3 deletions mod/data/classes/local/importer/preset_existing_importer.php
Expand Up @@ -51,7 +51,7 @@ public function __construct(manager $manager, string $fullname) {
throw new \coding_exception('Invalid preset provided');
}

$this->userid = $userid;
$this->userid = intval($userid);
$this->fullname = $fullname;
$cm = $manager->get_coursemodule();
$course = $cm->get_course();
Expand All @@ -62,9 +62,9 @@ public function __construct(manager $manager, string $fullname) {
/**
* Returns user ID
*
* @return int|string userid or empty string
* @return int userid
*/
public function get_userid() {
public function get_userid(): int {
return $this->userid;
}

Expand Down
44 changes: 44 additions & 0 deletions mod/data/classes/local/importer/preset_importer.php
Expand Up @@ -354,4 +354,48 @@ public function needs_mapping(): bool {
public function get_preset_selector(): array {
return ['name' => 'directory', 'value' => $this->get_directory()];
}

/**
* Helper function to finish up the import routine.
*
* Called from fields and presets pages.
*
* @param bool $overwritesettings Whether to overwrite activity settings or not.
* @param stdClass $instance database instance object
* @return void
*/
public function finish_import_process(bool $overwritesettings, stdClass $instance): void {
global $DB;
$this->import($overwritesettings);
$strimportsuccess = get_string('importsuccess', 'data');
$straddentries = get_string('addentries', 'data');
$strtodatabase = get_string('todatabase', 'data');
if (!$DB->get_records('data_records', ['dataid' => $instance->id])) {
\core\notification::success("$strimportsuccess <a href='edit.php?d=$instance->id'>$straddentries</a> $strtodatabase");
} else {
\core\notification::success($strimportsuccess);
}
}

/**
* Get the right importer instance from the provided parameters (POST or GET)
*
* @param manager $manager the current database manager
* @return preset_importer the relevant preset_importer instance
* @throws \moodle_exception when the file provided as parameter (POST or GET) does not exist
*/
public static function create_from_parameters(manager $manager): preset_importer {
global $CFG;
$fullname = optional_param('fullname', '', PARAM_PATH); // Directory the preset is in.
if (!$fullname) {
$presetdir = $CFG->tempdir . '/forms/' . required_param('directory', PARAM_FILE);
if (!file_exists($presetdir) || !is_dir($presetdir)) {
throw new \moodle_exception('cannotimport');
}
$importer = new preset_upload_importer($manager, $presetdir);
} else {
$importer = new preset_existing_importer($manager, $fullname);
}
return $importer;
}
}
11 changes: 9 additions & 2 deletions mod/data/classes/output/action_bar.php
Expand Up @@ -62,8 +62,11 @@ public function get_fields_action_bar(
global $PAGE, $DB;

$createfieldlink = new moodle_url('/mod/data/field.php', ['d' => $this->id]);
$presetslink = new moodle_url('/mod/data/preset.php', ['d' => $this->id]);

$menu = [
$createfieldlink->out(false) => get_string('managefields', 'mod_data'),
$presetslink->out(false) => get_string('usestandard', 'mod_data'),
];

$selected = $createfieldlink->out(false);
Expand Down Expand Up @@ -226,11 +229,15 @@ public function get_templates_action_bar(): string {
* @return string The HTML code for the action selector.
*/
public function get_presets_action_bar(): string {
global $PAGE;
global $PAGE, $DB;

$renderer = $PAGE->get_renderer('mod_data');
$data = $DB->get_record('data', ['id' => $this->id], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('data', $data->id, $data->course, null, MUST_EXIST);
if (!has_capability('mod/data:managetemplates', \context_module::instance($cm->id))) {
return '';
}
$presetsactionbar = new presets_action_bar($this->id);

return $renderer->render_presets_action_bar($presetsactionbar);
}

Expand Down

0 comments on commit d67fbbb

Please sign in to comment.