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 core_badges\output;
  18  
  19  use core_badges\badge;
  20  use moodle_url;
  21  use renderer_base;
  22  use single_button;
  23  use moodle_page;
  24  use url_select;
  25  
  26  /**
  27   * Class manage_badge_action_bar - Display the action bar
  28   *
  29   * @package   core_badges
  30   * @copyright 2021 Peter Dias
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class manage_badge_action_bar extends base_action_bar {
  34      /** @var badge $badge The badge we are managing. */
  35      protected $badge;
  36  
  37      /**
  38       * manage_badge_action_bar constructor
  39       *
  40       * @param badge $badge The badge we are viewing
  41       * @param moodle_page $page The page object
  42       */
  43      public function __construct(badge $badge, moodle_page $page) {
  44          parent::__construct($page, $badge->type);
  45          $this->badge = $badge;
  46      }
  47  
  48      /**
  49       * The template that this tertiary nav should use.
  50       *
  51       * @return string
  52       */
  53      public function get_template(): string {
  54          return 'core_badges/manage_badge';
  55      }
  56  
  57      /**
  58       * Export the action bar
  59       *
  60       * @param renderer_base $output
  61       * @return array
  62       */
  63      public function export_for_template(renderer_base $output): array {
  64          $elements = [];
  65          $params = ['type' => $this->type];
  66          if ($this->page->context->contextlevel == CONTEXT_COURSE) {
  67              $params['id'] = $this->page->context->instanceid;
  68          }
  69          $elements['button'] = new single_button(new moodle_url('/badges/index.php', $params), get_string('back'), 'get');
  70          $elements['urlselect'] = new url_select($this->generate_badge_navigation(), $this->page->url->out(false), null);
  71          foreach ($elements as $key => $element) {
  72              $elements[$key] = $element->export_for_template($output);
  73          }
  74          $additional = $this->get_third_party_nav_action($output);
  75          $elements += $additional ?: [];
  76  
  77          return $elements;
  78      }
  79  
  80      /**
  81       * Returns a multi dimensional array of the links that should be displayed when creating a badge.
  82       * The keys of the array feed into the text shown to the user and content of each element contain the following:
  83       *  - url               URL for the option
  84       *  - additionalparams  Additional params to feed into the url
  85       *  - capability        The capabilities to check that governs visibility
  86       * @return array
  87       */
  88      protected function get_badge_administration_mapping_construct(): array {
  89          return [
  90              'boverview' => [
  91                  'url' => '/badges/overview.php',
  92                  'capability' => ''
  93              ],
  94              'bdetails' => [
  95                  'url' => '/badges/edit.php',
  96                  'additionalparams' => ['action' => 'badge'],
  97                  'capability' => 'moodle/badges:configuredetails'
  98              ],
  99              'bcriteria' => [
 100                  'url' => '/badges/criteria.php',
 101                  'capability' => 'moodle/badges:configurecriteria'
 102              ],
 103              'bmessage' => [
 104                  'url' => '/badges/edit.php',
 105                  'additionalparams' => ['action' => 'message'],
 106                  'capability' => 'moodle/badges:configuremessages'
 107              ],
 108              'bawards' => [
 109                  'url' => '/badges/recipients.php',
 110                  'additionalparams' => ['sort' => 'dateissued', 'dir' => 'DESC'],
 111                  'capability' => 'moodle/badges:viewawarded'
 112              ],
 113              'bendorsement' => [
 114                  'url' => '/badges/endorsement.php',
 115                  'capability' => 'moodle/badges:configuredetails'
 116              ],
 117              'brelated' => [
 118                  'url' => '/badges/related.php',
 119                  'capability' => 'moodle/badges:configuredetails'
 120              ],
 121              'balignment' => [
 122                  'url' => '/badges/alignment.php',
 123                  'capability' => 'moodle/badges:configuredetails'
 124              ],
 125          ];
 126      }
 127  
 128      /**
 129       * Generate the options to be displayed when editing a badge. This feeds into a URL select which will be displayed
 130       * in the tertiary navigation.
 131       *
 132       * @return array
 133       */
 134      protected function generate_badge_navigation(): array {
 135          global $DB;
 136          $params = ['id' => $this->badge->id];
 137          $options = [];
 138          $construct = $this->get_badge_administration_mapping_construct();
 139          foreach ($construct as $stringidentifier => $checks) {
 140              if ($checks['capability'] && !has_capability($checks['capability'], $this->page->context)) {
 141                  continue;
 142              }
 143  
 144              $sql = '';
 145              switch ($stringidentifier) {
 146                  case 'bawards':
 147                      $sql = "SELECT COUNT(b.userid)
 148                                FROM {badge_issued} b
 149                          INNER JOIN {user} u ON b.userid = u.id
 150                               WHERE b.badgeid = :badgeid AND u.deleted = 0";
 151                      break;
 152                  case 'brelated':
 153                      $sql = "SELECT COUNT(br.badgeid)
 154                                FROM {badge_related} br
 155                               WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
 156                      break;
 157                  case 'balignment':
 158                      $sql = "SELECT COUNT(bc.id)
 159                                FROM {badge_alignment} bc
 160                               WHERE bc.badgeid = :badgeid";
 161                      break;
 162              }
 163  
 164              $content = null;
 165              if ($sql) {
 166                  $content = $DB->count_records_sql($sql, ['badgeid' => $this->badge->id, 'badgeid2' => $this->badge->id]);
 167              }
 168  
 169              $url = new moodle_url($checks['url'], $params + ($checks['additionalparams'] ?? []));
 170              $options[get_string($stringidentifier, 'core_badges', $content)] = $url->out(false);
 171          }
 172          return array_flip($options);
 173      }
 174  }