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] [Versions 401 and 402] [Versions 402 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  /**
  19   * A form for the creation and editing of groups.
  20   *
  21   * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package   core_group
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  use core_group\visibility;
  29  
  30  require_once($CFG->dirroot.'/lib/formslib.php');
  31  
  32  /**
  33   * Group form class
  34   *
  35   * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @package   core_group
  38   */
  39  class group_form extends moodleform {
  40  
  41      /**
  42       * Definition of the form
  43       */
  44      function definition () {
  45          global $USER, $CFG, $COURSE;
  46          $coursecontext = context_course::instance($COURSE->id);
  47  
  48          $mform =& $this->_form;
  49          $editoroptions = $this->_customdata['editoroptions'];
  50  
  51          $mform->addElement('header', 'general', get_string('general', 'form'));
  52  
  53          $mform->addElement('text','name', get_string('groupname', 'group'),'maxlength="254" size="50"');
  54          $mform->addRule('name', get_string('required'), 'required', null, 'client');
  55          $mform->setType('name', PARAM_TEXT);
  56  
  57          $mform->addElement('text','idnumber', get_string('idnumbergroup'), 'maxlength="100" size="10"');
  58          $mform->addHelpButton('idnumber', 'idnumbergroup');
  59          $mform->setType('idnumber', PARAM_RAW);
  60          if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
  61              $mform->hardFreeze('idnumber');
  62          }
  63  
  64          $mform->addElement('editor', 'description_editor', get_string('groupdescription', 'group'), null, $editoroptions);
  65          $mform->setType('description_editor', PARAM_RAW);
  66  
  67          $mform->addElement('passwordunmask', 'enrolmentkey', get_string('enrolmentkey', 'group'), 'maxlength="254" size="24"', get_string('enrolmentkey', 'group'));
  68          $mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
  69          $mform->setType('enrolmentkey', PARAM_RAW);
  70  
  71          $visibilityoptions = [
  72              GROUPS_VISIBILITY_ALL => get_string('visibilityall', 'group'),
  73              GROUPS_VISIBILITY_MEMBERS => get_string('visibilitymembers', 'group'),
  74              GROUPS_VISIBILITY_OWN => get_string('visibilityown', 'group'),
  75              GROUPS_VISIBILITY_NONE => get_string('visibilitynone', 'group')
  76          ];
  77          $mform->addElement('select', 'visibility', get_string('visibility', 'group'), $visibilityoptions);
  78          $mform->addHelpButton('visibility', 'visibility', 'group');
  79          $mform->setType('visibility', PARAM_INT);
  80  
  81          $mform->addElement('advcheckbox', 'participation', '', get_string('participation', 'group'));
  82          $mform->addHelpButton('participation', 'participation', 'group');
  83          $mform->setType('participation', PARAM_BOOL);
  84          $mform->setDefault('participation', 1);
  85          $mform->disabledIf('participation', 'visibility', 'in', [GROUPS_VISIBILITY_OWN, GROUPS_VISIBILITY_NONE]);
  86  
  87          // Group conversation messaging.
  88          if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
  89              $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
  90              $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
  91              $mform->disabledIf('enablemessaging', 'visibility', 'in', [GROUPS_VISIBILITY_OWN, GROUPS_VISIBILITY_NONE]);
  92          }
  93  
  94          $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
  95  
  96          $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
  97          $mform->setDefault('deletepicture', 0);
  98  
  99          $mform->addElement('filepicker', 'imagefile', get_string('newpicture', 'group'));
 100          $mform->addHelpButton('imagefile', 'newpicture', 'group');
 101  
 102          $mform->addElement('hidden','id');
 103          $mform->setType('id', PARAM_INT);
 104  
 105          $mform->addElement('hidden','courseid');
 106          $mform->setType('courseid', PARAM_INT);
 107  
 108          $this->add_action_buttons();
 109      }
 110  
 111      /**
 112       * Extend the form definition after the data has been parsed.
 113       */
 114      public function definition_after_data() {
 115          global $COURSE, $DB, $USER;
 116  
 117          $mform = $this->_form;
 118          $groupid = $mform->getElementValue('id');
 119          $coursecontext = context_course::instance($COURSE->id);
 120  
 121          if ($group = $DB->get_record('groups', array('id' => $groupid))) {
 122              // If can create group conversation then get if a conversation area exists and it is enabled.
 123              if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
 124                  if (\core_message\api::is_conversation_area_enabled('core_group', 'groups', $groupid, $coursecontext->id)) {
 125                      $mform->getElement('enablemessaging')->setSelected(1);
 126                  }
 127              }
 128              // Print picture.
 129              if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
 130                  $pic = get_string('none');
 131                  if ($mform->elementExists('deletepicture')) {
 132                      $mform->removeElement('deletepicture');
 133                  }
 134              }
 135              $imageelement = $mform->getElement('currentpicture');
 136              $imageelement->setValue($pic);
 137          } else {
 138              if ($mform->elementExists('currentpicture')) {
 139                  $mform->removeElement('currentpicture');
 140              }
 141              if ($mform->elementExists('deletepicture')) {
 142                  $mform->removeElement('deletepicture');
 143              }
 144          }
 145  
 146          if ($DB->record_exists('groups_members', ['groupid' => $groupid])) {
 147              // If the group has members, lock visibility and participation fields.
 148              /** @var MoodleQuickForm_select $visibility */
 149              $visibility = $mform->getElement('visibility');
 150              $visibility->freeze();
 151              /** @var MoodleQuickForm_advcheckbox $participation */
 152              $participation = $mform->getElement('participation');
 153              $participation->freeze();
 154          }
 155  
 156      }
 157  
 158      /**
 159       * Form validation
 160       *
 161       * @param array $data
 162       * @param array $files
 163       * @return array $errors An array of errors
 164       */
 165      function validation($data, $files) {
 166          global $COURSE, $DB, $CFG;
 167  
 168          $errors = parent::validation($data, $files);
 169  
 170          $name = trim($data['name']);
 171          if (isset($data['idnumber'])) {
 172              $idnumber = trim($data['idnumber']);
 173          } else {
 174              $idnumber = '';
 175          }
 176          if ($data['id'] and $group = $DB->get_record('groups', array('id'=>$data['id']))) {
 177              if (core_text::strtolower($group->name) != core_text::strtolower($name)) {
 178                  if (groups_get_group_by_name($COURSE->id,  $name)) {
 179                      $errors['name'] = get_string('groupnameexists', 'group', $name);
 180                  }
 181              }
 182              if (!empty($idnumber) && $group->idnumber != $idnumber) {
 183                  if (groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
 184                      $errors['idnumber']= get_string('idnumbertaken');
 185                  }
 186              }
 187  
 188              if ($data['enrolmentkey'] != '') {
 189                  $errmsg = '';
 190                  if (!empty($CFG->groupenrolmentkeypolicy) && $group->enrolmentkey !== $data['enrolmentkey']
 191                          && !check_password_policy($data['enrolmentkey'], $errmsg)) {
 192                      // Enforce password policy when the password is changed.
 193                      $errors['enrolmentkey'] = $errmsg;
 194                  } else {
 195                      // Prevent twice the same enrolment key in course groups.
 196                      $sql = "SELECT id FROM {groups} WHERE id <> :groupid AND courseid = :courseid AND enrolmentkey = :key";
 197                      $params = array('groupid' => $data['id'], 'courseid' => $COURSE->id, 'key' => $data['enrolmentkey']);
 198                      if ($DB->record_exists_sql($sql, $params)) {
 199                          $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
 200                      }
 201                  }
 202              }
 203  
 204          } else if (groups_get_group_by_name($COURSE->id, $name)) {
 205              $errors['name'] = get_string('groupnameexists', 'group', $name);
 206          } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id, $idnumber)) {
 207              $errors['idnumber']= get_string('idnumbertaken');
 208          } else if ($data['enrolmentkey'] != '') {
 209              $errmsg = '';
 210              if (!empty($CFG->groupenrolmentkeypolicy) && !check_password_policy($data['enrolmentkey'], $errmsg)) {
 211                  // Enforce password policy.
 212                  $errors['enrolmentkey'] = $errmsg;
 213              } else if ($DB->record_exists('groups', array('courseid' => $COURSE->id, 'enrolmentkey' => $data['enrolmentkey']))) {
 214                  // Prevent the same enrolment key from being used multiple times in course groups.
 215                  $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group');
 216              }
 217          }
 218  
 219          return $errors;
 220      }
 221  
 222      /**
 223       * Get editor options for this form
 224       *
 225       * @return array An array of options
 226       */
 227      function get_editor_options() {
 228          return $this->_customdata['editoroptions'];
 229      }
 230  }