Skip to content

Commit a00ccf2

Browse files
committed
MDL-81714 grades: Add regrading progress indicator to grade reports
1 parent 17c1519 commit a00ccf2

File tree

10 files changed

+226
-29
lines changed

10 files changed

+226
-29
lines changed

grade/lib.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,8 @@ public function __construct($id, $link, $string, $parent=null) {
882882
*/
883883
function print_grade_page_head(int $courseid, string $active_type, ?string $active_plugin = null, string|bool $heading = false,
884884
bool $return = false, $buttons = false, bool $shownavigation = true, ?string $headerhelpidentifier = null,
885-
?string $headerhelpcomponent = null, ?stdClass $user = null, ?action_bar $actionbar = null, $unused = null) {
885+
?string $headerhelpcomponent = null, ?stdClass $user = null, ?action_bar $actionbar = null, $unused = null,
886+
?\core\output\task_indicator $taskindicator = null) {
886887
global $CFG, $OUTPUT, $PAGE, $USER;
887888

888889
if ($heading !== false) {
@@ -992,6 +993,10 @@ function print_grade_page_head(int $courseid, string $active_type, ?string $acti
992993
$output = $OUTPUT->heading($heading);
993994
}
994995

996+
if ($taskindicator && $taskindicator->has_task_record()) {
997+
$output .= $OUTPUT->render($taskindicator);
998+
}
999+
9951000
if ($return) {
9961001
$returnval .= $output;
9971002
} else {

grade/report/grader/index.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,14 @@
124124
}
125125

126126
$reportname = get_string('pluginname', 'gradereport_grader');
127-
128-
// Do this check just before printing the grade header (and only do it once).
129-
grade_regrade_final_grades_if_required($course);
127+
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
128+
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
129+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
130+
if (!$taskindicator->has_task_record()) {
131+
// Do this check just before printing the grade header (and only do it once).
132+
grade_regrade_final_grades_if_required($course);
133+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
134+
}
130135

131136
//Initialise the grader report object that produces the table
132137
//the class grade_report_grader_ajax was removed as part of MDL-21562
@@ -148,8 +153,14 @@
148153
$numusers = $report->get_numusers(true, true);
149154

150155
$actionbar = new \gradereport_grader\output\action_bar($context, $report, $numusers);
151-
print_grade_page_head($COURSE->id, 'report', 'grader', false, false, $buttons, true,
152-
null, null, null, $actionbar);
156+
print_grade_page_head(
157+
$COURSE->id,
158+
'report',
159+
'grader',
160+
buttons: $buttons,
161+
actionbar: $actionbar,
162+
taskindicator: $taskindicator,
163+
);
153164

154165
// make sure separate group does not prevent view
155166
if ($report->currentgroup == -2) {

grade/report/outcomes/index.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@
4444
}
4545

4646
// First make sure we have proper final grades.
47-
grade_regrade_final_grades_if_required($course);
47+
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
48+
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
49+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
50+
if (!$taskindicator->has_task_record()) {
51+
grade_regrade_final_grades_if_required($course);
52+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
53+
}
4854

4955
// Grab all outcomes used in course.
5056
$report_info = array();
@@ -176,7 +182,7 @@
176182

177183
$html .= '</table>';
178184

179-
print_grade_page_head($courseid, 'report', 'outcomes');
185+
print_grade_page_head($courseid, 'report', 'outcomes', taskindicator: $taskindicator);
180186

181187
echo $html;
182188

grade/report/overview/index.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,18 @@
109109
$user_selector = true;
110110
}
111111

112+
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
113+
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
114+
112115
if (empty($userid)) {
113-
print_grade_page_head($courseid, 'report', 'overview', false, false, false,
114-
true, null, null, null, $actionbar);
116+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
117+
print_grade_page_head(
118+
$courseid,
119+
'report',
120+
'overview',
121+
actionbar: $actionbar,
122+
taskindicator: $taskindicator,
123+
);
115124

116125
groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
117126

@@ -126,9 +135,16 @@
126135
// Make sure we have proper final grades - this report shows grades from other courses, not just the
127136
// selected one, so we need to check and regrade all courses the user is enrolled in.
128137
$report->regrade_all_courses_if_needed(true);
129-
print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview') .
130-
' - ' . fullname($report->user), false, false, true, null, null,
131-
$report->user, $actionbar);
138+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
139+
print_grade_page_head(
140+
$courseid,
141+
'report',
142+
'overview',
143+
get_string('pluginname', 'gradereport_overview') . ' - ' . fullname($report->user),
144+
user: $report->user,
145+
actionbar: $actionbar,
146+
taskindicator: $taskindicator,
147+
);
132148
groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
133149

134150
if ($user_selector) {

grade/report/singleview/index.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,36 @@
178178
$PAGE->requires->js_call_amd('gradereport_singleview/group', 'init', [$itemtype]);
179179
}
180180

181+
// Make sure we have proper final grades.
182+
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
183+
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
184+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
185+
if (!$taskindicator->has_task_record()) {
186+
grade_regrade_final_grades_if_required($course);
187+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
188+
}
189+
181190
if ($itemtype == 'user') {
182-
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
183-
true, null, null, $report->screen->item, $actionbar);
191+
print_grade_page_head(
192+
$course->id,
193+
'report',
194+
'singleview',
195+
$reportname,
196+
buttons: $button,
197+
user: $report->screen->item,
198+
actionbar: $actionbar,
199+
taskindicator: $taskindicator,
200+
);
184201
} else {
185-
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
186-
true, null, null, null, $actionbar);
202+
print_grade_page_head(
203+
$course->id,
204+
'report',
205+
'singleview',
206+
$reportname,
207+
buttons: $button,
208+
actionbar: $actionbar,
209+
taskindicator: $taskindicator,
210+
);
187211
}
188212

189213
if ($data = data_submitted()) {
@@ -207,9 +231,6 @@
207231
}
208232
}
209233

210-
// Make sure we have proper final grades.
211-
grade_regrade_final_grades_if_required($course);
212-
213234
// Save the screen state in a session variable as last viewed state.
214235
$SESSION->gradereport_singleview["itemtype-{$context->id}"] = $itemtype;
215236
if ($itemid) {

grade/report/user/classes/external/user.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected static function get_report_data(
161161
require_once($CFG->dirroot . '/grade/report/user/lib.php');
162162

163163
// Force regrade to update items marked as 'needupdate'.
164-
grade_regrade_final_grades($course->id);
164+
grade_regrade_final_grades($course->id, async: false);
165165

166166
$gpr = new grade_plugin_return(
167167
[

grade/report/user/index.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@
8484
$USER->grade_last_report[$course->id] = 'user';
8585

8686
// First make sure we have proper final grades.
87-
grade_regrade_final_grades_if_required($course);
87+
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
88+
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
89+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
90+
if (!$taskindicator->has_task_record()) {
91+
grade_regrade_final_grades_if_required($course);
92+
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
93+
}
8894

8995
$gradesrenderer = $PAGE->get_renderer('core_grades');
9096

@@ -141,8 +147,13 @@
141147
if (is_null($userid)) { // Zero state.
142148
$actionbar = new \gradereport_user\output\action_bar($context, $userview, null, $currentgroup);
143149
// Print header.
144-
print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
145-
null, null, null, $actionbar);
150+
print_grade_page_head(
151+
$courseid,
152+
'report',
153+
'user',
154+
actionbar: $actionbar,
155+
taskindicator: $taskindicator,
156+
);
146157

147158
if (empty($gradableusers)) { // There are no available gradable users, display a notification.
148159
$message = $currentgroup ? get_string('nostudentsingroup') : get_string('nostudentsyet');
@@ -156,8 +167,13 @@
156167
$SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
157168

158169
$actionbar = new \gradereport_user\output\action_bar($context, $userview, 0, $currentgroup);
159-
print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
160-
null, null, null, $actionbar);
170+
print_grade_page_head(
171+
$courseid,
172+
'report',
173+
'user',
174+
actionbar: $actionbar,
175+
taskindicator: $taskindicator,
176+
);
161177

162178
while ($userdata = $gui->next_user()) {
163179
$user = $userdata->user;
@@ -177,7 +193,14 @@
177193
$report = new gradereport_user\report\user($courseid, $gpr, $context, $userid, $viewasuser);
178194
$actionbar = new \gradereport_user\output\action_bar($context, $userview, $report->user->id, $currentgroup);
179195

180-
print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user, $actionbar);
196+
print_grade_page_head(
197+
$courseid,
198+
'report',
199+
'user',
200+
user: $report->user,
201+
actionbar: $actionbar,
202+
taskindicator: $taskindicator,
203+
);
181204

182205
if ($currentgroup && !groups_is_member($currentgroup, $userid)) {
183206
echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
@@ -199,7 +222,13 @@
199222
$report = new gradereport_user\report\user($courseid, $gpr, $context, $userid ?? $USER->id);
200223

201224
// Print the page.
202-
print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user);
225+
print_grade_page_head(
226+
$courseid,
227+
'report',
228+
'user',
229+
user: $report->user,
230+
taskindicator: $taskindicator,
231+
);
203232

204233
if ($report->fill_table()) {
205234
echo $report->print_table(true);

lib/classes/output/task_indicator.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
namespace core\output;
17+
18+
use core\task\adhoc_task;
19+
use core\task\stored_progress_task_trait;
20+
use renderer_base;
21+
use stdClass;
22+
use core\task\task_base;
23+
use core\stored_progress_bar;
24+
use renderable;
25+
use templatable;
26+
27+
/**
28+
* Indicator for displaying status and progress of a background task
29+
*
30+
* @package core\output
31+
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
32+
* @author Mark Johnson <[email protected]>
33+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34+
*/
35+
class task_indicator implements renderable, templatable {
36+
37+
protected ?stdClass $taskrecord;
38+
39+
protected ?stored_progress_bar $progressbar;
40+
41+
public function __construct(
42+
protected adhoc_task $task,
43+
protected string $message,
44+
) {
45+
if (!class_uses($task::class, stored_progress_task_trait::class)) {
46+
throw new \coding_exception('task_indicator can only be used for tasks using stored_progress_task_trait.');
47+
}
48+
$this->taskrecord = \core\task\manager::get_queued_adhoc_task_record($this->task) ?: null;
49+
if ($this->taskrecord) {
50+
$this->task->set_id($this->taskrecord->id);
51+
$idnumber = stored_progress_bar::convert_to_idnumber($this->task::class, $this->task->get_id());
52+
$this->progressbar = stored_progress_bar::get_by_idnumber($idnumber);
53+
}
54+
}
55+
56+
/**
57+
* Return true if there is an existing record for this task.
58+
*
59+
* @return bool
60+
*/
61+
public function has_task_record(): bool {
62+
return !is_null($this->taskrecord);
63+
}
64+
65+
public function export_for_template(renderer_base $output): array {
66+
$export = [];
67+
if ($this->taskrecord) {
68+
$export['message'] = $this->message;
69+
$export['progress'] = $this->progressbar->get_content();
70+
}
71+
return $export;
72+
}
73+
74+
}

lib/classes/task/manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected static function task_is_scheduled($task) {
194194
* @param adhoc_task $task
195195
* @return \stdClass|false
196196
*/
197-
protected static function get_queued_adhoc_task_record($task) {
197+
public static function get_queued_adhoc_task_record($task) {
198198
global $DB;
199199

200200
$record = self::record_from_adhoc_task($task);

lib/templates/task_indicator.mustache

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{{!
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+
{{!
18+
@template core/task_indicator
19+
Progress bar.
20+
21+
Example context (json):
22+
{
23+
"message": "message",
24+
"progress": {
25+
"id": "progressbar_test",
26+
"idnumber": "progressbar_test",
27+
"width": "500",
28+
"value": "50"
29+
}
30+
}
31+
}}
32+
<div class="alert alert-info">
33+
<p>{{message}}</p>
34+
{{{progress}}}
35+
</div>

0 commit comments

Comments
 (0)