function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) { global $QTYPES, $DB; // Choose a random question from the category: // We need to make sure that no question is used more than once in the // quiz. Therfore the following need to be excluded: // 1. All questions that are explicitly assigned to the quiz // 2. All random questions // 3. All questions that are already chosen by an other random question // 4. Deleted questions if (!isset($cmoptions->questionsinuse)) { $cmoptions->questionsinuse = $attempt->layout; } if (!isset($this->catrandoms[$question->category][$question->questiontext])) { $catrandoms = $this->get_usable_questions_from_category($question->category, $question->questiontext == "1", $cmoptions->questionsinuse); $this->catrandoms[$question->category][$question->questiontext] = swapshuffle_assoc($catrandoms); } while ($wrappedquestion = array_pop( $this->catrandoms[$question->category][$question->questiontext])) { if (!preg_match("~(^|,)$wrappedquestion->id(,|$)~", $cmoptions->questionsinuse)) { /// $randomquestion is not in use and will therefore be used /// as the randomquestion here... $wrappedquestion = $DB->get_record('question', array('id' => $wrappedquestion->id)); global $QTYPES; $QTYPES[$wrappedquestion->qtype] ->get_question_options($wrappedquestion); $QTYPES[$wrappedquestion->qtype] ->create_session_and_responses($wrappedquestion, $state, $cmoptions, $attempt); $wrappedquestion->name_prefix = $question->name_prefix; $wrappedquestion->maxgrade = $question->maxgrade; $cmoptions->questionsinuse .= ",$wrappedquestion->id"; $state->options->question = &$wrappedquestion; return true; } } $question->questiontext = '<span class="notifyproblem">'. get_string('toomanyrandom', 'quiz'). '</span>'; $question->qtype = 'description'; $state->responses = array('' => ''); return true; }
/** * returns the given array, shuffled * * * @param array $things * @return array */ function shuffle_things($things) { $things = swapshuffle_assoc($things); $oldthings = $things; $things = array(); foreach ($oldthings as $key => $value) { $things[] = $value; // This loses the index key, but doesn't matter } return $things; }
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) { global $DB, $OUTPUT; if (!($state->options->subquestions = $DB->get_records('question_match_sub', array('question' => $question->id), 'id ASC'))) { echo $OUTPUT->notification('Error: Missing subquestions!'); return false; } foreach ($state->options->subquestions as $key => $subquestion) { // This seems rather over complicated, but it is useful for the // randomsamatch questiontype, which can then inherit the print // and grading functions. This way it is possible to define multiple // answers per question, each with different marks and feedback. $answer = new stdClass(); $answer->id = $subquestion->code; $answer->answer = $subquestion->answertext; $answer->fraction = 1.0; $state->options->subquestions[$key]->options->answers[$subquestion->code] = clone $answer; $state->responses[$key] = ''; } // Shuffle the answers if required if ($cmoptions->shuffleanswers and $question->options->shuffleanswers) { $state->options->subquestions = swapshuffle_assoc($state->options->subquestions); } return true; }
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) { if (!($subquestions = get_records('question_order_sub', 'question', $question->id, 'id ASC'))) { notify('Error: Missing subquestions!'); return false; } // Place the questions into the state options keyed by code foreach ($subquestions as $subquestion) { $state->options->subquestions[$subquestion->code] = $subquestion; } foreach ($state->options->subquestions as $key => $subquestion) { // This seems rather over complicated, but it is useful for the // randomsamatch questiontype, which can then inherit the print // and grading functions. This way it is possible to define multiple // answers per question, each with different marks and feedback. $answer = new stdClass(); $answer->id = $key; $answer->answer = $subquestion->answertext; $answer->fraction = 1.0; $state->options->subquestions[$key]->options->answers[$key] = clone $answer; $state->responses[$key] = ''; } // Add default defaultresponse value $state->options->defaultresponse = "no"; // Shuffle until the questions are not initially in a correct order // In a case where all items are identical, this will loop, so limit it to 10 tries. $correctorder = 1; $reshufflecount = 0; while ($correctorder && $reshufflecount < 10) { // Shuffle the questions $state->options->subquestions = swapshuffle_assoc($state->options->subquestions); // Assign default responses in order to check that the items are not already // in a correct order $state->responses = array(); $i = 1; foreach ($state->options->subquestions as $subquestion) { $state->responses[$subquestion->code] = $i; $i += 1; } // If all items are not in the correct order, accept the current shuffle // and continue $evaluatedresponses = $this->get_evaluated_responses($question, $state); $correctresponses = array(); foreach ($state->options->subquestions as $key => $subquestion) { if ($subquestion->questiontext) { // If some item is incorrect if (!$evaluatedresponses[$key]) { $correctorder = 0; } } } $reshufflecount += 1; } // Unset responses array $state->responses = array(); return true; }