Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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  /**
  18   * Data generator.
  19   *
  20   * @package    mod_h5pactivity
  21   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use mod_h5pactivity\local\manager;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * h5pactivity module data generator class.
  32   *
  33   * @package    mod_h5pactivity
  34   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  35   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_h5pactivity_generator extends testing_module_generator {
  38  
  39      /**
  40       * Creates new h5pactivity module instance. By default it contains a short
  41       * text file.
  42       *
  43       * @param array|stdClass $record data for module being generated. Requires 'course' key
  44       *     (an id or the full object). Also can have any fields from add module form.
  45       * @param null|array $options general options for course module. Since 2.6 it is
  46       *     possible to omit this argument by merging options into $record
  47       * @return stdClass record from module-defined table with additional field
  48       *     cmid (corresponding id in course_modules table)
  49       */
  50      public function create_instance($record = null, array $options = null): stdClass {
  51          global $CFG, $USER;
  52          // Ensure the record can be modified without affecting calling code.
  53          $record = (object)(array)$record;
  54  
  55          // Fill in optional values if not specified.
  56          if (!isset($record->packagefilepath)) {
  57              $record->packagefilepath = $CFG->dirroot.'/h5p/tests/fixtures/h5ptest.zip';
  58          }
  59          if (strpos($record->packagefilepath, $CFG->dirroot) !== 0) {
  60              $record->packagefilepath = "{$CFG->dirroot}/{$record->packagefilepath}";
  61          }
  62          if (!isset($record->grade)) {
  63              $record->grade = 100;
  64          }
  65          if (!isset($record->displayoptions)) {
  66              $factory = new \core_h5p\factory();
  67              $core = $factory->get_core();
  68              $config = \core_h5p\helper::decode_display_options($core);
  69              $record->displayoptions = \core_h5p\helper::get_display_options($core, $config);
  70          }
  71          if (!isset($record->enabletracking)) {
  72              $record->enabletracking = 1;
  73          }
  74          if (!isset($record->grademethod)) {
  75              $record->grademethod = manager::GRADEHIGHESTATTEMPT;
  76          }
  77          if (!isset($record->reviewmode)) {
  78              $record->reviewmode = manager::REVIEWCOMPLETION;
  79          }
  80  
  81          // The 'packagefile' value corresponds to the draft file area ID. If not specified, create from packagefilepath.
  82          if (empty($record->packagefile)) {
  83              if (!isloggedin() || isguestuser()) {
  84                  throw new coding_exception('H5P activity generator requires a current user');
  85              }
  86              if (!file_exists($record->packagefilepath)) {
  87                  throw new coding_exception("File {$record->packagefilepath} does not exist");
  88              }
  89              $usercontext = context_user::instance($USER->id);
  90  
  91              // Pick a random context id for specified user.
  92              $record->packagefile = file_get_unused_draft_itemid();
  93  
  94              // Add actual file there.
  95              $filerecord = ['component' => 'user', 'filearea' => 'draft',
  96                      'contextid' => $usercontext->id, 'itemid' => $record->packagefile,
  97                      'filename' => basename($record->packagefilepath), 'filepath' => '/'];
  98              $fs = get_file_storage();
  99              $fs->create_file_from_pathname($filerecord, $record->packagefilepath);
 100          }
 101  
 102          // Do work to actually add the instance.
 103          return parent::create_instance($record, (array)$options);
 104      }
 105  
 106      /**
 107       * Creata a fake attempt
 108       * @param stdClass $instance object returned from create_instance() call
 109       * @param stdClass|array $record
 110       * @return stdClass generated object
 111       * @throws coding_exception if function is not implemented by module
 112       */
 113      public function create_content($instance, $record = []) {
 114          global $DB, $USER;
 115  
 116          $currenttime = time();
 117          $cmid = $record['cmid'];
 118          $userid = $record['userid'] ?? $USER->id;
 119          $conditions = ['h5pactivityid' => $instance->id, 'userid' => $userid];
 120          $attemptnum = $DB->count_records('h5pactivity_attempts', $conditions) + 1;
 121          $attempt = (object)[
 122                  'h5pactivityid' => $instance->id,
 123                  'userid' => $userid,
 124                  'timecreated' => $currenttime,
 125                  'timemodified' => $currenttime,
 126                  'attempt' => $attemptnum,
 127                  'rawscore' => 3,
 128                  'maxscore' => 5,
 129                  'completion' => 1,
 130                  'success' => 1,
 131                  'scaled' => 0.6,
 132              ];
 133          $attempt->id = $DB->insert_record('h5pactivity_attempts', $attempt);
 134  
 135          // Create 3 diferent tracking results.
 136          $result = (object)[
 137                  'attemptid' => $attempt->id,
 138                  'subcontent' => '',
 139                  'timecreated' => $currenttime,
 140                  'interactiontype' => 'compound',
 141                  'description' => 'description for '.$userid,
 142                  'correctpattern' => '',
 143                  'response' => '',
 144                  'additionals' => '{"extensions":{"http:\/\/h5p.org\/x-api\/h5p-local-content-id":'.
 145                          $cmid.'},"contextExtensions":{}}',
 146                  'rawscore' => 3,
 147                  'maxscore' => 5,
 148                  'completion' => 1,
 149                  'success' => 1,
 150                  'scaled' => 0.6,
 151              ];
 152          $DB->insert_record('h5pactivity_attempts_results', $result);
 153  
 154          $result->subcontent = 'bd03477a-90a1-486d-890b-0657d6e80ffd';
 155          $result->interactiontype = 'compound';
 156          $result->response = '0[,]5[,]2[,]3';
 157          $result->additionals = '{"choices":[{"id":"0","description":{"en-US":"Blueberry\n"}},'.
 158                  '{"id":"1","description":{"en-US":"Raspberry\n"}},{"id":"5","description":'.
 159                  '{"en-US":"Strawberry\n"}},{"id":"2","description":{"en-US":"Cloudberry\n"}},'.
 160                  '{"id":"3","description":{"en-US":"Halle Berry\n"}},'.
 161                  '{"id":"4","description":{"en-US":"Cocktail cherry\n"}}],'.
 162                  '"extensions":{"http:\/\/h5p.org\/x-api\/h5p-local-content-id":'.$cmid.
 163                  ',"http:\/\/h5p.org\/x-api\/h5p-subContentId":"'.$result->interactiontype.
 164                  '"},"contextExtensions":{}}';
 165          $result->rawscore = 1;
 166          $result->scaled = 0.2;
 167          $DB->insert_record('h5pactivity_attempts_results', $result);
 168  
 169          $result->subcontent = '14fcc986-728b-47f3-915b-'.$userid;
 170          $result->interactiontype = 'matching';
 171          $result->correctpattern = '["0[.]1[,]1[.]0[,]2[.]2"]';
 172          $result->response = '1[.]0[,]0[.]1[,]2[.]2';
 173          $result->additionals = '{"source":[{"id":"0","description":{"en-US":"A berry"}}'.
 174                  ',{"id":"1","description":{"en-US":"An orange berry"}},'.
 175                  '{"id":"2","description":{"en-US":"A red berry"}}],'.
 176                  '"target":[{"id":"0","description":{"en-US":"Cloudberry"}},'.
 177                  '{"id":"1","description":{"en-US":"Blueberry"}},'.
 178                  '{"id":"2","description":{"en-US":"Redcurrant\n"}}],'.
 179                  '"contextExtensions":{}}';
 180          $result->rawscore = 2;
 181          $result->scaled = 0.4;
 182          $DB->insert_record('h5pactivity_attempts_results', $result);
 183  
 184          return $attempt;
 185      }
 186  
 187      /**
 188       * Create a H5P attempt.
 189       *
 190       * This method is user by behat generator.
 191       *
 192       * @param array $data the attempts data array
 193       */
 194      public function create_attempt(array $data): void {
 195          global $DB;
 196  
 197          if (!isset($data['h5pactivityid'])) {
 198              throw new coding_exception('Must specify h5pactivityid when creating a H5P attempt.');
 199          }
 200  
 201          if (!isset($data['userid'])) {
 202              throw new coding_exception('Must specify userid when creating a H5P attempt.');
 203          }
 204  
 205          // Defaults.
 206          $data['attempt'] = $data['attempt'] ?? 1;
 207          $data['rawscore'] = $data['rawscore'] ?? 0;
 208          $data['maxscore'] = $data['maxscore'] ?? 0;
 209          $data['duration'] = $data['duration'] ?? 0;
 210          $data['completion'] = $data['completion'] ?? 1;
 211          $data['success'] = $data['success'] ?? 0;
 212  
 213          $data['attemptid'] = $this->get_attempt_object($data);
 214  
 215          // Check interaction type and create a valid record for it.
 216          $data['interactiontype'] = $data['interactiontype'] ?? 'compound';
 217          $method = 'get_attempt_result_' . str_replace('-', '', $data['interactiontype']);
 218          if (!method_exists($this, $method)) {
 219              throw new Exception("Cannot create a {$data['interactiontype']} interaction statement");
 220          }
 221  
 222          $this->insert_statement($data, $this->$method($data));
 223  
 224          // If the activity has tracking enabled, try to recalculate grades.
 225          $activity = $DB->get_record('h5pactivity', ['id' => $data['h5pactivityid']]);
 226          if ($activity->enabletracking) {
 227              h5pactivity_update_grades($activity, $data['userid']);
 228          }
 229      }
 230  
 231      /**
 232       * Get or create an H5P attempt using the data array.
 233       *
 234       * @param array $attemptinfo the generator provided data
 235       * @return int the attempt id
 236       */
 237      private function get_attempt_object($attemptinfo): int {
 238          global $DB;
 239          $result = $DB->get_record('h5pactivity_attempts', [
 240              'userid' => $attemptinfo['userid'],
 241              'h5pactivityid' => $attemptinfo['h5pactivityid'],
 242              'attempt' => $attemptinfo['attempt'],
 243          ]);
 244          if ($result) {
 245              return $result->id;
 246          }
 247          return $this->new_user_attempt($attemptinfo);
 248      }
 249  
 250      /**
 251       * Creates a user attempt.
 252       *
 253       * @param array $attemptinfo the current attempt information.
 254       * @return int the h5pactivity_attempt ID
 255       */
 256      private function new_user_attempt(array $attemptinfo): int {
 257          global $DB;
 258          $record = (object)[
 259              'h5pactivityid' => $attemptinfo['h5pactivityid'],
 260              'userid' => $attemptinfo['userid'],
 261              'timecreated' => time(),
 262              'timemodified' => time(),
 263              'attempt' => $attemptinfo['attempt'],
 264              'rawscore' => $attemptinfo['rawscore'],
 265              'maxscore' => $attemptinfo['maxscore'],
 266              'duration' => $attemptinfo['duration'],
 267              'completion' => $attemptinfo['completion'],
 268              'success' => $attemptinfo['success'],
 269          ];
 270          if (empty($record->maxscore)) {
 271              $record->scaled = 0;
 272          } else {
 273              $record->scaled = $record->rawscore / $record->maxscore;
 274          }
 275          return $DB->insert_record('h5pactivity_attempts', $record);
 276      }
 277  
 278      /**
 279       * Insert a new statement into an attempt.
 280       *
 281       * If the interaction type is "compound" it will also update the attempt general result.
 282       *
 283       * @param array $attemptinfo the current attempt information
 284       * @param array $statement the statement tracking information
 285       * @return int the h5pactivity_attempt_result ID
 286       */
 287      private function insert_statement(array $attemptinfo, array $statement): int {
 288          global $DB;
 289          $record = $statement + [
 290              'attemptid' => $attemptinfo['attemptid'],
 291              'interactiontype' => $attemptinfo['interactiontype'] ?? 'compound',
 292              'timecreated' => time(),
 293              'rawscore' => $attemptinfo['rawscore'],
 294              'maxscore' => $attemptinfo['maxscore'],
 295              'duration' => $attemptinfo['duration'],
 296              'completion' => $attemptinfo['completion'],
 297              'success' => $attemptinfo['success'],
 298          ];
 299          $result = $DB->insert_record('h5pactivity_attempts_results', $record);
 300          if ($record['interactiontype'] == 'compound') {
 301              $attempt = (object)[
 302                  'id' => $attemptinfo['attemptid'],
 303                  'rawscore' => $record['rawscore'],
 304                  'maxscore' => $record['maxscore'],
 305                  'duration' => $record['duration'],
 306                  'completion' => $record['completion'],
 307                  'success' => $record['success'],
 308              ];
 309              $DB->update_record('h5pactivity_attempts', $attempt);
 310          }
 311          return $result;
 312      }
 313  
 314      /**
 315       * Generates a valid compound tracking result.
 316       *
 317       * @param array $attemptinfo the current attempt information.
 318       * @return array with the required statement data
 319       */
 320      private function get_attempt_result_compound(array $attemptinfo): array {
 321          $additionals = (object)[
 322              "extensions" => (object)[
 323                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 324              ],
 325              "contextExtensions" => (object)[],
 326          ];
 327  
 328          return [
 329              'subcontent' => '',
 330              'description' => '',
 331              'correctpattern' => '',
 332              'response' => '',
 333              'additionals' => json_encode($additionals),
 334          ];
 335      }
 336  
 337      /**
 338       * Generates a valid choice tracking result.
 339       *
 340       * @param array $attemptinfo the current attempt information.
 341       * @return array with the required statement data
 342       */
 343      private function get_attempt_result_choice(array $attemptinfo): array {
 344  
 345          $response = ($attemptinfo['rawscore']) ? '1[,]0' : '2[,]3';
 346  
 347          $additionals = (object)[
 348              "choices" => [
 349                  (object)[
 350                      "id" => "3",
 351                      "description" => (object)[
 352                          "en-US" => "Another wrong answer\n",
 353                      ],
 354                  ],
 355                  (object)[
 356                      "id" => "2",
 357                      "description" => (object)[
 358                          "en-US" => "Wrong answer\n",
 359                      ],
 360                  ],
 361                  (object)[
 362                      "id" => "1",
 363                      "description" => (object)[
 364                          "en-US" => "This is also a correct answer\n",
 365                      ],
 366                  ],
 367                  (object)[
 368                      "id" => "0",
 369                      "description" => (object)[
 370                          "en-US" => "This is a correct answer\n",
 371                      ],
 372                  ],
 373              ],
 374              "extensions" => (object)[
 375                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 376                  "http://h5p.org/x-api/h5p-subContentId" => "4367a919-ec47-43c9-b521-c22d9c0c0d8d",
 377              ],
 378              "contextExtensions" => (object)[],
 379          ];
 380  
 381          return [
 382              'subcontent' => microtime(),
 383              'description' => 'Select the correct answers',
 384              'correctpattern' => '["1[,]0"]',
 385              'response' => $response,
 386              'additionals' => json_encode($additionals),
 387          ];
 388      }
 389  
 390      /**
 391       * Generates a valid matching tracking result.
 392       *
 393       * @param array $attemptinfo the current attempt information.
 394       * @return array with the required statement data
 395       */
 396      private function get_attempt_result_matching(array $attemptinfo): array {
 397  
 398          $response = ($attemptinfo['rawscore']) ? '0[.]0[,]1[.]1' : '1[.]0[,]0[.]1';
 399  
 400          $additionals = (object)[
 401              "source" => [
 402                  (object)[
 403                      "id" => "0",
 404                      "description" => (object)[
 405                          "en-US" => "Drop item A\n",
 406                      ],
 407                  ],
 408                  (object)[
 409                      "id" => "1",
 410                      "description" => (object)[
 411                          "en-US" => "Drop item B\n",
 412                      ],
 413                  ],
 414              ],
 415              "target" => [
 416                  (object)[
 417                      "id" => "0",
 418                      "description" => (object)[
 419                          "en-US" => "Drop zone A\n",
 420                      ],
 421                  ],
 422                  (object)[
 423                      "id" => "1",
 424                      "description" => (object)[
 425                          "en-US" => "Drop zone B\n",
 426                      ],
 427                  ],
 428              ],
 429              "extensions" => [
 430                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 431                  "http://h5p.org/x-api/h5p-subContentId" => "682f1c74-c819-4e9d-8c36-12d9dc5fcdbc",
 432              ],
 433              "contextExtensions" => (object)[],
 434          ];
 435  
 436          return [
 437              'subcontent' => microtime(),
 438              'description' => 'Drag and Drop example 1',
 439              'correctpattern' => '["0[.]0[,]1[.]1"]',
 440              'response' => $response,
 441              'additionals' => json_encode($additionals),
 442          ];
 443      }
 444  
 445      /**
 446       * Generates a valid fill-in tracking result.
 447       *
 448       * @param array $attemptinfo the current attempt information.
 449       * @return array with the required statement data
 450       */
 451      private function get_attempt_result_fillin(array $attemptinfo): array {
 452  
 453          $response = ($attemptinfo['rawscore']) ? 'first[,]second' : 'something[,]else';
 454  
 455          $additionals = (object)[
 456              "extensions" => (object)[
 457                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 458                  "http://h5p.org/x-api/h5p-subContentId" => "1a3febd5-7edc-4336-8112-12756b945b62",
 459                  "https://h5p.org/x-api/case-sensitivity" => true,
 460                  "https://h5p.org/x-api/alternatives" => [
 461                      ["first"],
 462                      ["second"],
 463                  ],
 464              ],
 465              "contextExtensions" => (object)[
 466                  "https://h5p.org/x-api/h5p-reporting-version" => "1.1.0",
 467              ],
 468          ];
 469  
 470          return [
 471              'subcontent' => microtime(),
 472              'description' => '<p>This an example of missing word text.</p>
 473  
 474                      <p>The first answer if "first": the first answer is __________.</p>
 475  
 476                      <p>The second is second is "second": the secons answer is __________</p>',
 477              'correctpattern' => '["{case_matters=true}first[,]second"]',
 478              'response' => $response,
 479              'additionals' => json_encode($additionals),
 480          ];
 481      }
 482  
 483      /**
 484       * Generates a valid true-false tracking result.
 485       *
 486       * @param array $attemptinfo the current attempt information.
 487       * @return array with the required statement data
 488       */
 489      private function get_attempt_result_truefalse(array $attemptinfo): array {
 490  
 491          $response = ($attemptinfo['rawscore']) ? 'true' : 'false';
 492  
 493          $additionals = (object)[
 494              "extensions" => (object)[
 495                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 496                  "http://h5p.org/x-api/h5p-subContentId" => "5de9fb1e-aa03-4c9a-8cf0-3870b3f012ca",
 497              ],
 498              "contextExtensions" => (object)[],
 499          ];
 500  
 501          return [
 502              'subcontent' => microtime(),
 503              'description' => 'The correct answer is true.',
 504              'correctpattern' => '["true"]',
 505              'response' => $response,
 506              'additionals' => json_encode($additionals),
 507          ];
 508      }
 509  
 510      /**
 511       * Generates a valid long-fill-in tracking result.
 512       *
 513       * @param array $attemptinfo the current attempt information.
 514       * @return array with the required statement data
 515       */
 516      private function get_attempt_result_longfillin(array $attemptinfo): array {
 517  
 518          $response = ($attemptinfo['rawscore']) ? 'The Hobbit is book' : 'Who cares?';
 519  
 520          $additionals = (object)[
 521              "extensions" => (object)[
 522                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 523                  "http://h5p.org/x-api/h5p-subContentId" => "5de9fb1e-aa03-4c9a-8cf0-3870b3f012ca",
 524              ],
 525              "contextExtensions" => (object)[],
 526          ];
 527  
 528          return [
 529              'subcontent' => microtime(),
 530              'description' => '<p>Please describe the novel The Hobbit',
 531              'correctpattern' => '',
 532              'response' => $response,
 533              'additionals' => json_encode($additionals),
 534          ];
 535      }
 536  
 537      /**
 538       * Generates a valid sequencing tracking result.
 539       *
 540       * @param array $attemptinfo the current attempt information.
 541       * @return array with the required statement data
 542       */
 543      private function get_attempt_result_sequencing(array $attemptinfo): array {
 544  
 545          $response = ($attemptinfo['rawscore']) ? 'true' : 'false';
 546  
 547          $additionals = (object)[
 548              "extensions" => (object)[
 549                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 550                  "http://h5p.org/x-api/h5p-subContentId" => "5de9fb1e-aa03-4c9a-8cf0-3870b3f012ca",
 551              ],
 552              "contextExtensions" => (object)[],
 553          ];
 554  
 555          return [
 556              'subcontent' => microtime(),
 557              'description' => 'The correct answer is true.',
 558              'correctpattern' => '["{case_matters=true}first[,]second"]',
 559              'response' => $response,
 560              'additionals' => json_encode($additionals),
 561          ];
 562      }
 563  
 564      /**
 565       * Generates a valid other tracking result.
 566       *
 567       * @param array $attemptinfo the current attempt information.
 568       * @return array with the required statement data
 569       */
 570      private function get_attempt_result_other(array $attemptinfo): array {
 571  
 572          $additionals = (object)[
 573              "extensions" => (object)[
 574                  "http://h5p.org/x-api/h5p-local-content-id" => 1,
 575              ],
 576              "contextExtensions" => (object)[],
 577          ];
 578  
 579          return [
 580              'subcontent' => microtime(),
 581              'description' => '',
 582              'correctpattern' => '',
 583              'response' => '',
 584              'additionals' => json_encode($additionals),
 585          ];
 586      }
 587  }