1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
<?php // $Id: format.php,v 1.6 2006/04/10 22:10:33 gustav_delius Exp $
////////////////////////////////////////////////////////////////////////////
/// Academy of Nursing format
///
/// Based on missingword.php
///
/// This Moodle class provides all functions necessary to import and export
/// one-correct-answer multiple choice questions in this format:
///
/// As soon as we begin to explore our body parts as infants
/// we become students of {=anatomy and physiology ~reflexology
/// ~science ~experiment}, and in a sense we remain students for life.
///
/// Each answer is separated with a tilde ~, and the correct answer is
/// prefixed with an equals sign =
///
/// Afterwards, all short-answer questions are randomly packed into
/// 4-answer matching questions.
///
////////////////////////////////////////////////////////////////////////////
// Based on format.php, included by ../../import.php
class qformat_aon extends qformat_default {
function provide_import() {
return true;
}
function readquestion($lines) {
/// Given an array of lines known to define a question in
/// this format, this function converts it into a question
/// object suitable for processing and insertion into Moodle.
$question = $this->defaultquestion();
$text = implode($lines, " ");
/// Find answer section
$answerstart = strpos($text, "{");
if ($answerstart === false) {
if ($this->displayerrors) {
echo "<p>$text<p>Could not find a {";
}
return false;
}
$answerfinish = strpos($text, "}");
if ($answerfinish === false) {
if ($this->displayerrors) {
echo "<p>$text<p>Could not find a }";
}
return false;
}
$answerlength = $answerfinish - $answerstart;
$answertext = substr($text, $answerstart + 1, $answerlength - 1);
/// Save the new question text
$question->questiontext = addslashes(substr_replace($text, "_____", $answerstart, $answerlength+1));
$question->name = substr($question->questiontext, 0, 60)." ...";
/// Parse the answers
$answers = explode("~", $answertext);
$countanswers = count($answers);
switch ($countanswers) {
case 0: // invalid question
if ($this->displayerrors) {
echo "<p>No answers found in $answertext";
}
return false;
case 1:
$question->qtype = SHORTANSWER;
$answer = trim($answers[0]);
if ($answer[0] == "=") {
$answer = substr($answer, 1);
}
$question->answer[] = addslashes($answer);
$question->fraction[] = 1;
$question->feedback[] = "";
return $question;
default:
$question->qtype = MULTICHOICE;
$answers = swapshuffle($answers);
foreach ($answers as $key => $answer) {
$answer = trim($answer);
if ($answer[0] == "=") {
$question->fraction[$key] = 1;
$answer = substr($answer, 1);
} else {
$question->fraction[$key] = 0;
}
$question->answer[$key] = addslashes($answer);
$question->feedback[$key] = "";
}
$question->single = 1; // Only one answer is allowed
return $question;
}
}
function importpostprocess() {
/// Goes through the questionids, looking for shortanswer questions
/// and converting random groups of 4 into matching questions.
/// Doesn't handle shortanswer questions with more than one answer
global $CFG;
print_heading(count($this->questionids)." ".get_string("questions", "quiz"));
$questionids = implode(',', $this->questionids);
if (!$shortanswers = get_records_select("question",
"id IN ($questionids) AND qtype = ".SHORTANSWER,
"", "id,qtype")) {
return true;
}
$shortanswerids = array();
foreach ($shortanswers as $key => $shortanswer) {
$shortanswerids[] = $key;
}
$strmatch = get_string("match", "quiz")." (".$this->category->name.")";
$shortanswerids = swapshuffle($shortanswerids);
$count = $shortanswercount = count($shortanswerids);
$i = 1;
$matchcount = 0;
$question->category = $this->category->id;
$question->qtype = MATCH;
$question->questiontext = get_string("randomsamatchintro", "quiz");
$question->image = "";
while ($count > 4) {
$matchcount++;
$question->name = "$strmatch $i";
$question->subquestions = array();
$question->subanswers = array();
$extractids = implode(',', array_splice($shortanswerids, -4));
$count = count($shortanswerids);
$extracts = get_records_sql("SELECT q.questiontext, a.answer
FROM {$CFG->prefix}question q,
{$CFG->prefix}question_shortanswer sa,
{$CFG->prefix}question_answers a
WHERE q.id in ($extractids)
AND sa.question = q.id
AND a.id = sa.answers");
if (count($extracts) != 4) {
print_object($extracts);
notify("Could not find exactly four shortanswer questions with ids: $extractids");
continue;
}
$question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question->version = 1; // Original version of this question
$question->parent = 0;
if (!$question->id = insert_record("question", $question)) {
error("Could not insert new question!");
}
foreach ($extracts as $shortanswer) {
$question->subquestions[] = addslashes($shortanswer->questiontext);
$question->subanswers[] = addslashes($shortanswer->answer);
}
$result = save_question_options($question);
if (!empty($result->error)) {
notify("Error: $result->error");
}
if (!empty($result->notice)) {
notify($result->notice);
}
// Give the question a unique version stamp determined by question_hash()
set_field('question', 'version', question_hash($question), 'id', $question->id);
/// Delete the old short-answer questions
execute_sql("DELETE FROM {$CFG->prefix}question WHERE id IN ($extractids)", false);
execute_sql("DELETE FROM {$CFG->prefix}question_shortanswer WHERE question IN ($extractids)", false);
execute_sql("DELETE FROM {$CFG->prefix}question_answers WHERE question IN ($extractids)", false);
}
if ($count) { /// Delete the remaining ones
foreach ($shortanswerids as $shortanswerid) {
delete_records("question", "id", $shortanswerid);
delete_records("question_shortanswer", "question", $shortanswerid);
delete_records("question_answers", "question", $shortanswerid);
}
}
$info = "$shortanswercount ".get_string("shortanswer", "quiz").
" => $matchcount ".get_string("match", "quiz");
print_heading($info);
$options['category'] = $this->category->id;
echo "<center>";
print_single_button("multiple.php", $options, get_string("randomcreate", "quiz"));
echo "</center>";
return true;
}
}
?>
|