]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15405: moving function into file_storage & simplify things a bit.
authorscyrma <scyrma>
Tue, 2 Sep 2008 07:30:18 +0000 (07:30 +0000)
committerscyrma <scyrma>
Tue, 2 Sep 2008 07:30:18 +0000 (07:30 +0000)
lib/file/file_storage.php
lib/filelib.php

index e6a16c3359c44f77d4f0f142f84872f1fc513a09..ab7f2f02fcd8350b9cf5b58eeeedd496e59879f9 100644 (file)
@@ -691,6 +691,47 @@ class file_storage {
         $DB->insert_record('files_cleanup', $rec);
     }
 
+    /**
+     * Moves a file from user_draft filearea to it's final filearea and changes
+     * it's itemid if desired.
+     *
+     * @param int $draftfileid file id of the draft file
+     * @param object $context new context of the file
+     * @param string $filearea destination filearea
+     * @param int $itemid new itemid (if null, keeps current)
+     * @param string $filepath new file path (if null, keeps current)
+     * @param string $filename new file name (if null, keeps current)
+     * @return new stored_file
+     */
+    function move_to_final_destination($draftfileid, $context, $filearea, $itemid = null, $filepath = null, $filename = null) {
+
+        if (!$file = $this->get_file_by_id($draftfileid)) {
+            return false;
+        }
+        $params = $file->get_params();
+
+        $fs = get_file_storage();
+        
+        $newrecord = new object();
+        $newrecord->contextid = $context->id;
+        $newrecord->filearea = $filearea;
+        $newrecord->itemid = ($itemid ? $itemid : $params['itemid']);
+        $newrecord->filepath = ($filepath ? $filepath : $params['filepath']);
+        $newrecord->filename = ($filename ? $filename : $params['filename']);
+            
+        $newrecord->timecreated = $file->get_timecreated();
+        $newrecord->timemodified = $file->get_timemodified();
+        $newrecord->mimetype = $file->get_mimetype();
+        $newrecord->userid = $file->get_userid();
+            
+        if (!$newfile = $this->create_file_from_storedfile($newrecord, $draftfileid)) {
+            return false;
+        }
+
+        $file->delete();
+        return $newfile;
+    }
+    
     /**
      * Cron cleanup job.
      */
index 4c4a739019259a33c649b83cc356caf4c5fa3bc1..e178f273046426701c6eb016b5d810e3c197b227 100644 (file)
@@ -88,48 +88,6 @@ function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $cu
     return preg_replace($re, $newurl, $text);
 }
 
-/**
- * Moves a file from user_draft filearea to it's final filearea and changes
- * it's itemid if desired.
- *
- * @param object $context current context of the file
- * @param int $itemid current itemid
- * @param string $filename name of the file
- * @param string $filepath path of the file (should usually be '/')
- * @param string $newfilearea destination filearea
- * @param object $newcontext new context of the file
- * @param int $newitemid new itemid (if null, keeps current)
- * @param string $newfilename new file name (if null, keeps current)
- * @param string $newfilepath new file path (if null, keeps current)
- */
-function move_file_to_final_destination($context, $itemid, $filename, $filepath, $newfilearea, $newcontext, $newitemid = null, $newfilename = null, $newfilepath = null) {
-    
-    $fb = get_file_browser();
-    $file = $fb->get_file_info($context, 'user_draft', $itemid, $filepath, $filename);
-    $fid = $file->get_id(); // todo: this doesn't work. silly me can't figure out where to get file id.
-
-    // create a new file, based on $file
-    $fs = get_file_storage();
-    $newrecord = new object();
-    $newrecord->contextid = ($newcontext ? $newcontext->id : $context->id);
-    $newrecord->filearea = $newfilearea;
-    $newrecord->itemid = ($newitemid ? $newitemid : $itemid);
-    $newrecord->filepath = ($newfilepath ? $newfilepath : $filepath);
-    $newrecord->filename = ($newfilename ? $newfilename : $filename);
-    
-    $newrecord->timecreated = $file->get_timecreated();
-    $newrecord->timemodified = $file->get_timemodified();
-    $newrecord->mimetype = $file->get_mimetype();
-    $newrecord->userid = $file->get_userid();
-
-    if ($newfile = $finalfile->create_file_from_storedfile($newrecord, $fid)) {
-        // delete original file from user_draft
-        $file->delete();
-        // todo: return file_info for the new file.
-        return true;
-    }
-}
-
 /**
  * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
  * Due to security concerns only downloads from http(s) sources are supported.