Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Generator for the core_contentbank subsystem.
  19   *
  20   * @package    core_contentbank
  21   * @category   test
  22   * @copyright  2020 Sara Arjona <sara@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
  29  
  30  /**
  31   * Generator for the core_contentbank subsystem.
  32   *
  33   * @package    core_contentbank
  34   * @copyright  2020 Sara Arjona <sara@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_contentbank_generator extends \component_generator_base {
  38  
  39      /**
  40       * Populate contentbank database tables with relevant data to simulate the process of adding items to the content bank.
  41       *
  42       * @param string $contenttype Content bank plugin type to add. If none is defined, contenttype_testable is used.
  43       * @param int $itemstocreate Number of items to add to the content bank.
  44       * @param int $userid The user identifier creating the content.
  45       * @param context $context The context where the content will be created.
  46       * @param bool $convert2class Whether the class should return stdClass or plugin instance.
  47       * @param string $filepath The filepath of the file associated to the content to create.
  48       * @param string $contentname The name of the content that will be created.
  49       * @return array An array with all the records added to the content bank.
  50       */
  51      public function generate_contentbank_data(?string $contenttype, int $itemstocreate = 1, int $userid = 0,
  52              ?\context $context = null, bool $convert2class = true, string $filepath = 'contentfile.h5p',
  53              string $contentname = 'Test content '): array {
  54          global $DB, $USER;
  55  
  56          $records = [];
  57  
  58          $contenttype = $contenttype ?? 'contenttype_testable';
  59          $contenttypeclass = "\\$contenttype\\contenttype";
  60          if (!class_exists($contenttypeclass)) {
  61              // Early return with empty array because the contenttype doesn't exist.
  62              return $records;
  63          }
  64          if (empty($context)) {
  65              $context = \context_system::instance();
  66          }
  67          $type = new $contenttypeclass($context);
  68          $fs = get_file_storage();
  69          for ($i = 0; $i < $itemstocreate; $i++) {
  70              // Create content.
  71              $record = new stdClass();
  72              // If only 1 item is being created, do not add a number suffix to the content name.
  73              $record->name = ($itemstocreate === 1) ? $contentname : $contentname . $i;
  74              $record->configdata = '';
  75              $record->usercreated = $userid ?? $USER->id;
  76  
  77              $content = $type->create_content($record);
  78              $record = $content->get_content();
  79  
  80              // Create a dummy file.
  81              $filerecord = array(
  82                  'contextid' => $context->id,
  83                  'component' => 'contentbank',
  84                  'filearea' => 'public',
  85                  'itemid' => $record->id,
  86                  'filepath' => '/',
  87                  'filename' => basename($filepath)
  88              );
  89              if (file_exists($filepath)) {
  90                  $fs->create_file_from_pathname($filerecord, $filepath);
  91              } else {
  92                  $fs->create_file_from_string($filerecord, 'Dummy content ' . $i);
  93              }
  94  
  95              // Prepare the return value.
  96              if ($convert2class) {
  97                  $records[$record->id] = $content;
  98              } else {
  99                  $records[$record->id] = $record;
 100              }
 101          }
 102  
 103          return $records;
 104      }
 105  }