.
defined('MOODLE_INTERNAL') || die();
function game_bookquiz_continue( $id, $game, $attempt, $bookquiz, $chapterid, $context) {
if ($attempt != false and $bookquiz != false) {
return game_bookquiz_play( $id, $game, $attempt, $bookquiz, $chapterid, $context);
}
if ($attempt == false) {
$attempt = game_addattempt( $game);
}
$bookquiz = new stdClass();
$bookquiz->lastchapterid = 0;
$bookquiz->id = $attempt->id;
$bookquiz->bookid = $game->bookid;
if ( !game_insert_record( 'game_bookquiz', $bookquiz)) {
print_error( 'game_bookquiz_continue: error inserting in game_bookquiz');
}
return game_bookquiz_play( $id, $game, $attempt, $bookquiz, 0, $context);
}
function game_bookquiz_play( $id, $game, $attempt, $bookquiz, $chapterid, $context) {
global $DB, $OUTPUT, $cm;
if ($bookquiz->lastchapterid == 0) {
game_bookquiz_play_computelastchapter( $game, $bookquiz);
if ($bookquiz->lastchapterid == 0) {
print_error( get_string( 'bookquiz_empty', 'game'));
}
}
if ($chapterid == 0) {
$chapterid = $bookquiz->lastchapterid;
} else {
if (($DB->set_field( 'game_bookquiz', 'lastchapterid', $chapterid, array( 'id' => $bookquiz->id))) == false) {
print_error( "Can't update table game_bookquiz with lastchapterid to $chapterid");
}
}
$book = $DB->get_record( 'book', array('id' => $game->bookid));
if (!$chapter = $DB->get_record( 'book_chapters', array('id' => $chapterid))) {
print_error('Error reading book chapters.');
}
$select = "bookid = $game->bookid AND hidden = 0";
$chapters = $DB->get_records_select('book_chapters', $select, null, 'pagenum', 'id, pagenum, subchapter, title, hidden');
$okchapters = array();
if (($recs = $DB->get_records( 'game_bookquiz_chapters', array( 'attemptid' => $attempt->id))) != false) {
foreach ($recs as $rec) {
// The 1 means correct answer.
$okchapters[ $rec->chapterid] = 1;
}
}
// The 2 means current.
$showquestions = false;
$a = array( 'gameid' => $game->id, 'chapterid' => $chapterid);
if (($questions = $DB->get_records( 'game_bookquiz_questions', $a)) === false) {
if (!array_key_exists( $chapterid, $okchapters)) {
$okchapters[ $chapterid] = 1;
$newrec = new stdClass();
$newrec->attemptid = $attempt->id;
$newrec->chapterid = $chapterid;
if (!$DB->insert_record( 'game_bookquiz_chapters', $newrec)) {
print_error( "Can't insert to table game_bookquiz_chapters");
}
}
} else {
// Have to select random one question.
$questionid = game_bookquiz_selectrandomquestion( $questions);
if ($questionid != 0) {
$showquestions = true;
}
}
// Prepare chapter navigation icons.
$previd = null;
$nextid = null;
$found = 0;
$scoreattempt = 0;
$lastid = 0;
foreach ($chapters as $ch) {
$scoreattempt++;
if ($found) {
$nextid = $ch->id;
break;
}
if ($ch->id == $chapter->id) {
$found = 1;
}
if (!$found) {
$previd = $ch->id;
}
$lastid = $ch->id;
}
if ($ch == current($chapters)) {
$nextid = $ch->id;
}
if (count( $chapters)) {
$scoreattempt = ($scoreattempt - 1) / count( $chapters);
}
$chnavigation = '';
if ($previd) {
$chnavigation .= '';
} else {
$chnavigation .= '
';
}
$nextbutton = '';
if ($nextid) {
if (!$showquestions) {
$chnavigation .= '
';
$nextbutton = '
|
||
box_start('generalbox'); echo $toc; echo $OUTPUT->box_end(); ?> |
box_start('generalbox');
$content = '';
if (!$book->customtitles) {
if ($currsubtitle == ' ') {
$content .= ' '.$currtitle.' '; } else { $content .= ''.$currtitle.' ';
if ($nextbutton != '') {
echo $nextbutton;
}
echo format_text($content, FORMAT_HTML, $nocleanoption);
if ($nextbutton != '') {
echo $nextbutton;
}
echo ' ';
echo $OUTPUT->box_end();
// Lower navigation.
echo ''.$chnavigation.' '; ?> |