diff --git a/group/edit.php b/group/edit.php new file mode 100755 index 00000000000..83d0a97fd81 --- /dev/null +++ b/group/edit.php @@ -0,0 +1,267 @@ +libdir.'/moodlelib.php'); +require_once($CFG->libdir.'/uploadlib.php'); +require_once('lib.php'); +require_once('edit_form.php'); + +/// get url variables +$id = required_param('id', PARAM_INT); +$groupingid = optional_param('grouping', false, PARAM_INT); +$newgrouping= optional_param('newgrouping', false, PARAM_INT); +$groupid = optional_param('group', false, PARAM_INT); + +$delete = optional_param('delete', false, PARAM_BOOL); + +/// Course must be valid +if (!$course = get_record('course', 'id', $id)) { + error('Course ID was incorrect'); +} + +/// Delete action should not be called without a group id +if ($delete && !$groupid) { + error(get_string('errorinvalidgroup')); +} + +/// basic access control checks +if ($groupid) { + if (!$group = get_record('groups', 'id', $groupid)) { + error('Group ID was incorrect'); + } + require_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE), $course->id); +} + +/// First create the form +$editform = new group_edit_form('group.php', compact('group')); + +/// Override defaults if group is set +if (!empty($group)) { + $editform->set_data($group); +} + +if ($editform->is_cancelled()) { + redirect(groups_home_url($courseid, $groupid, $groupingid, false)); +} elseif ($data = $editform->get_data()) { + $success = true; + + // preprocess data + if ($delete) { + if ($success = groups_delete_group($groupid)) { + redirect(groups_home_url($course->id, null, $groupingid, false)); + } else { + print_error('erroreditgroup', 'group', groups_home_url($course->id)); + } + } elseif (empty($group)) { // New group + if (!$group = groups_create_group($course->id)) { + print_error('erroreditgroup'); + } else { + $success = (bool)$groupid = groups_create_group($course->id); + } + } elseif ($groupingid != $newgrouping) { // Moving group to new grouping + if ($groupingid != GROUP_NOT_IN_GROUPING) { + $success = $success && groups_remove_group_from_grouping($groupid, $groupingid); + } + } else { // Updating group + if (!groups_update_group($data, $course->id)) { + print_error('groupnotupdated'); + } + } + + // Handle file upload + if ($success) { + $um = new upload_manager('imagefile',false,false,$course=null,false,$modbytes=0,$silent=false,$allownull=true); + if ($um->preprocess_files()) { + require_once("$CFG->libdir/gdlib.php"); + + if (save_profile_image($groupid, $um, 'groups')) { + $groupsettings->picture = 1; + } + } else { + $success = false; + } + } + $success = $success && groups_set_group_settings($groupid, $groupsettings); + + if ($success) { + redirect(groups_home_url($course->id, $groupid, $groupingid, false)); + } else { + print_error('erroreditgroup', 'group', groups_home_url($course->id)); + } +} else { // Prepare and output form + $strgroups = get_string('groups'); + + if ($groupid) { + $strheading = get_string('editgroupsettings', 'group'); + } else { + $strheading = get_string('creategroup', 'group'); + } + + print_header("$course->shortname: ". $strheading, + $course->fullname, + "wwwroot/course/view.php?id=$courseid\">$course->shortname ". + "-> wwwroot/user/index.php?id=$courseid\">$strparticipants ". + "-> $strgroups", '', '', true, '', user_login_string($course, $USER)); + $editform->display(); + print_footer($course); +} + +/// OR, prepare the form. + +if ($groupid) { + // Form to edit existing group. + $group = groups_get_group_settings($groupid); + if (! $group) { + print_error('errorinvalidgroup', 'group', groups_home_url($courseid)); + } + $strname = s($group->name); + $strdesc = s($group->description); + + $strbutton = get_string('save', 'group'); + $strheading = get_string('editgroupsettings', 'group'); +} else { + // Form to create a new one. + $strname = get_string('defaultgroupname', 'group'); + $strdesc = ''; + $strbutton = $strheading = get_string('creategroup', 'group'); +} +$strgroups = get_string('groups'); +$strparticipants = get_string('participants'); +if ($delete) { + $strheading = get_string('deleteselectedgroup', 'group'); +} //else { $strheader = get_string('groupinfoedit'); } + +$maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes); +if (!empty($CFG->gdversion) and $maxbytes) { + $printuploadpicture = true; +} else { + $printuploadpicture = false; +} + +/// Print the page and form + +print_header("$course->shortname: ". $strheading, + $course->fullname, + "wwwroot/course/view.php?id=$courseid\">$course->shortname ". + "-> wwwroot/user/index.php?id=$courseid\">$strparticipants ". + "-> $strgroups", '', '', true, '', user_login_string($course, $USER)); + +$usehtmleditor = false; + +echo '