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   * tool_admin_presets specific renderers
  19   *
  20   * @package   tool_admin_presets
  21   * @copyright  2021 Amaia Anabitarte <amaia@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_admin_presets\output;
  25  
  26  use core_adminpresets\manager;
  27  use renderable;
  28  use templatable;
  29  use renderer_base;
  30  use stdClass;
  31  /**
  32   * Class containing data for admin_presets tool
  33   *
  34   * @copyright  2021 Amaia Anabitarte <amaia@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class presets_list implements renderable, templatable {
  38  
  39      /**
  40       * @var stdClass[]    Array of admin presets.
  41       */
  42      private $presets;
  43  
  44      /**
  45       * @var bool    Wether the action menu is visible.
  46       */
  47      private $showactions;
  48  
  49      /**
  50       * Construct this renderable.
  51       *
  52       * @param stdClass[] $presets   Array of existing admin presets.
  53       * @param bool $showactions Whether actions should be displayed or not.
  54       */
  55      public function __construct(array $presets, bool $showactions = false) {
  56          $this->presets = $presets;
  57          $this->showactions = $showactions;
  58      }
  59  
  60      /**
  61       * Export the data.
  62       *
  63       * @param renderer_base $output
  64       * @return stdClass
  65       */
  66      public function export_for_template(renderer_base $output): stdClass {
  67          global $DB;
  68  
  69          $context = new stdClass();
  70          $context->presets = [];
  71          foreach ($this->presets as $preset) {
  72              if ($preset->timeimported) {
  73                  $timeimportedstring = userdate($preset->timeimported);
  74              } else {
  75                  $timeimportedstring = '';
  76              }
  77  
  78              $data = [
  79                  'name' => format_text($preset->name, FORMAT_PLAIN),
  80                  'description' => format_text($preset->comments, FORMAT_HTML),
  81                  'release' => format_text($preset->moodlerelease, FORMAT_PLAIN),
  82                  'author' => format_text($preset->author, FORMAT_PLAIN),
  83                  'site' => format_text(clean_text($preset->site, PARAM_URL), FORMAT_PLAIN),
  84                  'timecreated' => userdate($preset->timecreated),
  85                  'timeimported' => $timeimportedstring
  86              ];
  87  
  88              if ($this->showactions) {
  89                  // Preset actions.
  90                  $actionsmenu = new \action_menu();
  91                  $actionsmenu->set_menu_trigger(get_string('actions'));
  92                  $actionsmenu->set_owner_selector('preset-actions-' . $preset->id);
  93  
  94                  $loadlink = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'id' => $preset->id]);
  95                  $actionsmenu->add(new \action_menu_link_secondary(
  96                      $loadlink, new \pix_icon('t/play', ''),
  97                      get_string('applyaction', 'tool_admin_presets')
  98                  ));
  99                  $downloadlink = new \moodle_url('/admin/tool/admin_presets/index.php',
 100                      ['action' => 'export', 'mode' => 'download_xml', 'sesskey' => sesskey(), 'id' => $preset->id]
 101                  );
 102                  $actionsmenu->add(new \action_menu_link_secondary(
 103                      $downloadlink,
 104                      new \pix_icon('t/download', ''),
 105                      get_string('download')
 106                  ));
 107  
 108                  // Delete button won't be displayed for the pre-installed core "Starter" and "Full" presets.
 109                  if ($preset->iscore == manager::NONCORE_PRESET) {
 110                      $deletelink = new \moodle_url('/admin/tool/admin_presets/index.php',
 111                      ['action' => 'delete', 'id' => $preset->id]
 112                      );
 113                      $actionsmenu->add(new \action_menu_link_secondary(
 114                          $deletelink,
 115                          new \pix_icon('i/delete', ''),
 116                          get_string('delete')
 117                      ));
 118                  }
 119  
 120                  // Look for preset applications.
 121                  if ($DB->get_records('adminpresets_app', ['adminpresetid' => $preset->id])) {
 122                      $params = ['action' => 'rollback', 'id' => $preset->id];
 123                      $rollbacklink = new \moodle_url('/admin/tool/admin_presets/index.php', $params);
 124                      $actionsmenu->add(new \action_menu_link_secondary(
 125                          $rollbacklink,
 126                          new \pix_icon('i/reload', ''),
 127                          get_string('showhistory', 'tool_admin_presets')
 128                      ));
 129                  }
 130                  $data['actions'] = $actionsmenu->export_for_template($output);
 131              }
 132              $context->presets[] = $data;
 133          }
 134          $context->nopresets = empty($context->presets);
 135          $context->showactions = $this->showactions;
 136  
 137          return $context;
 138      }
 139  }