Beispiel #1
0
/**
 * Handles the random jump between a branch table and end of branch or end of lesson (LESSON_RANDOMPAGE).
 *
 * @param lesson $lesson
 * @param int $pageid The id of the page that we are jumping from (?)
 * @return int The pageid of a random page that is within a branch table
 **/
function lesson_random_question_jump($lesson, $pageid)
{
    global $DB;
    // get the lesson pages
    $params = array("lessonid" => $lesson->id);
    if (!($lessonpages = $DB->get_records_select("lesson_pages", "lessonid = :lessonid", $params))) {
        print_error('cannotfindpages', 'lesson');
    }
    // go up the pages till branch table
    while ($pageid != 0) {
        // this condition should never be satisfied... only happens if there are no branch tables above this page
        if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
            break;
        }
        $pageid = $lessonpages[$pageid]->prevpageid;
    }
    // get the pages within the branch
    $pagesinbranch = $lesson->get_sub_pages_of($pageid, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
    if (count($pagesinbranch) == 0) {
        // there are no pages inside the branch, so return the next page
        return $lessonpages[$pageid]->nextpageid;
    } else {
        return $pagesinbranch[rand(0, count($pagesinbranch) - 1)]->id;
        // returns a random page id for the next page
    }
}