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\table;
|
|
|
18 |
|
|
|
19 |
use context;
|
|
|
20 |
use context_system;
|
|
|
21 |
use core_table\sql_table;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Table for penalty rule.
|
|
|
25 |
*
|
|
|
26 |
* @package gradepenalty_duedate
|
|
|
27 |
* @copyright 2024 Catalyst IT Australia Pty Ltd
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class penalty_rule_table extends sql_table {
|
|
|
31 |
/** @var context context */
|
|
|
32 |
protected context $context;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Sets up the table_log parameters.
|
|
|
36 |
*
|
|
|
37 |
* @param string $uniqueid unique id of form.
|
|
|
38 |
* @param string $contextid context id.
|
|
|
39 |
*/
|
|
|
40 |
public function __construct($uniqueid, $contextid) {
|
|
|
41 |
parent::__construct($uniqueid);
|
|
|
42 |
|
|
|
43 |
$this->context = context::instance_by_id($contextid);
|
|
|
44 |
|
|
|
45 |
// Add columns.
|
|
|
46 |
$this->define_columns([
|
|
|
47 |
'overdueby',
|
|
|
48 |
'penalty',
|
|
|
49 |
]);
|
|
|
50 |
|
|
|
51 |
// Add columns header.
|
|
|
52 |
$this->define_headers([
|
|
|
53 |
get_string('overdueby', 'gradepenalty_duedate'),
|
|
|
54 |
get_string('penalty', 'gradepenalty_duedate'),
|
|
|
55 |
]);
|
|
|
56 |
|
|
|
57 |
// Disable sorting.
|
|
|
58 |
$this->sortable(false);
|
|
|
59 |
|
|
|
60 |
// Disable hiding columns.
|
|
|
61 |
$this->collapsible(false);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
#[\Override]
|
|
|
65 |
public function query_db($pagesize, $useinitialsbar = true): void {
|
|
|
66 |
global $DB;
|
|
|
67 |
// Contexts to find the penalty rules.
|
|
|
68 |
$contextlevel = $this->context->contextlevel;
|
|
|
69 |
$contextids = [];
|
|
|
70 |
if ($contextlevel == CONTEXT_MODULE) {
|
|
|
71 |
// Module context.
|
|
|
72 |
$contextids[] = $this->context->id;
|
|
|
73 |
// Course context.
|
|
|
74 |
$contextids[] = $this->context->get_parent_context()->id;
|
|
|
75 |
// System context.
|
|
|
76 |
$contextids[] = context_system::instance()->id;
|
|
|
77 |
} else if ($contextlevel == CONTEXT_COURSE) {
|
|
|
78 |
// Course context.
|
|
|
79 |
$contextids[] = $this->context->id;
|
|
|
80 |
// System context.
|
|
|
81 |
$contextids[] = context_system::instance()->id;
|
|
|
82 |
} else if ($contextlevel == CONTEXT_SYSTEM) {
|
|
|
83 |
// System context.
|
|
|
84 |
$contextids[] = $this->context->id;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Find the penalty rules.
|
|
|
88 |
foreach ($contextids as $contextid) {
|
|
|
89 |
$params = ['contextid' => $contextid];
|
|
|
90 |
$total = $DB->count_records_sql($this->get_sql(true), $params);
|
|
|
91 |
if ($total > 0) {
|
|
|
92 |
break;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$this->pagesize($pagesize, $total);
|
|
|
97 |
$this->rawdata = $DB->get_records_sql($this->get_sql(), $params, $this->get_page_start(), $this->get_page_size());
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Builds the SQL query.
|
|
|
102 |
*
|
|
|
103 |
* @param bool $count When true, return the count SQL.
|
|
|
104 |
* @return string the SQL query.
|
|
|
105 |
*/
|
|
|
106 |
protected function get_sql($count = false): string {
|
|
|
107 |
if ($count) {
|
|
|
108 |
$select = "COUNT(1)";
|
|
|
109 |
$order = "";
|
|
|
110 |
} else {
|
|
|
111 |
$select = "r.overdueby, r.penalty, r.sortorder";
|
|
|
112 |
$order = " order by r.sortorder ASC";
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$sql = "SELECT $select
|
|
|
116 |
FROM {gradepenalty_duedate_rule} r
|
|
|
117 |
WHERE r.contextid = :contextid";
|
|
|
118 |
|
|
|
119 |
return $sql . $order;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Overdue column.
|
|
|
124 |
*
|
|
|
125 |
* @param object $row row object.
|
|
|
126 |
*/
|
|
|
127 |
public function col_overdueby($row): string {
|
|
|
128 |
// If this is the last rule, show the last row.
|
|
|
129 |
if (count($this->rawdata) === ($row->sortorder + 1)) {
|
|
|
130 |
// Find the previous rule.
|
|
|
131 |
foreach ($this->rawdata as $rule) {
|
|
|
132 |
if ($rule->sortorder == $row->sortorder - 1) {
|
|
|
133 |
return get_string('overdueby_lastrow', 'gradepenalty_duedate', format_time($rule->overdueby));
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
// There is only one row.
|
|
|
137 |
return get_string('overdueby_onerow', 'gradepenalty_duedate');
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
// This is not the last rule.
|
|
|
141 |
return get_string('overdueby_row', 'gradepenalty_duedate', format_time($row->overdueby));
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Penalty column.
|
|
|
146 |
*
|
|
|
147 |
* @param object $row row object.
|
|
|
148 |
*/
|
|
|
149 |
public function col_penalty($row): string {
|
|
|
150 |
return get_string('percents', 'moodle', format_float($row->penalty, -1));
|
|
|
151 |
}
|
|
|
152 |
}
|