Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * Numerical
  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  /** Numerical question type */
  29  define("LESSON_PAGE_NUMERICAL",     "8");
  30  
  31  use mod_lesson\local\numeric\helper;
  32  
  33  class lesson_page_type_numerical extends lesson_page {
  34  
  35      protected $type = lesson_page::TYPE_QUESTION;
  36      protected $typeidstring = 'numerical';
  37      protected $typeid = LESSON_PAGE_NUMERICAL;
  38      protected $string = null;
  39  
  40      public function get_typeid() {
  41          return $this->typeid;
  42      }
  43      public function get_typestring() {
  44          if ($this->string===null) {
  45              $this->string = get_string($this->typeidstring, 'lesson');
  46          }
  47          return $this->string;
  48      }
  49      public function get_idstring() {
  50          return $this->typeidstring;
  51      }
  52      public function display($renderer, $attempt) {
  53          global $USER, $PAGE;
  54          $mform = new lesson_display_answer_form_numerical(new moodle_url('/mod/lesson/continue.php'),
  55              array('contents' => $this->get_contents(), 'lessonid' => $this->lesson->id));
  56          $data = new stdClass;
  57          $data->id = $PAGE->cm->id;
  58          $data->pageid = $this->properties->id;
  59          if (isset($USER->modattempts[$this->lesson->id])) {
  60              $data->answer = s($attempt->useranswer);
  61          }
  62          $mform->set_data($data);
  63  
  64          // Trigger an event question viewed.
  65          $eventparams = array(
  66              'context' => context_module::instance($PAGE->cm->id),
  67              'objectid' => $this->properties->id,
  68              'other' => array(
  69                      'pagetype' => $this->get_typestring()
  70                  )
  71              );
  72  
  73          $event = \mod_lesson\event\question_viewed::create($eventparams);
  74          $event->trigger();
  75          return $mform->display();
  76      }
  77  
  78      /**
  79       * Creates answers for this page type.
  80       *
  81       * @param  object $properties The answer properties.
  82       */
  83      public function create_answers($properties) {
  84          if (isset($properties->enableotheranswers) && $properties->enableotheranswers) {
  85              $properties->response_editor = array_values($properties->response_editor);
  86              $properties->jumpto = array_values($properties->jumpto);
  87              $properties->score = array_values($properties->score);
  88              $wrongresponse = end($properties->response_editor);
  89              $wrongkey = key($properties->response_editor);
  90              $properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
  91          }
  92          parent::create_answers($properties);
  93      }
  94  
  95      /**
  96       * Update the answers for this page type.
  97       *
  98       * @param  object $properties The answer properties.
  99       * @param  context $context The context for this module.
 100       * @param  int $maxbytes The maximum bytes for any uploades.
 101       */
 102      public function update($properties, $context = null, $maxbytes = null) {
 103          if ($properties->enableotheranswers) {
 104              $properties->response_editor = array_values($properties->response_editor);
 105              $properties->jumpto = array_values($properties->jumpto);
 106              $properties->score = array_values($properties->score);
 107              $wrongresponse = end($properties->response_editor);
 108              $wrongkey = key($properties->response_editor);
 109              $properties->answer_editor[$wrongkey] = LESSON_OTHER_ANSWERS;
 110          }
 111          parent::update($properties, $context, $maxbytes);
 112      }
 113  
 114      public function check_answer() {
 115          $result = parent::check_answer();
 116  
 117          $mform = new lesson_display_answer_form_numerical(new moodle_url('/mod/lesson/continue.php'),
 118              array('contents' => $this->get_contents()));
 119          $data = $mform->get_data();
 120          require_sesskey();
 121  
 122          $formattextdefoptions = new stdClass();
 123          $formattextdefoptions->noclean = true;
 124          $formattextdefoptions->para = false;
 125  
 126          // set defaults
 127          $result->response = '';
 128          $result->newpageid = 0;
 129  
 130          if (!isset($data->answer)) {
 131              $result->noanswer = true;
 132              return $result;
 133          } else {
 134              $result->useranswer = $data->answer;
 135          }
 136          $result->studentanswer = $result->userresponse = $result->useranswer;
 137          $answers = $this->get_answers();
 138          foreach ($answers as $answer) {
 139              $answer = parent::rewrite_answers_urls($answer);
 140              if (strpos($answer->answer, ':')) {
 141                  // there's a pairs of values
 142                  list($min, $max) = explode(':', $answer->answer);
 143                  $minimum = (float) $min;
 144                  $maximum = (float) $max;
 145              } else {
 146                  // there's only one value
 147                  $minimum = (float) $answer->answer;
 148                  $maximum = $minimum;
 149              }
 150              if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) {
 151                  $result->newpageid = $answer->jumpto;
 152                  $result->response = format_text($answer->response, $answer->responseformat, $formattextdefoptions);
 153                  if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) {
 154                      $result->correctanswer = true;
 155                  }
 156                  if ($this->lesson->custom) {
 157                      if ($answer->score > 0) {
 158                          $result->correctanswer = true;
 159                      } else {
 160                          $result->correctanswer = false;
 161                      }
 162                  }
 163                  $result->answerid = $answer->id;
 164                  return $result;
 165              }
 166          }
 167          // We could check here to see if we have a wrong answer jump to use.
 168          if ($result->answerid == 0) {
 169              // Use the all other answers jump details if it is set up.
 170              $lastanswer = end($answers);
 171              // Double check that this is the @#wronganswer#@ answer.
 172              if (strpos($lastanswer->answer, LESSON_OTHER_ANSWERS) !== false) {
 173                  $otheranswers = end($answers);
 174                  $result->newpageid = $otheranswers->jumpto;
 175                  $result->response = format_text($otheranswers->response, $otheranswers->responseformat, $formattextdefoptions);
 176                  // Does this also need to do the jumpto_is_correct?
 177                  if ($this->lesson->custom) {
 178                      $result->correctanswer = ($otheranswers->score > 0);
 179                  }
 180                  $result->answerid = $otheranswers->id;
 181              }
 182          }
 183          return $result;
 184      }
 185  
 186      public function display_answers(html_table $table) {
 187          $answers = $this->get_answers();
 188          $options = new stdClass;
 189          $options->noclean = true;
 190          $options->para = false;
 191          $i = 1;
 192          foreach ($answers as $answer) {
 193              $answer = parent::rewrite_answers_urls($answer, false);
 194              $cells = array();
 195              if ($this->lesson->custom && $answer->score > 0) {
 196                  // if the score is > 0, then it is correct
 197                  $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
 198              } else if ($this->lesson->custom) {
 199                  $cells[] = '<label>' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
 200              } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
 201                  // underline correct answers
 202                  $cells[] = '<span class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</span>:' . "\n";
 203              } else {
 204                  $cells[] = '<label class="correct">' . get_string('answer', 'lesson') . ' ' . $i . '</label>:';
 205              }
 206              $formattedanswer = helper::lesson_format_numeric_value($answer->answer);
 207              $cells[] = format_text($formattedanswer, $answer->answerformat, $options);
 208              $table->data[] = new html_table_row($cells);
 209  
 210              $cells = array();
 211              $cells[] = '<label>' . get_string('response', 'lesson') . ' ' . $i . '</label>:';
 212              $cells[] = format_text($answer->response, $answer->responseformat, $options);
 213              $table->data[] = new html_table_row($cells);
 214  
 215              $cells = array();
 216              $cells[] = '<label>' . get_string('score', 'lesson') . '</label>:';
 217              $cells[] = $answer->score;
 218              $table->data[] = new html_table_row($cells);
 219  
 220              $cells = array();
 221              $cells[] = '<label>' . get_string('jump', 'lesson') . '</label>:';
 222              $cells[] = $this->get_jump_name($answer->jumpto);
 223              $table->data[] = new html_table_row($cells);
 224              if ($i === 1){
 225                  $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
 226              }
 227              $i++;
 228          }
 229          return $table;
 230      }
 231      public function stats(array &$pagestats, $tries) {
 232          $temp = $this->lesson->get_last_attempt($tries);
 233          if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
 234              $pagestats[$temp->pageid][$temp->useranswer]++;
 235          } else {
 236              $pagestats[$temp->pageid][$temp->useranswer] = 1;
 237          }
 238          if (isset($pagestats[$temp->pageid]["total"])) {
 239              $pagestats[$temp->pageid]["total"]++;
 240          } else {
 241              $pagestats[$temp->pageid]["total"] = 1;
 242          }
 243          return true;
 244      }
 245  
 246      public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
 247          $answers = $this->get_answers();
 248          $formattextdefoptions = new stdClass;
 249          $formattextdefoptions->para = false;  //I'll use it widely in this page
 250          foreach ($answers as $answer) {
 251              if ($useranswer == null && $i == 0) {
 252                  // I have the $i == 0 because it is easier to blast through it all at once.
 253                  if (isset($pagestats[$this->properties->id])) {
 254                      $stats = $pagestats[$this->properties->id];
 255                      $total = $stats["total"];
 256                      unset($stats["total"]);
 257                      foreach ($stats as $valentered => $ntimes) {
 258                          $valformatted = '';
 259                          if (!is_null($valentered) && trim($valentered) !== '') {  // Empty response, 0 could be ok.
 260                              $valformatted = s(format_float($valentered, strlen($valentered), true, true));
 261                          }
 262                          $data = '<input class="form-control" type="text" size="50" ' .
 263                                  'disabled="disabled" readonly="readonly" value="'. $valformatted .'" />';
 264                          $percent = $ntimes / $total * 100;
 265                          $percent = round($percent, 2);
 266                          $percent .= "% ".get_string("enteredthis", "lesson");
 267                          $answerdata->answers[] = array($data, $percent);
 268                      }
 269                  } else {
 270                      $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
 271                  }
 272                  $i++;
 273              } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) &&
 274                      empty($answerdata->answers)))) {
 275                  // Get in here when the user answered or for the last answer.
 276                  $valformatted = '';
 277                  if (!is_null($useranswer->useranswer) && trim($useranswer->useranswer) !== '') {  // Empty response, 0 could be ok.
 278                      $valformatted = s(format_float($useranswer->useranswer, strlen($useranswer->useranswer), true, true));
 279                  }
 280                  $data = '<input class="form-control" type="text" size="50" ' .
 281                          'disabled="disabled" readonly="readonly" value="' . $valformatted .'">';
 282                  if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
 283                      $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
 284                      $percent = round($percent, 2);
 285                      $percent .= "% ".get_string("enteredthis", "lesson");
 286                  } else {
 287                      $percent = get_string("nooneenteredthis", "lesson");
 288                  }
 289                  $answerdata->answers[] = array($data, $percent);
 290  
 291                  if ($answer->id == $useranswer->answerid) {
 292                      if ($answer->response == null) {
 293                          if ($useranswer->correct) {
 294                              $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
 295                          } else {
 296                              $answerdata->response = get_string("thatsthewronganswer", "lesson");
 297                          }
 298                      } else {
 299                          $answerdata->response = $answer->response;
 300                      }
 301                      if ($this->lesson->custom) {
 302                          $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
 303                      } elseif ($useranswer->correct) {
 304                          $answerdata->score = get_string("receivedcredit", "lesson");
 305                      } else {
 306                          $answerdata->score = get_string("didnotreceivecredit", "lesson");
 307                      }
 308                  } else {
 309                      $answerdata->response = get_string("thatsthewronganswer", "lesson");
 310                      if ($this->lesson->custom) {
 311                          $answerdata->score = get_string("pointsearned", "lesson").": 0";
 312                      } else {
 313                          $answerdata->score = get_string("didnotreceivecredit", "lesson");
 314                      }
 315                  }
 316              }
 317              $answerpage->answerdata = $answerdata;
 318          }
 319          return $answerpage;
 320      }
 321  
 322      /**
 323       * Make updates to the form data if required. In this case to put the all other answer data into the write section of the form.
 324       *
 325       * @param stdClass $data The form data to update.
 326       * @return stdClass The updated fom data.
 327       */
 328      public function update_form_data(stdClass $data) : stdClass {
 329          $answercount = count($this->get_answers());
 330  
 331          // If no answers provided, then we don't need to check anything.
 332          if (!$answercount) {
 333              return $data;
 334          }
 335  
 336          // Check for other answer entry.
 337          $lastanswer = $data->{'answer_editor[' . ($answercount - 1) . ']'};
 338          if (strpos($lastanswer, LESSON_OTHER_ANSWERS) !== false) {
 339              $data->{'answer_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
 340                      $data->{'answer_editor[' . ($answercount - 1) . ']'};
 341              $data->{'response_editor[' . ($this->lesson->maxanswers + 1) . ']'} =
 342                      $data->{'response_editor[' . ($answercount - 1) . ']'};
 343              $data->{'jumpto[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'jumpto[' . ($answercount - 1) . ']'};
 344              $data->{'score[' . ($this->lesson->maxanswers + 1) . ']'} = $data->{'score[' . ($answercount - 1) . ']'};
 345              $data->enableotheranswers = true;
 346  
 347              // Unset the old values.
 348              unset($data->{'answer_editor[' . ($answercount - 1) . ']'});
 349              unset($data->{'response_editor[' . ($answercount - 1) . ']'});
 350              unset($data->{'jumpto['. ($answercount - 1) . ']'});
 351              unset($data->{'score[' . ($answercount - 1) . ']'});
 352          }
 353  
 354          return $data;
 355      }
 356  
 357      /**
 358       * Custom formats the answer to display
 359       *
 360       * @param string $answer
 361       * @param context $context
 362       * @param int $answerformat
 363       * @param array $options Optional param for additional options.
 364       * @return string Returns formatted string
 365       */
 366      public function format_answer($answer, $context, $answerformat, $options = []) {
 367          $answer = helper::lesson_format_numeric_value($answer);
 368  
 369          return parent::format_answer($answer, $context, $answerformat, $options);
 370      }
 371  }
 372  
 373  class lesson_add_page_form_numerical extends lesson_add_page_form_base {
 374  
 375      public $qtype = 'numerical';
 376      public $qtypestring = 'numerical';
 377      protected $answerformat = '';
 378      protected $responseformat = LESSON_ANSWER_HTML;
 379  
 380      public function custom_definition() {
 381          $answercount = $this->_customdata['lesson']->maxanswers;
 382          for ($i = 0; $i < $answercount; $i++) {
 383              $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
 384              $this->add_answer($i, null, ($i < 1), '', [
 385                      'identifier' => 'numericanswer',
 386                      'component' => 'mod_lesson'
 387              ]);
 388              $this->add_response($i);
 389              $this->add_jumpto($i, null, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
 390              $this->add_score($i, null, ($i===0)?1:0);
 391          }
 392          // Wrong answer jump.
 393          $this->_form->addElement('header', 'wronganswer', get_string('allotheranswers', 'lesson'));
 394          $newcount = $answercount + 1;
 395          $this->_form->addElement('advcheckbox', 'enableotheranswers', get_string('enabled', 'lesson'));
 396          $this->add_response($newcount);
 397          $this->add_jumpto($newcount, get_string('allotheranswersjump', 'lesson'), LESSON_NEXTPAGE);
 398          $this->add_score($newcount, get_string('allotheranswersscore', 'lesson'), 0);
 399      }
 400  
 401      /**
 402       * We call get data when storing the data into the db. Override to format the floats properly
 403       *
 404       * @return object|void
 405       */
 406      public function get_data() : ?stdClass {
 407          $data = parent::get_data();
 408  
 409          if (!empty($data->answer_editor)) {
 410              foreach ($data->answer_editor as $key => $answer) {
 411                  $data->answer_editor[$key] = helper::lesson_unformat_numeric_value($answer);
 412              }
 413          }
 414  
 415          return $data;
 416      }
 417  
 418      /**
 419       * Return submitted data if properly submitted or returns NULL if validation fails or
 420       * if there is no submitted data with formatted numbers
 421       *
 422       * @return object submitted data; NULL if not valid or not submitted or cancelled
 423       */
 424      public function get_submitted_data() : ?stdClass {
 425          $data = parent::get_submitted_data();
 426  
 427          if (!empty($data->answer_editor)) {
 428              foreach ($data->answer_editor as $key => $answer) {
 429                  $data->answer_editor[$key] = helper::lesson_unformat_numeric_value($answer);
 430              }
 431          }
 432  
 433          return $data;
 434      }
 435  
 436      /**
 437       * Load in existing data as form defaults. Usually new entry defaults are stored directly in
 438       * form definition (new entry form); this function is used to load in data where values
 439       * already exist and data is being edited (edit entry form) after formatting numbers
 440       *
 441       *
 442       * @param stdClass|array $defaults object or array of default values
 443       */
 444      public function set_data($defaults) {
 445          if (is_object($defaults)) {
 446              $defaults = (array) $defaults;
 447          }
 448  
 449          $editor = 'answer_editor';
 450          foreach ($defaults as $key => $answer) {
 451              if (substr($key, 0, strlen($editor)) == $editor) {
 452                  $defaults[$key] = helper::lesson_format_numeric_value($answer);
 453              }
 454          }
 455  
 456          parent::set_data($defaults);
 457      }
 458  }
 459  
 460  class lesson_display_answer_form_numerical extends moodleform {
 461  
 462      public function definition() {
 463          global $USER, $OUTPUT;
 464          $mform = $this->_form;
 465          $contents = $this->_customdata['contents'];
 466  
 467          // Disable shortforms.
 468          $mform->setDisableShortforms();
 469  
 470          $mform->addElement('header', 'pageheader');
 471  
 472          $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
 473  
 474          $hasattempt = false;
 475          $attrs = array('size'=>'50', 'maxlength'=>'200');
 476          if (isset($this->_customdata['lessonid'])) {
 477              $lessonid = $this->_customdata['lessonid'];
 478              if (isset($USER->modattempts[$lessonid]->useranswer)) {
 479                  $attrs['readonly'] = 'readonly';
 480                  $hasattempt = true;
 481              }
 482          }
 483          $options = new stdClass;
 484          $options->para = false;
 485          $options->noclean = true;
 486  
 487          $mform->addElement('hidden', 'id');
 488          $mform->setType('id', PARAM_INT);
 489  
 490          $mform->addElement('hidden', 'pageid');
 491          $mform->setType('pageid', PARAM_INT);
 492  
 493          $mform->addElement('float', 'answer', get_string('youranswer', 'lesson'), $attrs);
 494  
 495          if ($hasattempt) {
 496              $this->add_action_buttons(null, get_string("nextpage", "lesson"));
 497          } else {
 498              $this->add_action_buttons(null, get_string("submit", "lesson"));
 499          }
 500      }
 501  }