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.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Output the override actionbar for this activity.
  19   *
  20   * @package   mod_assign
  21   * @copyright 2021 Adrian Greeve <adrian@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_assign\output;
  26  
  27  use core_availability\info_module;
  28  use moodle_url;
  29  use templatable;
  30  use renderable;
  31  use url_select;
  32  use single_button;
  33  
  34  /**
  35   * Output the override actionbar for this activity.
  36   *
  37   * @package   mod_assign
  38   * @copyright 2021 Adrian Greeve <adrian@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class override_actionmenu implements templatable, renderable {
  42  
  43      /** @var moodle_url The current url for this page. */
  44      protected $currenturl;
  45      /** @var \cm_info course module information */
  46      protected $cm;
  47      /** @var bool Can all groups be accessed */
  48      protected $canaccessallgroups;
  49      /** @var array Groups related to this activity */
  50      protected $groups;
  51  
  52      /**
  53       * Constructor for this action menu.
  54       *
  55       * @param moodle_url $currenturl The current url for this page.
  56       * @param \cm_info $cm course module information.
  57       */
  58      public function __construct(moodle_url $currenturl, \cm_info $cm) {
  59          $this->currenturl = $currenturl;
  60          $this->cm = $cm;
  61          $groupmode = groups_get_activity_groupmode($this->cm);
  62          $this->canaccessallgroups = ($groupmode === NOGROUPS) ||
  63                  has_capability('moodle/site:accessallgroups', $this->cm->context);
  64          $this->groups = $this->canaccessallgroups ? groups_get_all_groups($this->cm->course) :
  65                  groups_get_activity_allowed_groups($this->cm);
  66      }
  67  
  68      /**
  69       * Create a select menu for overrides.
  70       *
  71       * @return url_select A url select object.
  72       */
  73      protected function get_select_menu(): url_select {
  74          $userlink = new moodle_url('/mod/assign/overrides.php', ['cmid' => $this->cm->id, 'mode' => 'user']);
  75          $grouplink = new moodle_url('/mod/assign/overrides.php', ['cmid' => $this->cm->id, 'mode' => 'group']);
  76          $menu = [
  77              $userlink->out(false) => get_string('useroverrides', 'mod_assign'),
  78              $grouplink->out(false) => get_string('groupoverrides', 'mod_assign'),
  79          ];
  80          return new url_select($menu, $this->currenturl->out(false), null, 'mod_assign_override_select');
  81      }
  82  
  83      /**
  84       * Whether to show groups or not. Assignments can be have group overrides if there are groups available in the course.
  85       * There is no restriction related to the assignment group setting.
  86       *
  87       * @return bool
  88       */
  89      protected function show_groups(): bool {
  90          if ($this->canaccessallgroups) {
  91              $groups = groups_get_all_groups($this->cm->course);
  92          } else {
  93              $groups = groups_get_activity_allowed_groups($this->cm);
  94          }
  95          return !(empty($groups));
  96      }
  97  
  98      /**
  99       * Whether to enable/disable user override button or not.
 100       *
 101       * @return bool
 102       */
 103      protected function show_useroverride(): bool {
 104          global $DB;
 105          $users = [];
 106          $context = $this->cm->context;
 107          if ($this->canaccessallgroups) {
 108              $users = get_enrolled_users($context, '', 0, 'u.id');
 109          } else if ($this->groups) {
 110              $enrolledjoin = get_enrolled_join($context, 'u.id');
 111              list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($this->groups), SQL_PARAMS_NAMED);
 112              $params = $enrolledjoin->params + $ingroupparams;
 113              $sql = "SELECT u.id
 114                        FROM {user} u
 115                        JOIN {groups_members} gm ON gm.userid = u.id
 116                             {$enrolledjoin->joins}
 117                       WHERE gm.groupid $ingroupsql
 118                         AND {$enrolledjoin->wheres}";
 119              $users = $DB->get_records_sql($sql, $params);
 120          }
 121  
 122          $info = new info_module($this->cm);
 123          $users = $info->filter_user_list($users);
 124  
 125          return !empty($users);
 126      }
 127  
 128      /**
 129       * Data to be used in a template.
 130       *
 131       * @param \renderer_base $output renderer base output.
 132       * @return array The data to be used in a template.
 133       */
 134      public function export_for_template(\renderer_base $output): array {
 135  
 136          $type = $this->currenturl->get_param('mode');
 137          if ($type == 'user') {
 138              $text = get_string('addnewuseroverride', 'mod_assign');
 139          } else {
 140              $text = get_string('addnewgroupoverride', 'mod_assign');
 141          }
 142          $action = ($type == 'user') ? 'adduser' : 'addgroup';
 143  
 144          $params = ['cmid' => $this->currenturl->get_param('cmid'), 'action' => $action];
 145          $url = new moodle_url('/mod/assign/overrideedit.php', $params);
 146  
 147          $options = [];
 148          if ($action == 'addgroup' && !$this->show_groups()) {
 149              $options = ['disabled' => 'true'];
 150          } else if ($action === 'adduser' && !$this->show_useroverride()) {
 151              $options = ['disabled' => 'true'];
 152          }
 153          $overridebutton = new single_button($url, $text, 'post', true, $options);
 154  
 155          $urlselect = $this->get_select_menu();
 156          return [
 157              'addoverride' => $overridebutton->export_for_template($output),
 158              'urlselect' => $urlselect->export_for_template($output)
 159          ];
 160      }
 161  }