MDL-10383 - group edit form refactoring and minor bugfixing
[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
c4e953e6 17$courseid = optional_param('courseid', PARAM_INT);
2524b0f2 18$id = optional_param('id', 0, PARAM_INT);
08103c93
ML
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
c4e953e6 45$context = get_context_instance(CONTEXT_COURSE, $course->id);
46require_capability('moodle/course:managegroups', $context);
47
48$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&amp;group='.$id;
77e90955 49
c4e953e6 50if ($id and $delete) {
2524b0f2 51 if (!$confirm) {
52 print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
53 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
54 $optionsno = array('id'=>$courseid);
c4e953e6 55 notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'group.php', 'index.php', $optionsyes, $optionsno, 'get', 'get');
2524b0f2 56 print_footer();
57 die;
77e90955 58
2524b0f2 59 } else if (confirm_sesskey()){
60 if (groups_delete_group($id)) {
61 // MDL-9983
62 $eventdata = new object();
63 $eventdata->group = $id;
64 $eventdata->course = $courseid;
65 events_trigger('group_deleted', $eventdata);
66 redirect('index.php?id='.$course->id);
67 } else {
c4e953e6 68 print_error('erroreditgroup', 'group', $returnurl);
2524b0f2 69 }
e4596a4a 70 }
71}
72
2524b0f2 73/// First create the form
74$editform = new group_edit_form();
75$editform->set_data($group);
e4596a4a 76
77e90955 77if ($editform->is_cancelled()) {
2524b0f2 78 redirect($returnurl);
79
77e90955 80} elseif ($data = $editform->get_data()) {
2524b0f2 81
82 $result = false;
83 if ($data->id) {
84 if (!update_record('groups', $data)) {
85 error('Error updating group');
77e90955 86 }
2524b0f2 87 } else {
88 if (!$data->id = insert_record('groups', $data)) {
89 error('Error updating group');
e5138261 90 }
77e90955 91 }
77e90955 92
2524b0f2 93 //update image
94 require_once("$CFG->libdir/gdlib.php");
95 if (save_profile_image($data->id, $editform->_upload_manager, 'groups')) {
96 $data->picture = 1;
97 update_record('groups', $data);
77e90955 98 }
2524b0f2 99
100 redirect($returnurl);
101}
102
e4596a4a 103$strgroups = get_string('groups');
104$strparticipants = get_string('participants');
105
106if ($id) {
107 $strheading = get_string('editgroupsettings', 'group');
108} else {
109 $strheading = get_string('creategroup', 'group');
110}
111print_header("$course->shortname: ". $strheading,
2524b0f2 112 $course->fullname,
e4596a4a 113 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
114 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
2524b0f2 115 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
e4596a4a 116 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
117
118print_heading($strheading);
119
e4596a4a 120echo '<div id="grouppicture">';
121if ($id) {
122 print_group_picture($group, $course->id);
77e90955 123}
e4596a4a 124echo '</div>';
125$editform->display();
126print_footer($course);
77e90955 127?>