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

/mod/lesson/pagetypes/matching.php

https://bitbucket.org/moodle/moodle
PHP | 603 lines | 487 code | 69 blank | 47 comment | 120 complexity | 9be8201e44a3faca701a87caccf75a21 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Matching
  18. *
  19. * @package mod_lesson
  20. * @copyright 2009 Sam Hemelryk
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. **/
  23. defined('MOODLE_INTERNAL') || die();
  24. /** Matching question type */
  25. define("LESSON_PAGE_MATCHING", "5");
  26. class lesson_page_type_matching extends lesson_page {
  27. protected $type = lesson_page::TYPE_QUESTION;
  28. protected $typeid = LESSON_PAGE_MATCHING;
  29. protected $typeidstring = 'matching';
  30. protected $string = null;
  31. public function get_typeid() {
  32. return $this->typeid;
  33. }
  34. public function get_typestring() {
  35. if ($this->string===null) {
  36. $this->string = get_string($this->typeidstring, 'lesson');
  37. }
  38. return $this->string;
  39. }
  40. public function get_idstring() {
  41. return $this->typeidstring;
  42. }
  43. public function display($renderer, $attempt) {
  44. global $USER, $CFG, $PAGE;
  45. $mform = $this->make_answer_form($attempt);
  46. $data = new stdClass;
  47. $data->id = $PAGE->cm->id;
  48. $data->pageid = $this->properties->id;
  49. $mform->set_data($data);
  50. // Trigger an event question viewed.
  51. $eventparams = array(
  52. 'context' => context_module::instance($PAGE->cm->id),
  53. 'objectid' => $this->properties->id,
  54. 'other' => array(
  55. 'pagetype' => $this->get_typestring()
  56. )
  57. );
  58. $event = \mod_lesson\event\question_viewed::create($eventparams);
  59. $event->trigger();
  60. return $mform->display();
  61. }
  62. protected function make_answer_form($attempt=null) {
  63. global $USER, $CFG;
  64. // don't shuffle answers (could be an option??)
  65. $getanswers = array_slice($this->get_answers(), 2);
  66. $answers = array();
  67. foreach ($getanswers as $getanswer) {
  68. $answers[$getanswer->id] = $getanswer;
  69. }
  70. $responses = array();
  71. foreach ($answers as $answer) {
  72. // get all the response
  73. if ($answer->response != null) {
  74. $responses[] = trim($answer->response);
  75. }
  76. }
  77. $responseoptions = array(''=>get_string('choosedots'));
  78. if (!empty($responses)) {
  79. shuffle($responses);
  80. foreach ($responses as $response) {
  81. $responseoptions[htmlspecialchars($response)] = $response;
  82. }
  83. }
  84. if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) {
  85. $useranswers = explode(',', $attempt->useranswer);
  86. $t = 0;
  87. } else {
  88. $useranswers = array();
  89. }
  90. $action = $CFG->wwwroot.'/mod/lesson/continue.php';
  91. $params = array('answers'=>$answers, 'useranswers'=>$useranswers, 'responseoptions'=>$responseoptions, 'lessonid'=>$this->lesson->id, 'contents'=>$this->get_contents());
  92. $mform = new lesson_display_answer_form_matching($action, $params);
  93. return $mform;
  94. }
  95. public function create_answers($properties) {
  96. global $DB, $PAGE;
  97. // now add the answers
  98. $newanswer = new stdClass;
  99. $newanswer->lessonid = $this->lesson->id;
  100. $newanswer->pageid = $this->properties->id;
  101. $newanswer->timecreated = $this->properties->timecreated;
  102. $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
  103. $context = context_module::instance($cm->id);
  104. // Check for duplicate response format.
  105. $duplicateresponse = array();
  106. if (is_array($properties->response_editor) && // If there are response_editors to iterate.
  107. is_array(reset($properties->response_editor))) { // And they come split into text & format array.
  108. foreach ($properties->response_editor as $response) { // Iterate over all them.
  109. $duplicateresponse[] = $response['text']; // Picking the text only. This pagetype is that way.
  110. }
  111. $properties->response_editor = $duplicateresponse;
  112. }
  113. $answers = array();
  114. // need to add two to offset correct response and wrong response
  115. $this->lesson->maxanswers = $this->lesson->maxanswers + 2;
  116. for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
  117. $answer = clone($newanswer);
  118. if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
  119. $answer->answer = $properties->answer_editor[$i]['text'];
  120. $answer->answerformat = $properties->answer_editor[$i]['format'];
  121. }
  122. if (!empty($properties->response_editor[$i])) {
  123. $answer->response = $properties->response_editor[$i];
  124. $answer->responseformat = 0;
  125. }
  126. if (isset($properties->jumpto[$i])) {
  127. $answer->jumpto = $properties->jumpto[$i];
  128. }
  129. if ($this->lesson->custom && isset($properties->score[$i])) {
  130. $answer->score = $properties->score[$i];
  131. }
  132. if (isset($answer->answer) && $answer->answer != '') {
  133. $answer->id = $DB->insert_record("lesson_answers", $answer);
  134. $this->save_answers_files($context, $PAGE->course->maxbytes,
  135. $answer, $properties->answer_editor[$i]);
  136. $answers[$answer->id] = new lesson_page_answer($answer);
  137. } else if ($i < 2) {
  138. $answer->id = $DB->insert_record("lesson_answers", $answer);
  139. $answers[$answer->id] = new lesson_page_answer($answer);
  140. } else {
  141. break;
  142. }
  143. }
  144. $this->answers = $answers;
  145. return $answers;
  146. }
  147. public function check_answer() {
  148. global $CFG, $PAGE;
  149. $formattextdefoptions = new stdClass();
  150. $formattextdefoptions->noclean = true;
  151. $formattextdefoptions->para = false;
  152. $result = parent::check_answer();
  153. $mform = $this->make_answer_form();
  154. $data = $mform->get_data();
  155. require_sesskey();
  156. if (!$data) {
  157. $result->inmediatejump = true;
  158. $result->newpageid = $this->properties->id;
  159. return $result;
  160. }
  161. $response = $data->response;
  162. $getanswers = $this->get_answers();
  163. foreach ($getanswers as $key => $answer) {
  164. $getanswers[$key] = parent::rewrite_answers_urls($answer);
  165. }
  166. $correct = array_shift($getanswers);
  167. $wrong = array_shift($getanswers);
  168. $answers = array();
  169. foreach ($getanswers as $key => $answer) {
  170. if ($answer->answer !== '' or $answer->response !== '') {
  171. $answers[$answer->id] = $answer;
  172. }
  173. }
  174. // get the user's exact responses for record keeping
  175. $hits = 0;
  176. $userresponse = array();
  177. $result->studentanswerformat = FORMAT_HTML;
  178. foreach ($response as $id => $value) {
  179. if ($value == '') {
  180. $result->noanswer = true;
  181. return $result;
  182. }
  183. $value = htmlspecialchars_decode($value);
  184. $userresponse[] = $value;
  185. // Make sure the user's answer exists in question's answer
  186. if (array_key_exists($id, $answers)) {
  187. $answer = $answers[$id];
  188. $result->studentanswer .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value;
  189. if (trim($answer->response) == trim($value)) {
  190. $hits++;
  191. }
  192. }
  193. }
  194. $result->userresponse = implode(",", $userresponse);
  195. if ($hits == count($answers)) {
  196. $result->correctanswer = true;
  197. $result->response = format_text($correct->answer, $correct->answerformat, $formattextdefoptions);
  198. $result->answerid = $correct->id;
  199. $result->newpageid = $correct->jumpto;
  200. } else {
  201. $result->correctanswer = false;
  202. $result->response = format_text($wrong->answer, $wrong->answerformat, $formattextdefoptions);
  203. $result->answerid = $wrong->id;
  204. $result->newpageid = $wrong->jumpto;
  205. }
  206. return $result;
  207. }
  208. public function option_description_string() {
  209. return get_string("firstanswershould", "lesson");
  210. }
  211. public function display_answers(html_table $table) {
  212. $answers = $this->get_answers();
  213. $options = new stdClass;
  214. $options->noclean = true;
  215. $options->para = false;
  216. $i = 1;
  217. $n = 0;
  218. foreach ($answers as $answer) {
  219. $answer = parent::rewrite_answers_urls($answer);
  220. if ($n < 2) {
  221. if ($answer->answer != null) {
  222. $cells = array();
  223. if ($n == 0) {
  224. $cells[] = '<label>' . get_string('correctresponse', 'lesson') . '</label>';
  225. } else {
  226. $cells[] = '<label>' . get_string('wrongresponse', 'lesson') . '</label>';
  227. }
  228. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  229. $table->data[] = new html_table_row($cells);
  230. }
  231. if ($n == 0) {
  232. $cells = array();
  233. $cells[] = '<label>' . get_string('correctanswerscore', 'lesson') . '</label>: ';
  234. $cells[] = $answer->score;
  235. $table->data[] = new html_table_row($cells);
  236. $cells = array();
  237. $cells[] = '<label>' . get_string('correctanswerjump', 'lesson') . '</label>: ';
  238. $cells[] = $this->get_jump_name($answer->jumpto);
  239. $table->data[] = new html_table_row($cells);
  240. } elseif ($n == 1) {
  241. $cells = array();
  242. $cells[] = '<label>' . get_string('wronganswerscore', 'lesson') . '</label>: ';
  243. $cells[] = $answer->score;
  244. $table->data[] = new html_table_row($cells);
  245. $cells = array();
  246. $cells[] = '<label>' . get_string('wronganswerjump', 'lesson') . '</label>: ';
  247. $cells[] = $this->get_jump_name($answer->jumpto);
  248. $table->data[] = new html_table_row($cells);
  249. }
  250. if ($n === 0){
  251. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  252. }
  253. $n++;
  254. $i--;
  255. } else {
  256. $cells = array();
  257. if ($this->lesson->custom && $answer->score > 0) {
  258. // if the score is > 0, then it is correct
  259. $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . " {$i}</label>: \n";
  260. } else if ($this->lesson->custom) {
  261. $cells[] = '<label>' . get_string('answer', 'lesson') . " {$i}</label>: \n";
  262. } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
  263. $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . " {$i}</label>: \n";
  264. } else {
  265. $cells[] = '<label>' . get_string('answer', 'lesson') . " {$i}</label>: \n";
  266. }
  267. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  268. $table->data[] = new html_table_row($cells);
  269. $cells = array();
  270. $cells[] = '<label>' . get_string('matchesanswer', 'lesson') . " {$i}</label>: \n";
  271. $cells[] = format_text($answer->response, $answer->responseformat, $options);
  272. $table->data[] = new html_table_row($cells);
  273. }
  274. $i++;
  275. }
  276. return $table;
  277. }
  278. /**
  279. * Updates the page and its answers
  280. *
  281. * @global moodle_database $DB
  282. * @global moodle_page $PAGE
  283. * @param stdClass $properties
  284. * @return bool
  285. */
  286. public function update($properties, $context = null, $maxbytes = null) {
  287. global $DB, $PAGE;
  288. $answers = $this->get_answers();
  289. $properties->id = $this->properties->id;
  290. $properties->lessonid = $this->lesson->id;
  291. $properties->timemodified = time();
  292. $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
  293. $DB->update_record("lesson_pages", $properties);
  294. // Trigger an event: page updated.
  295. \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
  296. // need to add two to offset correct response and wrong response
  297. $this->lesson->maxanswers += 2;
  298. for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
  299. if (!array_key_exists($i, $this->answers)) {
  300. $this->answers[$i] = new stdClass;
  301. $this->answers[$i]->lessonid = $this->lesson->id;
  302. $this->answers[$i]->pageid = $this->id;
  303. $this->answers[$i]->timecreated = $this->timecreated;
  304. }
  305. if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
  306. $this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
  307. $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
  308. }
  309. if (!empty($properties->response_editor[$i])) {
  310. $this->answers[$i]->response = $properties->response_editor[$i];
  311. $this->answers[$i]->responseformat = 0;
  312. }
  313. if (isset($properties->jumpto[$i])) {
  314. $this->answers[$i]->jumpto = $properties->jumpto[$i];
  315. }
  316. if ($this->lesson->custom && isset($properties->score[$i])) {
  317. $this->answers[$i]->score = $properties->score[$i];
  318. }
  319. // we don't need to check for isset here because properties called it's own isset method.
  320. if ($this->answers[$i]->answer != '') {
  321. if (!isset($this->answers[$i]->id)) {
  322. $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
  323. } else {
  324. $DB->update_record("lesson_answers", $this->answers[$i]->properties());
  325. }
  326. // Save files in answers (no response_editor for matching questions).
  327. $this->save_answers_files($context, $maxbytes, $this->answers[$i], $properties->answer_editor[$i]);
  328. } else if ($i < 2) {
  329. if (!isset($this->answers[$i]->id)) {
  330. $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
  331. } else {
  332. $DB->update_record("lesson_answers", $this->answers[$i]->properties());
  333. }
  334. // Save files in answers (no response_editor for matching questions).
  335. $this->save_answers_files($context, $maxbytes, $this->answers[$i], $properties->answer_editor[$i]);
  336. } else if (isset($this->answers[$i]->id)) {
  337. $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
  338. unset($this->answers[$i]);
  339. }
  340. }
  341. return true;
  342. }
  343. public function stats(array &$pagestats, $tries) {
  344. $temp = $this->lesson->get_last_attempt($tries);
  345. if ($temp->correct) {
  346. if (isset($pagestats[$temp->pageid]["correct"])) {
  347. $pagestats[$temp->pageid]["correct"]++;
  348. } else {
  349. $pagestats[$temp->pageid]["correct"] = 1;
  350. }
  351. }
  352. if (isset($pagestats[$temp->pageid]["total"])) {
  353. $pagestats[$temp->pageid]["total"]++;
  354. } else {
  355. $pagestats[$temp->pageid]["total"] = 1;
  356. }
  357. return true;
  358. }
  359. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  360. $answers = array();
  361. foreach ($this->get_answers() as $answer) {
  362. $answers[$answer->id] = $answer;
  363. }
  364. $formattextdefoptions = new stdClass;
  365. $formattextdefoptions->para = false; //I'll use it widely in this page
  366. foreach ($answers as $answer) {
  367. if ($n == 0 && $useranswer != null && $useranswer->correct) {
  368. if ($answer->response == null && $useranswer != null) {
  369. $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
  370. } else {
  371. $answerdata->response = $answer->response;
  372. }
  373. if ($this->lesson->custom) {
  374. $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
  375. } else {
  376. $answerdata->score = get_string("receivedcredit", "lesson");
  377. }
  378. } elseif ($n == 1 && $useranswer != null && !$useranswer->correct) {
  379. if ($answer->response == null && $useranswer != null) {
  380. $answerdata->response = get_string("thatsthewronganswer", "lesson");
  381. } else {
  382. $answerdata->response = $answer->response;
  383. }
  384. if ($this->lesson->custom) {
  385. $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
  386. } else {
  387. $answerdata->score = get_string("didnotreceivecredit", "lesson");
  388. }
  389. } elseif ($n > 1) {
  390. $data = '<label class="accesshide" for="answer_' . $n . '">' . get_string('answer', 'lesson') . '</label>';
  391. $data .= strip_tags(format_string($answer->answer)) . ' ';
  392. if ($useranswer != null) {
  393. $userresponse = explode(",", $useranswer->useranswer);
  394. $data .= '<label class="accesshide" for="stu_answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
  395. $data .= "<select class=\"custom-select\" id=\"stu_answer_response_" . $n . "\" " .
  396. "disabled=\"disabled\"><option selected=\"selected\">";
  397. if (array_key_exists($i, $userresponse)) {
  398. $data .= $userresponse[$i];
  399. }
  400. $data .= "</option></select>";
  401. } else {
  402. $data .= '<label class="accesshide" for="answer_response_' . $n . '">' . get_string('matchesanswer', 'lesson') . '</label>';
  403. $data .= "<select class=\"custom-select\" id=\"answer_response_" . $n . "\" " .
  404. "disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
  405. }
  406. if ($n == 2) {
  407. if (isset($pagestats[$this->properties->id])) {
  408. if (!array_key_exists('correct', $pagestats[$this->properties->id])) {
  409. $pagestats[$this->properties->id]["correct"] = 0;
  410. }
  411. $percent = $pagestats[$this->properties->id]["correct"] / $pagestats[$this->properties->id]["total"] * 100;
  412. $percent = round($percent, 2);
  413. $percent .= "% ".get_string("answeredcorrectly", "lesson");
  414. } else {
  415. $percent = get_string("nooneansweredthisquestion", "lesson");
  416. }
  417. } else {
  418. $percent = '';
  419. }
  420. $answerdata->answers[] = array($data, $percent);
  421. $i++;
  422. }
  423. $n++;
  424. $answerpage->answerdata = $answerdata;
  425. }
  426. return $answerpage;
  427. }
  428. public function get_jumps() {
  429. global $DB;
  430. // The jumps for matching question type are stored in the 1st and 2nd answer record.
  431. $jumps = array();
  432. if ($answers = $DB->get_records("lesson_answers", array("lessonid" => $this->lesson->id, "pageid" => $this->properties->id), 'id', '*', 0, 2)) {
  433. foreach ($answers as $answer) {
  434. $jumps[] = $this->get_jump_name($answer->jumpto);
  435. }
  436. } else {
  437. $jumps[] = $this->get_jump_name($this->properties->nextpageid);
  438. }
  439. return $jumps;
  440. }
  441. }
  442. class lesson_add_page_form_matching extends lesson_add_page_form_base {
  443. public $qtype = 'matching';
  444. public $qtypestring = 'matching';
  445. protected $answerformat = LESSON_ANSWER_HTML;
  446. protected $responseformat = '';
  447. public function custom_definition() {
  448. $this->_form->addElement('header', 'correctresponse', get_string('correctresponse', 'lesson'));
  449. $this->_form->addElement('editor', 'answer_editor[0]', get_string('correctresponse', 'lesson'),
  450. array('rows' => '4', 'columns' => '80'),
  451. array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
  452. $this->_form->setType('answer_editor[0]', PARAM_RAW);
  453. $this->_form->setDefault('answer_editor[0]', array('text' => '', 'format' => FORMAT_HTML));
  454. $this->add_jumpto(0, get_string('correctanswerjump','lesson'), LESSON_NEXTPAGE);
  455. $this->add_score(0, get_string("correctanswerscore", "lesson"), 1);
  456. $this->_form->addElement('header', 'wrongresponse', get_string('wrongresponse', 'lesson'));
  457. $this->_form->addElement('editor', 'answer_editor[1]', get_string('wrongresponse', 'lesson'),
  458. array('rows' => '4', 'columns' => '80'),
  459. array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
  460. $this->_form->setType('answer_editor[1]', PARAM_RAW);
  461. $this->_form->setDefault('answer_editor[1]', array('text' => '', 'format' => FORMAT_HTML));
  462. $this->add_jumpto(1, get_string('wronganswerjump','lesson'), LESSON_THISPAGE);
  463. $this->add_score(1, get_string("wronganswerscore", "lesson"), 0);
  464. for ($i = 2; $i < $this->_customdata['lesson']->maxanswers+2; $i++) {
  465. $this->_form->addElement('header', 'matchingpair'.($i-1), get_string('matchingpair', 'lesson', $i-1));
  466. $this->add_answer($i, null, ($i < 4), LESSON_ANSWER_HTML);
  467. $required = ($i < 4);
  468. $label = get_string('matchesanswer','lesson');
  469. $count = $i;
  470. $this->_form->addElement('text', 'response_editor['.$count.']', $label, array('size'=>'50'));
  471. $this->_form->setType('response_editor['.$count.']', PARAM_NOTAGS);
  472. $this->_form->setDefault('response_editor['.$count.']', '');
  473. if ($required) {
  474. $this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
  475. }
  476. }
  477. }
  478. }
  479. class lesson_display_answer_form_matching extends moodleform {
  480. public function definition() {
  481. global $USER, $OUTPUT, $PAGE;
  482. $mform = $this->_form;
  483. $answers = $this->_customdata['answers'];
  484. $useranswers = $this->_customdata['useranswers'];
  485. $responseoptions = $this->_customdata['responseoptions'];
  486. $lessonid = $this->_customdata['lessonid'];
  487. $contents = $this->_customdata['contents'];
  488. // Disable shortforms.
  489. $mform->setDisableShortforms();
  490. $mform->addElement('header', 'pageheader');
  491. $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
  492. $hasattempt = false;
  493. $disabled = '';
  494. if (isset($useranswers) && !empty($useranswers)) {
  495. $hasattempt = true;
  496. $disabled = array('disabled' => 'disabled');
  497. }
  498. $options = new stdClass;
  499. $options->para = false;
  500. $options->noclean = true;
  501. $mform->addElement('hidden', 'id');
  502. $mform->setType('id', PARAM_INT);
  503. $mform->addElement('hidden', 'pageid');
  504. $mform->setType('pageid', PARAM_INT);
  505. $i = 0;
  506. foreach ($answers as $answer) {
  507. $mform->addElement('html', '<div class="answeroption">');
  508. if ($answer->response != null) {
  509. $responseid = 'response['.$answer->id.']';
  510. if ($hasattempt) {
  511. $responseid = 'response_'.$answer->id;
  512. $mform->addElement('hidden', 'response['.$answer->id.']', htmlspecialchars($useranswers[$i]));
  513. // Temporary fixed until MDL-38885 gets integrated
  514. $mform->setType('response', PARAM_TEXT);
  515. }
  516. $answer = lesson_page_type_matching::rewrite_answers_urls($answer);
  517. $mform->addElement('select', $responseid, format_text($answer->answer,$answer->answerformat,$options), $responseoptions, $disabled);
  518. $mform->setType($responseid, PARAM_TEXT);
  519. if ($hasattempt) {
  520. $mform->setDefault($responseid, htmlspecialchars(trim($useranswers[$i])));
  521. } else {
  522. $mform->setDefault($responseid, 'answeroption');
  523. }
  524. }
  525. $mform->addElement('html', '</div>');
  526. $i++;
  527. }
  528. if ($hasattempt) {
  529. $this->add_action_buttons(null, get_string("nextpage", "lesson"));
  530. } else {
  531. $this->add_action_buttons(null, get_string("submit", "lesson"));
  532. }
  533. }
  534. }