"MDL-14129, fix print_error"
[moodle.git] / group / group.php
CommitLineData
77e90955 1<?php
2/**
3 * Create group OR edit group settings.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author N.D.Freear AT open.ac.uk
2524b0f2 7 * @author J.White AT open.ac.uk
77e90955 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
10 */
c4e953e6 11
77e90955 12require_once('../config.php');
77e90955 13require_once('lib.php');
c4e953e6 14require_once('group_form.php');
77e90955 15
16/// get url variables
62d63838 17$courseid = optional_param('courseid', 0, PARAM_INT);
18$id = optional_param('id', 0, PARAM_INT);
19$delete = optional_param('delete', 0, PARAM_BOOL);
20$confirm = optional_param('confirm', 0, PARAM_BOOL);
77e90955 21
80efbf98 22if ($id) {
23 if (!$group = get_record('groups', 'id', $id)) {
5a2a5331 24 print_error('Group ID was incorrect');
2524b0f2 25 }
3980c306 26 $group->description = clean_text($group->description);
c4e953e6 27 if (empty($courseid)) {
28 $courseid = $group->courseid;
29
30 } else if ($courseid != $group->courseid) {
5a2a5331 31 print_error('Course ID was incorrect');
2524b0f2 32 }
c4e953e6 33
34 if (!$course = get_record('course', 'id', $courseid)) {
5a2a5331 35 print_error('Course ID was incorrect');
c4e953e6 36 }
37
2524b0f2 38} else {
c4e953e6 39 if (!$course = get_record('course', 'id', $courseid)) {
5a2a5331 40 print_error('Course ID was incorrect');
c4e953e6 41 }
2524b0f2 42 $group = new object();
c4e953e6 43 $group->courseid = $course->id;
77e90955 44}
45
ddff2fa8 46require_login($course);
c4e953e6 47$context = get_context_instance(CONTEXT_COURSE, $course->id);
48require_capability('moodle/course:managegroups', $context);
49
50$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&amp;group='.$id;
77e90955 51
c4e953e6 52if ($id and $delete) {
2524b0f2 53 if (!$confirm) {
54 print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
55 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
56 $optionsno = array('id'=>$courseid);
c4e953e6 57 notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'group.php', 'index.php', $optionsyes, $optionsno, 'get', 'get');
2524b0f2 58 print_footer();
59 die;
77e90955 60
2524b0f2 61 } else if (confirm_sesskey()){
62 if (groups_delete_group($id)) {
63 // MDL-9983
64 $eventdata = new object();
65 $eventdata->group = $id;
66 $eventdata->course = $courseid;
67 events_trigger('group_deleted', $eventdata);
68 redirect('index.php?id='.$course->id);
69 } else {
c4e953e6 70 print_error('erroreditgroup', 'group', $returnurl);
2524b0f2 71 }
e4596a4a 72 }
73}
74
2524b0f2 75/// First create the form
ddff2fa8 76$editform = new group_form();
2524b0f2 77$editform->set_data($group);
e4596a4a 78
77e90955 79if ($editform->is_cancelled()) {
2524b0f2 80 redirect($returnurl);
81
77e90955 82} elseif ($data = $editform->get_data()) {
2524b0f2 83
2524b0f2 84 if ($data->id) {
ddff2fa8 85 if (!groups_update_group($data, $editform->_upload_manager)) {
5a2a5331 86 print_error('Error updating group');
77e90955 87 }
2524b0f2 88 } else {
778918fd 89 if (!$id = groups_create_group($data, $editform->_upload_manager)) {
5a2a5331 90 print_error('Error creating group');
e5138261 91 }
778918fd 92 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&amp;group='.$id;
77e90955 93 }
77e90955 94
2524b0f2 95 redirect($returnurl);
96}
97
e4596a4a 98$strgroups = get_string('groups');
99$strparticipants = get_string('participants');
100
101if ($id) {
102 $strheading = get_string('editgroupsettings', 'group');
103} else {
104 $strheading = get_string('creategroup', 'group');
105}
778918fd 106
107
108$navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
109 array('name'=>$strgroups, 'link'=>$CFG->wwwroot.'/group/index.php?id='.$courseid, 'type'=>'misc'),
110 array('name'=>$strheading, 'link'=>'', 'type'=>'misc'));
111$navigation = build_navigation($navlinks);
112
113/// Print header
114print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
e4596a4a 115
e4596a4a 116echo '<div id="grouppicture">';
117if ($id) {
118 print_group_picture($group, $course->id);
77e90955 119}
e4596a4a 120echo '</div>';
121$editform->display();
122print_footer($course);
77e90955 123?>