mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
5a9a6dd
commit d67fbbb
Showing
24 changed files
with
469 additions
and
159 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.