-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.php
More file actions
140 lines (102 loc) · 4.07 KB
/
options.php
File metadata and controls
140 lines (102 loc) · 4.07 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Display information about all the mod_choicepath modules in the requested course.
*
* @package mod_choicepath
* @copyright 2024 Willian Mano <willianmanoaraujo@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__.'/../../config.php');
// Course module id.
$cmid = required_param('cmid', PARAM_INT);
$id = optional_param('id', null, PARAM_INT);
$action = optional_param('action', null, PARAM_ALPHANUMEXT);
$cm = get_coursemodule_from_id('choicepath', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
$moduleinstance = $DB->get_record('choicepath', ['id' => $cm->instance], '*', MUST_EXIST);
require_login($course, true, $cm);
$modulecontext = context_module::instance($cm->id);
require_capability('mod/choicepath:addinstance', $modulecontext);
$optionmodel = new \mod_choicepath\model\option();
$params = ['cmid' => $cmid];
$option = null;
if ($id) {
$params['id'] = $id;
$option = $optionmodel->find($id);
}
if ($action) {
$params['action'] = $action;
}
$PAGE->set_url('/mod/choicepath/options.php', $params);
$PAGE->set_title(format_string($moduleinstance->name));
$PAGE->set_heading(format_string($moduleinstance->name));
$PAGE->set_context($modulecontext);
$renderer = $PAGE->get_renderer('mod_choicepath');
if (!$action) {
$contentrenderable = new \mod_choicepath\output\options($moduleinstance, $cm);
echo $OUTPUT->header();
echo $renderer->render($contentrenderable);
echo $OUTPUT->footer();
exit;
}
$redirecturl = new moodle_url('/mod/choicepath/options.php', ['cmid' => $cmid]);
if ($action == 'delete') {
try {
if (!confirm_sesskey()) {
redirect($redirecturl, get_string('invaliddata', 'error'), null, \core\output\notification::NOTIFY_ERROR);
}
list($success, $message) = $optionmodel->delete($option->id);
if ($success) {
redirect($redirecturl, $message, null, \core\output\notification::NOTIFY_SUCCESS);
}
redirect($redirecturl, $message, null, \core\output\notification::NOTIFY_ERROR);
} catch (\Exception $e) {
redirect($redirecturl, get_string('invaliddata', 'error'), null, \core\output\notification::NOTIFY_ERROR);
}
}
if ($action == 'update') {
$option->cmid = $cmid;
}
$form = new \mod_choicepath\form\options(new moodle_url('/mod/choicepath/options.php', $params), $option);
if ($form->is_cancelled()) {
redirect($redirecturl);
}
if ($formdata = $form->get_data()) {
$option = new \stdClass();
$option->choicepathid = $moduleinstance->id;
$option->title = $formdata->title;
$option->description = $formdata->description['text'];
$option->descriptionformat = $formdata->description['format'];
$option->timemodified = time();
$success = false;
$message = '';
if ($action == 'create') {
$option->timecreated = time();
list($success, $message) = $optionmodel->create($option, $modulecontext);
}
if ($action == 'update') {
$option->id = $id;
list($success, $message) = $optionmodel->update($option, $modulecontext);
}
if ($success) {
redirect($redirecturl, $message, null, \core\output\notification::NOTIFY_SUCCESS);
}
redirect($redirecturl, $message, null, \core\output\notification::NOTIFY_ERROR);
}
echo $OUTPUT->header();
$form->display();
echo $OUTPUT->footer();