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 mod_assign\penalty;
|
|
|
18 |
|
|
|
19 |
use assign;
|
|
|
20 |
use core\context\module as context_module;
|
|
|
21 |
use core_grades\penalty_manager;
|
|
|
22 |
use grade_item;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Helper class for penalty in assignment module.
|
|
|
26 |
*
|
|
|
27 |
* @package mod_assign
|
|
|
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 helper {
|
|
|
32 |
/**
|
|
|
33 |
* Check if penalty is enabled for an assignment.
|
|
|
34 |
*
|
|
|
35 |
* @param int $assignid The assignment id.
|
|
|
36 |
*/
|
|
|
37 |
public static function is_penalty_enabled(int $assignid): bool {
|
|
|
38 |
// Get the assignment course module.
|
|
|
39 |
$cm = get_coursemodule_from_instance('assign', $assignid);
|
|
|
40 |
$context = context_module::instance($cm->id);
|
|
|
41 |
|
|
|
42 |
// Get the assignment instance.
|
|
|
43 |
$assign = new assign($context, $cm, $cm->course);
|
|
|
44 |
|
|
|
45 |
// Check if due date is set.
|
|
|
46 |
if (!$assign->get_instance()->duedate) {
|
|
|
47 |
return false;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
// Check if the grade type is set to GRADE_TYPE_VALUE (grade 1 to 100).
|
|
|
51 |
if ($assign->get_instance()->grade < GRADE_TYPE_VALUE) {
|
|
|
52 |
return false;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// Check if the assignment is set to use penalty.
|
|
|
56 |
if (!$assign->get_instance()->gradepenalty) {
|
|
|
57 |
return false;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
return true;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Apply penalty to a user.
|
|
|
65 |
*
|
|
|
66 |
* @param int $assignid The assignment id.
|
|
|
67 |
* @param int $userid The user id.
|
|
|
68 |
*/
|
|
|
69 |
public static function apply_penalty_to_user(int $assignid, int $userid): void {
|
|
|
70 |
global $CFG, $DB;
|
|
|
71 |
|
|
|
72 |
require_once($CFG->dirroot . '/mod/assign/locallib.php');
|
|
|
73 |
|
|
|
74 |
// Check if penalty is enabled for this assignment.
|
|
|
75 |
if (!self::is_penalty_enabled($assignid)) {
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// Get the assignment course module.
|
|
|
80 |
$cm = get_coursemodule_from_instance('assign', $assignid);
|
|
|
81 |
$context = context_module::instance($cm->id);
|
|
|
82 |
|
|
|
83 |
// Get the assignment instance.
|
|
|
84 |
$assign = new assign($context, $cm, $cm->course);
|
|
|
85 |
|
|
|
86 |
// Find the graded attempt.
|
|
|
87 |
$sql = "SELECT MAX(attemptnumber) as attemptnumber
|
|
|
88 |
FROM {assign_grades}
|
|
|
89 |
WHERE assignment = :assignid
|
|
|
90 |
AND userid = :userid
|
|
|
91 |
AND grade >= 0";
|
|
|
92 |
$assigngrade = $DB->get_record_sql($sql, ['assignid' => $assignid, 'userid' => $userid]);
|
|
|
93 |
|
|
|
94 |
// Get the submission.
|
|
|
95 |
if ($assign->get_instance()->teamsubmission) {
|
|
|
96 |
$submission = $assign->get_group_submission($userid, 0, false, $assigngrade->attemptnumber);
|
|
|
97 |
} else {
|
|
|
98 |
$submission = $assign->get_user_submission($userid, false, $assigngrade->attemptnumber);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
// Check if the submission is null.
|
|
|
102 |
if ($submission === null) {
|
|
|
103 |
debugging(
|
|
|
104 |
"Submission not found for user {$userid} in assignment {$assignid} attempt {$assigngrade->attemptnumber}",
|
|
|
105 |
);
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// Get submission date.
|
|
|
110 |
$submissiondate = $submission->timemodified;
|
|
|
111 |
|
|
|
112 |
// Check if we have valid submission date.
|
|
|
113 |
if (empty($submissiondate)) {
|
|
|
114 |
debugging(
|
|
|
115 |
"Invalid submission date for user {$userid} in assignment {$assignid} attempt {$assigngrade->attemptnumber}",
|
|
|
116 |
);
|
|
|
117 |
return;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
// Get the due date from the override if it exists. Otherwise, retrieve the date from the assignment settings.
|
|
|
121 |
$duedate = $assign->override_exists($userid)->duedate ?? $assign->get_instance()->duedate;
|
|
|
122 |
|
|
|
123 |
// Get extension.
|
|
|
124 |
$userflags = $assign->get_user_flags($userid, false);
|
|
|
125 |
if (!empty($userflags)) {
|
|
|
126 |
$duedate = max($userflags->extensionduedate, $duedate);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
// Get grade item.
|
|
|
130 |
$gradeitem = grade_item::fetch([
|
|
|
131 |
'courseid' => $assign->get_course()->id,
|
|
|
132 |
'itemtype' => 'mod',
|
|
|
133 |
'itemmodule' => 'assign',
|
|
|
134 |
'iteminstance' => $assign->get_instance()->id,
|
|
|
135 |
'itemnumber' => 0,
|
|
|
136 |
]);
|
|
|
137 |
|
|
|
138 |
// Apply penalty.
|
|
|
139 |
$container = penalty_manager::apply_grade_penalty_to_user($userid, $gradeitem, $submissiondate, $duedate);
|
|
|
140 |
if ($container->get_grade_before_penalties() == 0) {
|
|
|
141 |
// There is no deduction applied to grade 0.
|
|
|
142 |
$deductedpercentage = 0;
|
|
|
143 |
} else {
|
|
|
144 |
$deductedpercentage = $container->get_penalty() / $container->get_grade_before_penalties() * 100;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// Store the assign grade penalty.
|
|
|
148 |
$DB->set_field_select(
|
|
|
149 |
'assign_grades',
|
|
|
150 |
'penalty',
|
|
|
151 |
$deductedpercentage,
|
|
|
152 |
'assignment = :assignid AND userid = :userid AND attemptnumber = :attemptnumber',
|
|
|
153 |
['assignid' => $assignid, 'userid' => $userid, 'attemptnumber' => $assigngrade->attemptnumber],
|
|
|
154 |
);
|
|
|
155 |
}
|
|
|
156 |
}
|