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

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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   * Delete group
  20   *
  21   * @package   core_group
  22   * @copyright 2008 The Open University, s.marshall AT open.ac.uk
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once('../config.php');
  27  require_once ('lib.php');
  28  
  29  // Get and check parameters
  30  $courseid = required_param('courseid', PARAM_INT);
  31  $groupids = required_param('groups', PARAM_SEQUENCE);
  32  $confirm = optional_param('confirm', 0, PARAM_BOOL);
  33  
  34  $PAGE->set_url('/group/delete.php', array('courseid'=>$courseid,'groups'=>$groupids));
  35  $PAGE->set_pagelayout('standard');
  36  
  37  // Make sure course is OK and user has access to manage groups
  38  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  39      throw new \moodle_exception('invalidcourseid');
  40  }
  41  require_login($course);
  42  $context = context_course::instance($course->id);
  43  require_capability('moodle/course:managegroups', $context);
  44  $changeidnumber = has_capability('moodle/course:changeidnumber', $context);
  45  
  46  // Make sure all groups are OK and belong to course
  47  $groupidarray = explode(',',$groupids);
  48  $groupnames = array();
  49  foreach($groupidarray as $groupid) {
  50      if (!$group = $DB->get_record('groups', array('id' => $groupid))) {
  51          throw new \moodle_exception('invalidgroupid');
  52      }
  53      if (!empty($group->idnumber) && !$changeidnumber) {
  54          throw new \moodle_exception('grouphasidnumber', '', '', $group->name);
  55      }
  56      if ($courseid != $group->courseid) {
  57          throw new \moodle_exception('groupunknown', '', '', $group->courseid);
  58      }
  59      $groupnames[] = format_string($group->name);
  60  }
  61  
  62  $returnurl='index.php?id='.$course->id;
  63  
  64  if(count($groupidarray)==0) {
  65      throw new \moodle_exception('errorselectsome', 'group', $returnurl);
  66  }
  67  
  68  if ($confirm && data_submitted()) {
  69      if (!confirm_sesskey() ) {
  70          throw new \moodle_exception('confirmsesskeybad', 'error', $returnurl);
  71      }
  72  
  73      foreach($groupidarray as $groupid) {
  74          groups_delete_group($groupid);
  75      }
  76  
  77      redirect($returnurl);
  78  } else {
  79      $PAGE->set_title(get_string('deleteselectedgroup', 'group'));
  80      $PAGE->set_heading($course->fullname . ': '. get_string('deleteselectedgroup', 'group'));
  81      echo $OUTPUT->header();
  82      $optionsyes = array('courseid'=>$courseid, 'groups'=>$groupids, 'sesskey'=>sesskey(), 'confirm'=>1);
  83      $optionsno = array('id'=>$courseid);
  84      if(count($groupnames)==1) {
  85          $message=get_string('deletegroupconfirm', 'group', $groupnames[0]);
  86      } else {
  87          $message=get_string('deletegroupsconfirm', 'group').'<ul>';
  88          foreach($groupnames as $groupname) {
  89              $message.='<li>'.$groupname.'</li>';
  90          }
  91          $message.='</ul>';
  92      }
  93      $formcontinue = new single_button(new moodle_url('delete.php', $optionsyes), get_string('yes'), 'post');
  94      $formcancel = new single_button(new moodle_url('index.php', $optionsno), get_string('no'), 'get');
  95      echo $OUTPUT->confirm($message, $formcontinue, $formcancel);
  96      echo $OUTPUT->footer();
  97  }