Skip to content

Commit d67fbbb

Browse files
Laurent Davidlaurentdavid
authored andcommitted
MDL-75188 mod_data: Move import preset for to a modal
* 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
1 parent 5a9a6dd commit d67fbbb

24 files changed

+469
-159
lines changed

mod/data/amd/build/importpresets.min.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod/data/amd/build/importpresets.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod/data/amd/src/importpresets.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
/**
17+
* Javascript module for importing presets.
18+
*
19+
* @module mod_data/importpreset
20+
* @copyright 2022 Laurent David <laurent.david@moodle.com>
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
24+
import ModalForm from 'core_form/modalform';
25+
import Notification from 'core/notification';
26+
import {get_string as getString} from 'core/str';
27+
28+
const selectors = {
29+
importPresetButton: '[data-action="importpresets"]',
30+
};
31+
32+
/**
33+
* Initialize module
34+
*/
35+
export const init = () => {
36+
const importPresetButton = document.querySelector(selectors.importPresetButton);
37+
38+
importPresetButton.addEventListener('click', event => {
39+
event.preventDefault();
40+
41+
const modalForm = new ModalForm({
42+
modalConfig: {
43+
title: getString('importpreset', 'mod_data'),
44+
},
45+
formClass: 'mod_data\\form\\import_presets',
46+
args: {d: importPresetButton.getAttribute('data-dataid')},
47+
saveButtonText: getString('importandapply', 'mod_data'),
48+
});
49+
50+
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
51+
if (event.detail.result) {
52+
window.location.assign(event.detail.url);
53+
} else {
54+
Notification.addNotification({
55+
type: 'error',
56+
message: event.detail.errors.join('<br>')
57+
});
58+
}
59+
});
60+
modalForm.show();
61+
});
62+
};
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_data\form;
18+
19+
use context;
20+
use moodle_exception;
21+
use moodle_url;
22+
use core_form\dynamic_form;
23+
24+
/**
25+
* Import presets form.
26+
*
27+
* @package mod_data
28+
* @copyright 2022 Laurent David <laurent.david@moodle.com>
29+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30+
*/
31+
class import_presets extends dynamic_form {
32+
33+
/**
34+
* Process the form submission
35+
*
36+
* @return array
37+
* @throws moodle_exception
38+
*/
39+
public function process_dynamic_submission(): array {
40+
global $CFG;
41+
$filepath = $this->save_temp_file('importfile');
42+
$context = $this->get_context_for_dynamic_submission();
43+
$cm = get_coursemodule_from_id('data', $context->instanceid);
44+
$returnurl = new moodle_url('/mod/data/preset.php', [
45+
'd' => $cm->instance,
46+
'action' => 'importzip',
47+
'filepath' => str_replace($CFG->tempdir, '', $filepath)
48+
]);
49+
return [
50+
'result' => true,
51+
'url' => $returnurl->out(false),
52+
];
53+
}
54+
55+
/**
56+
* Get context
57+
*
58+
* @return context
59+
*/
60+
protected function get_context_for_dynamic_submission(): context {
61+
global $DB;
62+
$d = $this->optional_param('d', null, PARAM_INT);
63+
$data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
64+
$course = $DB->get_record('course', ['id' => $data->course], '*', MUST_EXIST);
65+
$cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);
66+
67+
return \context_module::instance($cm->id);
68+
}
69+
70+
/**
71+
* Set data
72+
*
73+
* @return void
74+
*/
75+
public function set_data_for_dynamic_submission(): void {
76+
$data = (object) [
77+
'd' => $this->optional_param('d', 0, PARAM_INT),
78+
];
79+
$this->set_data($data);
80+
}
81+
82+
/**
83+
* Has access ?
84+
*
85+
* @return void
86+
* @throws moodle_exception
87+
*/
88+
protected function check_access_for_dynamic_submission(): void {
89+
if (!has_capability('mod/data:managetemplates', $this->get_context_for_dynamic_submission())) {
90+
throw new moodle_exception('importpresetmissingcapability', 'data');
91+
}
92+
}
93+
94+
/**
95+
* Get page URL
96+
*
97+
* @return moodle_url
98+
*/
99+
protected function get_page_url_for_dynamic_submission(): moodle_url {
100+
$d = $this->optional_param('d', null, PARAM_INT);
101+
return new moodle_url('/mod/data/preset.php', ['d' => $d]);
102+
}
103+
104+
/**
105+
* Form definition
106+
*
107+
* @return void
108+
*/
109+
protected function definition() {
110+
$mform = $this->_form;
111+
$mform->addElement('html', \html_writer::div(get_string('importpreset_desc', 'data'), 'py-3'));
112+
$mform->addElement('hidden', 'd');
113+
$mform->setType('d', PARAM_INT);
114+
115+
$mform->addElement('filepicker', 'importfile', get_string('choosepreset', 'data'), null,
116+
['accepted_types' => '.zip']);
117+
$mform->addRule('importfile', null, 'required');
118+
}
119+
}

mod/data/classes/local/importer/preset_existing_importer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(manager $manager, string $fullname) {
5151
throw new \coding_exception('Invalid preset provided');
5252
}
5353

54-
$this->userid = $userid;
54+
$this->userid = intval($userid);
5555
$this->fullname = $fullname;
5656
$cm = $manager->get_coursemodule();
5757
$course = $cm->get_course();
@@ -62,9 +62,9 @@ public function __construct(manager $manager, string $fullname) {
6262
/**
6363
* Returns user ID
6464
*
65-
* @return int|string userid or empty string
65+
* @return int userid
6666
*/
67-
public function get_userid() {
67+
public function get_userid(): int {
6868
return $this->userid;
6969
}
7070

mod/data/classes/local/importer/preset_importer.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,48 @@ public function needs_mapping(): bool {
354354
public function get_preset_selector(): array {
355355
return ['name' => 'directory', 'value' => $this->get_directory()];
356356
}
357+
358+
/**
359+
* Helper function to finish up the import routine.
360+
*
361+
* Called from fields and presets pages.
362+
*
363+
* @param bool $overwritesettings Whether to overwrite activity settings or not.
364+
* @param stdClass $instance database instance object
365+
* @return void
366+
*/
367+
public function finish_import_process(bool $overwritesettings, stdClass $instance): void {
368+
global $DB;
369+
$this->import($overwritesettings);
370+
$strimportsuccess = get_string('importsuccess', 'data');
371+
$straddentries = get_string('addentries', 'data');
372+
$strtodatabase = get_string('todatabase', 'data');
373+
if (!$DB->get_records('data_records', ['dataid' => $instance->id])) {
374+
\core\notification::success("$strimportsuccess <a href='edit.php?d=$instance->id'>$straddentries</a> $strtodatabase");
375+
} else {
376+
\core\notification::success($strimportsuccess);
377+
}
378+
}
379+
380+
/**
381+
* Get the right importer instance from the provided parameters (POST or GET)
382+
*
383+
* @param manager $manager the current database manager
384+
* @return preset_importer the relevant preset_importer instance
385+
* @throws \moodle_exception when the file provided as parameter (POST or GET) does not exist
386+
*/
387+
public static function create_from_parameters(manager $manager): preset_importer {
388+
global $CFG;
389+
$fullname = optional_param('fullname', '', PARAM_PATH); // Directory the preset is in.
390+
if (!$fullname) {
391+
$presetdir = $CFG->tempdir . '/forms/' . required_param('directory', PARAM_FILE);
392+
if (!file_exists($presetdir) || !is_dir($presetdir)) {
393+
throw new \moodle_exception('cannotimport');
394+
}
395+
$importer = new preset_upload_importer($manager, $presetdir);
396+
} else {
397+
$importer = new preset_existing_importer($manager, $fullname);
398+
}
399+
return $importer;
400+
}
357401
}

mod/data/classes/output/action_bar.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ public function get_fields_action_bar(
6262
global $PAGE, $DB;
6363

6464
$createfieldlink = new moodle_url('/mod/data/field.php', ['d' => $this->id]);
65+
$presetslink = new moodle_url('/mod/data/preset.php', ['d' => $this->id]);
66+
6567
$menu = [
6668
$createfieldlink->out(false) => get_string('managefields', 'mod_data'),
69+
$presetslink->out(false) => get_string('usestandard', 'mod_data'),
6770
];
6871

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

231234
$renderer = $PAGE->get_renderer('mod_data');
235+
$data = $DB->get_record('data', ['id' => $this->id], '*', MUST_EXIST);
236+
$cm = get_coursemodule_from_instance('data', $data->id, $data->course, null, MUST_EXIST);
237+
if (!has_capability('mod/data:managetemplates', \context_module::instance($cm->id))) {
238+
return '';
239+
}
232240
$presetsactionbar = new presets_action_bar($this->id);
233-
234241
return $renderer->render_presets_action_bar($presetsactionbar);
235242
}
236243

0 commit comments

Comments
 (0)