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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
<?php // $Id: questiontype.php,v 1.3.2.2 2006/06/12 11:03:11 tjhunt Exp $
///////////////////
/// MULTICHOICE ///
///////////////////
/// QUESTION TYPE CLASS //////////////////
///
/// This class contains some special features in order to make the
/// question type embeddable within a multianswer (cloze) question
///
class question_multichoice_qtype extends default_questiontype {
function name() {
return 'multichoice';
}
function get_question_options(&$question) {
// Get additional information from database
// and attach it to the question object
if (!$question->options = get_record('question_multichoice', 'question',
$question->id)) {
notify('Error: Missing question options for multichoice question'.$question->id.'!');
return false;
}
if (!$question->options->answers = get_records_select('question_answers', 'id IN ('.$question->options->answers.')', 'id')) {
notify('Error: Missing question answers for multichoice question'.$question->id.'!');
return false;
}
return true;
}
function save_question_options($question) {
if (!$oldanswers = get_records("question_answers", "question",
$question->id, "id ASC")) {
$oldanswers = array();
}
// following hack to check at least two answers exist
$answercount = 0;
foreach ($question->answer as $key=>$dataanswer) {
if ($dataanswer != "") {
$answercount++;
}
}
$answercount += count($oldanswers);
if ($answercount < 2) { // check there are at lest 2 answers for multiple choice
$result->notice = get_string("notenoughanswers", "quiz", "2");
return $result;
}
// Insert all the new answers
$totalfraction = 0;
$maxfraction = -1;
$answers = array();
foreach ($question->answer as $key => $dataanswer) {
if ($dataanswer != "") {
if ($answer = array_shift($oldanswers)) { // Existing answer, so reuse it
$answer->answer = $dataanswer;
$answer->fraction = $question->fraction[$key];
$answer->feedback = $question->feedback[$key];
if (!update_record("question_answers", $answer)) {
$result->error = "Could not update quiz answer! (id=$answer->id)";
return $result;
}
} else {
unset($answer);
$answer->answer = $dataanswer;
$answer->question = $question->id;
$answer->fraction = $question->fraction[$key];
$answer->feedback = $question->feedback[$key];
if (!$answer->id = insert_record("question_answers", $answer)) {
$result->error = "Could not insert quiz answer! ";
return $result;
}
}
$answers[] = $answer->id;
if ($question->fraction[$key] > 0) { // Sanity checks
$totalfraction += $question->fraction[$key];
}
if ($question->fraction[$key] > $maxfraction) {
$maxfraction = $question->fraction[$key];
}
}
}
if ($options = get_record("question_multichoice", "question", $question->id)) {
$options->answers = implode(",",$answers);
$options->single = $question->single;
$options->shuffleanswers = $question->shuffleanswers;
if (!update_record("question_multichoice", $options)) {
$result->error = "Could not update quiz multichoice options! (id=$options->id)";
return $result;
}
} else {
unset($options);
$options->question = $question->id;
$options->answers = implode(",",$answers);
$options->single = $question->single;
$options->shuffleanswers = $question->shuffleanswers;
if (!insert_record("question_multichoice", $options)) {
$result->error = "Could not insert quiz multichoice options!";
return $result;
}
}
// delete old answer records
if (!empty($oldanswers)) {
foreach($oldanswers as $oa) {
delete_records('question_answers', 'id', $oa->id);
}
}
/// Perform sanity checks on fractional grades
if ($options->single) {
if ($maxfraction != 1) {
$maxfraction = $maxfraction * 100;
$result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);
return $result;
}
} else {
$totalfraction = round($totalfraction,2);
if ($totalfraction != 1) {
$totalfraction = $totalfraction * 100;
$result->noticeyesno = get_string("fractionsaddwrong", "quiz", $totalfraction);
return $result;
}
}
return true;
}
/**
* Deletes question from the question-type specific tables
*
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
function delete_question($questionid) {
delete_records("question_multichoice", "question", $questionid);
return true;
}
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
// create an array of answerids ??? why so complicated ???
$answerids = array_values(array_map(create_function('$val',
'return $val->id;'), $question->options->answers));
// Shuffle the answers if required
if ($cmoptions->shuffleanswers and $question->options->shuffleanswers) {
$answerids = swapshuffle($answerids);
}
$state->options->order = $answerids;
// Create empty responses
if ($question->options->single) {
$state->responses = array('' => '');
} else {
$state->responses = array();
}
return true;
}
function restore_session_and_responses(&$question, &$state) {
// The serialized format for multiple choice quetsions
// is an optional comma separated list of answer ids (the order of the
// answers) followed by a colon, followed by another comma separated
// list of answer ids, which are the radio/checkboxes that were
// ticked.
// E.g. 1,3,2,4:2,4 means that the answers were shown in the order
// 1, 3, 2 and then 4 and the answers 2 and 4 were checked.
$pos = strpos($state->responses[''], ':');
if (false === $pos) { // No order of answers is given, so use the default
$state->options->order = array_keys($question->options->answers);
} else { // Restore the order of the answers
$state->options->order = explode(',', substr($state->responses[''], 0,
$pos));
$state->responses[''] = substr($state->responses[''], $pos + 1);
}
// Restore the responses
// This is done in different ways if only a single answer is allowed or
// if multiple answers are allowed. For single answers the answer id is
// saved in $state->responses[''], whereas for the multiple answers case
// the $state->responses array is indexed by the answer ids and the
// values are also the answer ids (i.e. key = value).
if (empty($state->responses[''])) { // No previous responses
if ($question->options->single) {
$state->responses = array('' => '');
} else {
$state->responses = array();
}
} else {
if ($question->options->single) {
$state->responses = array('' => $state->responses['']);
} else {
// Get array of answer ids
$state->responses = explode(',', $state->responses['']);
// Create an array indexed by these answer ids
$state->responses = array_flip($state->responses);
// Set the value of each element to be equal to the index
array_walk($state->responses, create_function('&$a, $b',
'$a = $b;'));
}
}
return true;
}
function save_session_and_responses(&$question, &$state) {
// Bundle the answer order and the responses into the legacy answer
// field.
// The serialized format for multiple choice quetsions
// is (optionally) a comma separated list of answer ids
// followed by a colon, followed by another comma separated
// list of answer ids, which are the radio/checkboxes that were
// ticked.
// E.g. 1,3,2,4:2,4 means that the answers were shown in the order
// 1, 3, 2 and then 4 and the answers 2 and 4 were checked.
$responses = implode(',', $state->options->order) . ':';
$responses .= implode(',', $state->responses);
// Set the legacy answer field
if (!set_field('question_states', 'answer', $responses, 'id',
$state->id)) {
return false;
}
return true;
}
function get_correct_responses(&$question, &$state) {
if ($question->options->single) {
foreach ($question->options->answers as $answer) {
if (((int) $answer->fraction) === 1) {
return array('' => $answer->id);
}
}
return null;
} else {
$responses = array();
foreach ($question->options->answers as $answer) {
if (((float) $answer->fraction) > 0.0) {
$responses[$answer->id] = (string) $answer->id;
}
}
return empty($responses) ? null : $responses;
}
}
function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
global $CFG;
$answers = &$question->options->answers;
$correctanswers = $this->get_correct_responses($question, $state);
$readonly = empty($options->readonly) ? '' : 'readonly="readonly"';
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->para = false;
// Print formulation
$questiontext = format_text($question->questiontext,
$question->questiontextformat,
$formatoptions, $cmoptions->course);
$image = get_question_image($question, $cmoptions->course);
$answerprompt = ($question->options->single) ? get_string('singleanswer', 'quiz') :
get_string('multipleanswers', 'quiz');
// Print each answer in a separate row
foreach ($state->options->order as $key => $aid) {
$answer = &$answers[$aid];
$qnumchar = chr(ord('a') + $key);
$checked = '';
if ($question->options->single) {
$type = 'type="radio"';
$name = "name=\"{$question->name_prefix}\"";
if (isset($state->responses['']) and $aid == $state->responses['']) {
$checked = 'checked="checked"';
}
} else {
$type = ' type="checkbox" ';
$name = "name=\"{$question->name_prefix}{$aid}\"";
$checked = isset($state->responses[$aid])
? 'checked="checked"' : '';
}
$a->id = $question->name_prefix . $aid;
// Print the control
$a->control = "<input $readonly id=\"$a->id\" $name $checked $type value=\"$aid\"" .
" alt=\"" . s($answer->answer) . '" />';
// Print the text by the control highlighting if correct responses
// should be shown and the current answer is the correct answer in
// the single selection case or has a positive score in the multiple
// selection case
$a->class = ($options->readonly && $options->correct_responses &&
is_array($correctanswers) && in_array($aid, $correctanswers)) ?
'highlight' : '';
// Print the answer text
$a->text = format_text("$qnumchar. $answer->answer", FORMAT_MOODLE, $formatoptions, $cmoptions->course);
// Print feedback if feedback is on
$a->feedback = (($options->feedback || $options->correct_responses) && $checked) ?
$feedback = format_text($answer->feedback, true, $formatoptions, $cmoptions->course) : '';
$anss[] = clone($a);
}
include("$CFG->dirroot/question/type/multichoice/display.html");
}
function grade_responses(&$question, &$state, $cmoptions) {
$answers = $question->options->answers;
$testedstate = clone($state);
$teststate = clone($state);
$state->raw_grade = 0;
foreach($answers as $answer) {
$teststate->responses = array('' => $answer->id);
if($question->options->single) {
if($this->compare_responses($question, $testedstate,
$teststate)) {
$state->raw_grade = $answer->fraction;
break;
}
} else {
foreach($state->responses as $response) {
$testedstate->responses = array('' => $response);
if($this->compare_responses($question, $testedstate,
$teststate)) {
$state->raw_grade += $answer->fraction;
break;
}
}
}
}
if (empty($state->raw_grade)) {
$state->raw_grade = 0;
}
// Make sure we don't assign negative or too high marks
$state->raw_grade = min(max((float) $state->raw_grade,
0.0), 1.0) * $question->maxgrade;
// Apply the penalty for this attempt
$state->penalty = $question->penalty * $question->maxgrade;
// mark the state as graded
$state->event = ($state->event == QUESTION_EVENTCLOSE) ? QUESTION_EVENTCLOSEANDGRADE : QUESTION_EVENTGRADE;
return true;
}
// ULPGC ecastro
function get_actual_response($question, $state) {
$answers = $question->options->answers;
if (!empty($state->responses)) {
foreach ($state->responses as $aid =>$rid){
$responses[] = (!empty($answers[$rid]) ? $answers[$rid]->answer : '');
}
} else {
$responses[] = '';
}
return $responses;
}
/// BACKUP FUNCTIONS ////////////////////////////
/*
* Backup the data in the question
*
* This is used in question/backuplib.php
*/
function backup($bf,$preferences,$question,$level=6) {
$status = true;
$multichoices = get_records("question_multichoice","question",$question,"id");
//If there are multichoices
if ($multichoices) {
//Iterate over each multichoice
foreach ($multichoices as $multichoice) {
$status = fwrite ($bf,start_tag("MULTICHOICE",$level,true));
//Print multichoice contents
fwrite ($bf,full_tag("LAYOUT",$level+1,false,$multichoice->layout));
fwrite ($bf,full_tag("ANSWERS",$level+1,false,$multichoice->answers));
fwrite ($bf,full_tag("SINGLE",$level+1,false,$multichoice->single));
fwrite ($bf,full_tag("SHUFFLEANSWERS",$level+1,false,$multichoice->shuffleanswers));
$status = fwrite ($bf,end_tag("MULTICHOICE",$level,true));
}
//Now print question_answers
$status = question_backup_answers($bf,$preferences,$question);
}
return $status;
}
/// RESTORE FUNCTIONS /////////////////
/*
* Restores the data in the question
*
* This is used in question/restorelib.php
*/
function restore($old_question_id,$new_question_id,$info,$restore) {
$status = true;
//Get the multichoices array
$multichoices = $info['#']['MULTICHOICE'];
//Iterate over multichoices
for($i = 0; $i < sizeof($multichoices); $i++) {
$mul_info = $multichoices[$i];
//Now, build the question_multichoice record structure
$multichoice->question = $new_question_id;
$multichoice->layout = backup_todb($mul_info['#']['LAYOUT']['0']['#']);
$multichoice->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
$multichoice->single = backup_todb($mul_info['#']['SINGLE']['0']['#']);
$multichoice->shuffleanswers = backup_todb($mul_info['#']['SHUFFLEANSWERS']['0']['#']);
//We have to recode the answers field (a list of answers id)
//Extracts answer id from sequence
$answers_field = "";
$in_first = true;
$tok = strtok($multichoice->answers,",");
while ($tok) {
//Get the answer from backup_ids
$answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
if ($answer) {
if ($in_first) {
$answers_field .= $answer->new_id;
$in_first = false;
} else {
$answers_field .= ",".$answer->new_id;
}
}
//check for next
$tok = strtok(",");
}
//We have the answers field recoded to its new ids
$multichoice->answers = $answers_field;
//The structure is equal to the db, so insert the question_shortanswer
$newid = insert_record ("question_multichoice",$multichoice);
//Do some output
if (($i+1) % 50 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
if (!$newid) {
$status = false;
}
}
return $status;
}
function restore_recode_answer($state, $restore) {
$pos = strpos($state->answer, ':');
$order = array();
$responses = array();
if (false === $pos) { // No order of answers is given, so use the default
if ($state->answer) {
$responses = explode(',', $state->answer);
}
} else {
$order = explode(',', substr($state->answer, 0, $pos));
if ($responsestring = substr($state->answer, $pos + 1)) {
$responses = explode(',', $responsestring);
}
}
if ($order) {
foreach ($order as $key => $oldansid) {
$answer = backup_getid($restore->backup_unique_code,"question_answers",$oldansid);
if ($answer) {
$order[$key] = $answer->new_id;
} else {
echo 'Could not recode multichoice answer id '.$oldansid.' for state '.$oldid.'<br />';
}
}
}
if ($responses) {
foreach ($responses as $key => $oldansid) {
$answer = backup_getid($restore->backup_unique_code,"question_answers",$oldansid);
if ($answer) {
$responses[$key] = $answer->new_id;
} else {
echo 'Could not recode multichoice response answer id '.$oldansid.' for state '.$oldid.'<br />';
}
}
}
return implode(',', $order).':'.implode(',', $responses);
}
}
//// END OF CLASS ////
//////////////////////////////////////////////////////////////////////////
//// INITIATION - Without this line the question type is not in use... ///
//////////////////////////////////////////////////////////////////////////
$QTYPES['multichoice']= new question_multichoice_qtype();
// The following adds the questiontype to the menu of types shown to teachers
$QTYPE_MENU['multichoice'] = get_string("multichoice", "quiz");
?>
|