MDL-22574, added MoodleQuickForm_filemanager support, and added dispose function...
authorDongsheng Cai <unoter@gmail.com>
Tue, 8 Jun 2010 03:09:14 +0000 (03:09 +0000)
committerDongsheng Cai <unoter@gmail.com>
Tue, 8 Jun 2010 03:09:14 +0000 (03:09 +0000)
lib/formslib.php

index 20e3be8..304f83f 100644 (file)
@@ -554,7 +554,7 @@ abstract class moodleform {
 
         $element = $this->_form->getElement($elname);
 
-        if ($element instanceof MoodleQuickForm_filepicker) {
+        if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
             $values = $this->_form->exportValues($elname);
             if (empty($values[$elname])) {
                 return false;
@@ -604,7 +604,7 @@ abstract class moodleform {
 
         $element = $this->_form->getElement($elname);
 
-        if ($element instanceof MoodleQuickForm_filepicker) {
+        if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
             $values = $this->_form->exportValues($elname);
             if (empty($values[$elname])) {
                 return false;
@@ -626,6 +626,33 @@ abstract class moodleform {
         return false;
     }
 
+    /**
+     * Dispose form element draft files
+     *
+     * @global object $USER
+     * @param string $elname name of element
+     */
+    function dispose($elname) {
+        global $USER;
+
+        if (!$this->is_submitted() or !$this->is_validated()) {
+            return false;
+        }
+
+        $element = $this->_form->getElement($elname);
+
+        if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
+            $values = $this->_form->exportValues($elname);
+            if (empty($values[$elname])) {
+                return false;
+            }
+            $draftid = $values[$elname];
+            $fs = get_file_storage();
+            $context = get_context_instance(CONTEXT_USER, $USER->id);
+            $fs->delete_area_files($context->id, 'user_draft', $draftid);
+        }
+    }
+
     /**
      * Save file to local filesystem pool
      *
@@ -716,7 +743,7 @@ abstract class moodleform {
 
         $element = $this->_form->getElement($elname);
 
-        if ($element instanceof MoodleQuickForm_filepicker) {
+        if ($element instanceof MoodleQuickForm_filepicker || $element instanceof MoodleQuickForm_filemanager) {
             $values = $this->_form->exportValues($elname);
             if (empty($values[$elname])) {
                 return false;