[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lesson/pagetypes/ -> matching.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1