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  /**
  19   * Auto group form
  20   *
  21   * @package    core_group
  22   * @copyright  2007 mattc-catalyst (http://moodle.com)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  27  }
  28  
  29  require_once($CFG->dirroot.'/lib/formslib.php');
  30  require_once($CFG->dirroot.'/cohort/lib.php');
  31  
  32  /**
  33   * Auto group form class
  34   *
  35   * @package    core_group
  36   * @copyright  2007 mattc-catalyst (http://moodle.com)
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class autogroup_form extends moodleform {
  40  
  41      /**
  42       * Form Definition
  43       */
  44      function definition() {
  45          global $USER, $COURSE;
  46          $coursecontext = context_course::instance($COURSE->id);
  47  
  48          $mform =& $this->_form;
  49  
  50          $mform->addElement('header', 'autogroup', get_string('general'));
  51  
  52          $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
  53          $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
  54          $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
  55          $mform->setType('namingscheme', PARAM_TEXT);
  56          // There must not be duplicate group names in course.
  57          $template = get_string('grouptemplate', 'group');
  58          $gname = groups_parse_name($template, 0);
  59          if (!groups_get_group_by_name($COURSE->id, $gname)) {
  60              $mform->setDefault('namingscheme', $template);
  61          }
  62  
  63          $options = array('groups' => get_string('numgroups', 'group'),
  64                           'members' => get_string('nummembers', 'group'));
  65          $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
  66  
  67          $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
  68          $mform->setType('number', PARAM_INT);
  69          $mform->addRule('number', null, 'numeric', null, 'client');
  70          $mform->addRule('number', get_string('required'), 'required', null, 'client');
  71  
  72          // Enable group messaging for the groups to be auto-created.
  73          if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
  74              $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
  75              $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
  76          }
  77  
  78          $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group'));
  79          $mform->setExpanded('groupmembershdr', true);
  80  
  81          $options = array(0=>get_string('all'));
  82          $options += $this->_customdata['roles'];
  83          $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
  84  
  85          $student = get_archetype_roles('student');
  86          $student = reset($student);
  87  
  88          if ($student and array_key_exists($student->id, $options)) {
  89              $mform->setDefault('roleid', $student->id);
  90          }
  91  
  92          $coursecontext = context_course::instance($COURSE->id);
  93          if ($cohorts = cohort_get_available_cohorts($coursecontext, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0)) {
  94              $options = array(0 => get_string('anycohort', 'cohort'));
  95              foreach ($cohorts as $c) {
  96                  $options[$c->id] = format_string($c->name, true, [
  97                      'context' => context::instance_by_id($c->contextid),
  98                  ]);
  99              }
 100              $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
 101              $mform->setDefault('cohortid', '0');
 102          } else {
 103              $mform->addElement('hidden','cohortid');
 104              $mform->setType('cohortid', PARAM_INT);
 105              $mform->setConstant('cohortid', '0');
 106          }
 107  
 108          if ($groupings = groups_get_all_groupings($COURSE->id)) {
 109              $options = array();
 110              $options[0] = get_string('none');
 111              foreach ($groupings as $grouping) {
 112                  $options[$grouping->id] = format_string($grouping->name);
 113              }
 114              $mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options);
 115              $mform->setDefault('groupingid', 0);
 116              $mform->disabledIf('groupingid', 'notingroup', 'checked');
 117          } else {
 118              $mform->addElement('hidden', 'groupingid');
 119              $mform->setType('groupingid', PARAM_INT);
 120              $mform->setConstant('groupingid', 0);
 121          }
 122  
 123          if ($groups = groups_get_all_groups($COURSE->id)) {
 124              $options = array();
 125              $options[0] = get_string('none');
 126              foreach ($groups as $group) {
 127                  $options[$group->id] = format_string($group->name);
 128              }
 129              $mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options);
 130              $mform->setDefault('groupid', 0);
 131              $mform->disabledIf('groupid', 'notingroup', 'checked');
 132          } else {
 133              $mform->addElement('hidden', 'groupid');
 134              $mform->setType('groupid', PARAM_INT);
 135              $mform->setConstant('groupid', 0);
 136          }
 137  
 138          $options = array('no'        => get_string('noallocation', 'group'),
 139                           'random'    => get_string('random', 'group'),
 140                           'firstname' => get_string('byfirstname', 'group'),
 141                           'lastname'  => get_string('bylastname', 'group'),
 142                           'idnumber'  => get_string('byidnumber', 'group'));
 143          $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
 144          $mform->setDefault('allocateby', 'random');
 145  
 146          $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
 147          $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
 148  
 149          $mform->addElement('checkbox', 'notingroup', get_string('notingroup', 'group'));
 150          $mform->disabledIf('notingroup', 'groupingid', 'neq', 0);
 151          $mform->disabledIf('notingroup', 'groupid', 'neq', 0);
 152  
 153          if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
 154              $mform->addElement('checkbox', 'includeonlyactiveenrol', get_string('includeonlyactiveenrol', 'group'), '');
 155              $mform->addHelpButton('includeonlyactiveenrol', 'includeonlyactiveenrol', 'group');
 156              $mform->setDefault('includeonlyactiveenrol', true);
 157          }
 158  
 159          $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group'));
 160  
 161          $options = array('0' => get_string('nogrouping', 'group'),
 162                           '-1'=> get_string('newgrouping', 'group'));
 163          if ($groupings = groups_get_all_groupings($COURSE->id)) {
 164              foreach ($groupings as $grouping) {
 165                  $options[$grouping->id] = strip_tags(format_string($grouping->name));
 166              }
 167          }
 168          $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
 169          if ($groupings) {
 170              $mform->setDefault('grouping', '-1');
 171          }
 172  
 173          $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
 174          $mform->setType('groupingname', PARAM_TEXT);
 175          $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
 176  
 177          $mform->addElement('hidden','courseid');
 178          $mform->setType('courseid', PARAM_INT);
 179  
 180          $mform->addElement('hidden','seed');
 181          $mform->setType('seed', PARAM_INT);
 182  
 183          $buttonarray = array();
 184          $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'));
 185          $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
 186          $buttonarray[] = &$mform->createElement('cancel');
 187          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 188          $mform->closeHeaderBefore('buttonar');
 189      }
 190  
 191      /**
 192       * Performs validation of the form information
 193       *
 194       * @param array $data
 195       * @param array $files
 196       * @return array $errors An array of $errors
 197       */
 198      function validation($data, $files) {
 199          global $CFG, $COURSE;
 200          $errors = parent::validation($data, $files);
 201  
 202          if ($data['allocateby'] != 'no') {
 203              $source = array();
 204              if ($data['cohortid']) {
 205                  $source['cohortid'] = $data['cohortid'];
 206              }
 207              if ($data['groupingid']) {
 208                  $source['groupingid'] = $data['groupingid'];
 209              }
 210              if ($data['groupid']) {
 211                  $source['groupid'] = $data['groupid'];
 212              }
 213              if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
 214                  $errors['roleid'] = get_string('nousersinrole', 'group');
 215              }
 216  
 217             /// Check the number entered is sane
 218              if ($data['groupby'] == 'groups') {
 219                  $usercnt = count($users);
 220  
 221                  if ($data['number'] > $usercnt || $data['number'] < 1) {
 222                      $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
 223                  }
 224              }
 225          }
 226  
 227          //try to detect group name duplicates
 228          $name = groups_parse_name(trim($data['namingscheme']), 0);
 229          if (groups_get_group_by_name($COURSE->id, $name)) {
 230              $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
 231          }
 232  
 233          // check grouping name duplicates
 234          if ( isset($data['grouping']) && $data['grouping'] == '-1') {
 235              $name = trim($data['groupingname']);
 236              if (empty($name)) {
 237                  $errors['groupingname'] = get_string('required');
 238              } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
 239                  $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
 240              }
 241          }
 242  
 243         /// Check the naming scheme
 244          if ($data['groupby'] == 'groups' and $data['number'] == 1) {
 245              // we can use the name as is because there will be only one group max
 246          } else {
 247              $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
 248              if ($matchcnt != 1) {
 249                  $errors['namingscheme'] = get_string('badnamingscheme', 'group');
 250              }
 251          }
 252  
 253          return $errors;
 254      }
 255  }