$retakes = 0;
}
- $params = array ("lessonid" => $lesson->id, "userid" => $userid, "retry" => $retakes);
- if (!$seenbranches = $DB->get_records_select("lesson_branch", "lessonid = :lessonid AND userid = :userid AND retry = :retry", $params,
- "timeseen DESC")) {
+ if (!$seenbranches = $lesson->get_content_pages_viewed($retakes, $userid, 'timeseen DESC')) {
print_error('cannotfindrecords', 'lesson');
}
return $DB->get_records('lesson_attempts', $params, 'timeseen ASC');
}
+
+ /**
+ * Get a list of content pages (formerly known as branch tables) viewed in the lesson for the given user during an attempt.
+ *
+ * @param int $lessonattempt the lesson attempt number (also known as retries)
+ * @param int $userid the user id to retrieve the data from
+ * @param string $sort an order to sort the results in (a valid SQL ORDER BY parameter)
+ * @param string $fields a comma separated list of fields to return
+ * @return array of pages
+ * @since Moodle 3.3
+ */
+ public function get_content_pages_viewed($lessonattempt, $userid = null, $sort = '', $fields = '*') {
+ global $USER, $DB;
+
+ if ($userid === null) {
+ $userid = $USER->id;
+ }
+ $conditions = array("lessonid" => $this->properties->id, "userid" => $userid, "retry" => $lessonattempt);
+ return $DB->get_records('lesson_branch', $conditions, $sort, $fields);
+ }
+
/**
* Returns the first page for the lesson or false if there isn't one.
*
}
}
- if ($branchtables = $DB->get_records('lesson_branch', array("lessonid" => $this->properties->id, "userid" => $USER->id,
- "retry" => $retriescount), 'timeseen DESC')) {
+ if ($branchtables = $this->get_content_pages_viewed($retriescount, $USER->id, 'timeseen DESC')) {
// In here, user has viewed a branch table.
$lastbranchtable = current($branchtables);
if (count($allattempts) > 0) {
$viewedbranches = array();
// collect all of the branch tables viewed
- if ($branches = $DB->get_records("lesson_branch", array ("lessonid"=>$lesson->id, "userid"=>$USER->id, "retry"=>$ntries), 'timeseen ASC', 'id, pageid')) {
+ if ($branches = $lesson->get_content_pages_viewed($ntries, $USER->id, 'timeseen ASC', 'id, pageid')) {
foreach($branches as $branch) {
$viewedbranches[$branch->pageid] = $branch;
}