You are here

function _as_moodle_questions in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/questions_export/questions_export.admin.inc \_as_moodle_questions()
1 call to _as_moodle_questions()
_questions_export_moodle in includes/questions_export/questions_export.admin.inc
Exports questions using the Moodle export engine.

File

includes/questions_export/questions_export.admin.inc, line 182

Code

function _as_moodle_questions($drupal_questions) {
  $moodle_questions = array();

  // moodle questions
  // TODO turn them into Moodle model
  foreach ($drupal_questions as $dq) {
    $mq = new stdClass();
    $mq->id = "drupal_nid:{$dq->nid}, drupal_vid:{$dq->vid}, author:{$dq->name}";
    $mq->name = $dq->title;
    $mq->questiontext = $dq->body;
    $mq->questiontextformat = FORMAT_HTML;

    // FIXME where does Drupal Quiz store this?
    $qtypeMap = array(
      // Drupal to Moodle
      'long_answer' => 'essay',
      'short_answer' => 'shortanswer',
      'multichoice' => 'multichoice',
      'choice' => 'multichoice',
      'true_false' => 'truefalse',
      'quiz_directions' => 'description',
      'matching' => 'match',
    );
    $mq->qtype = $qtypeMap[$dq->type];
    $mq->options = new stdClass();
    $mq->options->answers = array();
    switch ($dq->type) {

      // TODO refactor this into the questiontype classes
      case 'matching':

        // TODO why doesn't matching have an ->answers property?
        for ($i = 0; !empty($dq->{$i}); $i += 1) {
          $match_answer = $dq->{$i};
          $questiontext = $match_answer->question;
          $answertext = $match_answer->answer;
          $matches[] = (object) compact('questiontext', 'answertext');
        }
        $mq->options->subquestions = $matches;
        break;
      case 'choice':
        $mq->singlequestion = !$question->choice_multi;
        foreach ($question->alternatives as $alternative) {
          $moodle_answer = new stdClass();
          $moodle_answer->answer = $alternative['answer'];
          $moodle_answer->feedback = $alternative['feedback_if_chosen'];
          $moodle_answer->fraction = $alternative['score_if_chosen'];
          $mq->options->answers[] = $moodle_answer;
        }
        break;
      case 'multichoice':
        $mq->singlequestion = $dq->multiple_answers ? 0 : 1;
        foreach ($dq->answers as $drupal_answer) {
          $moodle_answer = new stdClass();
          $moodle_answer->answer = $drupal_answer['answer'];
          $moodle_answer->feedback = $drupal_answer['feedback'];
          $moodle_answer->fraction = $drupal_answer['is_correct'] ? 1 : 0;
          $mq->options->answers[] = $moodle_answer;
        }
        break;
      case 'true_false':
        $moodle_true_answer = new stdClass();
        $moodle_true_answer->fraction = $dq->correct_answer ? 1 : 0;
        $mq->options->answers[] = $moodle_true_answer;
        $moodle_false_answer = new stdClass();
        $moodle_false_answer->fraction = $dq->correct_answer ? 0 : 1;
        $mq->options->answers[] = $moodle_false_answer;
        $mq->options->trueanswer = 0;

        // indices above
        $mq->options->falseanswer = 1;
        break;
    }
    $moodle_questions[] = $mq;
  }
  return $moodle_questions;
}