You are here

class qformat_examview in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/question/format/examview/format.php \qformat_examview

Hierarchy

Expanded class hierarchy of qformat_examview

File

includes/moodle/question/format/examview/format.php, line 11

View source
class qformat_examview extends qformat_default {
  var $qtypes = array(
    'tf' => TRUEFALSE,
    'mc' => MULTICHOICE,
    'yn' => TRUEFALSE,
    'co' => SHORTANSWER,
    'ma' => MATCH,
    'mtf' => 99,
    'nr' => NUMERICAL,
    'pr' => 99,
    'es' => 99,
    'ca' => 99,
    'ot' => 99,
    'sa' => ESSAY,
  );
  var $matching_questions = array();
  function provide_import() {
    return true;
  }

  /**
   * unxmlise reconstructs part of the xml data structure in order
   * to identify the actual data therein
   * @param array $xml section of the xml data structure
   * @return string data with evrything else removed
   */
  function unxmlise($xml) {

    // if it's not an array then it's probably just data
    if (!is_array($xml)) {
      $text = s(addslashes($xml));
    }
    else {

      // otherwise parse the array
      $text = '';
      foreach ($xml as $tag => $data) {

        // if tag is '@' then it's attributes and we don't care
        if ($tag !== '@') {
          $text = $text . $this
            ->unxmlise($data);
        }
      }
    }

    // currently we throw the tags we found
    $text = strip_tags($text);
    return $text;
  }
  function parse_matching_groups($matching_groups) {
    if (empty($matching_groups)) {
      return;
    }
    foreach ($matching_groups as $match_group) {
      $newgroup = NULL;
      $groupname = trim($match_group['@']['name']);
      $questiontext = $this
        ->unxmlise($match_group['#']['text'][0]['#']);
      $newgroup->questiontext = trim($questiontext);
      $newgroup->subchoices = array();
      $newgroup->subquestions = array();
      $newgroup->subanswers = array();
      $choices = $match_group['#']['choices']['0']['#'];
      foreach ($choices as $key => $value) {
        if (strpos(trim($key), 'choice-') !== FALSE) {
          $key = strtoupper(trim(str_replace('choice-', '', $key)));
          $newgroup->subchoices[$key] = trim($value['0']['#']);
        }
      }
      $this->matching_questions[$groupname] = $newgroup;
    }
  }
  function parse_ma($qrec, $groupname) {
    $match_group = $this->matching_questions[$groupname];
    $phrase = trim($this
      ->unxmlise($qrec['text']['0']['#']));
    $answer = trim($this
      ->unxmlise($qrec['answer']['0']['#']));
    $answer = strip_tags($answer);
    $match_group->subquestions[] = $phrase;
    $match_group->subanswers[] = $match_group->subchoices[$answer];
    $this->matching_questions[$groupname] = $match_group;
    return NULL;
  }
  function process_matches(&$questions) {
    if (empty($this->matching_questions)) {
      return;
    }
    foreach ($this->matching_questions as $match_group) {
      $question = $this
        ->defaultquestion();
      $htmltext = s(addslashes($match_group->questiontext));
      $question->questiontext = $htmltext;
      $question->name = $question->questiontext;
      $question->qtype = MATCH;
      $question->subquestions = array();
      $question->subanswers = array();
      foreach ($match_group->subquestions as $key => $value) {
        $htmltext = s(addslashes($value));
        $question->subquestions[] = $htmltext;
        $htmltext = s(addslashes($match_group->subanswers[$key]));
        $question->subanswers[] = $htmltext;
      }
      $questions[] = $question;
    }
  }
  function cleanUnicode($text) {
    return str_replace('’', "'", $text);
  }
  function readquestions($lines) {

    /// Parses an array of lines into an array of questions,

    /// where each item is a question object as defined by

    /// readquestion().
    $questions = array();
    $currentquestion = array();
    $text = implode($lines, ' ');
    $text = $this
      ->cleanUnicode($text);
    $xml = xmlize($text, 0);
    if (!empty($xml['examview']['#']['matching-group'])) {
      $this
        ->parse_matching_groups($xml['examview']['#']['matching-group']);
    }
    $questionNode = $xml['examview']['#']['question'];
    foreach ($questionNode as $currentquestion) {
      if ($question = $this
        ->readquestion($currentquestion)) {
        $questions[] = $question;
      }
    }
    $this
      ->process_matches($questions);
    return $questions;
  }

  // end readquestions
  function readquestion($qrec) {
    $type = trim($qrec['@']['type']);
    $question = $this
      ->defaultquestion();
    if (array_key_exists($type, $this->qtypes)) {
      $question->qtype = $this->qtypes[$type];
    }
    else {
      $question->qtype = null;
    }
    $question->single = 1;

    // Only one answer is allowed
    $htmltext = $this
      ->unxmlise($qrec['#']['text'][0]['#']);
    $question->questiontext = $htmltext;
    $question->name = shorten_text($question->questiontext, 250);
    switch ($question->qtype) {
      case MULTICHOICE:
        $question = $this
          ->parse_mc($qrec['#'], $question);
        break;
      case MATCH:
        $groupname = trim($qrec['@']['group']);
        $question = $this
          ->parse_ma($qrec['#'], $groupname);
        break;
      case TRUEFALSE:
        $question = $this
          ->parse_tf_yn($qrec['#'], $question);
        break;
      case SHORTANSWER:
        $question = $this
          ->parse_co($qrec['#'], $question);
        break;
      case ESSAY:
        $question = $this
          ->parse_sa($qrec['#'], $question);
        break;
      case NUMERICAL:
        $question = $this
          ->parse_nr($qrec['#'], $question);
        break;
        break;
      default:
        print "<p>Question type " . $type . " import not supported for " . $question->questiontext . "<p>";
        $question = NULL;
    }

    // end switch ($question->qtype)
    return $question;
  }

  // end readquestion
  function parse_tf_yn($qrec, $question) {
    $choices = array(
      'T' => 1,
      'Y' => 1,
      'F' => 0,
      'N' => 0,
    );
    $answer = trim($qrec['answer'][0]['#']);
    $question->answer = $choices[$answer];
    $question->correctanswer = $question->answer;
    if ($question->answer == 1) {
      $question->feedbacktrue = 'Correct';
      $question->feedbackfalse = 'Incorrect';
    }
    else {
      $question->feedbacktrue = 'Incorrect';
      $question->feedbackfalse = 'Correct';
    }
    return $question;
  }
  function parse_mc($qrec, $question) {
    $answer = 'choice-' . strtolower(trim($qrec['answer'][0]['#']));
    $choices = $qrec['choices'][0]['#'];
    foreach ($choices as $key => $value) {
      if (strpos(trim($key), 'choice-') !== FALSE) {
        $question->answer[$key] = s($this
          ->unxmlise($value[0]['#']));
        if (strcmp($key, $answer) == 0) {
          $question->fraction[$key] = 1;
          $question->feedback[$key] = 'Correct';
        }
        else {
          $question->fraction[$key] = 0;
          $question->feedback[$key] = 'Incorrect';
        }
      }
    }
    return $question;
  }
  function parse_co($qrec, $question) {
    $question->usecase = 0;
    $answer = trim($this
      ->unxmlise($qrec['answer'][0]['#']));
    $answer = strip_tags($answer);
    $answers = explode("\n", $answer);
    foreach ($answers as $key => $value) {
      $value = trim($value);
      if (strlen($value) > 0) {
        $question->answer[$key] = addslashes($value);
        $question->fraction[$key] = 1;
        $question->feedback[$key] = "Correct";
      }
    }
    return $question;
  }
  function parse_sa($qrec, $question) {
    $feedback = trim($this
      ->unxmlise($qrec['answer'][0]['#']));
    $question->feedback = $feedback;
    $question->fraction = 0;
    return $question;
  }
  function parse_nr($qrec, $question) {
    $answer = trim($this
      ->unxmlise($qrec['answer'][0]['#']));
    $answer = strip_tags($answer);
    $answers = explode("\n", $answer);
    foreach ($answers as $key => $value) {
      $value = trim($value);
      if (is_numeric($value)) {
        $errormargin = 0;
        $question->answer[$key] = $value;
        $question->fraction[$key] = 1;
        $question->feedback[$key] = "Correct";
        $question->min[$key] = $question->answer[$key] - $errormargin;
        $question->max[$key] = $question->answer[$key] + $errormargin;
      }
    }
    return $question;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
qformat_default::$canaccessbackupdata property
qformat_default::$category property
qformat_default::$catfromfile property
qformat_default::$cattofile property
qformat_default::$contextfromfile property
qformat_default::$contexttofile property
qformat_default::$course property
qformat_default::$displayerrors property
qformat_default::$filename property
qformat_default::$importerrors property
qformat_default::$matchgrades property
qformat_default::$questionids property
qformat_default::$questions property
qformat_default::$realfilename property
qformat_default::$stoponerror property
qformat_default::$translator property
qformat_default::count_questions function Count all non-category questions in the questions array.
qformat_default::create_category_path function find and/or create the category described by a delimited list e.g. $course$/tom/dick/harry or tom/dick/harry
qformat_default::defaultquestion function return an "empty" question Somewhere to specify question parameters that are not handled by import but are required db fields. This should not be overridden.
qformat_default::error function Handle parsing error
qformat_default::exportpostprocess function Do an post-processing that may be required
qformat_default::exportpreprocess function Do any pre-processing that may be required 1
qformat_default::exportprocess function Do the export For most types this should not need to be overrided 1
qformat_default::export_file_extension function Return the files extension appropriate for this type override if you don't want .txt 3
qformat_default::format_question_text function where question specifies a moodle (text) format this performs the conversion.
qformat_default::get_category_path function get the category as a path (e.g., tom/dick/harry)
qformat_default::importimagefile function Import an image file encoded in base64 format
qformat_default::importpostprocess function Override if any post-processing is required 2
qformat_default::importpreprocess function Perform any required pre-processing 2
qformat_default::importprocess function Process the file This method should not normally be overidden 1
qformat_default::presave_process function Enable any processing to be done on the content just prior to the file being saved default is to do nothing 2
qformat_default::provide_export function 4
qformat_default::question_get_export_dir function get directory into which export is going
qformat_default::readdata function Return complete file within an array, one item per line 1
qformat_default::setCategory function set the category
qformat_default::setCatfromfile function set catfromfile
qformat_default::setCattofile function set cattofile
qformat_default::setContextfromfile function set contextfromfile
qformat_default::setContexts function set an array of contexts.
qformat_default::setContexttofile function set contexttofile
qformat_default::setCourse function set the course class variable
qformat_default::setFilename function set the filename
qformat_default::setMatchgrades function set matchgrades
qformat_default::setQuestions function Set the specific questions to export. Should not include questions with parents (sub questions of cloze question type). Only used for question export.
qformat_default::setRealfilename function set the "real" filename (this is what the user typed, regardless of wha happened next)
qformat_default::setStoponerror function set stoponerror
qformat_default::set_can_access_backupdata function
qformat_default::try_exporting_using_qtypes function Provide export functionality for plugin questiontypes Do not override
qformat_default::try_importing_using_qtypes function Import for questiontype plugins Do not override.
qformat_default::writequestion function convert a single question object into text output in the given format. This must be overriden 4
qformat_examview::$matching_questions property
qformat_examview::$qtypes property
qformat_examview::cleanUnicode function
qformat_examview::parse_co function
qformat_examview::parse_ma function
qformat_examview::parse_matching_groups function
qformat_examview::parse_mc function
qformat_examview::parse_nr function
qformat_examview::parse_sa function
qformat_examview::parse_tf_yn function
qformat_examview::process_matches function
qformat_examview::provide_import function Overrides qformat_default::provide_import
qformat_examview::readquestion function Given the data known to define a question in this format, this function converts it into a question object suitable for processing and insertion into Moodle. Overrides qformat_default::readquestion
qformat_examview::readquestions function Parses an array of lines into an array of questions, where each item is a question object as defined by readquestion(). Questions are defined as anything between blank lines. Overrides qformat_default::readquestions
qformat_examview::unxmlise function unxmlise reconstructs part of the xml data structure in order to identify the actual data therein