[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/glossary/tests/generator/ -> lib.php (source)

   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, $USER, $CFG;
 111          $this->entrycount++;
 112          $now = time();
 113          $record = (array)$record + array(
 114              'glossaryid' => $glossary->id,
 115              'timecreated' => $now,
 116              'timemodified' => $now,
 117              'userid' => $USER->id,
 118              'concept' => 'Glossary entry '.$this->entrycount,
 119              'definition' => 'Definition of glossary entry '.$this->entrycount,
 120              'definitionformat' => FORMAT_MOODLE,
 121              'definitiontrust' => 0,
 122              'usedynalink' => $CFG->glossary_linkentries,
 123              'casesensitive' => $CFG->glossary_casesensitive,
 124              'fullmatch' => $CFG->glossary_fullmatch
 125          );
 126          if (!isset($record['teacherentry']) || !isset($record['approved'])) {
 127              $context = context_module::instance($glossary->cmid);
 128              if (!isset($record['teacherentry'])) {
 129                  $record['teacherentry'] = has_capability('mod/glossary:manageentries', $context, $record['userid']);
 130              }
 131              if (!isset($record['approved'])) {
 132                  $defaultapproval = $glossary->defaultapproval;
 133                  $record['approved'] = ($defaultapproval || has_capability('mod/glossary:approve', $context));
 134              }
 135          }
 136  
 137          $id = $DB->insert_record('glossary_entries', $record);
 138  
 139          if ($aliases) {
 140              foreach ($aliases as $alias) {
 141                  $ar = new stdClass();
 142                  $ar->entryid = $id;
 143                  $ar->alias = $alias;
 144                  $DB->insert_record('glossary_alias', $ar);
 145              }
 146          }
 147  
 148          return $DB->get_record('glossary_entries', array('id' => $id), '*', MUST_EXIST);
 149      }
 150  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1