PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/question/format/examview/format.php

https://github.com/kpike/moodle
PHP | 313 lines | 236 code | 34 blank | 43 comment | 21 complexity | 2610c44e3b6fbca86790f2de8e9bd075 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Examview question importer.
  18. *
  19. * @package qformat
  20. * @subpackage examview
  21. * @copyright 2005 Howard Miller
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once ($CFG->libdir . '/xmlize.php');
  26. /**
  27. * Examview question importer.
  28. *
  29. * @copyright 2005 Howard Miller
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class qformat_examview extends qformat_default {
  33. public $qtypes = array(
  34. 'tf' => TRUEFALSE,
  35. 'mc' => MULTICHOICE,
  36. 'yn' => TRUEFALSE,
  37. 'co' => SHORTANSWER,
  38. 'ma' => MATCH,
  39. 'mtf' => 99,
  40. 'nr' => NUMERICAL,
  41. 'pr' => 99,
  42. 'es' => 99,
  43. 'ca' => 99,
  44. 'ot' => 99,
  45. 'sa' => ESSAY
  46. );
  47. public $matching_questions = array();
  48. function provide_import() {
  49. return true;
  50. }
  51. /**
  52. * unxmlise reconstructs part of the xml data structure in order
  53. * to identify the actual data therein
  54. * @param array $xml section of the xml data structure
  55. * @return string data with evrything else removed
  56. */
  57. function unxmlise( $xml ) {
  58. // if it's not an array then it's probably just data
  59. if (!is_array($xml)) {
  60. $text = s($xml);
  61. }
  62. else {
  63. // otherwise parse the array
  64. $text = '';
  65. foreach ($xml as $tag=>$data) {
  66. // if tag is '@' then it's attributes and we don't care
  67. if ($tag!=='@') {
  68. $text = $text . $this->unxmlise( $data );
  69. }
  70. }
  71. }
  72. // currently we throw the tags we found
  73. $text = strip_tags($text);
  74. return $text;
  75. }
  76. function parse_matching_groups($matching_groups)
  77. {
  78. if (empty($matching_groups)) {
  79. return;
  80. }
  81. foreach($matching_groups as $match_group) {
  82. $newgroup = NULL;
  83. $groupname = trim($match_group['@']['name']);
  84. $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
  85. $newgroup->questiontext = trim($questiontext);
  86. $newgroup->subchoices = array();
  87. $newgroup->subquestions = array();
  88. $newgroup->subanswers = array();
  89. $choices = $match_group['#']['choices']['0']['#'];
  90. foreach($choices as $key => $value) {
  91. if (strpos(trim($key),'choice-') !== FALSE) {
  92. $key = strtoupper(trim(str_replace('choice-', '', $key)));
  93. $newgroup->subchoices[$key] = trim($value['0']['#']);
  94. }
  95. }
  96. $this->matching_questions[$groupname] = $newgroup;
  97. }
  98. }
  99. function parse_ma($qrec, $groupname)
  100. {
  101. $match_group = $this->matching_questions[$groupname];
  102. $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
  103. $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
  104. $answer = strip_tags( $answer );
  105. $match_group->subquestions[] = $phrase;
  106. $match_group->subanswers[] = $match_group->subchoices[$answer];
  107. $this->matching_questions[$groupname] = $match_group;
  108. return NULL;
  109. }
  110. function process_matches(&$questions)
  111. {
  112. if (empty($this->matching_questions)) {
  113. return;
  114. }
  115. foreach($this->matching_questions as $match_group) {
  116. $question = $this->defaultquestion();
  117. $htmltext = s($match_group->questiontext);
  118. $question->questiontext = $htmltext;
  119. $question->name = $question->questiontext;
  120. $question->qtype = MATCH;
  121. $question->subquestions = array();
  122. $question->subanswers = array();
  123. foreach($match_group->subquestions as $key => $value) {
  124. $htmltext = s($value);
  125. $question->subquestions[] = $htmltext;
  126. $htmltext = s($match_group->subanswers[$key]);
  127. $question->subanswers[] = $htmltext;
  128. }
  129. $questions[] = $question;
  130. }
  131. }
  132. function cleanUnicode($text) {
  133. return str_replace('&#x2019;', "'", $text);
  134. }
  135. function readquestions($lines) {
  136. /// Parses an array of lines into an array of questions,
  137. /// where each item is a question object as defined by
  138. /// readquestion().
  139. $questions = array();
  140. $currentquestion = array();
  141. $text = implode($lines, ' ');
  142. $text = $this->cleanUnicode($text);
  143. $xml = xmlize($text, 0);
  144. if (!empty($xml['examview']['#']['matching-group'])) {
  145. $this->parse_matching_groups($xml['examview']['#']['matching-group']);
  146. }
  147. $questionNode = $xml['examview']['#']['question'];
  148. foreach($questionNode as $currentquestion) {
  149. if ($question = $this->readquestion($currentquestion)) {
  150. $questions[] = $question;
  151. }
  152. }
  153. $this->process_matches($questions);
  154. return $questions;
  155. }
  156. // end readquestions
  157. function readquestion($qrec)
  158. {
  159. $type = trim($qrec['@']['type']);
  160. $question = $this->defaultquestion();
  161. if (array_key_exists($type, $this->qtypes)) {
  162. $question->qtype = $this->qtypes[$type];
  163. }
  164. else {
  165. $question->qtype = null;
  166. }
  167. $question->single = 1;
  168. // Only one answer is allowed
  169. $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
  170. $question->questiontext = $htmltext;
  171. $question->name = shorten_text( $question->questiontext, 250 );
  172. switch ($question->qtype) {
  173. case MULTICHOICE:
  174. $question = $this->parse_mc($qrec['#'], $question);
  175. break;
  176. case MATCH:
  177. $groupname = trim($qrec['@']['group']);
  178. $question = $this->parse_ma($qrec['#'], $groupname);
  179. break;
  180. case TRUEFALSE:
  181. $question = $this->parse_tf_yn($qrec['#'], $question);
  182. break;
  183. case SHORTANSWER:
  184. $question = $this->parse_co($qrec['#'], $question);
  185. break;
  186. case ESSAY:
  187. $question = $this->parse_sa($qrec['#'], $question);
  188. break;
  189. case NUMERICAL:
  190. $question = $this->parse_nr($qrec['#'], $question);
  191. break;
  192. break;
  193. default:
  194. print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
  195. $question = NULL;
  196. }
  197. // end switch ($question->qtype)
  198. return $question;
  199. }
  200. // end readquestion
  201. function parse_tf_yn($qrec, $question)
  202. {
  203. $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
  204. $answer = trim($qrec['answer'][0]['#']);
  205. $question->answer = $choices[$answer];
  206. $question->correctanswer = $question->answer;
  207. if ($question->answer == 1) {
  208. $question->feedbacktrue = 'Correct';
  209. $question->feedbackfalse = 'Incorrect';
  210. } else {
  211. $question->feedbacktrue = 'Incorrect';
  212. $question->feedbackfalse = 'Correct';
  213. }
  214. return $question;
  215. }
  216. function parse_mc($qrec, $question)
  217. {
  218. $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
  219. $choices = $qrec['choices'][0]['#'];
  220. foreach($choices as $key => $value) {
  221. if (strpos(trim($key),'choice-') !== FALSE) {
  222. $question->answer[$key] = s($this->unxmlise($value[0]['#']));
  223. if (strcmp($key, $answer) == 0) {
  224. $question->fraction[$key] = 1;
  225. $question->feedback[$key] = 'Correct';
  226. } else {
  227. $question->fraction[$key] = 0;
  228. $question->feedback[$key] = 'Incorrect';
  229. }
  230. }
  231. }
  232. return $question;
  233. }
  234. function parse_co($qrec, $question)
  235. {
  236. $question->usecase = 0;
  237. $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
  238. $answer = strip_tags( $answer );
  239. $answers = explode("\n",$answer);
  240. foreach($answers as $key => $value) {
  241. $value = trim($value);
  242. if (strlen($value) > 0) {
  243. $question->answer[$key] = $value;
  244. $question->fraction[$key] = 1;
  245. $question->feedback[$key] = "Correct";
  246. }
  247. }
  248. return $question;
  249. }
  250. function parse_sa($qrec, $question) {
  251. $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
  252. $question->feedback = $feedback;
  253. $question->fraction = 0;
  254. return $question;
  255. }
  256. function parse_nr($qrec, $question)
  257. {
  258. $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
  259. $answer = strip_tags( $answer );
  260. $answers = explode("\n",$answer);
  261. foreach($answers as $key => $value) {
  262. $value = trim($value);
  263. if (is_numeric($value)) {
  264. $errormargin = 0;
  265. $question->answer[$key] = $value;
  266. $question->fraction[$key] = 1;
  267. $question->feedback[$key] = "Correct";
  268. $question->min[$key] = $question->answer[$key] - $errormargin;
  269. $question->max[$key] = $question->answer[$key] + $errormargin;
  270. }
  271. }
  272. return $question;
  273. }
  274. }
  275. // end class