MDL-75955 gradereport_summary: ensure page context is always set.

Remove global course object, and pass via entity constructor.
This commit is contained in:
Paul Holden 2022-10-11 21:27:02 +01:00
parent e4c5a12a1c
commit ffea18688c
3 changed files with 28 additions and 34 deletions

View File

@ -21,10 +21,10 @@ use grade_item;
use grade_plugin_return;
use grade_report_summary;
use lang_string;
use stdClass;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\report\column;
use core_reportbuilder\local\report\filter;
use core_grades\local\helpers\helpers;
defined('MOODLE_INTERNAL') || die;
@ -40,12 +40,24 @@ require_once($CFG->dirroot . '/grade/lib.php');
*/
class grade_items extends base {
/** @var array Grade report. */
/** @var stdClass Course */
public $course;
/** @var grade_report_summary Grade report. */
public $report;
/** @var array Ungraded grade items counts with sql info. */
public $ungradedcounts;
/**
* Entity constructor
*
* @param stdClass $course
*/
public function __construct(stdClass $course) {
$this->course = $course;
}
/**
* Database tables that this entity uses and their default aliases
*
@ -64,34 +76,23 @@ class grade_items extends base {
return new lang_string('gradeitem', 'grades');
}
/**
* The default machine-readable name for this entity that will be used in the internal names of the columns/filters
*
* @return string
*/
protected function get_default_entity_name(): string {
return 'grade_items';
}
/**
* Initialise the entity
*
* @return base
*/
public function initialise(): base {
global $COURSE;
$context = \context_course::instance($COURSE->id);
$context = \context_course::instance($this->course->id);
$gpr = new grade_plugin_return(
[
'type' => 'report',
'plugin' => 'summary',
'course' => $COURSE,
'course' => $this->course,
]
);
$this->report = new grade_report_summary($COURSE->id, $gpr, $context);
$this->report = new grade_report_summary($this->course->id, $gpr, $context);
$this->ungradedcounts = $this->report->ungraded_counts();
$columns = $this->get_all_columns();
@ -252,12 +253,9 @@ class grade_items extends base {
* @return filter[]
*/
protected function get_all_filters(): array {
$filters = [];
$itemtypes = $this->report->item_types();
$tablealias = $this->get_table_alias('grade_items');
// Activity type filter.
// Activity type filter (for performance only load options on demand).
$filters[] = (new filter(
select::class,
'name',
@ -266,7 +264,7 @@ class grade_items extends base {
"coalesce({$tablealias}.itemmodule,{$tablealias}.itemtype)"
))
->add_joins($this->get_joins())
->set_options($itemtypes);
->set_options_callback([$this->report, 'item_types']);
return $filters;
}

View File

@ -33,23 +33,19 @@ class summary extends system_report {
* Initialise report, we need to set the main table, load our entities and set columns/filters
*/
protected function initialise(): void {
global $COURSE;
global $PAGE;
// We need to ensure page context is always set, as required by output and string formatting.
$course = get_course($this->get_context()->instanceid);
$PAGE->set_context($this->get_context());
// Our main entity, it contains all of the column definitions that we need.
$entitymain = new grade_items();
$context = $this->get_context();
$COURSE = get_course($context->instanceid);
$entitymain = new grade_items($course);
$entitymainalias = $entitymain->get_table_alias('grade_items');
$this->set_main_table('grade_items', $entitymainalias);
$this->add_entity($entitymain);
// Any columns required by actions should be defined here to ensure they're always available.
$this->add_base_fields("{$entitymainalias}.id");
$courseid = $this->get_context()->instanceid;
$param1 = database::generate_param_name();
$param2 = database::generate_param_name();
$param3 = database::generate_param_name();
@ -66,7 +62,7 @@ class summary extends system_report {
$wheresql .= " AND ($entitymainalias.gradetype = :$param2 OR $entitymainalias.gradetype = :$param3)";
$this->add_base_condition_sql($wheresql,
[$param1 => $courseid, $param2 => GRADE_TYPE_VALUE, $param3 => GRADE_TYPE_SCALE]);
[$param1 => $course->id, $param2 => GRADE_TYPE_VALUE, $param3 => GRADE_TYPE_SCALE]);
// Now we can call our helper methods to add the content we want to include in the report.
$this->add_columns();

View File

@ -48,12 +48,12 @@ class grade_report_summary extends grade_report {
*
* @param int $courseid
* @param object $gpr grade plugin return tracking object
* @param string $context
* @param context_course $context
*/
public function __construct($courseid, $gpr, $context) {
parent::__construct($courseid, $gpr, $context);
$this->canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($this->course->id));
$this->canviewhidden = has_capability('moodle/grade:viewhidden', $context);
$this->setup_groups();
}