]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15405: Add support for relinking of inline media
authorscyrma <scyrma>
Thu, 4 Sep 2008 01:56:50 +0000 (01:56 +0000)
committerscyrma <scyrma>
Thu, 4 Sep 2008 01:56:50 +0000 (01:56 +0000)
lib/filelib.php
mod/forum/lib.php

index e178f273046426701c6eb016b5d810e3c197b227..08e6bd4d76a853508450f1e2690a84a5d771b329 100644 (file)
@@ -66,6 +66,7 @@ function get_file_url($path, $options=null, $type='coursefile') {
  * must be called once for each file.
  *
  * @uses $CFG
+ * @see file_storage::move_draft_to_final()
  *
  * @param $text string text to modify
  * @param $contextid int context that the file should be assigned to
@@ -75,17 +76,38 @@ function get_file_url($path, $options=null, $type='coursefile') {
  * @param $currentcontextid int the current contextid of the file
  * @param $currentfilearea string the current filearea of the file (defaults
  *   to "user_draft")
+ * @param $overwrite bool wether the files should overwrite existing files
  * @return string modified $text, or null if an error occured.
  */
-function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $currentcontextid, $currentfilearea = 'userdraft') {
+function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $currentcontextid, $currentfilearea = 'user_draft', $overwrite = false) {
     global $CFG;
 
-    //TODO: complete, test and adjust if the filearea is removed from the url.
+    $context = get_context_instance_by_id($contextid);
+    $fs = get_file_storage();
 
-    $re = $CFG->wwwroot .'\/draftfile.php\/'. $currentcontextid .'\/'. $currentfilearea .'\/'. $itemid .'\/(?[A-Fa-f0-9]+)\/([.]+)/';
-    $newurl = $CFG->wwwroot .'/userfile.php/'. $contextid .'/'. $filearea .'/'. $itemid .'/'. $filepath .'/\0';
-    
-    return preg_replace($re, $newurl, $text);
+    $re = '|'. preg_quote($CFG->wwwroot) .'/draftfile.php/'. $currentcontextid .'/'. $currentfilearea .'/([0-9]+)/(?:[A-Fa-f0-9]+)/([^"]+)|';
+    $matches = array();
+    if (!preg_match_all($re, $text, $matches, PREG_SET_ORDER)) {
+        return $text; // no draftfile url in text.
+    }
+
+    foreach($matches as $file) {
+        if ($newfiles = $fs->move_draft_to_final($file[1], $contextid, $filearea, $itemid, $filepath, $overwrite)) {
+            foreach($newfiles as $newfile) {
+                var_dump($newfile);
+                if ($context->contextlevel == 'CONTEXT_USER') {
+                    //TODO: get the good url for userfile.php
+                    //$newurl = $CFG->wwwroot .'/userfile.php/'. $contextid .'/'. $filearea .'/'. $itemid;
+                } else {
+                    $newurl = $CFG->wwwroot .'/pluginfile.php/'. $contextid .'/'. $filearea .'/'. $itemid . $newfile->get_filepath() . $newfile->get_filename();
+                }
+                if ($newurl) { // can be removed whenever userfile.php newurl is properly built
+                    $text = str_replace('"'. $file[0] .'"', '"'. $newurl .'"', $text);
+                }
+            }
+        } // else file not found, wrong file, or string is just not a file so we leave it alone.
+    }
+    return $text;
 }
 
 /**
index de39837629c7a63abdf006458f6059eea80c62f6..45fadf3bbc72f001933884ca90a5aeea25e4d254 100644 (file)
@@ -3969,7 +3969,8 @@ function forum_print_attachments($post, $cm, $type) {
 function forum_pluginfile($course, $cminfo, $context, $filearea, $args) {
     global $CFG, $DB;
 
-    if ($filearea !== 'forum_attachment') {
+    $fileareas = array('forum_attachment', 'forum_post');
+    if (!in_array($filearea, $fileareas)) {
         return false;
     }
 
@@ -4053,7 +4054,24 @@ function forum_add_attachment($post, $cm, $mform=null, &$message=null, $remove_p
 }
 
 /**
- *
+ * Relinks urls linked to draftfile.php in $post->message.
+ */
+function forum_relink_inline_attachments($post, $cm){
+    global $DB;
+    
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    $authorcontext = get_context_instance(CONTEXT_USER, $post->userid);
+
+    if ($post->message = file_rewrite_urls($post->message, $context->id, '/', 'forum_post', $post->id, $authorcontext->id)) {
+      $DB->set_field("forum_posts", "message", $post->message, array("id" => $post->id));
+      return true;
+    } else {
+      return false;
+    }
+}
+
+/**
+ * Add a new post in an existing discussion.
  */
 function forum_add_new_post($post, $mform, &$message) {
     global $USER, $CFG, $DB;
@@ -4071,6 +4089,7 @@ function forum_add_new_post($post, $mform, &$message) {
         return false;
     }
 
+    forum_relink_inline_attachments($post, $cm);   
     forum_add_attachment($post, $cm, $mform, $message, false);
 
     // Update discussion modified date
@@ -4085,7 +4104,7 @@ function forum_add_new_post($post, $mform, &$message) {
 }
 
 /**
- *
+ * Update a post
  */
 function forum_update_post($post, $mform, &$message) {
     global $USER, $CFG, $DB;
@@ -4113,6 +4132,7 @@ function forum_update_post($post, $mform, &$message) {
         return false;
     }
 
+    forum_relink_inline_attachments($post, $cm);
     forum_add_attachment($post, $cm, $mform, $message, true);
 
     if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
@@ -4175,7 +4195,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) {
         return 0;
     }
 
-    // save attachment if present
+    forum_relink_inline_attachments($post, $cm);
     forum_add_attachment($post, $cm, $mform, $message, false);
 
     if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {