public function get_last_page_seen($retriescount) {
        global $DB, $USER;

        $lastpageseen = false;
        $allattempts = $this->get_attempts($retriescount);
        if (!empty($allattempts)) {
            $attempt = end($allattempts);
            $attemptpage = $this->load_page($attempt->pageid);
            $jumpto = $DB->get_field('lesson_answers', 'jumpto', array('id' => $attempt->answerid));
            // Convert the jumpto to a proper page id.
            if ($jumpto == 0) {
                // Check if a question has been incorrectly answered AND no more attempts at it are left.
                $nattempts = $this->get_attempts($attempt->retry, false, $attempt->pageid, $USER->id);
                if (count($nattempts) >= $this->properties->maxattempts) {
                    $lastpageseen = $this->get_next_page($attemptpage->nextpageid);
                } else {
                    $lastpageseen = $attempt->pageid;
                }
            } else if ($jumpto == LESSON_NEXTPAGE) {
                $lastpageseen = $this->get_next_page($attemptpage->nextpageid);
            } else if ($jumpto == LESSON_CLUSTERJUMP) {
                $lastpageseen = $this->cluster_jump($attempt->pageid);
            } else {
                $lastpageseen = $jumpto;
            }
        }

        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) {
                if ($lastbranchtable->timeseen > $attempt->timeseen) {
                    // This branch table was viewed more recently than the question page.
                    if (!empty($lastbranchtable->nextpageid)) {
                        $lastpageseen = $lastbranchtable->nextpageid;
                    } else {
                        // Next page ID did not exist prior to MDL-34006.
                        $lastpageseen = $lastbranchtable->pageid;
                    }
                }
            } else {
                // Has not answered any questions but has viewed a branch table.
                if (!empty($lastbranchtable->nextpageid)) {
                    $lastpageseen = $lastbranchtable->nextpageid;
                } else {
                    // Next page ID did not exist prior to MDL-34006.
                    $lastpageseen = $lastbranchtable->pageid;
                }
            }
        }
        return $lastpageseen;
    }