Skip to content

Commit

Permalink
Merge branch 'MDL-71899-311-2' of git://github.com/HuongNV13/moodle i…
Browse files Browse the repository at this point in the history
…nto MOODLE_311_STABLE
  • Loading branch information
junpataleta authored and stronk7 committed Sep 9, 2021
2 parents b1216ee + 3a42d25 commit f88a73b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
4 changes: 3 additions & 1 deletion completion/classes/cm_completion_details.php
Expand Up @@ -65,7 +65,9 @@ class cm_completion_details {
*/
public function __construct(completion_info $completioninfo, cm_info $cminfo, int $userid, bool $returndetails = true) {
$this->completioninfo = $completioninfo;
$this->completiondata = $completioninfo->get_data($cminfo, false, $userid);
// We need to pass wholecourse = true here for better performance. All the course's completion data for the current
// logged-in user will get in a single query instead of multiple queries and loaded to cache.
$this->completiondata = $completioninfo->get_data($cminfo, true, $userid);
$this->cminfo = $cminfo;
$this->userid = $userid;
$this->returndetails = $returndetails;
Expand Down
3 changes: 2 additions & 1 deletion course/classes/cache/course_image.php
Expand Up @@ -56,7 +56,8 @@ public static function get_instance_for_cache(cache_definition $definition): cou
* @return string|bool Returns course image url as a string or false if the image is not exist
*/
public function load_for_cache($key) {
$course = get_fast_modinfo($key)->get_course();
// We should use get_course() instead of get_fast_modinfo() for better performance.
$course = get_course($key);
return $this->get_image_url_from_overview_files($course);
}

Expand Down
46 changes: 18 additions & 28 deletions lib/completionlib.php
Expand Up @@ -1055,39 +1055,29 @@ public function get_data($cm, $wholecourse = false, $userid = 0, $modinfo = null
// Fetch completion data for all of the activities in the course ONLY if we're caching the fetched completion data.
// If we're not caching the completion data, then just fetch the completion data for the user in this course module.
if ($usecache && $wholecourse) {
// Get whole course data for cache
$alldatabycmc = $DB->get_records_sql("
SELECT
cmc.*
FROM
{course_modules} cm
INNER JOIN {course_modules_completion} cmc ON cmc.coursemoduleid=cm.id
WHERE
cm.course=? AND cmc.userid=?", array($this->course->id, $userid));

// Reindex by cm id
$alldata = array();
// Get whole course data for cache.
$alldatabycmc = $DB->get_records_sql("SELECT cm.id AS cmid, cmc.*
FROM {course_modules} cm
LEFT JOIN {course_modules_completion} cmc ON cmc.coursemoduleid = cm.id
AND cmc.userid = ?
INNER JOIN {modules} m ON m.id = cm.module
WHERE m.visible = 1 AND cm.course = ?", [$userid, $this->course->id]);

// Reindex by course module id.
foreach ($alldatabycmc as $data) {
$alldata[$data->coursemoduleid] = (array)$data;
}

// Get the module info and build up condition info for each one
if (empty($modinfo)) {
$modinfo = get_fast_modinfo($this->course, $userid);
}
foreach ($modinfo->cms as $othercm) {
if (isset($alldata[$othercm->id])) {
$data = $alldata[$othercm->id];
if (empty($data->coursemoduleid)) {
$cacheddata[$data->cmid] = $defaultdata;
$cacheddata[$data->cmid]['coursemoduleid'] = $data->cmid;
} else {
// Row not present counts as 'not complete'.
$data = $defaultdata;
$data['coursemoduleid'] = $othercm->id;
$cacheddata[$data->cmid] = (array) $data;
}
// Make sure we're working on a cm_info object.
$othercminfo = cm_info::create($othercm, $userid);
$cmstd = new stdClass();
$cmstd->id = $data->cmid;
$cmstd->course = $this->course->id;
$othercminfo = cm_info::create($cmstd, $userid);
// Add the other completion data for this user in this module instance.
$data += $this->get_other_cm_completion_data($othercminfo, $userid);
$cacheddata[$othercminfo->id] = $data;
$cacheddata[$othercminfo->id] += $this->get_other_cm_completion_data($othercminfo, $userid);
}

if (!isset($cacheddata[$cminfo->id])) {
Expand Down

0 comments on commit f88a73b

Please sign in to comment.