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.

Differences Between: [Versions 401 and 402] [Versions 401 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  namespace core_adminpresets;
  18  
  19  /**
  20   * Admin presets helper class.
  21   *
  22   * @package    core_adminpresets
  23   * @copyright  2021 Sara Arjona (sara@moodle.com)
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class helper {
  27  
  28      /**
  29       * Create an empty preset.
  30       *
  31       * @param array $data Preset data. Supported values:
  32       *   - name. To define the preset name.
  33       *   - comments. To change the comments field.
  34       *   - author. To update the author field.
  35       *   - iscore. Whether the preset is a core preset or not. Valid values on \core_adminpresets\manager class.
  36       * @return int The identifier of the preset created.
  37       */
  38      public static function create_preset(array $data): int {
  39          global $CFG, $USER, $DB;
  40  
  41          $name = array_key_exists('name', $data) ? $data['name'] : '';
  42          $comments = array_key_exists('comments', $data) ? $data['comments'] : '';
  43          $author = array_key_exists('author', $data) ? $data['author'] : fullname($USER);
  44          $iscore = array_key_exists('iscore', $data) ? $data['iscore'] : manager::NONCORE_PRESET;
  45  
  46          // Validate iscore value.
  47          $allowed = [manager::NONCORE_PRESET, manager::STARTER_PRESET, manager::FULL_PRESET];
  48          if (!in_array($iscore, $allowed)) {
  49              $iscore = manager::NONCORE_PRESET;
  50          }
  51  
  52          $preset = [
  53              'userid' => $USER->id,
  54              'name' => $name,
  55              'comments' => $comments,
  56              'site' => $CFG->wwwroot,
  57              'author' => $author,
  58              'moodleversion' => $CFG->version,
  59              'moodlerelease' => $CFG->release,
  60              'iscore' => $iscore,
  61              'timecreated' => time(),
  62              'timeimported' => 0,
  63          ];
  64  
  65          $presetid = $DB->insert_record('adminpresets', $preset);
  66          return $presetid;
  67      }
  68  
  69      /**
  70       * Helper method to add a setting item to a preset.
  71       *
  72       * @param int $presetid Preset identifier where the item will belong.
  73       * @param string $name Item name.
  74       * @param string $value Item value.
  75       * @param string|null $plugin Item plugin.
  76       * @param string|null $advname If the item is an advanced setting, the name of the advanced setting should be specified here.
  77       * @param string|null $advvalue If the item is an advanced setting, the value of the advanced setting should be specified here.
  78       * @return int The item identificator.
  79       */
  80      public static function add_item(int $presetid, string $name, string $value, ?string $plugin = 'none',
  81              ?string $advname = null, ?string $advvalue = null): int {
  82          global $DB;
  83  
  84          $presetitem = [
  85              'adminpresetid' => $presetid,
  86              'plugin' => $plugin,
  87              'name' => $name,
  88              'value' => $value,
  89          ];
  90          $itemid = $DB->insert_record('adminpresets_it', $presetitem);
  91  
  92          if (!empty($advname)) {
  93              $presetadv = [
  94                  'itemid' => $itemid,
  95                  'name' => $advname,
  96                  'value' => $advvalue,
  97              ];
  98              $DB->insert_record('adminpresets_it_a', $presetadv);
  99          }
 100  
 101          return $itemid;
 102      }
 103  
 104      /**
 105       * Helper method to add a plugin to a preset.
 106       *
 107       * @param int $presetid Preset identifier where the item will belong.
 108       * @param string $plugin Plugin type.
 109       * @param string $name Plugin name.
 110       * @param int $enabled Whether the plugin will be enabled or not.
 111       * @return int The plugin identificator.
 112       */
 113      public static function add_plugin(int $presetid, string $plugin, string $name, int $enabled): int {
 114          global $DB;
 115  
 116          $pluginentry = [
 117              'adminpresetid' => $presetid,
 118              'plugin' => $plugin,
 119              'name' => $name,
 120              'enabled' => $enabled,
 121          ];
 122          $pluginid = $DB->insert_record('adminpresets_plug', $pluginentry);
 123  
 124          return $pluginid;
 125      }
 126  
 127      /**
 128       * Apply the given preset. If it's a filename, the preset will be imported and then applied.
 129       *
 130       * @param string $presetnameorfile The preset name to be applied or a valid preset file to be imported and applied.
 131       * @return int|null The preset identifier that has been applied or null if the given value was not valid.
 132       */
 133      public static function change_default_preset(string $presetnameorfile): ?int {
 134          global $DB;
 135  
 136          $presetid = null;
 137  
 138          // Check if the given variable points to a valid preset file to be imported and applied.
 139          if (is_readable($presetnameorfile)) {
 140              $xmlcontent = file_get_contents($presetnameorfile);
 141              $manager = new manager();
 142              list($xmlnotused, $preset) = $manager->import_preset($xmlcontent);
 143              if (!is_null($preset)) {
 144                  list($applied) = $manager->apply_preset($preset->id);
 145                  if (!empty($applied)) {
 146                      $presetid = $preset->id;
 147                  }
 148              }
 149          } else {
 150              // Check if the given preset exists; if that's the case, it will be applied.
 151              $stringmanager = get_string_manager();
 152              if ($stringmanager->string_exists($presetnameorfile . 'preset', 'core_adminpresets')) {
 153                  $params = ['name' => get_string($presetnameorfile . 'preset', 'core_adminpresets')];
 154              } else {
 155                  $params = ['name' => $presetnameorfile];
 156              }
 157              if ($preset = $DB->get_record('adminpresets', $params)) {
 158                  $manager = new manager();
 159                  list($applied) = $manager->apply_preset($preset->id);
 160                  if (!empty($applied)) {
 161                      $presetid = $preset->id;
 162                  }
 163              }
 164          }
 165  
 166          return $presetid;
 167      }
 168  
 169      /**
 170       * Helper method to create default site admin presets and initialize them.
 171       */
 172      public static function create_default_presets(): void {
 173          // Create the "Starter" site admin preset.
 174          $data = [
 175              'name' => get_string('starterpreset', 'core_adminpresets'),
 176              'comments' => get_string('starterpresetdescription', 'core_adminpresets'),
 177              'iscore' => manager::STARTER_PRESET,
 178          ];
 179          $presetid = static::create_preset($data);
 180  
 181          // Add settings to the "Starter" site admin preset.
 182          static::add_item($presetid, 'usecomments', '0');
 183          static::add_item($presetid, 'usetags', '0');
 184          static::add_item($presetid, 'enablenotes', '0');
 185          static::add_item($presetid, 'enableblogs', '0');
 186          static::add_item($presetid, 'enablebadges', '0');
 187          static::add_item($presetid, 'enableanalytics', '0');
 188          static::add_item($presetid, 'enabled', '0', 'core_competency');
 189          static::add_item($presetid, 'pushcourseratingstouserplans', '0', 'core_competency');
 190          static::add_item($presetid, 'showdataretentionsummary', '0', 'tool_dataprivacy');
 191          static::add_item($presetid, 'forum_maxattachments', '3');
 192          static::add_item($presetid, 'guestloginbutton', '0');
 193          // Set Activity chooser tabs to "Starred, All, Recommended".
 194          static::add_item($presetid, 'activitychoosertabmode', '1');
 195  
 196          // Modules: Hide chat, database, external tool (lti), IMS content package (imscp), lesson, SCORM, survey, wiki, workshop.
 197          static::add_plugin($presetid, 'mod', 'chat', false);
 198          static::add_plugin($presetid, 'mod', 'data', false);
 199          static::add_plugin($presetid, 'mod', 'lti', false);
 200          static::add_plugin($presetid, 'mod', 'imscp', false);
 201          static::add_plugin($presetid, 'mod', 'lesson', false);
 202          static::add_plugin($presetid, 'mod', 'scorm', false);
 203          static::add_plugin($presetid, 'mod', 'survey', false);
 204          static::add_plugin($presetid, 'mod', 'wiki', false);
 205          static::add_plugin($presetid, 'mod', 'workshop', false);
 206  
 207          // Availability restrictions: Hide Grouping, User profile.
 208          static::add_plugin($presetid, 'availability', 'grouping', false);
 209          static::add_plugin($presetid, 'availability', 'profile', false);
 210  
 211          // Blocks: Hide Activities, Blog menu, Blog tags, Comments, Course completion status, Courses, Flickr,
 212          // Global search, Latest badges, Learning plans, Logged in user, Login, Main menu, Mentees, Network servers, Online users,
 213          // Private files, Recent blog entries, Recently accessed courses, Search forums, Section links, Social activities,
 214          // Starred courses, Tags, YouTube.
 215          // Hidden by default: Course/site summary, RSS feeds, Self completion, Feedback.
 216          static::add_plugin($presetid, 'block', 'activity_modules', false);
 217          static::add_plugin($presetid, 'block', 'blog_menu', false);
 218          static::add_plugin($presetid, 'block', 'blog_tags', false);
 219          static::add_plugin($presetid, 'block', 'comments', false);
 220          static::add_plugin($presetid, 'block', 'completionstatus', false);
 221          static::add_plugin($presetid, 'block', 'course_summary', false);
 222          static::add_plugin($presetid, 'block', 'course_list', false);
 223          static::add_plugin($presetid, 'block', 'tag_flickr', false);
 224          static::add_plugin($presetid, 'block', 'globalsearch', false);
 225          static::add_plugin($presetid, 'block', 'badges', false);
 226          static::add_plugin($presetid, 'block', 'lp', false);
 227          static::add_plugin($presetid, 'block', 'myprofile', false);
 228          static::add_plugin($presetid, 'block', 'login', false);
 229          static::add_plugin($presetid, 'block', 'site_main_menu', false);
 230          static::add_plugin($presetid, 'block', 'mentees', false);
 231          static::add_plugin($presetid, 'block', 'mnet_hosts', false);
 232          static::add_plugin($presetid, 'block', 'private_files', false);
 233          static::add_plugin($presetid, 'block', 'blog_recent', false);
 234          static::add_plugin($presetid, 'block', 'rss_client', false);
 235          static::add_plugin($presetid, 'block', 'search_forums', false);
 236          static::add_plugin($presetid, 'block', 'section_links', false);
 237          static::add_plugin($presetid, 'block', 'selfcompletion', false);
 238          static::add_plugin($presetid, 'block', 'social_activities', false);
 239          static::add_plugin($presetid, 'block', 'tags', false);
 240          static::add_plugin($presetid, 'block', 'tag_youtube', false);
 241          static::add_plugin($presetid, 'block', 'feedback', false);
 242          static::add_plugin($presetid, 'block', 'online_users', false);
 243          static::add_plugin($presetid, 'block', 'recentlyaccessedcourses', false);
 244          static::add_plugin($presetid, 'block', 'starredcourses', false);
 245  
 246          // Course formats: Disable Social format.
 247          static::add_plugin($presetid, 'format', 'social', false);
 248  
 249          // Data formats: Disable Javascript Object Notation (.json).
 250          static::add_plugin($presetid, 'dataformat', 'json', false);
 251  
 252          // Enrolments: Disable Cohort sync, Guest access.
 253          static::add_plugin($presetid, 'enrol', 'cohort', false);
 254          static::add_plugin($presetid, 'enrol', 'guest', false);
 255  
 256          // Filter: Disable MathJax, Activity names auto-linking.
 257          static::add_plugin($presetid, 'filter', 'mathjaxloader', TEXTFILTER_DISABLED);
 258          static::add_plugin($presetid, 'filter', 'activitynames', TEXTFILTER_DISABLED);
 259  
 260          // Question behaviours: Disable Adaptive mode (no penalties), Deferred feedback with CBM, Immediate feedback with CBM.
 261          static::add_plugin($presetid, 'qbehaviour', 'adaptivenopenalty', false);
 262          static::add_plugin($presetid, 'qbehaviour', 'deferredcbm', false);
 263          static::add_plugin($presetid, 'qbehaviour', 'immediatecbm', false);
 264  
 265          // Question types: Disable Calculated, Calculated multichoice, Calculated simple, Drag and drop markers,
 266          // Drag and drop onto image, Embedded answers (Cloze), Numerical, Random short-answer matching.
 267          static::add_plugin($presetid, 'qtype', 'calculated', false);
 268          static::add_plugin($presetid, 'qtype', 'calculatedmulti', false);
 269          static::add_plugin($presetid, 'qtype', 'calculatedsimple', false);
 270          static::add_plugin($presetid, 'qtype', 'ddmarker', false);
 271          static::add_plugin($presetid, 'qtype', 'ddimageortext', false);
 272          static::add_plugin($presetid, 'qtype', 'multianswer', false);
 273          static::add_plugin($presetid, 'qtype', 'numerical', false);
 274          static::add_plugin($presetid, 'qtype', 'randomsamatch', false);
 275  
 276          // Repositories: Disable Server files, URL downloader, Wikimedia.
 277          static::add_plugin($presetid, 'repository', 'local', false);
 278          static::add_plugin($presetid, 'repository', 'url', false);
 279          static::add_plugin($presetid, 'repository', 'wikimedia', false);
 280  
 281          // Text editors: Disable TinyMCE HTML editor.
 282          static::add_plugin($presetid, 'editor', 'tinymce', false);
 283  
 284          // Create the "Full" site admin preset.
 285          $data = [
 286              'name' => get_string('fullpreset', 'core_adminpresets'),
 287              'comments' => get_string('fullpresetdescription', 'core_adminpresets'),
 288              'iscore' => manager::FULL_PRESET,
 289          ];
 290          $presetid = static::create_preset($data);
 291  
 292          // Add settings to the "Full" site admin preset.
 293          static::add_item($presetid, 'usecomments', '1');
 294          static::add_item($presetid, 'usetags', '1');
 295          static::add_item($presetid, 'enablenotes', '1');
 296          static::add_item($presetid, 'enableblogs', '1');
 297          static::add_item($presetid, 'enablebadges', '1');
 298          static::add_item($presetid, 'enableanalytics', '1');
 299          static::add_item($presetid, 'enabled', '1', 'core_competency');
 300          static::add_item($presetid, 'pushcourseratingstouserplans', '1', 'core_competency');
 301          static::add_item($presetid, 'showdataretentionsummary', '1', 'tool_dataprivacy');
 302          static::add_item($presetid, 'forum_maxattachments', '9');
 303          static::add_item($presetid, 'guestloginbutton', '1');
 304          // Set Activity chooser tabs to the default value ("Starred, All, Activities, Resources, Recommended").
 305          static::add_item($presetid, 'activitychoosertabmode', '0');
 306  
 307          // Modules: Enable chat, database, external tool (lti), IMS content package (imscp), lesson, SCORM, survey, wiki, workshop.
 308          static::add_plugin($presetid, 'mod', 'chat', true);
 309          static::add_plugin($presetid, 'mod', 'data', true);
 310          static::add_plugin($presetid, 'mod', 'lti', true);
 311          static::add_plugin($presetid, 'mod', 'imscp', true);
 312          static::add_plugin($presetid, 'mod', 'lesson', true);
 313          static::add_plugin($presetid, 'mod', 'scorm', true);
 314          static::add_plugin($presetid, 'mod', 'survey', true);
 315          static::add_plugin($presetid, 'mod', 'wiki', true);
 316          static::add_plugin($presetid, 'mod', 'workshop', true);
 317  
 318          // Availability restrictions: Enable Grouping, User profile.
 319          static::add_plugin($presetid, 'availability', 'grouping', true);
 320          static::add_plugin($presetid, 'availability', 'profile', true);
 321  
 322          // Blocks: Enable Activities, Blog menu, Blog tags, Comments, Course completion status, Courses, Flickr,
 323          // Global search, Latest badges, Learning plans, Logged in user, Login, Main menu, Mentees, Network servers, Online users,
 324          // Private files, Recent blog entries, Recently accessed courses, Search forums, Section links, Social activities,
 325          // Starred courses, Tags, YouTube.
 326          // Hidden by default: Course/site summary, RSS feeds, Self completion, Feedback.
 327          static::add_plugin($presetid, 'block', 'activity_modules', true);
 328          static::add_plugin($presetid, 'block', 'blog_menu', true);
 329          static::add_plugin($presetid, 'block', 'blog_tags', true);
 330          static::add_plugin($presetid, 'block', 'comments', true);
 331          static::add_plugin($presetid, 'block', 'completionstatus', true);
 332          static::add_plugin($presetid, 'block', 'course_list', true);
 333          static::add_plugin($presetid, 'block', 'tag_flickr', true);
 334          static::add_plugin($presetid, 'block', 'globalsearch', true);
 335          static::add_plugin($presetid, 'block', 'badges', true);
 336          static::add_plugin($presetid, 'block', 'lp', true);
 337          static::add_plugin($presetid, 'block', 'myprofile', true);
 338          static::add_plugin($presetid, 'block', 'login', true);
 339          static::add_plugin($presetid, 'block', 'site_main_menu', true);
 340          static::add_plugin($presetid, 'block', 'mentees', true);
 341          static::add_plugin($presetid, 'block', 'mnet_hosts', true);
 342          static::add_plugin($presetid, 'block', 'private_files', true);
 343          static::add_plugin($presetid, 'block', 'blog_recent', true);
 344          static::add_plugin($presetid, 'block', 'search_forums', true);
 345          static::add_plugin($presetid, 'block', 'section_links', true);
 346          static::add_plugin($presetid, 'block', 'social_activities', true);
 347          static::add_plugin($presetid, 'block', 'tags', true);
 348          static::add_plugin($presetid, 'block', 'online_users', true);
 349          static::add_plugin($presetid, 'block', 'recentlyaccessedcourses', true);
 350          static::add_plugin($presetid, 'block', 'starredcourses', true);
 351  
 352          // Course formats: Enable Social format.
 353          static::add_plugin($presetid, 'format', 'social', true);
 354  
 355          // Data formats: Enable Javascript Object Notation (.json).
 356          static::add_plugin($presetid, 'dataformat', 'json', true);
 357  
 358          // Enrolments: Enable Cohort sync, Guest access.
 359          static::add_plugin($presetid, 'enrol', 'cohort', true);
 360          static::add_plugin($presetid, 'enrol', 'guest', true);
 361  
 362          // Filter: Enable MathJax, Activity names auto-linking.
 363          static::add_plugin($presetid, 'filter', 'mathjaxloader', TEXTFILTER_ON);
 364          static::add_plugin($presetid, 'filter', 'activitynames', TEXTFILTER_ON);
 365  
 366          // Question behaviours: Enable Adaptive mode (no penalties), Deferred feedback with CBM, Immediate feedback with CBM.
 367          static::add_plugin($presetid, 'qbehaviour', 'adaptivenopenalty', true);
 368          static::add_plugin($presetid, 'qbehaviour', 'deferredcbm', true);
 369          static::add_plugin($presetid, 'qbehaviour', 'immediatecbm', true);
 370  
 371          // Question types: Enable Calculated, Calculated multichoice, Calculated simple, Drag and drop markers,
 372          // Drag and drop onto image, Embedded answers (Cloze), Numerical, Random short-answer matching.
 373          static::add_plugin($presetid, 'qtype', 'calculated', true);
 374          static::add_plugin($presetid, 'qtype', 'calculatedmulti', true);
 375          static::add_plugin($presetid, 'qtype', 'calculatedsimple', true);
 376          static::add_plugin($presetid, 'qtype', 'ddmarker', true);
 377          static::add_plugin($presetid, 'qtype', 'ddimageortext', true);
 378          static::add_plugin($presetid, 'qtype', 'multianswer', true);
 379          static::add_plugin($presetid, 'qtype', 'numerical', true);
 380          static::add_plugin($presetid, 'qtype', 'randomsamatch', true);
 381  
 382          // Repositories: Enable Server files, URL downloader, Wikimedia.
 383          static::add_plugin($presetid, 'repository', 'local', true);
 384          static::add_plugin($presetid, 'repository', 'url', true);
 385          static::add_plugin($presetid, 'repository', 'wikimedia', true);
 386  
 387          // Text editors: Enable TinyMCE HTML editor.
 388          static::add_plugin($presetid, 'editor', 'tinymce', true);
 389      }
 390  }