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  namespace tool_admin_presets\local\action;
  18  
  19  use moodle_exception;
  20  use stdClass;
  21  use tool_admin_presets\form\continue_form;
  22  use tool_admin_presets\form\load_form;
  23  use tool_admin_presets\output\presets_list;
  24  
  25  /**
  26   * This class extends base class and handles load function.
  27   *
  28   * @package          tool_admin_presets
  29   * @copyright        2021 Pimenko <support@pimenko.com><pimenko.com>
  30   * @author           Jordan Kesraoui | Sylvain Revenu | Pimenko based on David MonllaĆ³ <david.monllao@urv.cat> code
  31   * @license          http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class load extends base {
  34  
  35      /**
  36       * Executes the settings load into the system
  37       */
  38      public function execute(): void {
  39          global $OUTPUT;
  40  
  41          confirm_sesskey();
  42  
  43          $url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
  44          $this->moodleform = new load_form($url);
  45  
  46          if ($this->moodleform->is_cancelled()) {
  47              redirect(new \moodle_url('/admin/tool/admin_presets/index.php?action=base'));
  48          }
  49  
  50          if ($this->moodleform->is_submitted() && $this->moodleform->is_validated() && ($this->moodleform->get_data())) {
  51              // Apply preset settings and set plugins visibility.
  52              [$applied, $skipped] = $this->manager->apply_preset($this->id);
  53  
  54              if (empty($applied)) {
  55                  $message = [
  56                      'message' => get_string('nothingloaded', 'tool_admin_presets'),
  57                      'closebutton' => true,
  58                      'announce' => true,
  59                  ];
  60              } else {
  61                  $message = [
  62                      'message' => get_string('settingsappliednotification', 'tool_admin_presets'),
  63                      'closebutton' => true,
  64                      'announce' => true,
  65                  ];
  66              }
  67              $application = new stdClass();
  68              $applieddata = new stdClass();
  69              $applieddata->show = !empty($applied);
  70              $applieddata->message = $message;
  71              $applieddata->heading = get_string('settingsapplied', 'tool_admin_presets');
  72              $applieddata->caption = get_string('settingsapplied', 'tool_admin_presets');
  73              $applieddata->settings = $applied;
  74              $application->appliedchanges = $applieddata;
  75  
  76              $skippeddata = new stdClass();
  77              $skippeddata->show = !empty($skipped);
  78              $skippeddata->heading = get_string('settingsnotapplied', 'tool_admin_presets');
  79              $skippeddata->caption = get_string('settingsnotapplicable', 'tool_admin_presets');
  80              $skippeddata->settings = $skipped;
  81              $application->skippedchanges = $skippeddata;
  82  
  83              $this->outputs = $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);
  84              $url = new \moodle_url('/admin/tool/admin_presets/index.php');
  85              $this->moodleform = new continue_form($url);
  86          }
  87      }
  88  
  89      /**
  90       * Displays the select preset settings to select what to import.
  91       * Loads the preset data and displays a settings tree.
  92       *
  93       * It checks the Moodle version and it only allows users to import
  94       * the preset available settings.
  95       */
  96      public function show(): void {
  97          $this->display_preset(true);
  98      }
  99  
 100      /**
 101       * Displays a preset information (name, description, settings different from the current configuration...).
 102       */
 103      public function preview(): void {
 104          $this->display_preset(false, false);
 105      }
 106  
 107      /**
 108       * Method to prepare the information to preview/load the preset.
 109       *
 110       * @param bool $displayform Whether the form should be displayed in the page or not.
 111       * @param bool $raiseexception Whether the exception should be raised or not when the preset doesn't exist. When it's set
 112       * to false, a message is displayed, instead of raising the exception.
 113       */
 114      protected function display_preset(bool $displayform = true, bool $raiseexception = true) {
 115          global $DB, $OUTPUT;
 116  
 117          $data = new stdClass();
 118          $data->id = $this->id;
 119  
 120          // Preset data.
 121          if (!$preset = $DB->get_record('adminpresets', ['id' => $data->id])) {
 122              if ($raiseexception) {
 123                  throw new moodle_exception('errornopreset', 'core_adminpresets');
 124              } else {
 125                  $this->outputs = get_string('errornopreset', 'core_adminpresets');
 126                  return;
 127              }
 128          }
 129  
 130          // Print preset basic data.
 131          $list = new presets_list([$preset]);
 132          $this->outputs = $OUTPUT->render($list);
 133  
 134          // Simulate preset application to display settings and plugins that will change.
 135          [$applied] = $this->manager->apply_preset($this->id, true);
 136  
 137          // Order the applied array by the visiblename column.
 138          if (!empty($applied)) {
 139              $visiblenamecolumn = array_column($applied, 'visiblename');
 140              array_multisort($visiblenamecolumn, SORT_ASC, $applied);
 141          }
 142  
 143          $application = new stdClass();
 144          $applieddata = new stdClass();
 145          $applieddata->show = !empty($applied);
 146          $applieddata->heading = get_string('settingstobeapplied', 'tool_admin_presets');
 147          $applieddata->caption = get_string('settingsapplied', 'tool_admin_presets');
 148          $applieddata->settings = $applied;
 149          $applieddata->beforeapplying = true;
 150          $application->appliedchanges = $applieddata;
 151          if ($displayform) {
 152              if (empty($applied)) {
 153                  // Display a warning when no settings will be applied.
 154                  $applieddata->message = get_string('nosettingswillbeapplied', 'tool_admin_presets');
 155  
 156                  // Only display the Continue button.
 157                  $url = new \moodle_url('/admin/tool/admin_presets/index.php');
 158                  $this->moodleform = new continue_form($url);
 159              } else {
 160                  // Display the form to apply the preset.
 161                  $url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
 162                  $this->moodleform = new load_form($url);
 163                  $this->moodleform->set_data($data);
 164              }
 165          }
 166  
 167          $this->outputs .= $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);
 168      }
 169  
 170      protected function get_explanatory_description(): ?string {
 171          $text = null;
 172          if ($this->mode == 'show') {
 173              $text = get_string('loaddescription', 'tool_admin_presets');
 174          }
 175  
 176          return $text;
 177      }
 178  }