From cf12af751904749cd814012ffd83d71bbfcf7f85 Mon Sep 17 00:00:00 2001 From: Laurent David Date: Wed, 12 Oct 2022 14:39:27 +0200 Subject: [PATCH] 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) --- mod/data/amd/build/importpresets.min.js | 2 +- mod/data/amd/build/importpresets.min.js.map | 2 +- mod/data/amd/src/importpresets.js | 2 +- mod/data/classes/form/import_presets.php | 28 ++++++++----------- mod/data/classes/output/action_bar.php | 17 ++++++----- mod/data/classes/output/presets.php | 1 - .../classes/output/presets_action_bar.php | 14 ++++++---- .../classes/output/zero_state_action_bar.php | 5 ++-- mod/data/preset.php | 1 - mod/data/templates/presets.mustache | 6 ++-- .../templates/presets_action_bar.mustache | 6 ++-- 11 files changed, 42 insertions(+), 42 deletions(-) diff --git a/mod/data/amd/build/importpresets.min.js b/mod/data/amd/build/importpresets.min.js index 7036e26924f..0914786d6d0 100644 --- a/mod/data/amd/build/importpresets.min.js +++ b/mod/data/amd/build/importpresets.min.js @@ -5,6 +5,6 @@ define("mod_data/importpresets",["exports","core_form/modalform","core/notificat * @module mod_data/importpreset * @copyright 2022 Laurent David * @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("
")})})),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("
")})})),modalForm.show()}))}})); //# sourceMappingURL=importpresets.min.js.map \ No newline at end of file diff --git a/mod/data/amd/build/importpresets.min.js.map b/mod/data/amd/build/importpresets.min.js.map index 360c8f8160b..3f14ff27d88 100644 --- a/mod/data/amd/build/importpresets.min.js.map +++ b/mod/data/amd/build/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 .\n\n/**\n * Javascript module for importing presets.\n *\n * @module mod_data/importpreset\n * @copyright 2022 Laurent David \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('
')\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"} \ No newline at end of file +{"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 .\n\n/**\n * Javascript module for importing presets.\n *\n * @module mod_data/importpreset\n * @copyright 2022 Laurent David \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('
')\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"} \ No newline at end of file diff --git a/mod/data/amd/src/importpresets.js b/mod/data/amd/src/importpresets.js index 7782ff9fa79..370391f0084 100644 --- a/mod/data/amd/src/importpresets.js +++ b/mod/data/amd/src/importpresets.js @@ -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'), }); diff --git a/mod/data/classes/form/import_presets.php b/mod/data/classes/form/import_presets.php index 2a937cbcac8..e2a57e25076 100644 --- a/mod/data/classes/form/import_presets.php +++ b/mod/data/classes/form/import_presets.php @@ -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'); } diff --git a/mod/data/classes/output/action_bar.php b/mod/data/classes/output/action_bar.php index 5b7751b8e28..429b3f2b7c9 100644 --- a/mod/data/classes/output/action_bar.php +++ b/mod/data/classes/output/action_bar.php @@ -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); } diff --git a/mod/data/classes/output/presets.php b/mod/data/classes/output/presets.php index 633989807c0..87dedc5f236 100644 --- a/mod/data/classes/output/presets.php +++ b/mod/data/classes/output/presets.php @@ -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; diff --git a/mod/data/classes/output/presets_action_bar.php b/mod/data/classes/output/presets_action_bar.php index ab0d91e23e6..36d335a49b5 100644 --- a/mod/data/classes/output/presets_action_bar.php +++ b/mod/data/classes/output/presets_action_bar.php @@ -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), ]; } diff --git a/mod/data/classes/output/zero_state_action_bar.php b/mod/data/classes/output/zero_state_action_bar.php index 26c4d3aa6a6..5c4632a5237 100644 --- a/mod/data/classes/output/zero_state_action_bar.php +++ b/mod/data/classes/output/zero_state_action_bar.php @@ -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); } diff --git a/mod/data/preset.php b/mod/data/preset.php index 19753b0b8ce..2aba66f36cb 100644 --- a/mod/data/preset.php +++ b/mod/data/preset.php @@ -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(); diff --git a/mod/data/templates/presets.mustache b/mod/data/templates/presets.mustache index 0df8adc93b6..270f7dac7c5 100644 --- a/mod/data/templates/presets.mustache +++ b/mod/data/templates/presets.mustache @@ -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}}
- + diff --git a/mod/data/templates/presets_action_bar.mustache b/mod/data/templates/presets_action_bar.mustache index 47ebc8bf4b1..cf2cef09cb3 100644 --- a/mod/data/templates/presets_action_bar.mustache +++ b/mod/data/templates/presets_action_bar.mustache @@ -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 } }}