Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * core_notes data generator.
  19   *
  20   * @package    core_notes
  21   * @category   test
  22   * @copyright  2013 Ankit Agarwal
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * core_notes data generator class.
  30   *
  31   * @package    core_notes
  32   * @category   test
  33   * @copyright  2013 Ankit Agarwal
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class core_notes_generator extends component_generator_base {
  37  
  38      /**
  39       * @var number of created instances
  40       */
  41      protected $instancecount = 0;
  42  
  43      /**
  44       * To be called from data reset code only,
  45       * do not use in tests.
  46       * @return void
  47       */
  48      public function reset() {
  49          $this->instancecount = 0;
  50      }
  51  
  52      /**
  53       * Create a new note.
  54       *
  55       * @param array|stdClass $record
  56       * @throws coding_exception
  57       * @return stdClass activity record with extra cmid field
  58       */
  59      public function create_instance($record = null) {
  60          global $CFG, $USER;
  61          require_once("$CFG->dirroot/notes/lib.php");
  62  
  63          $this->instancecount++;
  64          $i = $this->instancecount;
  65          $record = (object)(array)$record;
  66  
  67          if (empty($record->courseid)) {
  68              throw new coding_exception('Module generator requires $record->courseid.');
  69          }
  70          if (empty($record->userid)) {
  71              throw new coding_exception('Module generator requires $record->userid.');
  72          }
  73          if (!isset($record->module)) {
  74              $record->module = 'notes';
  75          }
  76          if (!isset($record->groupid)) {
  77              $record->groupid = 0;
  78          }
  79          if (!isset($record->moduleid)) {
  80              $record->moduleid = 0;
  81          }
  82          if (!isset($record->coursemoduleid)) {
  83              $record->coursemoduleid = 0;
  84          }
  85          if (!isset($record->subject)) {
  86              $record->subject = '';
  87          }
  88          if (!isset($record->summary)) {
  89              $record->summary = null;
  90          }
  91          if (!isset($record->content)) {
  92              $record->content = "This is test generated note - $i .";
  93          }
  94          if (!isset($record->uniquehash)) {
  95              $record->uniquehash = '';
  96          }
  97          if (!isset($record->rating)) {
  98              $record->rating = 0;
  99          }
 100          if (!isset($record->format)) {
 101              $record->format = FORMAT_PLAIN;
 102          }
 103          if (!isset($record->summaryformat)) {
 104              $record->summaryformat = FORMAT_MOODLE;
 105          }
 106          if (!isset($record->attachment)) {
 107              $record->attachment = null;
 108          }
 109          if (!isset($record->publishstate)) {
 110              $record->publishstate = NOTES_STATE_SITE;
 111          }
 112          if (!isset($record->lastmodified)) {
 113              $record->lastmodified = time();
 114          }
 115          if (!isset($record->created)) {
 116              $record->created = time();
 117          }
 118          if (!isset($record->usermodified)) {
 119              $record->usermodified = $USER->id;
 120          }
 121  
 122          note_save($record);
 123          return $record;
 124      }
 125  
 126  }
 127