diff --git a/course/format/classes/external/get_state.php b/course/format/classes/external/get_state.php index 76970e0777f..7e6a2d512e8 100644 --- a/course/format/classes/external/get_state.php +++ b/course/format/classes/external/get_state.php @@ -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); } } diff --git a/course/format/classes/output/local/state/cm.php b/course/format/classes/output/local/state/cm.php index 2fef3e68709..84853d4f2ad 100644 --- a/course/format/classes/output/local/state/cm.php +++ b/course/format/classes/output/local/state/cm.php @@ -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 * @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;