-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.php
105 lines (85 loc) · 3.61 KB
/
edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?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/>.
/**
* Edit page
*
* @package tool_carcastc
* @copyright 2021, Carlos Castillo <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
$id = optional_param('id', 0, PARAM_INT);
if ($id) {
// If id is sent as param then return the row filter by this id.
$row = \tool_carcastc\tool_carcastc_model::get_row(['id' => $id]);
$courseid = $row->courseid;
$urlparams = ['id' => $id];
$title = get_string('edit', 'tool_carcastc');
} else {
// Process to delete row.
if ($deleteid = optional_param('delete', null, PARAM_INT)) {
require_sesskey();
$record = \tool_carcastc\tool_carcastc_model::get_row(['id' => $deleteid]);
require_login(get_course($record->courseid));
require_capability('tool/carcastc:edit', context_course::instance($record->courseid));
\tool_carcastc\tool_carcastc_model::delete_row(['id' => $deleteid]);
redirect(new moodle_url('/admin/tool/carcastc/index.php', ['courseid' => $record->courseid]));
}
// Else index.php sent courseid as parameter so is a new row.
$courseid = required_param('courseid', PARAM_INT);
// Cast var $row to object to populate the form.
$row = (object)['courseid' => $courseid];
$urlparams = ['courseid' => $courseid];
$title = get_string('new', 'tool_carcastc');
}
// Pass argument to query string.
$url = new moodle_url('/admin/tool/carcastc/edit.php', $urlparams);
// Set most used strings in variable.
$pnstring = get_string('pluginname', 'tool_carcastc');
$PAGE->set_context(context_system::instance());
$PAGE->set_url($url);
$PAGE->set_pagelayout('report');
// Force users logged and check view capability.
require_login();
$context = context_course::instance($courseid);
require_capability('tool/carcastc:edit', $context);
$PAGE->set_title($title);
$PAGE->set_heading($pnstring);
// Instantiate tool_carcastc_formdata.
$mform = new \tool_carcastc\tool_carcastc_formdata();
if (isset($row->id)) {
$editoroptions = ['trusttext' => true, 'subdirs' => true, 'maxfiles' => -1, 'maxbytes' => 0, 'context' => $context];
file_prepare_standard_editor($row, 'description', $editoroptions, $editoroptions['context'],
'tool_carcastc', 'rowfile', $row->id);
}
// Set default data (if any).
$mform->set_data($row);
// Set url for redirect.
$home = new \moodle_url('/admin/tool/carcastc/index.php', ['courseid' => $courseid]);
// Form processing and displaying is done here.
if ($mform->is_cancelled()) {
// Handle form cancel operation, if cancel button is present on form.
redirect($home);
} else if ($form = $mform->get_data()) {
// In this case you process validated data. $form->get_data() returns data posted in form.
\tool_carcastc\tool_carcastc_model::save_row($form);
redirect($home);
}
// Displays the form.
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$mform->display();
echo $OUTPUT->footer();