MDL-10383 - groups/groupings refactoring and improvements - basic UI for groupings...
[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)) {
77e90955 24 error('Group ID was incorrect');
2524b0f2 25 }
c4e953e6 26 if (empty($courseid)) {
27 $courseid = $group->courseid;
28
29 } else if ($courseid != $group->courseid) {
30 error('Course ID was incorrect');
2524b0f2 31 }
c4e953e6 32
33 if (!$course = get_record('course', 'id', $courseid)) {
34 error('Course ID was incorrect');
35 }
36
2524b0f2 37} else {
c4e953e6 38 if (!$course = get_record('course', 'id', $courseid)) {
39 error('Course ID was incorrect');
40 }
2524b0f2 41 $group = new object();
c4e953e6 42 $group->courseid = $course->id;
77e90955 43}
44
ddff2fa8 45require_login($course);
c4e953e6 46$context = get_context_instance(CONTEXT_COURSE, $course->id);
47require_capability('moodle/course:managegroups', $context);
48
49$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&amp;group='.$id;
77e90955 50
c4e953e6 51if ($id and $delete) {
2524b0f2 52 if (!$confirm) {
53 print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
54 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
55 $optionsno = array('id'=>$courseid);
c4e953e6 56 notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'group.php', 'index.php', $optionsyes, $optionsno, 'get', 'get');
2524b0f2 57 print_footer();
58 die;
77e90955 59
2524b0f2 60 } else if (confirm_sesskey()){
61 if (groups_delete_group($id)) {
62 // MDL-9983
63 $eventdata = new object();
64 $eventdata->group = $id;
65 $eventdata->course = $courseid;
66 events_trigger('group_deleted', $eventdata);
67 redirect('index.php?id='.$course->id);
68 } else {
c4e953e6 69 print_error('erroreditgroup', 'group', $returnurl);
2524b0f2 70 }
e4596a4a 71 }
72}
73
2524b0f2 74/// First create the form
ddff2fa8 75$editform = new group_form();
2524b0f2 76$editform->set_data($group);
e4596a4a 77
77e90955 78if ($editform->is_cancelled()) {
2524b0f2 79 redirect($returnurl);
80
77e90955 81} elseif ($data = $editform->get_data()) {
2524b0f2 82
2524b0f2 83 if ($data->id) {
ddff2fa8 84 if (!groups_update_group($data, $editform->_upload_manager)) {
2524b0f2 85 error('Error updating group');
77e90955 86 }
2524b0f2 87 } else {
ddff2fa8 88 if (!groups_create_group($data, $editform->_upload_manager)) {
2524b0f2 89 error('Error updating group');
e5138261 90 }
77e90955 91 }
77e90955 92
2524b0f2 93 redirect($returnurl);
94}
95
e4596a4a 96$strgroups = get_string('groups');
97$strparticipants = get_string('participants');
98
99if ($id) {
100 $strheading = get_string('editgroupsettings', 'group');
101} else {
102 $strheading = get_string('creategroup', 'group');
103}
104print_header("$course->shortname: ". $strheading,
2524b0f2 105 $course->fullname,
e4596a4a 106 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
107 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
2524b0f2 108 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
e4596a4a 109 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
110
111print_heading($strheading);
112
e4596a4a 113echo '<div id="grouppicture">';
114if ($id) {
115 print_group_picture($group, $course->id);
77e90955 116}
e4596a4a 117echo '</div>';
118$editform->display();
119print_footer($course);
77e90955 120?>