Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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   * Page helper.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 David Monllao
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_dataprivacy;
  26  use context_system;
  27  use moodle_url;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Page helper.
  33   *
  34   * @package    tool_dataprivacy
  35   * @copyright  2018 David Monllao
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class page_helper {
  39  
  40      /**
  41       * Sets up $PAGE for data privacy admin pages.
  42       *
  43       * @param moodle_url $url The page URL.
  44       * @param string $title The page's title.
  45       * @param string $attachtoparentnode The parent navigation node where this page can be accessed from.
  46       * @param string $requiredcapability The required capability to view this page.
  47       */
  48      public static function setup(moodle_url $url, $title, $attachtoparentnode = '',
  49                                   $requiredcapability = 'tool/dataprivacy:managedataregistry') {
  50          global $PAGE, $SITE;
  51  
  52          $context = context_system::instance();
  53  
  54          require_login();
  55          if (isguestuser()) {
  56              throw new \moodle_exception('noguest');
  57          }
  58  
  59          // TODO Check that data privacy is enabled.
  60          require_capability($requiredcapability, $context);
  61  
  62          $PAGE->navigation->override_active_url($url);
  63  
  64          $PAGE->set_url($url);
  65          $PAGE->set_context($context);
  66          $PAGE->set_pagelayout('admin');
  67          $PAGE->set_title($title);
  68          $PAGE->set_heading($SITE->fullname);
  69          $PAGE->set_secondary_active_tab('users');
  70          $PAGE->set_primary_active_tab('siteadminnode');
  71  
  72          // If necessary, override the settings navigation to add this page into the breadcrumb navigation.
  73          if ($attachtoparentnode) {
  74              if ($siteadmin = $PAGE->settingsnav->find('root', \navigation_node::TYPE_SITE_ADMIN)) {
  75                  $PAGE->navbar->add($siteadmin->get_content(), $siteadmin->action());
  76              }
  77              if ($dataprivacy = $PAGE->settingsnav->find('privacy', \navigation_node::TYPE_SETTING)) {
  78                  $PAGE->navbar->add($dataprivacy->get_content(), $dataprivacy->action());
  79              }
  80              if ($dataregistry = $PAGE->settingsnav->find($attachtoparentnode, \navigation_node::TYPE_SETTING)) {
  81                  $PAGE->navbar->add($dataregistry->get_content(), $dataregistry->action());
  82              }
  83  
  84              $PAGE->navbar->add($title, $url);
  85          }
  86      }
  87  }