Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Class containing data for the data registry defaults.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 Jun Pataleta
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_dataprivacy\output;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use action_menu_link_primary;
  28  use coding_exception;
  29  use moodle_exception;
  30  use moodle_url;
  31  use renderable;
  32  use renderer_base;
  33  use stdClass;
  34  use templatable;
  35  use tool_dataprivacy\data_registry;
  36  use tool_dataprivacy\external\category_exporter;
  37  use tool_dataprivacy\external\purpose_exporter;
  38  
  39  /**
  40   * Class containing data for the data registry defaults.
  41   *
  42   * @copyright  2018 Jun Pataleta
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class defaults_page implements renderable, templatable {
  46  
  47      /** @var int $mode The display mode. */
  48      protected $mode = null;
  49  
  50      /** @var int $category The default category for the given mode. */
  51      protected $category = null;
  52  
  53      /** @var int $purpose The default purpose for the given mode. */
  54      protected $purpose = null;
  55  
  56      /** @var stdClass[] $otherdefaults Other defaults for the given mode. */
  57      protected $otherdefaults = [];
  58  
  59      /** @var bool $canedit Whether editing is allowed. */
  60      protected $canedit = false;
  61  
  62      /**
  63       * Construct this renderable.
  64       *
  65       * @param int $mode The display mode.
  66       * @param int $category The default category for the given mode.
  67       * @param int $purpose The default purpose for the given mode.
  68       * @param stdClass[] $otherdefaults Other defaults for the given mode.
  69       * @param bool $canedit Whether editing is allowed.
  70       */
  71      public function __construct($mode, $category, $purpose, $otherdefaults = [], $canedit = false) {
  72          $this->mode = $mode;
  73          $this->category = $category;
  74          $this->purpose = $purpose;
  75          $this->otherdefaults = $otherdefaults;
  76          $this->canedit = $canedit;
  77      }
  78  
  79      /**
  80       * Export this data so it can be used as the context for a mustache template.
  81       *
  82       * @param renderer_base $output
  83       * @return stdClass
  84       * @throws coding_exception
  85       * @throws moodle_exception
  86       */
  87      public function export_for_template(renderer_base $output) {
  88          $data = new stdClass();
  89  
  90          // Set tab URLs.
  91          $coursecaturl = new moodle_url('/admin/tool/dataprivacy/defaults.php', ['mode' => CONTEXT_COURSECAT]);
  92          $courseurl = new moodle_url('/admin/tool/dataprivacy/defaults.php', ['mode' => CONTEXT_COURSE]);
  93          $moduleurl = new moodle_url('/admin/tool/dataprivacy/defaults.php', ['mode' => CONTEXT_MODULE]);
  94          $blockurl = new moodle_url('/admin/tool/dataprivacy/defaults.php', ['mode' => CONTEXT_BLOCK]);
  95          $data->coursecaturl = $coursecaturl;
  96          $data->courseurl = $courseurl;
  97          $data->moduleurl = $moduleurl;
  98          $data->blockurl = $blockurl;
  99  
 100          // Set display mode.
 101          switch ($this->mode) {
 102              case CONTEXT_COURSECAT:
 103                  $data->modecoursecat = true;
 104                  break;
 105              case CONTEXT_COURSE:
 106                  $data->modecourse = true;
 107                  break;
 108              case CONTEXT_MODULE:
 109                  $data->modemodule = true;
 110                  break;
 111              case CONTEXT_BLOCK:
 112                  $data->modeblock = true;
 113                  break;
 114              default:
 115                  $data->modecoursecat = true;
 116                  break;
 117          }
 118  
 119          // Set config variables.
 120          $configname = \context_helper::get_class_for_level($this->mode);
 121          list($purposevar, $categoryvar) = data_registry::var_names_from_context($configname);
 122          $data->categoryvar = $categoryvar;
 123          $data->purposevar = $purposevar;
 124  
 125          // Set default category.
 126          $data->categoryid = $this->category;
 127          $data->category = category_exporter::get_name($this->category);
 128  
 129          // Set default purpose.
 130          $data->purposeid = $this->purpose;
 131          $data->purpose = purpose_exporter::get_name($this->purpose);
 132  
 133          // Set other defaults.
 134          $otherdefaults = [];
 135          $url = new moodle_url('#');
 136          foreach ($this->otherdefaults as $pluginname => $values) {
 137              $defaults = [
 138                  'name' => $values->name,
 139                  'category' => category_exporter::get_name($values->category),
 140                  'purpose' => purpose_exporter::get_name($values->purpose),
 141              ];
 142              if ($this->canedit) {
 143                  $actions = [];
 144                  // Edit link.
 145                  $editattrs = [
 146                      'data-action' => 'edit-activity-defaults',
 147                      'data-contextlevel' => $this->mode,
 148                      'data-activityname' => $pluginname,
 149                      'data-category' => $values->category,
 150                      'data-purpose' => $values->purpose,
 151                  ];
 152                  $editlink = new action_menu_link_primary($url, new \pix_icon('t/edit', get_string('edit')),
 153                      get_string('edit'), $editattrs);
 154                  $actions[] = $editlink->export_for_template($output);
 155  
 156                  // Delete link.
 157                  $deleteattrs = [
 158                      'data-action' => 'delete-activity-defaults',
 159                      'data-contextlevel' => $this->mode,
 160                      'data-activityname' => $pluginname,
 161                      'data-activitydisplayname' => $values->name,
 162                  ];
 163                  $deletelink = new action_menu_link_primary($url, new \pix_icon('t/delete', get_string('delete')),
 164                      get_string('delete'), $deleteattrs);
 165                  $actions[] = $deletelink->export_for_template($output);
 166  
 167                  $defaults['actions'] = $actions;
 168              }
 169              $otherdefaults[] = (object)$defaults;
 170          }
 171          $data->otherdefaults = $otherdefaults;
 172  
 173          $data->canedit = $this->canedit;
 174          $data->contextlevel = $this->mode;
 175  
 176          return $data;
 177      }
 178  }