Beispiel #1
0
 public function test_quiz_number_of_pages() {
     $this->assertEquals(quiz_number_of_pages('0'), 1);
     $this->assertEquals(quiz_number_of_pages('0,0'), 2);
     $this->assertEquals(quiz_number_of_pages('0,0,0'), 3);
     $this->assertEquals(quiz_number_of_pages('1,0'), 1);
     $this->assertEquals(quiz_number_of_pages('1,2,0'), 1);
     $this->assertEquals(quiz_number_of_pages('1,0,2,0'), 2);
     $this->assertEquals(quiz_number_of_pages('1,2,3,0'), 1);
     $this->assertEquals(quiz_number_of_pages('1,2,3,0'), 1);
     $this->assertEquals(quiz_number_of_pages('0,1,0,0,2,0'), 4);
 }
Beispiel #2
0
$PAGE->requires->yui2_lib('container');
$PAGE->requires->yui2_lib('dragdrop');
$PAGE->requires->skip_link_to('questionbank', get_string('skipto', 'access', get_string('questionbank', 'question')));
$PAGE->requires->skip_link_to('quizcontentsblock', get_string('skipto', 'access', get_string('questionsinthisquiz', 'quiz')));
$PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
$PAGE->set_heading($course->fullname);
$node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
if ($node) {
    $node->make_active();
}
echo $OUTPUT->header();
// Initialise the JavaScript.
$quizeditconfig = new stdClass();
$quizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
$quizeditconfig->dialoglisteners = array();
$numberoflisteners = max(quiz_number_of_pages($quiz->questions), 1);
for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
    $quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
}
$PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
$PAGE->requires->js('/question/qengine.js');
$PAGE->requires->js('/mod/quiz/edit.js');
$PAGE->requires->js_init_call('quiz_edit_init');
// Print the tabs to switch mode.
if ($quiz_reordertool) {
    $currenttab = 'reorder';
} else {
    $currenttab = 'edit';
}
$tabs = array(array(new tabobject('edit', new moodle_url($thispageurl, array('reordertool' => 0)), get_string('editingquiz', 'quiz')), new tabobject('reorder', new moodle_url($thispageurl, array('reordertool' => 1)), get_string('orderingquiz', 'quiz'))));
print_tabs($tabs, $currenttab);
Beispiel #3
0
/**
 * Add a question to a quiz
 *
 * Adds a question to a quiz by updating $quiz as well as the
 * quiz and quiz_question_instances tables. It also adds a page break
 * if required.
 * @param int $id The id of the question to be added
 * @param object $quiz The extended quiz object as used by edit.php
 *      This is updated by this function
 * @param int $page Which page in quiz to add the question on. If 0 (default),
 *      add at the end
 * @return bool false if the question was already in the quiz
 */
function quiz_add_quiz_question($id, $quiz, $page = 0) {
    global $DB;
    $questions = explode(',', quiz_clean_layout($quiz->questions));
    if (in_array($id, $questions)) {
        return false;
    }

    // Remove ending page break if it is not needed.
    if ($breaks = array_keys($questions, 0)) {
        // Determine location of the last two page breaks.
        $end = end($breaks);
        $last = prev($breaks);
        $last = $last ? $last : -1;
        if (!$quiz->questionsperpage || (($end - $last - 1) < $quiz->questionsperpage)) {
            array_pop($questions);
        }
    }
    if (is_int($page) && $page >= 1) {
        $numofpages = quiz_number_of_pages($quiz->questions);
        if ($numofpages<$page) {
            // The page specified does not exist in quiz.
            $page = 0;
        } else {
            // Add ending page break - the following logic requires doing
            // this at this point.
            $questions[] = 0;
            $currentpage = 1;
            $addnow = false;
            foreach ($questions as $question) {
                if ($question == 0) {
                    $currentpage++;
                    // The current page is the one after the one we want to add on,
                    // so we add the question before adding the current page.
                    if ($currentpage == $page + 1) {
                        $questions_new[] = $id;
                    }
                }
                $questions_new[] = $question;
            }
            $questions = $questions_new;
        }
    }
    if ($page == 0) {
        // Add question.
        $questions[] = $id;
        // Add ending page break.
        $questions[] = 0;
    }

    // Save new questionslist in database.
    $quiz->questions = implode(',', $questions);
    $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));

    // Add the new question instance.
    $instance = new stdClass();
    $instance->quiz = $quiz->id;
    $instance->question = $id;
    $instance->grade = $DB->get_field('question', 'defaultmark', array('id' => $id));
    $DB->insert_record('quiz_question_instances', $instance);
}
    ?>
        <script type="text/javascript">
            // Do nothing, but you have to have a script tag before a noscript tag.
        </script>
        <noscript>
        <div>
        <?php 
    print_heading(get_string('noscript', 'quiz'));
    ?>
        </div>
        </noscript>
        <?php 
}
echo '<div>';
/// Print the navigation panel if required
$numpages = quiz_number_of_pages($attempt->layout);
if ($numpages > 1) {
    quiz_print_navigation_panel($page, $numpages);
}
/// Print all the questions
$number = quiz_first_questionnumber($attempt->layout, $pagelist);
foreach ($pagequestions as $i) {
    $options = quiz_get_renderoptions($quiz->review, $states[$i]);
    // Print the question
    print_question($questions[$i], $states[$i], $number, $quiz, $options);
    save_question_session($questions[$i], $states[$i]);
    $number += $questions[$i]->length;
}
/// Print the submit buttons
$strconfirmattempt = addslashes(get_string("confirmclose", "quiz"));
$onclick = "return confirm('{$strconfirmattempt}')";
Beispiel #5
0
/**
 * Add a question to a quiz
 *
 * Adds a question to a quiz by updating $quiz as well as the
 * quiz and quiz_question_instances tables. It also adds a page break
 * if required.
 * @return boolean         false if the question was already in the quiz
 * @param int $id          The id of the question to be added
 * @param object $quiz  The extended quiz object as used by edit.php
 *                         This is updated by this function
 * @param int $page  Which page in quiz to add the question on; if 0 (default), add at the end
 */
function quiz_add_quiz_question($id, &$quiz, $page = 0)
{
    global $DB;
    $questions = explode(',', $quiz->questions);
    if (in_array($id, $questions)) {
        return false;
    }
    // remove ending page break if it is not needed
    if ($breaks = array_keys($questions, 0)) {
        // determine location of the last two page breaks
        $end = end($breaks);
        $last = prev($breaks);
        $last = $last ? $last : -1;
        if (!$quiz->questionsperpage || $end - $last - 1 < $quiz->questionsperpage) {
            array_pop($questions);
        }
    }
    if (is_int($page) && $page >= 1) {
        $numofpages = quiz_number_of_pages($quiz->questions);
        if ($numofpages < $page) {
            //the page specified does not exist in quiz
            $page = 0;
        } else {
            // add ending page break - the following logic requires doing
            //this at this point
            $questions[] = 0;
            $currentpage = 1;
            $addnow = false;
            foreach ($questions as $question) {
                if ($question == 0) {
                    $currentpage++;
                    //The current page is the one after the one we want to add on,
                    //so we add the question before adding the current page.
                    if ($currentpage == $page + 1) {
                        $questions_new[] = $id;
                    }
                }
                $questions_new[] = $question;
            }
            $questions = $questions_new;
        }
    }
    if ($page == 0) {
        // add question
        $questions[] = $id;
        // add ending page break
        $questions[] = 0;
    }
    // Save new questionslist in database
    $quiz->questions = implode(',', $questions);
    if (!$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id))) {
        print_error('cannotsavequestion', 'quiz');
    }
    // update question grades
    $quiz->grades[$id] = $DB->get_field('question', 'defaultgrade', array('id' => $id));
    quiz_update_question_instance($quiz->grades[$id], $id, $quiz->instance);
    return true;
}
    $sloodle->response->quick_output(-701, 'QUIZ', 'Quiz not available.', FALSE);
    exit;
}
if ($finishattempt) {
    // redirect('review.php?attempt='.$attempt->id);
    //$sloodle->response->quick_output(-701, 'QUIZ', 'Got to finishattempt - but do not yet have Sloodle code to handle it.', FALSE);
    //exit();
}
/// Print the quiz page ////////////////////////////////////////////////////////
if (!$isnotify) {
    $pagequestions = explode(',', $pagelist);
    $lastquestion = count($pagequestions);
    // Only output quiz data if a question has not been requetsed
    if ($limittoquestion == 0) {
        $output[] = array('quiz', $quiz->attempts, $quiz->name, $quiz->timelimit, $quiz->id, $lastquestion);
        $output[] = array('quizpages', quiz_number_of_pages($attempt->layout), $page, $pagelist);
    }
    // We will keep track of question numbers local to this quiz
    $localqnum = 0;
    /// Print all the questions
    $number = quiz_first_questionnumber($attempt->layout, $pagelist);
    foreach ($pagequestions as $i) {
        $options = quiz_get_renderoptions($quiz->review, $states[$i]);
        // Print the question
        // var_dump($questions[$i]);
        $q = $questions[$i];
        $localqnum++;
        if ($limittoquestion == $q->id) {
            //echo "<hr>"; print_r($q); echo "<hr>";
            // Make sure the variables exist (avoids warnings!)
            if (!isset($q->single)) {