MDL-10383 - deprecated get_groups(), user_group() and ismember()
[moodle.git] / group / edit.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 */
11/// include libraries
12require_once('../config.php');
13require_once($CFG->libdir.'/moodlelib.php');
14require_once($CFG->libdir.'/uploadlib.php');
15require_once('lib.php');
16require_once('edit_form.php');
17
18/// get url variables
08103c93 19$courseid = required_param('courseid', PARAM_INT);
2524b0f2 20$id = optional_param('id', 0, PARAM_INT);
08103c93
ML
21$delete = optional_param('delete', 0, PARAM_BOOL);
22$confirm = optional_param('confirm', 0, PARAM_BOOL);
77e90955 23
2524b0f2 24/// Course must be valid
80efbf98 25if (!$course = get_record('course', 'id', $courseid)) {
77e90955 26 error('Course ID was incorrect');
27}
28
29/// Delete action should not be called without a group id
80efbf98 30if ($delete && !$id) {
77e90955 31 error(get_string('errorinvalidgroup'));
32}
33
2524b0f2 34/// basic access control checks
35if (! $course = get_record('course', 'id', $courseid)) {
36 error("Incorrect course id ");
08103c93 37}
2524b0f2 38$context = get_context_instance(CONTEXT_COURSE, $course->id);
39require_capability('moodle/course:managegroups', $context);
40
41$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&amp;group='.$id;
08103c93 42
80efbf98 43if ($id) {
44 if (!$group = get_record('groups', 'id', $id)) {
77e90955 45 error('Group ID was incorrect');
2524b0f2 46 }
47 if ($group->courseid != $courseid) {
48 error('incorrect courseid');
49 }
50} else {
51 $group = new object();
52 $group->courseid = $courseid;
77e90955 53}
54
2524b0f2 55if ($id and $delete) {
77e90955 56
2524b0f2 57 if (!$confirm) {
58 print_header(get_string('deleteselectedgroup', 'group'), get_string('deleteselectedgroup', 'group'));
59 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
60 $optionsno = array('id'=>$courseid);
61 if (!$group = get_record('groups', 'id', $id)) {
62 error('Group ID was incorrect');
63 }
64 notice_yesno(get_string('deletegroupconfirm', 'group', $group->name), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
65 print_footer();
66 die;
77e90955 67
2524b0f2 68 } else if (confirm_sesskey()){
69 if (groups_delete_group($id)) {
70 // MDL-9983
71 $eventdata = new object();
72 $eventdata->group = $id;
73 $eventdata->course = $courseid;
74 events_trigger('group_deleted', $eventdata);
75 redirect('index.php?id='.$course->id);
76 } else {
77 print_error('erroreditgroup', 'group', groups_home_url($course->id));
78 }
e4596a4a 79 }
80}
81
2524b0f2 82/// First create the form
83$editform = new group_edit_form();
84$editform->set_data($group);
e4596a4a 85
77e90955 86if ($editform->is_cancelled()) {
2524b0f2 87 redirect($returnurl);
88
77e90955 89} elseif ($data = $editform->get_data()) {
2524b0f2 90
91 $result = false;
92 if ($data->id) {
93 if (!update_record('groups', $data)) {
94 error('Error updating group');
77e90955 95 }
2524b0f2 96 } else {
97 if (!$data->id = insert_record('groups', $data)) {
98 error('Error updating group');
e5138261 99 }
77e90955 100 }
77e90955 101
2524b0f2 102 //update image
103 require_once("$CFG->libdir/gdlib.php");
104 if (save_profile_image($data->id, $editform->_upload_manager, 'groups')) {
105 $data->picture = 1;
106 update_record('groups', $data);
77e90955 107 }
2524b0f2 108
109 redirect($returnurl);
110}
111
e4596a4a 112$strgroups = get_string('groups');
113$strparticipants = get_string('participants');
114
115if ($id) {
116 $strheading = get_string('editgroupsettings', 'group');
117} else {
118 $strheading = get_string('creategroup', 'group');
119}
120print_header("$course->shortname: ". $strheading,
2524b0f2 121 $course->fullname,
e4596a4a 122 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
123 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
2524b0f2 124 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
e4596a4a 125 "-> $strheading", '', '', true, '', user_login_string($course, $USER));
126
127print_heading($strheading);
128
e4596a4a 129echo '<div id="grouppicture">';
130if ($id) {
131 print_group_picture($group, $course->id);
77e90955 132}
e4596a4a 133echo '</div>';
134$editform->display();
135print_footer($course);
77e90955 136?>