Skip to content

Commit

Permalink
MDL-81714 grades: Add regrading progress indicator to grade reports
Browse files Browse the repository at this point in the history
  • Loading branch information
marxjohnson committed May 15, 2024
1 parent 17c1519 commit a00ccf2
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 29 deletions.
7 changes: 6 additions & 1 deletion grade/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@ public function __construct($id, $link, $string, $parent=null) {
*/
function print_grade_page_head(int $courseid, string $active_type, ?string $active_plugin = null, string|bool $heading = false,
bool $return = false, $buttons = false, bool $shownavigation = true, ?string $headerhelpidentifier = null,
?string $headerhelpcomponent = null, ?stdClass $user = null, ?action_bar $actionbar = null, $unused = null) {
?string $headerhelpcomponent = null, ?stdClass $user = null, ?action_bar $actionbar = null, $unused = null,
?\core\output\task_indicator $taskindicator = null) {
global $CFG, $OUTPUT, $PAGE, $USER;

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

if ($taskindicator && $taskindicator->has_task_record()) {
$output .= $OUTPUT->render($taskindicator);
}

if ($return) {
$returnval .= $output;
} else {
Expand Down
21 changes: 16 additions & 5 deletions grade/report/grader/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@
}

$reportname = get_string('pluginname', 'gradereport_grader');

// Do this check just before printing the grade header (and only do it once).
grade_regrade_final_grades_if_required($course);
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
if (!$taskindicator->has_task_record()) {
// Do this check just before printing the grade header (and only do it once).
grade_regrade_final_grades_if_required($course);
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
}

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

$actionbar = new \gradereport_grader\output\action_bar($context, $report, $numusers);
print_grade_page_head($COURSE->id, 'report', 'grader', false, false, $buttons, true,
null, null, null, $actionbar);
print_grade_page_head(
$COURSE->id,
'report',
'grader',
buttons: $buttons,
actionbar: $actionbar,
taskindicator: $taskindicator,
);

// make sure separate group does not prevent view
if ($report->currentgroup == -2) {
Expand Down
10 changes: 8 additions & 2 deletions grade/report/outcomes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
}

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

// Grab all outcomes used in course.
$report_info = array();
Expand Down Expand Up @@ -176,7 +182,7 @@

$html .= '</table>';

print_grade_page_head($courseid, 'report', 'outcomes');
print_grade_page_head($courseid, 'report', 'outcomes', taskindicator: $taskindicator);

echo $html;

Expand Down
26 changes: 21 additions & 5 deletions grade/report/overview/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,18 @@
$user_selector = true;
}

$regradetask = \core_course\task\regrade_final_grades::create($courseid);
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');

if (empty($userid)) {
print_grade_page_head($courseid, 'report', 'overview', false, false, false,
true, null, null, null, $actionbar);
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
print_grade_page_head(
$courseid,
'report',
'overview',
actionbar: $actionbar,
taskindicator: $taskindicator,
);

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

Expand All @@ -126,9 +135,16 @@
// Make sure we have proper final grades - this report shows grades from other courses, not just the
// selected one, so we need to check and regrade all courses the user is enrolled in.
$report->regrade_all_courses_if_needed(true);
print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview') .
' - ' . fullname($report->user), false, false, true, null, null,
$report->user, $actionbar);
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
print_grade_page_head(
$courseid,
'report',
'overview',
get_string('pluginname', 'gradereport_overview') . ' - ' . fullname($report->user),
user: $report->user,
actionbar: $actionbar,
taskindicator: $taskindicator,
);
groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));

if ($user_selector) {
Expand Down
35 changes: 28 additions & 7 deletions grade/report/singleview/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,36 @@
$PAGE->requires->js_call_amd('gradereport_singleview/group', 'init', [$itemtype]);
}

// Make sure we have proper final grades.
$regradetask = \core_course\task\regrade_final_grades::create($courseid);
$indicatormessage = get_string('recalculatinggradesadhoc', 'grades');
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
if (!$taskindicator->has_task_record()) {
grade_regrade_final_grades_if_required($course);
$taskindicator = new \core\output\task_indicator($regradetask, $indicatormessage);
}

if ($itemtype == 'user') {
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
true, null, null, $report->screen->item, $actionbar);
print_grade_page_head(
$course->id,
'report',
'singleview',
$reportname,
buttons: $button,
user: $report->screen->item,
actionbar: $actionbar,
taskindicator: $taskindicator,
);
} else {
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
true, null, null, null, $actionbar);
print_grade_page_head(
$course->id,
'report',
'singleview',
$reportname,
buttons: $button,
actionbar: $actionbar,
taskindicator: $taskindicator,
);
}

if ($data = data_submitted()) {
Expand All @@ -207,9 +231,6 @@
}
}

// Make sure we have proper final grades.
grade_regrade_final_grades_if_required($course);

// Save the screen state in a session variable as last viewed state.
$SESSION->gradereport_singleview["itemtype-{$context->id}"] = $itemtype;
if ($itemid) {
Expand Down
2 changes: 1 addition & 1 deletion grade/report/user/classes/external/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected static function get_report_data(
require_once($CFG->dirroot . '/grade/report/user/lib.php');

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

$gpr = new grade_plugin_return(
[
Expand Down
43 changes: 36 additions & 7 deletions grade/report/user/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@
$USER->grade_last_report[$course->id] = 'user';

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

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

Expand Down Expand Up @@ -141,8 +147,13 @@
if (is_null($userid)) { // Zero state.
$actionbar = new \gradereport_user\output\action_bar($context, $userview, null, $currentgroup);
// Print header.
print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
null, null, null, $actionbar);
print_grade_page_head(
$courseid,
'report',
'user',
actionbar: $actionbar,
taskindicator: $taskindicator,
);

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

$actionbar = new \gradereport_user\output\action_bar($context, $userview, 0, $currentgroup);
print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
null, null, null, $actionbar);
print_grade_page_head(
$courseid,
'report',
'user',
actionbar: $actionbar,
taskindicator: $taskindicator,
);

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

print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user, $actionbar);
print_grade_page_head(
$courseid,
'report',
'user',
user: $report->user,
actionbar: $actionbar,
taskindicator: $taskindicator,
);

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

// Print the page.
print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user);
print_grade_page_head(
$courseid,
'report',
'user',
user: $report->user,
taskindicator: $taskindicator,
);

if ($report->fill_table()) {
echo $report->print_table(true);
Expand Down
74 changes: 74 additions & 0 deletions lib/classes/output/task_indicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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/>.
namespace core\output;

use core\task\adhoc_task;
use core\task\stored_progress_task_trait;
use renderer_base;
use stdClass;
use core\task\task_base;
use core\stored_progress_bar;
use renderable;
use templatable;

/**
* Indicator for displaying status and progress of a background task
*
* @package core\output
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class task_indicator implements renderable, templatable {

protected ?stdClass $taskrecord;

protected ?stored_progress_bar $progressbar;

public function __construct(
protected adhoc_task $task,
protected string $message,
) {
if (!class_uses($task::class, stored_progress_task_trait::class)) {
throw new \coding_exception('task_indicator can only be used for tasks using stored_progress_task_trait.');
}
$this->taskrecord = \core\task\manager::get_queued_adhoc_task_record($this->task) ?: null;
if ($this->taskrecord) {
$this->task->set_id($this->taskrecord->id);
$idnumber = stored_progress_bar::convert_to_idnumber($this->task::class, $this->task->get_id());
$this->progressbar = stored_progress_bar::get_by_idnumber($idnumber);
}
}

/**
* Return true if there is an existing record for this task.
*
* @return bool
*/
public function has_task_record(): bool {
return !is_null($this->taskrecord);
}

public function export_for_template(renderer_base $output): array {
$export = [];
if ($this->taskrecord) {
$export['message'] = $this->message;
$export['progress'] = $this->progressbar->get_content();
}
return $export;
}

}
2 changes: 1 addition & 1 deletion lib/classes/task/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected static function task_is_scheduled($task) {
* @param adhoc_task $task
* @return \stdClass|false
*/
protected static function get_queued_adhoc_task_record($task) {
public static function get_queued_adhoc_task_record($task) {
global $DB;

$record = self::record_from_adhoc_task($task);
Expand Down
35 changes: 35 additions & 0 deletions lib/templates/task_indicator.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{!
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/>.
}}
{{!
@template core/task_indicator
Progress bar.
Example context (json):
{
"message": "message",
"progress": {
"id": "progressbar_test",
"idnumber": "progressbar_test",
"width": "500",
"value": "50"
}
}
}}
<div class="alert alert-info">
<p>{{message}}</p>
{{{progress}}}
</div>

0 comments on commit a00ccf2

Please sign in to comment.