MDL-16756: Groups bugfixes and multiple select support for deletion
[moodle.git] / group / delete.php
CommitLineData
f6eece19 1<?php
2/**
3 * Delete group
4 *
5 * @copyright &copy; 2008 The Open University
6 * @author s.marshall AT open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package groups
9 */
10
11require_once('../config.php');
12require_once('lib.php');
13
14// Get and check parameters
15$courseid = required_param('courseid', PARAM_INT);
16$groupids = required_param('groups', PARAM_SEQUENCE);
17$confirm = optional_param('confirm', 0, PARAM_BOOL);
18
19// Make sure course is OK and user has access to manage groups
20if (!$course = $DB->get_record('course', array('id' => $courseid))) {
21 error('Course ID was incorrect');
22}
23require_login($course);
24$context = get_context_instance(CONTEXT_COURSE, $course->id);
25require_capability('moodle/course:managegroups', $context);
26
27// Make sure all groups are OK and belong to course
28$groupidarray = explode(',',$groupids);
29$groupnames = array();
30foreach($groupidarray as $groupid) {
31 if (!$group = $DB->get_record('groups', array('id' => $groupid))) {
32 error('Group ID was incorrect');
33 }
34 if ($courseid != $group->courseid) {
35 error('Group not on required course');
36 }
37 $groupnames[] = format_string($group->name);
38}
39
40$returnurl='index.php?id='.$course->id;
41
42if(count($groupidarray)==0) {
43 print_error('errorselectsome','group',$returnurl);
44}
45
46if ($confirm && data_submitted()) {
47 if (!confirm_sesskey() ) {
48 print_error('confirmsesskeybad','error',$returnurl);
49 }
50 $DB->begin_sql();
51 foreach($groupidarray as $groupid) {
52 if (!groups_delete_group($groupid)) {
53 print_error('erroreditgroup', 'group', $returnurl);
54 }
55 }
56 $DB->commit_sql();
57 redirect($returnurl);
58} else {
59 print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
60 $optionsyes = array('courseid'=>$courseid, 'groups'=>$groupids, 'sesskey'=>sesskey(), 'confirm'=>1);
61 $optionsno = array('id'=>$courseid);
62 if(count($groupnames)==1) {
63 $message=get_string('deletegroupconfirm', 'group', $groupnames[0]);
64 } else {
65 $message=get_string('deletegroupsconfirm', 'group').'<ul>';
66 foreach($groupnames as $groupname) {
67 $message.='<li>'.$groupname.'</li>';
68 }
69 $message.='</ul>';
70 }
71 notice_yesno($message, 'delete.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
72 print_footer();
73}
74?>