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.
/group/ -> grouping.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [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  /**
  19   * Create grouping OR edit grouping settings.
  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  require_once('../config.php');
  26  require_once ('lib.php');
  27  require_once ('grouping_form.php');
  28  
  29  /// get url variables
  30  $courseid = optional_param('courseid', 0, PARAM_INT);
  31  $id       = optional_param('id', 0, PARAM_INT);
  32  $delete   = optional_param('delete', 0, PARAM_BOOL);
  33  $confirm  = optional_param('confirm', 0, PARAM_BOOL);
  34  
  35  $url = new moodle_url('/group/grouping.php');
  36  if ($id) {
  37      $url->param('id', $id);
  38      if (!$grouping = $DB->get_record('groupings', array('id'=>$id))) {
  39          throw new \moodle_exception('invalidgroupid');
  40      }
  41      $grouping->description = clean_text($grouping->description);
  42      if (empty($courseid)) {
  43          $courseid = $grouping->courseid;
  44      } else if ($courseid != $grouping->courseid) {
  45          throw new \moodle_exception('invalidcourseid');
  46      } else {
  47          $url->param('courseid', $courseid);
  48      }
  49  
  50      if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  51          throw new \moodle_exception('invalidcourseid');
  52      }
  53  
  54  } else {
  55      $url->param('courseid', $courseid);
  56      if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  57          throw new \moodle_exception('invalidcourseid');
  58      }
  59      $grouping = new stdClass();
  60      $grouping->courseid = $course->id;
  61  }
  62  
  63  $PAGE->set_url($url);
  64  
  65  require_login($course);
  66  $context = context_course::instance($course->id);
  67  require_capability('moodle/course:managegroups', $context);
  68  
  69  $strgroupings = get_string('groupings', 'group');
  70  $PAGE->set_title($strgroupings);
  71  $PAGE->set_heading($course->fullname. ': '.$strgroupings);
  72  $PAGE->set_pagelayout('admin');
  73  navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $course->id)));
  74  
  75  $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
  76  
  77  if ($id and $delete) {
  78      if (!empty($grouping->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
  79          throw new \moodle_exception('groupinghasidnumber', '', '', $grouping->name);
  80      }
  81      if (!$confirm) {
  82          $PAGE->set_title(get_string('deletegrouping', 'group'));
  83          $PAGE->set_heading($course->fullname. ': '. get_string('deletegrouping', 'group'));
  84          echo $OUTPUT->header();
  85          $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
  86          $optionsno  = array('id'=>$courseid);
  87          $formcontinue = new single_button(new moodle_url('grouping.php', $optionsyes), get_string('yes'), 'get');
  88          $formcancel = new single_button(new moodle_url('groupings.php', $optionsno), get_string('no'), 'get');
  89          echo $OUTPUT->confirm(get_string('deletegroupingconfirm', 'group', $grouping->name), $formcontinue, $formcancel);
  90          echo $OUTPUT->footer();
  91          die;
  92  
  93      } else if (confirm_sesskey()){
  94          if (groups_delete_grouping($id)) {
  95              redirect($returnurl);
  96          } else {
  97              throw new \moodle_exception('erroreditgrouping', 'group', $returnurl);
  98          }
  99      }
 100  }
 101  
 102  // Prepare the description editor: We do support files for grouping descriptions
 103  $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$course->maxbytes, 'trust'=>true, 'context'=>$context, 'noclean'=>true);
 104  if (!empty($grouping->id)) {
 105      $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', $grouping->id);
 106  } else {
 107      $grouping = file_prepare_standard_editor($grouping, 'description', $editoroptions, $context, 'grouping', 'description', null);
 108  }
 109  
 110  /// First create the form
 111  $editform = new grouping_form(null, compact('editoroptions'));
 112  $editform->set_data($grouping);
 113  
 114  if ($editform->is_cancelled()) {
 115      redirect($returnurl);
 116  
 117  } elseif ($data = $editform->get_data()) {
 118      $success = true;
 119      if (!has_capability('moodle/course:changeidnumber', $context)) {
 120          // Remove the idnumber if the user doesn't have permission to modify it
 121          unset($data->idnumber);
 122      }
 123  
 124      if ($data->id) {
 125          groups_update_grouping($data, $editoroptions);
 126      } else {
 127          groups_create_grouping($data, $editoroptions);
 128      }
 129  
 130      redirect($returnurl);
 131  
 132  }
 133  
 134  $strparticipants = get_string('participants');
 135  if ($id) {
 136      $strheading = get_string('editgroupingsettings', 'group');
 137  } else {
 138      $strheading = get_string('creategrouping', 'group');
 139  }
 140  
 141  $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
 142  $PAGE->navbar->add($strgroupings, new moodle_url('/group/groupings.php', array('id'=>$courseid)));
 143  $PAGE->navbar->add($strheading);
 144  
 145  /// Print header
 146  echo $OUTPUT->header();
 147  echo $OUTPUT->heading($strheading);
 148  $editform->display();
 149  echo $OUTPUT->footer();