Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Contains class core_group\output\user_groups_editable
  19   *
  20   * @package   core_group
  21   * @copyright 2017 Damyon Wiese
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_group\output;
  26  
  27  use context_course;
  28  use core_user;
  29  use core_external;
  30  use coding_exception;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  require_once($CFG->dirroot . '/group/lib.php');
  35  
  36  /**
  37   * Class to display list of user groups.
  38   *
  39   * @package   core_group
  40   * @copyright 2017 Damyon Wiese
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class user_groups_editable extends \core\output\inplace_editable {
  44  
  45      /** @var $coursegroups */
  46      private $coursegroups = null;
  47      /** @var $context */
  48      private $context = null;
  49  
  50      /**
  51       * Constructor.
  52       *
  53       * @param \stdClass $course The current course
  54       * @param \context $context The course context
  55       * @param \stdClass $user The current user
  56       * @param \stdClass[] $coursegroups The list of course groups from groups_get_all_groups with membership.
  57       * @param array $value Array of groupids.
  58       */
  59      public function __construct($course, $context, $user, $coursegroups, $value) {
  60          // Check capabilities to get editable value.
  61          $editable = has_capability('moodle/course:managegroups', $context) && !empty($coursegroups);
  62  
  63          // Invent an itemid.
  64          $itemid = $course->id . ':' . $user->id;
  65  
  66          $value = json_encode($value);
  67  
  68          // Remember these for the display value.
  69          $this->coursegroups = $coursegroups;
  70          $this->context = $context;
  71  
  72          parent::__construct('core_group', 'user_groups', $itemid, $editable, $value, $value);
  73  
  74          // Assignable groups.
  75          $options = [];
  76  
  77          foreach ($coursegroups as $group) {
  78              $options[$group->id] = format_string($group->name, true, ['context' => $this->context]);
  79          }
  80  
  81          $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $this->context));
  82          $this->edithint = get_string('editusersgroupsa', 'group', $fullname);
  83          $this->editlabel = get_string('editusersgroupsa', 'group', $fullname);
  84  
  85          $attributes = ['multiple' => true];
  86          $this->set_type_autocomplete($options, $attributes);
  87      }
  88  
  89      /**
  90       * Export this data so it can be used as the context for a mustache template.
  91       *
  92       * @param \renderer_base $output
  93       * @return array
  94       */
  95      public function export_for_template(\renderer_base $output) {
  96          $listofgroups = [];
  97          $groupids = json_decode($this->value);
  98          foreach ($groupids as $id) {
  99              $listofgroups[] = format_string($this->coursegroups[$id]->name, true, ['context' => $this->context]);
 100          }
 101  
 102          if (!empty($listofgroups)) {
 103              $this->displayvalue = implode(', ', $listofgroups);
 104          } else {
 105              $this->displayvalue = get_string('groupsnone');
 106          }
 107          return parent::export_for_template($output);
 108      }
 109  
 110      /**
 111       * Updates the value in database and returns itself, called from inplace_editable callback
 112       *
 113       * @param int $itemid
 114       * @param mixed $newvalue
 115       * @return \self
 116       */
 117      public static function update($itemid, $newvalue) {
 118          // Check caps.
 119          // Do the thing.
 120          // Return one of me.
 121          // Validate the inputs.
 122          list($courseid, $userid) = explode(':', $itemid, 2);
 123  
 124          $courseid = clean_param($courseid, PARAM_INT);
 125          $userid = clean_param($userid, PARAM_INT);
 126          $groupids = json_decode($newvalue);
 127          foreach ($groupids as $index => $groupid) {
 128              $groupids[$index] = clean_param($groupid, PARAM_INT);
 129          }
 130  
 131          // Check user is enrolled in the course.
 132          $context = context_course::instance($courseid);
 133          core_external::validate_context($context);
 134  
 135          if (!is_enrolled($context, $userid)) {
 136              throw new coding_exception('User does not belong to the course');
 137          }
 138  
 139          // Check that all the groups belong to the course.
 140          $coursegroups = groups_get_all_groups($courseid, 0, 0, 'g.*', true);
 141  
 142          $byid = [];
 143          foreach ($groupids as $groupid) {
 144              if (!isset($coursegroups[$groupid])) {
 145                  throw new coding_exception('Group does not belong to the course');
 146              }
 147              $byid[$groupid] = $groupid;
 148          }
 149          $groupids = $byid;
 150          // Check permissions.
 151          require_capability('moodle/course:managegroups', $context);
 152  
 153          // Process adds.
 154          foreach ($groupids as $groupid) {
 155              if (!isset($coursegroups[$groupid]->members[$userid])) {
 156                  // Add them.
 157                  groups_add_member($groupid, $userid);
 158                  // Keep this variable in sync.
 159                  $coursegroups[$groupid]->members[$userid] = $userid;
 160              }
 161          }
 162  
 163          // Process removals.
 164          foreach ($coursegroups as $groupid => $group) {
 165              if (isset($group->members[$userid]) && !isset($groupids[$groupid])) {
 166                  if (groups_remove_member_allowed($groupid, $userid)) {
 167                      groups_remove_member($groupid, $userid);
 168                      unset($coursegroups[$groupid]->members[$userid]);
 169                  } else {
 170                      $groupids[$groupid] = $groupid;
 171                  }
 172              }
 173          }
 174  
 175          $course = get_course($courseid);
 176          $user = core_user::get_user($userid);
 177          return new self($course, $context, $user, $coursegroups, array_values($groupids));
 178      }
 179  }