PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/shortanswer.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 372 lines | 302 code | 34 blank | 36 comment | 76 complexity | 6528321fd4bdb9e1f85db0627234dd66 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. * Short answer
  18. *
  19. * @package mod
  20. * @subpackage lesson
  21. * @copyright 2009 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. **/
  24. defined('MOODLE_INTERNAL') || die();
  25. /** Short answer question type */
  26. define("LESSON_PAGE_SHORTANSWER", "1");
  27. class lesson_page_type_shortanswer extends lesson_page {
  28. protected $type = lesson_page::TYPE_QUESTION;
  29. protected $typeidstring = 'shortanswer';
  30. protected $typeid = LESSON_PAGE_SHORTANSWER;
  31. protected $string = null;
  32. public function get_typeid() {
  33. return $this->typeid;
  34. }
  35. public function get_typestring() {
  36. if ($this->string===null) {
  37. $this->string = get_string($this->typeidstring, 'lesson');
  38. }
  39. return $this->string;
  40. }
  41. public function get_idstring() {
  42. return $this->typeidstring;
  43. }
  44. public function display($renderer, $attempt) {
  45. global $USER, $CFG, $PAGE;
  46. $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
  47. $data = new stdClass;
  48. $data->id = $PAGE->cm->id;
  49. $data->pageid = $this->properties->id;
  50. if (isset($USER->modattempts[$this->lesson->id])) {
  51. $data->answer = s($attempt->useranswer);
  52. }
  53. $mform->set_data($data);
  54. return $mform->display();
  55. }
  56. public function check_answer() {
  57. global $CFG;
  58. $result = parent::check_answer();
  59. $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
  60. $data = $mform->get_data();
  61. require_sesskey();
  62. $studentanswer = trim($data->answer);
  63. if ($studentanswer === '') {
  64. $result->noanswer = true;
  65. return $result;
  66. }
  67. $i=0;
  68. $answers = $this->get_answers();
  69. foreach ($answers as $answer) {
  70. $i++;
  71. $expectedanswer = $answer->answer; // for easier handling of $answer->answer
  72. $ismatch = false;
  73. $markit = false;
  74. $useregexp = ($this->qoption);
  75. if ($useregexp) { //we are using 'normal analysis', which ignores case
  76. $ignorecase = '';
  77. if (substr($expectedanswer, -2) == '/i') {
  78. $expectedanswer = substr($expectedanswer, 0, -2);
  79. $ignorecase = 'i';
  80. }
  81. } else {
  82. $expectedanswer = str_replace('*', '#####', $expectedanswer);
  83. $expectedanswer = preg_quote($expectedanswer, '/');
  84. $expectedanswer = str_replace('#####', '.*', $expectedanswer);
  85. }
  86. // see if user typed in any of the correct answers
  87. if ((!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) or ($this->lesson->custom && $answer->score > 0) ) {
  88. if (!$useregexp) { // we are using 'normal analysis', which ignores case
  89. if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) {
  90. $ismatch = true;
  91. }
  92. } else {
  93. if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) {
  94. $ismatch = true;
  95. }
  96. }
  97. if ($ismatch == true) {
  98. $result->correctanswer = true;
  99. }
  100. } else {
  101. if (!$useregexp) { //we are using 'normal analysis'
  102. // see if user typed in any of the wrong answers; don't worry about case
  103. if (preg_match('/^'.$expectedanswer.'$/i',$studentanswer)) {
  104. $ismatch = true;
  105. }
  106. } else { // we are using regular expressions analysis
  107. $startcode = substr($expectedanswer,0,2);
  108. switch ($startcode){
  109. //1- check for absence of required string in $studentanswer (coded by initial '--')
  110. case "--":
  111. $expectedanswer = substr($expectedanswer,2);
  112. if (!preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer)) {
  113. $ismatch = true;
  114. }
  115. break;
  116. //2- check for code for marking wrong strings (coded by initial '++')
  117. case "++":
  118. $expectedanswer=substr($expectedanswer,2);
  119. $markit = true;
  120. //check for one or several matches
  121. if (preg_match_all('/'.$expectedanswer.'/'.$ignorecase,$studentanswer, $matches)) {
  122. $ismatch = true;
  123. $nb = count($matches[0]);
  124. $original = array();
  125. $marked = array();
  126. $fontStart = '<span class="incorrect matches">';
  127. $fontEnd = '</span>';
  128. for ($i = 0; $i < $nb; $i++) {
  129. array_push($original,$matches[0][$i]);
  130. array_push($marked,$fontStart.$matches[0][$i].$fontEnd);
  131. }
  132. $studentanswer = str_replace($original, $marked, $studentanswer);
  133. }
  134. break;
  135. //3- check for wrong answers belonging neither to -- nor to ++ categories
  136. default:
  137. if (preg_match('/^'.$expectedanswer.'$/'.$ignorecase,$studentanswer, $matches)) {
  138. $ismatch = true;
  139. }
  140. break;
  141. }
  142. $result->correctanswer = false;
  143. }
  144. }
  145. if ($ismatch) {
  146. $result->newpageid = $answer->jumpto;
  147. if (trim(strip_tags($answer->response))) {
  148. $result->response = $answer->response;
  149. }
  150. $result->answerid = $answer->id;
  151. break; // quit answer analysis immediately after a match has been found
  152. }
  153. }
  154. $result->userresponse = $studentanswer;
  155. //clean student answer as it goes to output.
  156. $result->studentanswer = s($studentanswer);
  157. return $result;
  158. }
  159. public function option_description_string() {
  160. if ($this->properties->qoption) {
  161. return " - ".get_string("casesensitive", "lesson");
  162. }
  163. return parent::option_description_string();
  164. }
  165. public function display_answers(html_table $table) {
  166. $answers = $this->get_answers();
  167. $options = new stdClass;
  168. $options->noclean = true;
  169. $options->para = false;
  170. $i = 1;
  171. foreach ($answers as $answer) {
  172. $cells = array();
  173. if ($this->lesson->custom && $answer->score > 0) {
  174. // if the score is > 0, then it is correct
  175. $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
  176. } else if ($this->lesson->custom) {
  177. $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
  178. } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
  179. // underline correct answers
  180. $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
  181. } else {
  182. $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
  183. }
  184. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  185. $table->data[] = new html_table_row($cells);
  186. $cells = array();
  187. $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
  188. $cells[] = format_text($answer->response, $answer->responseformat, $options);
  189. $table->data[] = new html_table_row($cells);
  190. $cells = array();
  191. $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
  192. $cells[] = $answer->score;
  193. $table->data[] = new html_table_row($cells);
  194. $cells = array();
  195. $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
  196. $cells[] = $this->get_jump_name($answer->jumpto);
  197. $table->data[] = new html_table_row($cells);
  198. if ($i === 1){
  199. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  200. }
  201. $i++;
  202. }
  203. return $table;
  204. }
  205. public function stats(array &$pagestats, $tries) {
  206. if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
  207. $temp = $tries[$this->lesson->maxattempts - 1];
  208. } else {
  209. // else, user attempted the question less than the max, so grab the last one
  210. $temp = end($tries);
  211. }
  212. if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
  213. $pagestats[$temp->pageid][$temp->useranswer]++;
  214. } else {
  215. $pagestats[$temp->pageid][$temp->useranswer] = 1;
  216. }
  217. if (isset($pagestats[$temp->pageid]["total"])) {
  218. $pagestats[$temp->pageid]["total"]++;
  219. } else {
  220. $pagestats[$temp->pageid]["total"] = 1;
  221. }
  222. return true;
  223. }
  224. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  225. $answers = $this->get_answers();
  226. $formattextdefoptions = new stdClass;
  227. $formattextdefoptions->para = false; //I'll use it widely in this page
  228. foreach ($answers as $answer) {
  229. if ($useranswer == null && $i == 0) {
  230. // I have the $i == 0 because it is easier to blast through it all at once.
  231. if (isset($pagestats[$this->properties->id])) {
  232. $stats = $pagestats[$this->properties->id];
  233. $total = $stats["total"];
  234. unset($stats["total"]);
  235. foreach ($stats as $valentered => $ntimes) {
  236. $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($valentered).'" />';
  237. $percent = $ntimes / $total * 100;
  238. $percent = round($percent, 2);
  239. $percent .= "% ".get_string("enteredthis", "lesson");
  240. $answerdata->answers[] = array($data, $percent);
  241. }
  242. } else {
  243. $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
  244. }
  245. $i++;
  246. } else if ($useranswer != null && ($answer->id == $useranswer->answerid || $answer == end($answers))) {
  247. // get in here when what the user entered is not one of the answers
  248. $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">';
  249. if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
  250. $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
  251. $percent = round($percent, 2);
  252. $percent .= "% ".get_string("enteredthis", "lesson");
  253. } else {
  254. $percent = get_string("nooneenteredthis", "lesson");
  255. }
  256. $answerdata->answers[] = array($data, $percent);
  257. if ($answer->id == $useranswer->answerid) {
  258. if ($answer->response == null) {
  259. if ($useranswer->correct) {
  260. $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
  261. } else {
  262. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  263. }
  264. } else {
  265. $answerdata->response = $answer->response;
  266. }
  267. if ($this->lesson->custom) {
  268. $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
  269. } elseif ($useranswer->correct) {
  270. $answerdata->score = get_string("receivedcredit", "lesson");
  271. } else {
  272. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  273. }
  274. // We have found the correct answer, do not process any more answers.
  275. $answerpage->answerdata = $answerdata;
  276. break;
  277. } else {
  278. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  279. if ($this->lesson->custom) {
  280. $answerdata->score = get_string("pointsearned", "lesson").": 0";
  281. } else {
  282. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  283. }
  284. }
  285. }
  286. $answerpage->answerdata = $answerdata;
  287. }
  288. return $answerpage;
  289. }
  290. }
  291. class lesson_add_page_form_shortanswer extends lesson_add_page_form_base {
  292. public $qtype = 'shortanswer';
  293. public $qtypestring = 'shortanswer';
  294. public function custom_definition() {
  295. $this->_form->addElement('checkbox', 'qoption', get_string('options', 'lesson'), get_string('casesensitive', 'lesson')); //oh my, this is a regex option!
  296. $this->_form->setDefault('qoption', 0);
  297. $this->_form->addHelpButton('qoption', 'casesensitive', 'lesson');
  298. for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
  299. $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
  300. $this->add_answer($i);
  301. $this->add_response($i);
  302. $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
  303. $this->add_score($i, null, ($i===0)?1:0);
  304. }
  305. }
  306. }
  307. class lesson_display_answer_form_shortanswer extends moodleform {
  308. public function definition() {
  309. global $OUTPUT, $USER;
  310. $mform = $this->_form;
  311. $contents = $this->_customdata['contents'];
  312. $hasattempt = false;
  313. $attrs = array('size'=>'50', 'maxlength'=>'200');
  314. if (isset($this->_customdata['lessonid'])) {
  315. $lessonid = $this->_customdata['lessonid'];
  316. if (isset($USER->modattempts[$lessonid]->useranswer)) {
  317. $attrs['readonly'] = 'readonly';
  318. $hasattempt = true;
  319. }
  320. }
  321. $mform->addElement('header', 'pageheader');
  322. $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
  323. $options = new stdClass;
  324. $options->para = false;
  325. $options->noclean = true;
  326. $mform->addElement('hidden', 'id');
  327. $mform->setType('id', PARAM_INT);
  328. $mform->addElement('hidden', 'pageid');
  329. $mform->setType('pageid', PARAM_INT);
  330. $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
  331. $mform->setType('answer', PARAM_TEXT);
  332. if ($hasattempt) {
  333. $this->add_action_buttons(null, get_string("nextpage", "lesson"));
  334. } else {
  335. $this->add_action_buttons(null, get_string("submit", "lesson"));
  336. }
  337. }
  338. }