MDL-75188 mod_data: Use cmid instead of id
* Based on previous changes, fix ids so they are consistent with other modules (in preparation for MDL-75255)
This commit is contained in:
committed by
Laurent David
parent
d67fbbb80d
commit
cf12af7519
+1
-1
@@ -5,6 +5,6 @@ define("mod_data/importpresets",["exports","core_form/modalform","core/notificat
|
||||
* @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
|
||||
*/Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_modalform=_interopRequireDefault(_modalform),_notification=_interopRequireDefault(_notification);const selectors_importPresetButton='[data-action="importpresets"]';_exports.init=()=>{const importPresetButton=document.querySelector(selectors_importPresetButton);importPresetButton.addEventListener("click",(event=>{event.preventDefault();const modalForm=new _modalform.default({modalConfig:{title:(0,_str.get_string)("importpreset","mod_data")},formClass:"mod_data\\form\\import_presets",args:{d:importPresetButton.getAttribute("data-dataid")},saveButtonText:(0,_str.get_string)("importandapply","mod_data")});modalForm.addEventListener(modalForm.events.FORM_SUBMITTED,(event=>{event.detail.result?window.location.assign(event.detail.url):_notification.default.addNotification({type:"error",message:event.detail.errors.join("<br>")})})),modalForm.show()}))}}));
|
||||
*/Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_modalform=_interopRequireDefault(_modalform),_notification=_interopRequireDefault(_notification);const selectors_importPresetButton='[data-action="importpresets"]';_exports.init=()=>{const importPresetButton=document.querySelector(selectors_importPresetButton);importPresetButton.addEventListener("click",(event=>{event.preventDefault();const modalForm=new _modalform.default({modalConfig:{title:(0,_str.get_string)("importpreset","mod_data")},formClass:"mod_data\\form\\import_presets",args:{cmid:importPresetButton.dataset.dataid},saveButtonText:(0,_str.get_string)("importandapply","mod_data")});modalForm.addEventListener(modalForm.events.FORM_SUBMITTED,(event=>{event.detail.result?window.location.assign(event.detail.url):_notification.default.addNotification({type:"error",message:event.detail.errors.join("<br>")})})),modalForm.show()}))}}));
|
||||
|
||||
//# sourceMappingURL=importpresets.min.js.map
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"importpresets.min.js","sources":["../src/importpresets.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Javascript module for importing presets.\n *\n * @module mod_data/importpreset\n * @copyright 2022 Laurent David <[email protected]>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport ModalForm from 'core_form/modalform';\nimport Notification from 'core/notification';\nimport {get_string as getString} from 'core/str';\n\nconst selectors = {\n importPresetButton: '[data-action=\"importpresets\"]',\n};\n\n/**\n * Initialize module\n */\nexport const init = () => {\n const importPresetButton = document.querySelector(selectors.importPresetButton);\n\n importPresetButton.addEventListener('click', event => {\n event.preventDefault();\n\n const modalForm = new ModalForm({\n modalConfig: {\n title: getString('importpreset', 'mod_data'),\n },\n formClass: 'mod_data\\\\form\\\\import_presets',\n args: {d: importPresetButton.getAttribute('data-dataid')},\n saveButtonText: getString('importandapply', 'mod_data'),\n });\n\n modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {\n if (event.detail.result) {\n window.location.assign(event.detail.url);\n } else {\n Notification.addNotification({\n type: 'error',\n message: event.detail.errors.join('<br>')\n });\n }\n });\n modalForm.show();\n });\n};\n"],"names":["selectors","importPresetButton","document","querySelector","addEventListener","event","preventDefault","modalForm","ModalForm","modalConfig","title","formClass","args","d","getAttribute","saveButtonText","events","FORM_SUBMITTED","detail","result","window","location","assign","url","addNotification","type","message","errors","join","show"],"mappings":";;;;;;;0LA2BMA,6BACkB,8CAMJ,WACVC,mBAAqBC,SAASC,cAAcH,8BAElDC,mBAAmBG,iBAAiB,SAASC,QACzCA,MAAMC,uBAEAC,UAAY,IAAIC,mBAAU,CAC5BC,YAAa,CACTC,OAAO,mBAAU,eAAgB,aAErCC,UAAW,iCACXC,KAAM,CAACC,EAAGZ,mBAAmBa,aAAa,gBAC1CC,gBAAgB,mBAAU,iBAAkB,cAGhDR,UAAUH,iBAAiBG,UAAUS,OAAOC,gBAAgBZ,QACpDA,MAAMa,OAAOC,OACbC,OAAOC,SAASC,OAAOjB,MAAMa,OAAOK,2BAEvBC,gBAAgB,CACzBC,KAAM,QACNC,QAASrB,MAAMa,OAAOS,OAAOC,KAAK,aAI9CrB,UAAUsB"}
|
||||
{"version":3,"file":"importpresets.min.js","sources":["../src/importpresets.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Javascript module for importing presets.\n *\n * @module mod_data/importpreset\n * @copyright 2022 Laurent David <[email protected]>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport ModalForm from 'core_form/modalform';\nimport Notification from 'core/notification';\nimport {get_string as getString} from 'core/str';\n\nconst selectors = {\n importPresetButton: '[data-action=\"importpresets\"]',\n};\n\n/**\n * Initialize module\n */\nexport const init = () => {\n const importPresetButton = document.querySelector(selectors.importPresetButton);\n\n importPresetButton.addEventListener('click', event => {\n event.preventDefault();\n\n const modalForm = new ModalForm({\n modalConfig: {\n title: getString('importpreset', 'mod_data'),\n },\n formClass: 'mod_data\\\\form\\\\import_presets',\n args: {cmid: importPresetButton.dataset.dataid},\n saveButtonText: getString('importandapply', 'mod_data'),\n });\n\n modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {\n if (event.detail.result) {\n window.location.assign(event.detail.url);\n } else {\n Notification.addNotification({\n type: 'error',\n message: event.detail.errors.join('<br>')\n });\n }\n });\n modalForm.show();\n });\n};\n"],"names":["selectors","importPresetButton","document","querySelector","addEventListener","event","preventDefault","modalForm","ModalForm","modalConfig","title","formClass","args","cmid","dataset","dataid","saveButtonText","events","FORM_SUBMITTED","detail","result","window","location","assign","url","addNotification","type","message","errors","join","show"],"mappings":";;;;;;;0LA2BMA,6BACkB,8CAMJ,WACVC,mBAAqBC,SAASC,cAAcH,8BAElDC,mBAAmBG,iBAAiB,SAASC,QACzCA,MAAMC,uBAEAC,UAAY,IAAIC,mBAAU,CAC5BC,YAAa,CACTC,OAAO,mBAAU,eAAgB,aAErCC,UAAW,iCACXC,KAAM,CAACC,KAAMZ,mBAAmBa,QAAQC,QACxCC,gBAAgB,mBAAU,iBAAkB,cAGhDT,UAAUH,iBAAiBG,UAAUU,OAAOC,gBAAgBb,QACpDA,MAAMc,OAAOC,OACbC,OAAOC,SAASC,OAAOlB,MAAMc,OAAOK,2BAEvBC,gBAAgB,CACzBC,KAAM,QACNC,QAAStB,MAAMc,OAAOS,OAAOC,KAAK,aAI9CtB,UAAUuB"}
|
||||
@@ -43,7 +43,7 @@ export const init = () => {
|
||||
title: getString('importpreset', 'mod_data'),
|
||||
},
|
||||
formClass: 'mod_data\\form\\import_presets',
|
||||
args: {d: importPresetButton.getAttribute('data-dataid')},
|
||||
args: {cmid: importPresetButton.dataset.dataid},
|
||||
saveButtonText: getString('importandapply', 'mod_data'),
|
||||
});
|
||||
|
||||
|
||||
@@ -40,9 +40,8 @@ class import_presets extends dynamic_form {
|
||||
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,
|
||||
'id' => $context->instanceid,
|
||||
'action' => 'importzip',
|
||||
'filepath' => str_replace($CFG->tempdir, '', $filepath)
|
||||
]);
|
||||
@@ -58,13 +57,10 @@ class import_presets extends dynamic_form {
|
||||
* @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);
|
||||
$cmid = $this->optional_param('cmid', null, PARAM_INT);
|
||||
$cm = get_coursemodule_from_id('data', $cmid);
|
||||
$context = \context_module::instance($cm->id);
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +70,7 @@ class import_presets extends dynamic_form {
|
||||
*/
|
||||
public function set_data_for_dynamic_submission(): void {
|
||||
$data = (object) [
|
||||
'd' => $this->optional_param('d', 0, PARAM_INT),
|
||||
'cmid' => $this->optional_param('cmid', 0, PARAM_INT),
|
||||
];
|
||||
$this->set_data($data);
|
||||
}
|
||||
@@ -97,8 +93,8 @@ class import_presets extends dynamic_form {
|
||||
* @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]);
|
||||
$cmid = $this->optional_param('cmid', null, PARAM_INT);
|
||||
return new moodle_url('/mod/data/preset.php', ['id' => $cmid]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,11 +104,11 @@ class import_presets extends dynamic_form {
|
||||
*/
|
||||
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('html', \html_writer::div(get_string('importpreset_desc', 'mod_data'), 'py-3'));
|
||||
$mform->addElement('hidden', 'cmid');
|
||||
$mform->setType('cmid', PARAM_INT);
|
||||
|
||||
$mform->addElement('filepicker', 'importfile', get_string('choosepreset', 'data'), null,
|
||||
$mform->addElement('filepicker', 'importfile', get_string('choosepreset', 'mod_data'), null,
|
||||
['accepted_types' => '.zip']);
|
||||
$mform->addRule('importfile', null, 'required');
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ class action_bar {
|
||||
/** @var int $id The database module id. */
|
||||
private $id;
|
||||
|
||||
/** @var int $cmid The database course module id. */
|
||||
private $cmid;
|
||||
|
||||
/** @var moodle_url $currenturl The URL of the current page. */
|
||||
private $currenturl;
|
||||
|
||||
@@ -43,6 +46,8 @@ class action_bar {
|
||||
*/
|
||||
public function __construct(int $id, moodle_url $pageurl) {
|
||||
$this->id = $id;
|
||||
[$course, $cm] = get_course_and_cm_from_instance($this->id, 'data');
|
||||
$this->cmid = $cm->id;
|
||||
$this->currenturl = $pageurl;
|
||||
}
|
||||
|
||||
@@ -61,8 +66,8 @@ class action_bar {
|
||||
): string {
|
||||
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]);
|
||||
$createfieldlink = new moodle_url('/mod/data/field.php', ['id' => $this->cmid]);
|
||||
$presetslink = new moodle_url('/mod/data/preset.php', ['id' => $this->cmid]);
|
||||
|
||||
$menu = [
|
||||
$createfieldlink->out(false) => get_string('managefields', 'mod_data'),
|
||||
@@ -229,15 +234,13 @@ class action_bar {
|
||||
* @return string The HTML code for the action selector.
|
||||
*/
|
||||
public function get_presets_action_bar(): string {
|
||||
global $PAGE, $DB;
|
||||
global $PAGE;
|
||||
|
||||
$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))) {
|
||||
if (!has_capability('mod/data:managetemplates', \context_module::instance($this->cmid))) {
|
||||
return '';
|
||||
}
|
||||
$presetsactionbar = new presets_action_bar($this->id);
|
||||
$presetsactionbar = new presets_action_bar($this->cmid);
|
||||
return $renderer->render_presets_action_bar($presetsactionbar);
|
||||
}
|
||||
|
||||
|
||||
@@ -131,7 +131,6 @@ class presets implements templatable, renderable {
|
||||
* @return stdClass the resulting action menu
|
||||
*/
|
||||
private function get_preset_action_menu(renderer_base $output, $preset, ?int $userid): stdClass {
|
||||
global $PAGE;
|
||||
|
||||
$actions = new stdClass();
|
||||
$actionmenu = null;
|
||||
|
||||
@@ -30,15 +30,15 @@ use renderable;
|
||||
class presets_action_bar implements templatable, renderable {
|
||||
|
||||
/** @var int $id The database module id. */
|
||||
private $id;
|
||||
private $cmid;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param int $id The database module id
|
||||
* @param int $cmid The database module id
|
||||
*/
|
||||
public function __construct(int $id) {
|
||||
$this->id = $id;
|
||||
public function __construct(int $cmid) {
|
||||
$this->cmid = $cmid;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,9 +48,11 @@ class presets_action_bar implements templatable, renderable {
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_template(\renderer_base $output): array {
|
||||
$importpresetlink = new moodle_url('/mod/data/preset.php', ['d' => $this->id, 'action' => 'import']);
|
||||
$importpresetlink = new moodle_url('/mod/data/preset.php', [
|
||||
'id' => $this->cmid, 'action' => 'import'
|
||||
]);
|
||||
return [
|
||||
'd' => $this->id,
|
||||
'id' => $this->cmid,
|
||||
'importpreseturl' => $importpresetlink->out(false),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -53,8 +53,9 @@ class zero_state_action_bar implements templatable, renderable {
|
||||
|
||||
$data = [];
|
||||
if ($this->manager->can_manage_templates()) {
|
||||
$cm = $this->manager->get_coursemodule();
|
||||
$instance = $this->manager->get_instance();
|
||||
$params = ['d' => $instance->id, 'backto' => $PAGE->url->out(false)];
|
||||
$params = ['id' => $cm->id, 'backto' => $PAGE->url->out(false)];
|
||||
|
||||
$usepresetlink = new moodle_url('/mod/data/preset.php', $params);
|
||||
$usepresetbutton = new \single_button($usepresetlink,
|
||||
@@ -70,7 +71,7 @@ class zero_state_action_bar implements templatable, renderable {
|
||||
$importpresetbutton = new \single_button($importpresetlink,
|
||||
get_string('importpreset', 'mod_data'), 'get', false, [
|
||||
'data-action' => 'importpresets',
|
||||
'data-dataid' => $instance->id
|
||||
'data-dataid' => $cm->id,
|
||||
]);
|
||||
$data['importpresetbutton'] = $importpresetbutton->export_for_template($output);
|
||||
}
|
||||
|
||||
@@ -116,7 +116,6 @@ if ($action === 'export') {
|
||||
|
||||
if ($action == 'importzip') {
|
||||
$filepath = optional_param('filepath', '', PARAM_PATH);
|
||||
$manager = manager::create_from_coursemodule($cm);
|
||||
$importer = new preset_upload_importer($manager, $CFG->tempdir . $filepath);
|
||||
if ($importer->needs_mapping()) {
|
||||
echo $OUTPUT->header();
|
||||
|
||||
@@ -18,13 +18,13 @@
|
||||
|
||||
Context variables required for this template:
|
||||
* formactionurl - The form action url.
|
||||
* d - The database id.
|
||||
* id - The database course module id (cmid).
|
||||
* presets - List of presets containing id, name, fullname, shortname and actions.
|
||||
|
||||
Example context (json):
|
||||
{
|
||||
"formactionurl": "http://www.example.com",
|
||||
"d": 1,
|
||||
"id": 1,
|
||||
"presets": [
|
||||
{
|
||||
"id": 1,
|
||||
@@ -51,7 +51,7 @@
|
||||
{{#str}}presetshelp, mod_data{{/str}}
|
||||
|
||||
<form method="post" action="{{formactionurl}}" class="mt-4">
|
||||
<input type="hidden" name="d" value="{{d}}">
|
||||
<input type="hidden" name="id" value="{{id}}">
|
||||
<input type="hidden" name="mode" value="usepreset">
|
||||
<input type="hidden" name="action" value="select">
|
||||
|
||||
|
||||
@@ -18,18 +18,18 @@
|
||||
|
||||
Context variables required for this template:
|
||||
* importpreseturl - The url to the import preset page.
|
||||
* d - The database id.
|
||||
* id - The database course module id (cmid).
|
||||
|
||||
Example context (json):
|
||||
{
|
||||
"importpreseturl": "http://www.example.com",
|
||||
"d" : 1
|
||||
"id" : 1
|
||||
}
|
||||
}}
|
||||
<div class="container-fluid tertiary-navigation">
|
||||
<div class="row float-right">
|
||||
<div class="navitem">
|
||||
<a role="button" href="{{importpreseturl}}" class="btn btn-secondary" data-action="importpresets" data-dataid="{{d}}">{{#str}}import, core{{/str}}</a>
|
||||
<a role="button" href="{{importpreseturl}}" class="btn btn-secondary" data-action="importpresets" data-dataid="{{id}}">{{#str}}import, core{{/str}}</a>
|
||||
</div>
|
||||
</div>
|
||||
{{#js}}
|
||||
|
||||
Reference in New Issue
Block a user