| 1441 |
ariadna |
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 gradepenalty_duedate;
|
|
|
18 |
|
|
|
19 |
use context;
|
|
|
20 |
use context_system;
|
|
|
21 |
use core\lang_string;
|
|
|
22 |
use core\persistent;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* To create/load/update/delete penalty rules.
|
|
|
26 |
*
|
|
|
27 |
* @package gradepenalty_duedate
|
|
|
28 |
* @copyright 2024 Catalyst IT Australia Pty Ltd
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class penalty_rule extends persistent {
|
|
|
32 |
/** The table name this persistent object maps to. */
|
|
|
33 |
const TABLE = 'gradepenalty_duedate_rule';
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Return the definition of the properties of this model.
|
|
|
37 |
*/
|
|
|
38 |
protected static function define_properties(): array {
|
|
|
39 |
return [
|
|
|
40 |
'contextid' => [
|
|
|
41 |
'type' => PARAM_INT,
|
|
|
42 |
'null' => NULL_NOT_ALLOWED,
|
|
|
43 |
],
|
|
|
44 |
'overdueby' => [
|
|
|
45 |
'type' => PARAM_INT,
|
|
|
46 |
'null' => NULL_NOT_ALLOWED,
|
|
|
47 |
],
|
|
|
48 |
'penalty' => [
|
|
|
49 |
'type' => PARAM_FLOAT,
|
|
|
50 |
'null' => NULL_NOT_ALLOWED,
|
|
|
51 |
],
|
|
|
52 |
'sortorder' => [
|
|
|
53 |
'type' => PARAM_INT,
|
|
|
54 |
'null' => NULL_NOT_ALLOWED,
|
|
|
55 |
'default' => 0,
|
|
|
56 |
],
|
|
|
57 |
];
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Validate the overdueby before saving.
|
|
|
62 |
*
|
|
|
63 |
* @param int $value overdueby value.
|
|
|
64 |
* @return true|lang_string error message if overdueby is invalid.
|
|
|
65 |
*/
|
|
|
66 |
protected function validate_overdueby($value): bool|lang_string {
|
|
|
67 |
if ($value < constants::OVERDUEBY_MIN) {
|
|
|
68 |
return new lang_string('error_overdueby_minvalue', 'gradepenalty_duedate', constants::OVERDUEBY_MIN);
|
|
|
69 |
}
|
|
|
70 |
return true;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Validate the penalty before saving.
|
|
|
75 |
*
|
|
|
76 |
* @param int $value penalty value.
|
|
|
77 |
* @return true|lang_string error message if penalty is invalid.
|
|
|
78 |
*/
|
|
|
79 |
protected function validate_penalty($value): bool|lang_string {
|
|
|
80 |
if ($value < constants::PENALTY_MIN) {
|
|
|
81 |
return new lang_string('error_penalty_minvalue', 'gradepenalty_duedate', constants::PENALTY_MIN);
|
|
|
82 |
} else if ($value > constants::PENALTY_MAX) {
|
|
|
83 |
return new lang_string('error_penalty_maxvalue', 'gradepenalty_duedate', constants::PENALTY_MAX);
|
|
|
84 |
}
|
|
|
85 |
return true;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Get the penalty rules for a context.
|
|
|
90 |
* If not found, it will get the rules from the parent context.
|
|
|
91 |
*
|
|
|
92 |
* @param int $contextid context id
|
|
|
93 |
* @return array penalty_rule records.
|
|
|
94 |
*/
|
|
|
95 |
public static function get_rules(int $contextid): array {
|
|
|
96 |
$rules = [];
|
|
|
97 |
$currentcontext = context::instance_by_id($contextid);
|
|
|
98 |
while (empty($rules) && $currentcontext) {
|
|
|
99 |
$rules = self::get_records(['contextid' => $currentcontext->id], 'sortorder');
|
|
|
100 |
$currentcontext = $currentcontext->get_parent_context();
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return $rules;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Reset rules for a context.
|
|
|
108 |
* Delete all rules for the context.
|
|
|
109 |
*
|
|
|
110 |
* @param int $contextid context id.
|
|
|
111 |
* @return void
|
|
|
112 |
*/
|
|
|
113 |
public static function reset_rules(int $contextid): void {
|
|
|
114 |
// Get rules for the context.
|
|
|
115 |
$rules = self::get_records(['contextid' => $contextid]);
|
|
|
116 |
|
|
|
117 |
// Delete all rules.
|
|
|
118 |
foreach ($rules as $rule) {
|
|
|
119 |
$rule->delete();
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// Check if it is system context, create a default rule.
|
|
|
123 |
if ($contextid == context_system::instance()->id) {
|
|
|
124 |
$rule = new penalty_rule();
|
|
|
125 |
$rule->set('contextid', $contextid);
|
|
|
126 |
$rule->set('overdueby', 1);
|
|
|
127 |
$rule->set('penalty', 0);
|
|
|
128 |
$rule->set('sortorder', 0);
|
|
|
129 |
$rule->save();
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Check if rules are overridden in a context.
|
|
|
135 |
*
|
|
|
136 |
* @param int $contextid context id.
|
|
|
137 |
* @return bool true if rules are overridden.
|
|
|
138 |
*/
|
|
|
139 |
public static function is_overridden(int $contextid): bool {
|
|
|
140 |
// Exclude system context.
|
|
|
141 |
if ($contextid == context_system::instance()->id) {
|
|
|
142 |
return false;
|
|
|
143 |
}
|
|
|
144 |
$rules = self::get_records(['contextid' => $contextid]);
|
|
|
145 |
// If there is no rules in parent contexts, still consider they are overridden.
|
|
|
146 |
return count($rules) > 0;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Check if the rules are inherited from the parent context.
|
|
|
151 |
*
|
|
|
152 |
* @param int $contextid context id.
|
|
|
153 |
* @return bool true if rules are inherited.
|
|
|
154 |
*/
|
|
|
155 |
public static function is_inherited(int $contextid): bool {
|
|
|
156 |
// Exclude system context.
|
|
|
157 |
if ($contextid == context_system::instance()->id) {
|
|
|
158 |
return false;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
$rules = self::get_records(['contextid' => $contextid]);
|
|
|
162 |
$parentrules = self::get_rules($contextid);
|
|
|
163 |
return count($rules) == 0 && count($parentrules) > 0;
|
|
|
164 |
}
|
|
|
165 |
}
|