MDL-81610 course: Optimise core_courseformat\external\get_state
core_courseformat\external\get_state calls export_for_tempalte for each course module, which in turn calls completion_info::is_tracked_user() for the current user on the current course. The result of this call will be the same for every course module, so does not need to be called what may be several hundred times. This change allows the result of is_tracked_user to be passed to the cm state object, then calls it once and passes it to each cm's object. If it is not passed in, it will be computed on demand as before.
This commit is contained in:
+4
-2
@@ -60,7 +60,7 @@ class get_state extends external_api {
|
||||
* @return string Course state in JSON
|
||||
*/
|
||||
public static function execute(int $courseid): string {
|
||||
global $PAGE, $CFG;
|
||||
global $PAGE, $CFG, $USER;
|
||||
|
||||
require_once($CFG->dirroot.'/course/lib.php');
|
||||
|
||||
@@ -73,6 +73,8 @@ class get_state extends external_api {
|
||||
|
||||
$courseformat = course_get_format($courseid);
|
||||
$modinfo = $courseformat->get_modinfo();
|
||||
$completioninfo = new \completion_info(get_course($courseid));
|
||||
$istrackeduser = $completioninfo->is_tracked_user($USER->id);
|
||||
|
||||
// Get the proper renderer.
|
||||
$renderer = $courseformat->get_renderer($PAGE);
|
||||
@@ -106,7 +108,7 @@ class get_state extends external_api {
|
||||
if ($cm->is_visible_on_course_page()) {
|
||||
// Only return this course module data if it's visible by current user on the course page.
|
||||
$section = $sections[$cm->sectionnum];
|
||||
$cmstate = new $cmclass($courseformat, $section, $cm);
|
||||
$cmstate = new $cmclass($courseformat, $section, $cm, istrackeduser: $istrackeduser);
|
||||
$result->cm[] = $cmstate->export_for_template($renderer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,37 +33,26 @@ require_once($CFG->libdir . '/completionlib.php');
|
||||
/**
|
||||
* Contains the ajax update course module structure.
|
||||
*
|
||||
* @package core_course
|
||||
* @package core_courseformat
|
||||
* @copyright 2021 Ferran Recio <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cm implements renderable {
|
||||
|
||||
/** @var course_format the course format class */
|
||||
protected $format;
|
||||
|
||||
/** @var section_info the course section class */
|
||||
protected $section;
|
||||
|
||||
/** @var bool if cmitem HTML content must be exported as well */
|
||||
protected $exportcontent;
|
||||
|
||||
/** @var cm_info the course module to display */
|
||||
protected $cm;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param course_format $format the course format
|
||||
* @param section_info $section the section data
|
||||
* @param cm_info $cm the course module data
|
||||
* @param bool $exportcontent = false if pre-rendered cmitem must be exported.
|
||||
*/
|
||||
public function __construct(course_format $format, section_info $section, cm_info $cm, bool $exportcontent = false) {
|
||||
$this->format = $format;
|
||||
$this->section = $section;
|
||||
$this->cm = $cm;
|
||||
$this->exportcontent = $exportcontent;
|
||||
public function __construct(
|
||||
/** @var course_format $format The course format. */
|
||||
protected course_format $format,
|
||||
/** @var section_info $section The section data. */
|
||||
protected section_info $section,
|
||||
/** @var cm_info $cm The course module data. */
|
||||
protected cm_info $cm,
|
||||
/** @var bool $exportcontent False if pre-rendered cmitem HTML content must be exported. */
|
||||
protected bool $exportcontent = false,
|
||||
/** @var ?bool $istrackeduser If is_tracked_user is pre-computed for this CM's course, it can be provided here. */
|
||||
protected ?bool $istrackeduser = null,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,7 +104,7 @@ class cm implements renderable {
|
||||
|
||||
// Completion status.
|
||||
$completioninfo = new completion_info($course);
|
||||
$data->istrackeduser = $completioninfo->is_tracked_user($USER->id);
|
||||
$data->istrackeduser = $this->istrackeduser ?? $completioninfo->is_tracked_user($USER->id);
|
||||
if ($data->istrackeduser && $completioninfo->is_enabled($cm)) {
|
||||
$completiondata = $completioninfo->get_data($cm);
|
||||
$data->completionstate = $completiondata->completionstate;
|
||||
|
||||
Reference in New Issue
Block a user