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.
   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   * mod_glossary data generator.
  19   *
  20   * @package    mod_glossary
  21   * @category   test
  22   * @copyright  2013 Marina Glancy
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * mod_glossary data generator class.
  30   *
  31   * @package    mod_glossary
  32   * @category   test
  33   * @copyright  2013 Marina Glancy
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mod_glossary_generator extends testing_module_generator {
  37  
  38      /**
  39       * @var int keep track of how many entries have been created.
  40       */
  41      protected $entrycount = 0;
  42  
  43      /**
  44       * @var int keep track of how many entries have been created.
  45       */
  46      protected $categorycount = 0;
  47  
  48      /**
  49       * To be called from data reset code only,
  50       * do not use in tests.
  51       * @return void
  52       */
  53      public function reset() {
  54          $this->entrycount = 0;
  55          $this->categorycount = 0;
  56          parent::reset();
  57      }
  58  
  59      public function create_instance($record = null, array $options = null) {
  60          global $CFG;
  61  
  62          // Add default values for glossary.
  63          $record = (array)$record + array(
  64              'globalglossary' => 0,
  65              'mainglossary' => 0,
  66              'defaultapproval' => $CFG->glossary_defaultapproval,
  67              'allowduplicatedentries' => $CFG->glossary_dupentries,
  68              'allowcomments' => $CFG->glossary_allowcomments,
  69              'usedynalink' => $CFG->glossary_linkbydefault,
  70              'displayformat' => 'dictionary',
  71              'approvaldisplayformat' => 'default',
  72              'entbypage' => !empty($CFG->glossary_entbypage) ? $CFG->glossary_entbypage : 10,
  73              'showalphabet' => 1,
  74              'showall' => 1,
  75              'showspecial' => 1,
  76              'allowprintview' => 1,
  77              'rsstype' => 0,
  78              'rssarticles' => 0,
  79              'grade' => 100,
  80              'assessed' => 0,
  81          );
  82  
  83          return parent::create_instance($record, (array)$options);
  84      }
  85  
  86      public function create_category($glossary, $record = array(), $entries = array()) {
  87          global $CFG, $DB;
  88          $this->categorycount++;
  89          $record = (array)$record + array(
  90              'name' => 'Glossary category '.$this->categorycount,
  91              'usedynalink' => $CFG->glossary_linkbydefault,
  92          );
  93          $record['glossaryid'] = $glossary->id;
  94  
  95          $id = $DB->insert_record('glossary_categories', $record);
  96  
  97          if ($entries) {
  98              foreach ($entries as $entry) {
  99                  $ce = new stdClass();
 100                  $ce->categoryid = $id;
 101                  $ce->entryid = $entry->id;
 102                  $DB->insert_record('glossary_entries_categories', $ce);
 103              }
 104          }
 105  
 106          return $DB->get_record('glossary_categories', array('id' => $id), '*', MUST_EXIST);
 107      }
 108  
 109      public function create_content($glossary, $record = array(), $aliases = array()) {
 110          global $DB;
 111  
 112          $entry = $this->create_entry((array)$record + ['glossaryid' => $glossary->id]);
 113  
 114          if ($aliases) {
 115              foreach ($aliases as $alias) {
 116                  $ar = new stdClass();
 117                  $ar->entryid = $entry->id;
 118                  $ar->alias = $alias;
 119                  $DB->insert_record('glossary_alias', $ar);
 120              }
 121          }
 122  
 123          if (array_key_exists('tags', $record)) {
 124              $tags = is_array($record['tags']) ? $record['tags'] : preg_split('/,/', $record['tags']);
 125  
 126              core_tag_tag::set_item_tags('mod_glossary', 'glossary_entries', $entry->id,
 127                  context_module::instance($glossary->cmid), $tags);
 128          }
 129  
 130          return $entry;
 131      }
 132  
 133      /**
 134       * Create an entry.
 135       *
 136       * @param array $data Data to create the entry record.
 137       *        In addition to columns in the entry table, the following attributes are supported:
 138       *         - glossaryid (required): Id of the glossary where the entry will be created.
 139       *         - categoryids: Array of ids for the categories this entry belongs to.
 140       *
 141       * @return stdClass Entry record.
 142       */
 143      public function create_entry(array $data): stdClass {
 144          global $DB, $USER, $CFG;
 145  
 146          // Prepare glossary.
 147          $coursemodule = get_coursemodule_from_instance('glossary', $data['glossaryid']);
 148          $glossary = $DB->get_record('glossary', ['id' => $data['glossaryid']], '*', MUST_EXIST);
 149          $glossary->cmid = $coursemodule->id;
 150  
 151          unset($data['glossaryid']);
 152  
 153          // Prepare category ids.
 154          $categoryids = $data['categoryids'] ?? [];
 155  
 156          unset($data['categoryids']);
 157  
 158          // Create entry.
 159          $this->entrycount++;
 160          $now = time();
 161          $record = $data + [
 162              'glossaryid' => $glossary->id,
 163              'timecreated' => $now,
 164              'timemodified' => $now,
 165              'userid' => $USER->id,
 166              'concept' => 'Glossary entry '.$this->entrycount,
 167              'definition' => 'Definition of glossary entry '.$this->entrycount,
 168              'definitionformat' => FORMAT_MOODLE,
 169              'definitiontrust' => 0,
 170              'usedynalink' => $CFG->glossary_linkentries,
 171              'casesensitive' => $CFG->glossary_casesensitive,
 172              'fullmatch' => $CFG->glossary_fullmatch
 173          ];
 174          if (!isset($record['teacherentry']) || !isset($record['approved'])) {
 175              $context = context_module::instance($glossary->cmid);
 176              if (!isset($record['teacherentry'])) {
 177                  $record['teacherentry'] = has_capability('mod/glossary:manageentries', $context, $record['userid']);
 178              }
 179              if (!isset($record['approved'])) {
 180                  $defaultapproval = $glossary->defaultapproval;
 181                  $record['approved'] = ($defaultapproval || has_capability('mod/glossary:approve', $context));
 182              }
 183          }
 184  
 185          $id = $DB->insert_record('glossary_entries', $record);
 186  
 187          foreach ($categoryids as $categoryid) {
 188              $DB->insert_record('glossary_entries_categories', ['entryid' => $id, 'categoryid' => $categoryid]);
 189          }
 190  
 191          return $DB->get_record('glossary_entries', array('id' => $id), '*', MUST_EXIST);
 192      }
 193  }