PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/question/format/learnwise/format.php

https://bitbucket.org/ceu/moodle_demo
PHP | 151 lines | 133 code | 8 blank | 10 comment | 3 complexity | 994cbf5720be24a39678e35ea9ecff13 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php // $Id: format.php,v 1.4.4.1 2007/11/02 16:21:06 tjhunt Exp $
  2. // Alton College, Hampshire, UK - Tom Flannaghan, Andrew Walker
  3. // Imports learnwise multiple choice quizzes (single and multiple answers)
  4. // currently ignores the deduct attribute for multiple answer questions
  5. // deductions are currently simply found by dividing the award for the incorrect
  6. // answer by the total number of options
  7. // Based on format.php, included by ../../import.php
  8. /**
  9. * @package questionbank
  10. * @subpackage importexport
  11. */
  12. class qformat_learnwise extends qformat_default {
  13. function provide_import() {
  14. return true;
  15. }
  16. function readquestions($lines) {
  17. $questions = array();
  18. $currentquestion = array();
  19. foreach($lines as $line) {
  20. $line = trim($line);
  21. $currentquestion[] = $line;
  22. if ($question = $this->readquestion($currentquestion)) {
  23. $questions[] = $question;
  24. $currentquestion = array();
  25. }
  26. }
  27. return $questions;
  28. }
  29. function readquestion($lines) {
  30. $text = implode(' ', $lines);
  31. $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);
  32. $startpos = strpos($text, '<question type');
  33. $endpos = strpos($text, '</question>');
  34. if ($startpos === false || $endpos === false) {
  35. return false;
  36. }
  37. preg_match("/<question type=[\"\']([^\"\']+)[\"\']>/i", $text, $matches);
  38. $type = strtolower($matches[1]); // multichoice or multianswerchoice
  39. $questiontext = $this->unhtmlentities($this->stringbetween($text, '<text>', '</text>'));
  40. $questionhint = $this->unhtmlentities($this->stringbetween($text, '<hint>', '</hint>'));
  41. $questionaward = $this->stringbetween($text, '<award>', '</award>');
  42. $optionlist = $this->stringbetween($text, '<answer>', '</answer>');
  43. $optionlist = explode('<option', $optionlist);
  44. $n = 0;
  45. $optionscorrect = array();
  46. $optionstext = array();
  47. if ($type == 'multichoice') {
  48. foreach ($optionlist as $option) {
  49. $correct = $this->stringbetween($option, ' correct="', '">');
  50. $answer = $this->stringbetween($option, '">', '</option>');
  51. $optionscorrect[$n] = $correct;
  52. $optionstext[$n] = $this->unhtmlentities($answer);
  53. ++$n;
  54. }
  55. } else if ($type == 'multianswerchoice') {
  56. $numcorrect = 0;
  57. $totalaward = 0;
  58. $optionsaward = array();
  59. foreach ($optionlist as $option) {
  60. preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
  61. preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);
  62. $correct = $correctmatch[1];
  63. $award = $awardmatch[1];
  64. if ($correct == 'yes') {
  65. $totalaward += $award;
  66. ++$numcorrect;
  67. }
  68. $answer = $this->stringbetween($option, '">', '</option>');
  69. $optionscorrect[$n] = $correct;
  70. $optionstext[$n] = $this->unhtmlentities($answer);
  71. $optionsaward[$n] = $award;
  72. ++$n;
  73. }
  74. } else {
  75. echo "<p>I don't understand this question type (type = <strong>$type</strong>).</p>\n";
  76. }
  77. $question = $this->defaultquestion();
  78. $question->qtype = MULTICHOICE;
  79. $question->name = substr($questiontext, 0, 30);
  80. if (strlen($questiontext) > 30) {
  81. $question->name .= '...';
  82. }
  83. $question->questiontext = $questiontext;
  84. $question->single = ($type == 'multichoice') ? 1 : 0;
  85. $question->feedback[] = '';
  86. $question->fraction = array();
  87. $question->answer = array();
  88. for ($n = 0; $n < count($optionstext); ++$n) {
  89. if ($optionstext[$n]) {
  90. if (!isset($numcorrect)) { // single answer
  91. if ($optionscorrect[$n] == 'yes') {
  92. $fraction = (int) $questionaward;
  93. } else {
  94. $fraction = 0;
  95. }
  96. } else { // mulitple answers
  97. if ($optionscorrect[$n] == 'yes') {
  98. $fraction = $optionsaward[$n] / $totalaward;
  99. } else {
  100. $fraction = -$optionsaward[$n] / count($optionstext);
  101. }
  102. }
  103. $question->fraction[] = $fraction;
  104. $question->answer[] = $optionstext[$n];
  105. $question->feedback[] = ''; // no feedback in this type
  106. }
  107. }
  108. return $question;
  109. }
  110. function stringbetween($text, $start, $end) {
  111. $startpos = strpos($text, $start) + strlen($start);
  112. $endpos = strpos($text, $end);
  113. if ($startpos <= $endpos) {
  114. return substr($text, $startpos, $endpos - $startpos);
  115. }
  116. }
  117. function unhtmlentities($string) {
  118. $transtable = get_html_translation_table(HTML_ENTITIES);
  119. $transtable = array_flip($transtable);
  120. return strtr($string, $transtable);
  121. }
  122. }
  123. ?>