MDL-83917 core_completion: Create function count_modules_completed
The function returns the number of modules completed by a user and executes a COUNT aggregate function to avoid running many queries to obtain this information, aiming to optimize performance. Co-authored-by: Carlos Castillo <[email protected]>
This commit is contained in:
co-authored by
Carlos Castillo
parent
ab5692acdf
commit
e1688e7094
@@ -1641,6 +1641,27 @@ class completion_info {
|
||||
|
||||
return (array)$data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of modules completed by a user in one specific course.
|
||||
*
|
||||
* @param int $userid The User ID.
|
||||
* @return int Total number of modules completed by a user
|
||||
*/
|
||||
public function count_modules_completed(int $userid): int {
|
||||
global $DB;
|
||||
|
||||
$sql = "SELECT COUNT(1)
|
||||
FROM {course_modules} cm
|
||||
JOIN {course_modules_completion} cmc ON cm.id = cmc.coursemoduleid
|
||||
WHERE cm.course = :courseid
|
||||
AND cmc.userid = :userid
|
||||
AND (cmc.completionstate = " . COMPLETION_COMPLETE . "
|
||||
OR cmc.completionstate = " . COMPLETION_COMPLETE_PASS . ")";
|
||||
$params = ['courseid' => $this->course_id, 'userid' => $userid];
|
||||
|
||||
return $DB->count_records_sql($sql, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user