1 |
efrain |
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 |
$returnurl = new moodle_url('/mod/data/preset.php', [
|
|
|
44 |
'id' => $context->instanceid,
|
|
|
45 |
'action' => 'importzip',
|
|
|
46 |
'filepath' => str_replace($CFG->tempdir, '', $filepath)
|
|
|
47 |
]);
|
|
|
48 |
return [
|
|
|
49 |
'result' => true,
|
|
|
50 |
'url' => $returnurl->out(false),
|
|
|
51 |
];
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Get context
|
|
|
56 |
*
|
|
|
57 |
* @return context
|
|
|
58 |
*/
|
|
|
59 |
protected function get_context_for_dynamic_submission(): context {
|
|
|
60 |
$cmid = $this->optional_param('cmid', null, PARAM_INT);
|
|
|
61 |
$cm = get_coursemodule_from_id('data', $cmid);
|
|
|
62 |
$context = \context_module::instance($cm->id);
|
|
|
63 |
return $context;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Set data
|
|
|
68 |
*
|
|
|
69 |
* @return void
|
|
|
70 |
*/
|
|
|
71 |
public function set_data_for_dynamic_submission(): void {
|
|
|
72 |
$data = (object) [
|
|
|
73 |
'cmid' => $this->optional_param('cmid', 0, PARAM_INT),
|
|
|
74 |
];
|
|
|
75 |
$this->set_data($data);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Has access ?
|
|
|
80 |
*
|
|
|
81 |
* @return void
|
|
|
82 |
* @throws moodle_exception
|
|
|
83 |
*/
|
|
|
84 |
protected function check_access_for_dynamic_submission(): void {
|
|
|
85 |
if (!has_capability('mod/data:managetemplates', $this->get_context_for_dynamic_submission())) {
|
|
|
86 |
throw new moodle_exception('importpresetmissingcapability', 'data');
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Get page URL
|
|
|
92 |
*
|
|
|
93 |
* @return moodle_url
|
|
|
94 |
*/
|
|
|
95 |
protected function get_page_url_for_dynamic_submission(): moodle_url {
|
|
|
96 |
$cmid = $this->optional_param('cmid', null, PARAM_INT);
|
|
|
97 |
return new moodle_url('/mod/data/preset.php', ['id' => $cmid]);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Form definition
|
|
|
102 |
*
|
|
|
103 |
* @return void
|
|
|
104 |
*/
|
|
|
105 |
protected function definition() {
|
|
|
106 |
$mform = $this->_form;
|
|
|
107 |
$mform->addElement('html', \html_writer::div(get_string('importpreset_desc', 'mod_data'), 'py-3'));
|
|
|
108 |
$mform->addElement('hidden', 'cmid');
|
|
|
109 |
$mform->setType('cmid', PARAM_INT);
|
|
|
110 |
|
|
|
111 |
$mform->addElement('filepicker', 'importfile', get_string('choosepreset', 'mod_data'), null,
|
|
|
112 |
['accepted_types' => '.zip']);
|
|
|
113 |
$mform->addRule('importfile', null, 'required');
|
|
|
114 |
}
|
|
|
115 |
}
|