MDL-58266 core_completion: Add new view table.

This commit is contained in:
hieuvu
2022-10-14 00:30:28 +07:00
committed by Thong Bui
parent e4c5a12a1c
commit c6e018e04e
15 changed files with 412 additions and 30 deletions
+67 -17
View File
@@ -1091,13 +1091,16 @@ class completion_info {
// 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 cm.id AS cmid, cmc.*
$alldatabycmc = $DB->get_records_sql("SELECT cm.id AS cmid, cmc.*,
CASE WHEN cmv.id IS NULL THEN 0 ELSE 1 END AS viewed
FROM {course_modules} cm
LEFT JOIN {course_modules_completion} cmc ON cmc.coursemoduleid = cm.id
AND cmc.userid = ?
LEFT JOIN {course_modules_completion} cmc
ON cmc.coursemoduleid = cm.id AND cmc.userid = ?
LEFT JOIN {course_modules_viewed} cmv
ON cmv.coursemoduleid = cm.id AND cmv.userid = ?
INNER JOIN {modules} m ON m.id = cm.module
WHERE m.visible = 1 AND cm.course = ?", [$userid, $this->course->id]);
WHERE m.visible = 1 AND cm.course = ?",
[$userid, $userid, $this->course->id]);
$cminfos = get_fast_modinfo($cm->course, $userid)->get_cms();
// Reindex by course module id.
@@ -1125,14 +1128,7 @@ class completion_info {
$data = $cacheddata[$cminfo->id];
} else {
// Get single record
$data = $DB->get_record('course_modules_completion', array('coursemoduleid' => $cminfo->id, 'userid' => $userid));
if ($data) {
$data = (array)$data;
} else {
// Row not present counts as 'not complete'.
$data = $defaultdata;
}
$data = $this->get_completion_data($cminfo->id, $userid, $defaultdata);
// Put in cache.
$cacheddata[$cminfo->id] = $data;
}
@@ -1188,10 +1184,8 @@ class completion_info {
// If view is required, try and fetch from the db. In some cases, cache can be invalid.
if ($cm->completionview == COMPLETION_VIEW_REQUIRED) {
$data['viewed'] = COMPLETION_INCOMPLETE;
$record = $DB->get_record('course_modules_completion', array('coursemoduleid' => $cm->id, 'userid' => $userid));
if ($record) {
$data['viewed'] = ($record->viewed == COMPLETION_VIEWED ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE);
}
$record = $DB->record_exists('course_modules_viewed', ['coursemoduleid' => $cm->id, 'userid' => $userid]);
$data['viewed'] = $record ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
}
return $data;
@@ -1265,6 +1259,19 @@ class completion_info {
// Has real (nonzero) id meaning that a database row exists, update
$DB->update_record('course_modules_completion', $data);
}
$dataview = new stdClass();
$dataview->coursemoduleid = $data->coursemoduleid;
$dataview->userid = $data->userid;
$dataview->id = $DB->get_field('course_modules_viewed', 'id',
['coursemoduleid' => $dataview->coursemoduleid, 'userid' => $dataview->userid]);
if (!$data->viewed && $dataview->id) {
$DB->delete_records('course_modules_viewed', ['id' => $dataview->id]);
}
if (!$dataview->id && $data->viewed) {
$dataview->timecreated = time();
$dataview->id = $DB->insert_record('course_modules_viewed', $dataview);
}
$transaction->allow_commit();
$cmcontext = context_module::instance($data->coursemoduleid);
@@ -1602,6 +1609,49 @@ class completion_info {
throw new moodle_exception('err_system','completion',
$CFG->wwwroot.'/course/view.php?id='.$this->course->id,null,$error);
}
/**
* Get completion data include viewed field.
*
* @param int $coursemoduleid The course module id.
* @param int $userid The User ID.
* @param array $defaultdata Default data completion.
* @return array Data completion retrieved.
*/
public function get_completion_data(int $coursemoduleid, int $userid, array $defaultdata): array {
global $DB;
// MySQL doesn't support FULL JOIN syntax, so we use UNION in the below SQL to help MySQL.
$sql = "SELECT cmc.*, cmv.coursemoduleid as cmvcoursemoduleid, cmv.userid as cmvuserid
FROM {course_modules_completion} cmc
LEFT JOIN {course_modules_viewed} cmv ON cmc.coursemoduleid = cmv.coursemoduleid AND cmc.userid = cmv.userid
WHERE cmc.coursemoduleid = :cmccoursemoduleid AND cmc.userid = :cmcuserid
UNION
SELECT cmc2.*, cmv2.coursemoduleid as cmvcoursemoduleid, cmv2.userid as cmvuserid
FROM {course_modules_completion} cmc2
RIGHT JOIN {course_modules_viewed} cmv2
ON cmc2.coursemoduleid = cmv2.coursemoduleid AND cmc2.userid = cmv2.userid
WHERE cmv2.coursemoduleid = :cmvcoursemoduleid AND cmv2.userid = :cmvuserid";
$data = $DB->get_record_sql($sql, ['cmccoursemoduleid' => $coursemoduleid, 'cmcuserid' => $userid,
'cmvcoursemoduleid' => $coursemoduleid, 'cmvuserid' => $userid]);
if (!$data) {
$data = $defaultdata;
} else {
if (empty($data->coursemoduleid) && empty($data->userid)) {
$data->coursemoduleid = $data->cmvcoursemoduleid;
$data->userid = $data->cmvuserid;
}
unset($data->cmvcoursemoduleid);
unset($data->cmvuserid);
// When reseting all state in the completion, we need to keep current view state.
$data->viewed = 1;
}
return (array)$data;
}
}
/**