MDL-46416 group: Allow group pictures to be deleted

This commit is contained in:
Frederic Massart
2014-11-25 11:58:13 +08:00
parent 21449d01b7
commit d996c620a9
2 changed files with 49 additions and 6 deletions
+36
View File
@@ -66,6 +66,11 @@ class group_form extends moodleform {
$mform->addHelpButton('enrolmentkey', 'enrolmentkey', 'group');
$mform->setType('enrolmentkey', PARAM_RAW);
$mform->addElement('static', 'currentpicture', get_string('currentpicture'));
$mform->addElement('checkbox', 'deletepicture', get_string('delete'));
$mform->setDefault('deletepicture', 0);
$options = array(get_string('no'), get_string('yes'));
$mform->addElement('select', 'hidepicture', get_string('hidepicture'), $options);
@@ -81,6 +86,37 @@ class group_form extends moodleform {
$this->add_action_buttons();
}
/**
* Extend the form definition after the data has been parsed.
*/
public function definition_after_data() {
global $COURSE, $DB;
$mform = $this->_form;
$groupid = $mform->getElementValue('id');
if ($group = $DB->get_record('groups', array('id' => $groupid))) {
// Print picture.
if (!($pic = print_group_picture($group, $COURSE->id, true, true, false))) {
$pic = get_string('none');
if ($mform->elementExists('deletepicture')) {
$mform->removeElement('deletepicture');
}
}
$imageelement = $mform->getElement('currentpicture');
$imageelement->setValue($pic);
} else {
if ($mform->elementExists('currentpicture')) {
$mform->removeElement('currentpicture');
}
if ($mform->elementExists('deletepicture')) {
$mform->removeElement('deletepicture');
}
}
}
/**
* Form validation
*
+13 -6
View File
@@ -344,18 +344,25 @@ function groups_update_group_icon($group, $data, $editform) {
$fs = get_file_storage();
$context = context_course::instance($group->courseid, MUST_EXIST);
$newpicture = $group->picture;
//TODO: it would make sense to allow picture deleting too (skodak)
if ($iconfile = $editform->save_temp_file('imagefile')) {
if (!empty($data->deletepicture)) {
$fs->delete_area_files($context->id, 'group', 'icon', $group->id);
$newpicture = 0;
} else if ($iconfile = $editform->save_temp_file('imagefile')) {
if ($rev = process_new_icon($context, 'group', 'icon', $group->id, $iconfile)) {
$DB->set_field('groups', 'picture', $rev, array('id'=>$group->id));
$group->picture = $rev;
$newpicture = $rev;
} else {
$fs->delete_area_files($context->id, 'group', 'icon', $group->id);
$DB->set_field('groups', 'picture', 0, array('id'=>$group->id));
$group->picture = 0;
$newpicture = 0;
}
@unlink($iconfile);
}
if ($newpicture != $group->picture) {
$DB->set_field('groups', 'picture', $newpicture, array('id' => $group->id));
$group->picture = $newpicture;
// Invalidate the group data as we've updated the group record.
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($group->courseid));
}