MDL-46416 group: Allow group pictures to be deleted
authorFrederic Massart <fred@moodle.com>
Thu, 6 Nov 2014 07:39:12 +0000 (15:39 +0800)
committerFrederic Massart <fred@moodle.com>
Tue, 25 Nov 2014 03:58:13 +0000 (11:58 +0800)
group/group_form.php
group/lib.php

index c29947a..1ebf853 100644 (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
      *
index e1645b9..f97c026 100644 (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));
     }