-
Notifications
You must be signed in to change notification settings - Fork 1
/
setcontrol.php
70 lines (60 loc) · 2.53 KB
/
setcontrol.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
<?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/>.
/**
* Code to update if a course module is controlled by a consentform module or not in response to an ajax call.
*
* @package mod_consentform
* @copyright 2020 Thomas Niedermaier, Medical University of Vienna <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once(__DIR__ . '/../../config.php');
require_once(dirname(__FILE__) . '/locallib.php');
// Check access.
if (!confirm_sesskey()) {
throw new moodle_exception('invalidsesskey', 'error');
}
// Get the params.
$ischecked = required_param('ischecked', PARAM_BOOL); // Is the checkbox clicked or not?
$cmidcontrolled = required_param('value', PARAM_INT); // The ID of the dependent coursemodule.
$cmidcontroller = required_param('cmid', PARAM_INT); // The ID of this consentform module.
$course = get_course_and_cm_from_cmid($cmidcontrolled)[0];
require_course_login($course);
// Update database entry.
// Legend: $ret... 1 if db-entry was made, 2 if db-entry was removed (or not found), 3 if nothing was done.
$ret = 3;
if (is_numeric($cmidcontrolled)) {
if ($ischecked) { // Checkbox is clicked.
// If NO db-entry yet make it.
if (!consentform_find_entry_availability($cmidcontrolled, $cmidcontroller)) {
if ($ok = consentform_make_entry_availability($course->id, $cmidcontrolled, $cmidcontroller)) {
$ret = 1;
} else {
$ret = 3;
}
}
} else { // Checkbox is deselected.
// If DB-entry exists remove it.
if (consentform_find_entry_availability($cmidcontrolled, $cmidcontroller)) {
if ($ok = consentform_delete_entry_availability($course->id, $cmidcontrolled, $cmidcontroller)) {
$ret = 2;
}
} else {
$ret = 3;
}
}
}
echo $ret;