Skip to content
Permalink
Browse files

MDL-21904 Add Aiken format to the possible question export formats

  • Loading branch information...
Jean-Michel Vedrine
Jean-Michel Vedrine committed Oct 9, 2014
1 parent 6597413 commit ed6bf151da57e52a4f6fd4526d85d35b763fb8bf
Showing with 79 additions and 1 deletion.
  1. +79 −1 question/format/aiken/format.php
@@ -57,6 +57,10 @@ public function provide_import() {
return true;
}
public function provide_export() {
return true;
}
public function readquestions($lines) {
$questions = array();
$question = $this->defaultquestion();
@@ -65,7 +69,7 @@ public function readquestions($lines) {
$stp = strpos($line, $endchar, 0);
$newlines = explode($endchar, $line);
$linescount = count($newlines);
for ($i=0; $i < $linescount; $i++) {
for ($i = 0; $i < $linescount; $i++) {
$nowline = trim($newlines[$i]);
// Go through the array and build an object called $question
// When done, add $question to $questions.
@@ -123,6 +127,80 @@ public function readquestion($lines) {
// This is no longer needed but might still be called by default.php.
return;
}
/**
* Do the export
* We need to override this function because we don't want any output
* even a blank line for non multichoice questions.
* @return stored_file
*/
public function exportprocess() {
global $CFG, $DB, $USER;
// Get the questions (from database) in this category
// only get q's with no parents (no cloze subquestions specifically).
if ($this->category) {
$questions = get_questions_category($this->category, true);
} else {
$questions = $this->questions;
}
$count = 0;
$endchar = chr(13);
// Results are first written into string (and then to a file)
// so create/initialize the string here.
$expout = '';
// Iterate through questions.
foreach ($questions as $question) {
// Used by file api.
$contextid = $DB->get_field('question_categories', 'contextid',
array('id' => $question->category));
$question->contextid = $contextid;
// Do not export hidden questions.
if (!empty($question->hidden)) {
continue;
}
// Only export multichoice questions.
if ($question->qtype != 'multichoice') {
continue;
}
// Export the question displaying message.
$count++;
if (question_has_capability_on($question, 'view', $question->category)) {
$expout .= str_replace("\n", '', question_utils::to_plain_text($question->questiontext,
$question->questiontextformat, array('para' => false, 'newlines' => false))) . $endchar;
$num = 0;
foreach ($question->options->answers as $answer) {
$number = chr(ord('A') + $num);
$expout .= $number . ') ' . str_replace("\n", '', question_utils::to_plain_text($answer->answer,
$answer->answerformat, array('para' => false, 'newlines' => false))) . $endchar;
if ($answer->fraction > .99) {
$correctanswer = $number;
}
$num++;
}
// Add the correct answer.
$expout .= 'ANSWER: ' . $correctanswer . $endchar;
}
}
// Continue path for following error checks.
$course = $this->course;
$continuepath = "$CFG->wwwroot/question/export.php?courseid=$course->id";
// Did we actually process anything ?
if ($count == 0) {
print_error('noquestions', 'question', $continuepath);
}
return $expout;
}
}

0 comments on commit ed6bf15

Please sign in to comment.
You can’t perform that action at this time.