]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14589 sample function for draft area files migration and relinking
authorskodak <skodak>
Sun, 7 Sep 2008 22:34:18 +0000 (22:34 +0000)
committerskodak <skodak>
Sun, 7 Sep 2008 22:34:18 +0000 (22:34 +0000)
lib/filelib.php

index 7f3c6f5736b3097461e8467d6ff8847507313504..8afd5a340d67ffab663b7c3531a8ab605e6514cb 100644 (file)
@@ -58,6 +58,47 @@ function get_file_url($path, $options=null, $type='coursefile') {
     return $ffurl;
 }
 
+/**
+ * Converts absolute links in text and moves draft files.
+ * @param int $draftitemid
+ * @param string $text usually html text with embedded links to draft area
+ * @param int $contextid
+ * @param string $filearea
+ * @param int $itemid
+ * @param boolean $https force https
+ * @return string text with relative links starting with @@PLUGINFILE@@
+ */
+function file_convert_draftarea($text, $draftitemid, $contextid, $filearea, $itemid, $https=false) {
+    global $CFG, $USER;
+
+    if ($CFG->slasharguments) {
+        $draftbase = "$CFG->wwwroot/draftfile.php/user_draft/$draftitemid/";
+    } else {
+        $draftbase = "$CFG->wwwroot/draftfile.php?file=/user_draft/$draftitemid/";
+    }
+
+    if ($https) {
+        $draftbase = str_replace('http://', 'https://', $draftbase);
+    }
+
+    // replace absolute links
+    $text = str_ireplace($draftbase, '@@PLUGINFILE@@/');
+
+    $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+
+    // move draft files
+    $fs = get_file_storage();
+    if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id', 'false')) {
+        $file_record = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid);
+        foreach ($files as $file) {
+            $fs->create_file_from_stored($file_record, $file);
+            $file->delete();
+        }
+    }
+
+    return $text;
+}
+
 /**
  * Finds occurences of a link to "draftfile.php" in text and replaces the 
  * address based on passed information. Matching is performed using the given