-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_controller_course.php
More file actions
executable file
·426 lines (359 loc) · 12.3 KB
/
program_controller_course.php
File metadata and controls
executable file
·426 lines (359 loc) · 12.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?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 <https://www.gnu.org/licenses/>.
// Project implemented by the \"Recovery, Transformation and Resilience Plan.
// Funded by the European Union - Next GenerationEU\".
//
// Produced by the UNIMOODLE University Group: Universities of
// Valladolid, Complutense de Madrid, UPV/EHU, León, Salamanca,
// Illes Balears, Valencia, Rey Juan Carlos, La Laguna, Zaragoza, Málaga,
// Córdoba, Extremadura, Vigo, Las Palmas de Gran Canaria y Burgos.
/**
* Version details
*
* @package quizaccess_sebprogram
* @copyright 2023 Proyecto UNIMOODLE
* @author UNIMOODLE Group (Coordinator) <direccion.area.estrategia.digital@uva.es>
* @author ISYC <soporte@isyc.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace quizaccess_sebprogram;
use core\notification;
use moodle_url;
use quizaccess_sebprogram\local\table\program_list_course;
/**
* Class for manipulating with the template records.
*/
class program_controller_course {
/**
* View action.
*/
const ACTION_VIEW = 'view';
/**
* Add action.
*/
const ACTION_ADD = 'add';
/**
* Edit action.
*/
const ACTION_EDIT = 'edit';
/**
* Delete action.
*/
const ACTION_DELETE = 'delete';
/**
* Course id.
*
* @var int
*/
private $courseid;
/**
* Locally cached $OUTPUT object.
* @var \bootstrap_renderer
*/
protected $output;
/**
* Constructor for the class.
*
* @param datatype $courseid description
*/
public function __construct($courseid) {
global $OUTPUT;
$this->courseid = $courseid;
$this->output = $OUTPUT;
}
/**
* Execute required action.
*
* @param string $action Action to execute.
*/
public function execute($action) {
switch($action) {
case self::ACTION_ADD:
case self::ACTION_EDIT:
$this->edit($action, optional_param('id', null, PARAM_INT));
break;
case self::ACTION_DELETE:
$this->delete(required_param('id', PARAM_INT));
break;
default:
$this->view();
break;
}
}
/**
* Set external page for the manager.
*/
protected function set_external_page() {
admin_externalpage_setup('quizaccess_sebprogram/view');
}
/**
* Return record instance.
*
* @param int $id
* @param \stdClass|null $data
*
* @return \quizaccess_sebprogram\program
*/
protected function get_instance($id = 0, \stdClass $data = null) {
return new program($id, $data);
}
/**
* Get instance of program_dependency
*
* @param int $id
* @param \stdClass $data
* @return program_dependency
*/
private function get_instance_dependency($id = 0, \stdClass $data = null) {
return new program_dependency($id, $data);
}
/**
* Print out all records in a table.
*/
protected function display_all_records() {
global $DB;
$records = program::get_records_course($this->courseid, 'id');
$table = new program_list_course();
$table->display($records);
}
/**
* Returns a text for create new record button.
* @return string
*/
protected function get_create_button_text() : string {
return get_string('addprogram', 'quizaccess_sebprogram');
}
/**
* Returns form for the record.
*
* @param \quizaccess_sebprogram\template_course|null $instance
*
* @return \quizaccess_sebprogram\local\form\template_course
*/
protected function get_form($instance) : \quizaccess_sebprogram\local\form\template_course {
global $PAGE;
return new \quizaccess_sebprogram\local\form\template_course($PAGE->url->out(false), ['persistent' => $instance]);
}
/**
* View page heading string.
* @return string
*/
protected function get_view_heading() : string {
return get_string('managetemplates', 'quizaccess_sebprogram');
}
/**
* New record heading string.
* @return string
*/
protected function get_new_heading() : string {
return get_string('newprogram', 'quizaccess_sebprogram');
}
/**
* Edit record heading string.
* @return string
*/
protected function get_edit_heading() : string {
return get_string('edittemplate', 'quizaccess_sebprogram');
}
/**
* Returns base URL for the manager.
* @return string
*/
public static function get_base_url() : string {
return ''.new moodle_url("/mod/quiz/accessrule/sebprogram/view_course.php",
['course' => optional_param('course', 0, PARAM_INT)]).'';
}
/**
* A description of the entire PHP function.
*
* @param datatype $action description
* @param datatype|null $id description
* @throws \Exception description of exception
* @return void
*/
protected function edit($action, $id = null) {
global $PAGE;
$PAGE->set_url(new \moodle_url(static::get_base_url(), ['action' => $action, 'id' => $id]));
$instance = null;
if ($id) {
$instance = $this->get_instance($id);
}
$form = $this->get_form($instance);
if ($form->is_cancelled()) {
redirect(new \moodle_url(static::get_base_url()));
} else if ($data = $form->get_data()) {
$data->timemodified = time();
unset($data->submitbutton);
try {
$selecteditems = $data->my_autocomplete_program;
$programselectlist = $form->get_program_select_list();
$insert = array_diff($selecteditems, $programselectlist);
$delete = array_diff($programselectlist, $selecteditems);
if (empty($data->id)) { // Create program.
$data->courseid = $this->courseid;
if ($this->program_unique_name_check($data)) {
notification::error(get_string('duplicatetemplate', 'quizaccess_sebprogram'));
redirect(new \moodle_url(static::get_base_url()));
}
$data->timecreated = time();
$persistent = $this->get_instance(0, $data);
$programcreated = $persistent->create();
$this->create_program_dependency($programcreated->get('id'), $insert);
} else {// Update program.
$instance->from_record($data);
$instance->update();
$this->create_program_dependency($data->id, $insert);
$this->delete_program_dependency($data->id, $delete, $form->get_records_program_select());
}
notification::success(get_string('changessaved'));
} catch (\Exception $e) {
notification::error($e->getMessage());
}
redirect(new \moodle_url(static::get_base_url()));
} else {
if (empty($instance)) {
$form->set_data(['display' => 1]);
$this->header($this->get_new_heading());
} else {
$this->header($this->get_edit_heading());
}
}
$form->display();
$this->footer();
}
/**
* Execute delete action.
*
* @param int $id ID of the region.
*/
protected function delete($id) {
global $DB;
require_sesskey();
$instance = $this->get_instance($id);
$dependencyrecords = $DB->get_records("quizaccess_sebprogram_depend", ['idprogram_dependency' => $id]);
if ($instance->can_delete()) {
$instance->delete();
foreach ($dependencyrecords as $dependencyrecord) {
$DB->delete_records("quizaccess_sebprogram_depend", ['id' => $dependencyrecord->id]);
}
notification::success(get_string('deleted'));
redirect(new \moodle_url(static::get_base_url()));
} else {
notification::warning(get_string('cantdelete', 'quizaccess_sebprogram'));
redirect(new \moodle_url(static::get_base_url()));
}
}
/**
* Execute view action.
*/
protected function view() {
global $PAGE;
// Avoid 'This page did not call $PAGE->set_url(...)' warning.
// Details at https://tracker.moodle.org/browse/MDL-75450.
$PAGE->set_url(new \moodle_url(static::get_base_url()));
$this->header($this->get_view_heading());
$this->print_add_button();
$this->display_all_records();
$this->footer();
}
/**
* Print out add button.
*/
protected function print_add_button() {
// Ignored for now session_start();.
$urlquiz = $_SESSION['urleditquiz'];
echo $this->output->single_button(
$urlquiz,
get_string('returntoquiz', 'quizaccess_sebprogram'),
);
echo $this->output->single_button(
new \moodle_url(static::get_base_url(), ['action' => self::ACTION_ADD]),
$this->get_create_button_text()
);
}
/**
* Print out page header.
* @param string $title Title to display.
*/
protected function header($title) {
echo $this->output->header();
echo $this->output->heading($title);
}
/**
* Print out the page footer.
*
* @return void
*/
protected function footer() {
echo $this->output->footer();
}
/**
* Returns a text for create new record button.
* @return string
*/
protected function get_create_program_button_text() : string {
return get_string('addprogram', 'quizaccess_sebprogram');
}
/**
* Create program dependency
*
* @param datatype $idprogram description
* @param datatype $insert description
* @throws Some_Exception_Class description of exception
* @return Some_Return_Value
*/
private function create_program_dependency($idprogram, $insert) {
foreach ($insert as $value) {
$data['idprogram'] = $idprogram;
$data['idprogram_dependency'] = $value;
$persistent = $this->get_instance_dependency(0, (object)$data);
$persistent->create();
}
}
/**
* Deletes program dependencies based on given parameters.
*
* @param datatype $idprogram description
* @param datatype $delete description
* @param array $records description
* @throws Some_Exception_Class description of exception
* @return Some_Return_Value
*/
private function delete_program_dependency($idprogram, $delete, $records) {
$keysrecord = array_keys($records);
foreach ($delete as $value) {
$posrecordprogram = array_search($value, array_column($records, 'idprogram_dependency'));
$iddependency = $keysrecord[$posrecordprogram];
$instance = $this->get_instance_dependency($iddependency);
$instance->delete();
}
}
/**
* Searches for another program with the same name to prevent duplicates
* @param object $programdata program data
* @return bool indicating if record exists.
*/
protected function program_unique_name_check($programdata) : bool {
global $DB;
$existsprogam = false;
if ($DB->get_record_select('quizaccess_seb_program',
'courseid = ? AND ' . $DB->sql_compare_text('title') . ' = ?' ,
[$programdata->courseid, $programdata->title], '*')) {
$existsprogam = true;
}
return $existsprogam;
}
}