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] [Versions 39 and 310]

   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_resource
  21   * @copyright 2013 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * Resource module data generator class.
  30   *
  31   * @package    mod_resource
  32   * @copyright 2013 The Open University
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class mod_resource_generator extends testing_module_generator {
  36  
  37      /**
  38       * Creates new resource module instance. By default it contains a short
  39       * text file.
  40       *
  41       * @param array|stdClass $record data for module being generated. Requires 'course' key
  42       *     (an id or the full object). Also can have any fields from add module form.
  43       * @param null|array $options general options for course module. Since 2.6 it is
  44       *     possible to omit this argument by merging options into $record
  45       * @return stdClass record from module-defined table with additional field
  46       *     cmid (corresponding id in course_modules table)
  47       */
  48      public function create_instance($record = null, array $options = null) {
  49          global $CFG, $USER;
  50          require_once($CFG->dirroot . '/lib/resourcelib.php');
  51          // Ensure the record can be modified without affecting calling code.
  52          $record = (object)(array)$record;
  53  
  54          // Fill in optional values if not specified.
  55          if (!isset($record->display)) {
  56              $record->display = RESOURCELIB_DISPLAY_AUTO;
  57          }
  58          if (!isset($record->printintro)) {
  59              $record->printintro = 0;
  60          }
  61          if (!isset($record->showsize)) {
  62              $record->showsize = 0;
  63          }
  64          if (!isset($record->showtype)) {
  65              $record->showtype = 0;
  66          }
  67  
  68          // The 'files' value corresponds to the draft file area ID. If not
  69          // specified, create a default file.
  70          if (!isset($record->files)) {
  71              if (empty($USER->username) || $USER->username === 'guest') {
  72                  throw new coding_exception('resource generator requires a current user');
  73              }
  74              $usercontext = context_user::instance($USER->id);
  75  
  76              // Pick a random context id for specified user.
  77              $record->files = file_get_unused_draft_itemid();
  78  
  79              // Add actual file there.
  80              $filerecord = array('component' => 'user', 'filearea' => 'draft',
  81                      'contextid' => $usercontext->id, 'itemid' => $record->files,
  82                      'filename' => 'resource' . ($this->instancecount+1) . '.txt', 'filepath' => '/');
  83              $fs = get_file_storage();
  84              $fs->create_file_from_string($filerecord, 'Test resource ' . ($this->instancecount+1) . ' file');
  85          }
  86  
  87          // Do work to actually add the instance.
  88          return parent::create_instance($record, (array)$options);
  89      }
  90  }