MDL-33832 Fixed filling of files.source field
authorMarina Glancy <marina@moodle.com>
Wed, 20 Jun 2012 08:09:37 +0000 (16:09 +0800)
committerMarina Glancy <marina@moodle.com>
Wed, 20 Jun 2012 08:09:37 +0000 (16:09 +0800)
- also supress the error on unserialise (because people already may have broken files.source field)
- also display details about unparseable JSON error
- make source field look the same across repositories

lib/filelib.php
lib/form/filemanager.js
repository/boxnet/lib.php
repository/dropbox/lib.php
repository/filepicker.php
repository/lib.php
repository/repository_ajax.php
repository/s3/lib.php
repository/upload/lib.php

index 0d18cfa..45f4f7a 100644 (file)
@@ -601,7 +601,7 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {
             }
             // find the file this draft file was created from and count all references in local
             // system pointing to that file
-            $source = unserialize($file->get_source());
+            $source = @unserialize($file->get_source());
             if (isset($source->original)) {
                 $item->refcount = $fs->search_references_count($source->original);
             }
@@ -679,7 +679,7 @@ function file_get_submitted_draft_itemid($elname) {
  * @return stored_file
  */
 function file_restore_source_field_from_draft_file($storedfile) {
-    $source = unserialize($storedfile->get_source());
+    $source = @unserialize($storedfile->get_source());
     if (!empty($source)) {
         if (is_object($source)) {
             $restoredsource = $source->source;
index e433d35..ac7ffaa 100644 (file)
@@ -155,9 +155,8 @@ M.form_filemanager.init = function(Y, options) {
                         try {
                             data = Y.JSON.parse(o.responseText);
                         } catch(e) {
-                            // TODO display error
                             scope.print_msg(M.str.repository.invalidjson, 'error');
-                            //scope.display_error(M.str.repository.invalidjson+'<pre>'+stripHTML(o.responseText)+'</pre>', 'invalidjson')
+                            Y.error(M.str.repository.invalidjson+":\n"+o.responseText);
                             return;
                         }
                         if (data && data.tree && scope.set_current_tree) {
index 6578afe..323cc40 100644 (file)
@@ -319,7 +319,7 @@ class repository_boxnet extends repository {
         $fileid = array_pop($array);
         $fileinfo = $this->boxclient->get_file_info($fileid);
         if (!empty($fileinfo)) {
-            return 'Box:' . (string)$fileinfo->file_name;
+            return 'Box: ' . (string)$fileinfo->file_name;
         } else {
             return $url;
         }
index 7a12bb9..186e63b 100644 (file)
@@ -432,7 +432,7 @@ class repository_dropbox extends repository {
      * @return string|null
      */
     public function get_file_source_info($filepath) {
-        return 'Dropbox:' . $filepath;
+        return 'Dropbox: ' . $filepath;
     }
 
     /**
index ebd9c9f..20b67ca 100644 (file)
@@ -306,7 +306,8 @@ case 'download':
         $record->userid       = $USER->id;
         $record->contextid = $user_context->id;
 
-        $record->source = serialize((object)array('source' => $thefile['url']));
+        $sourcefield = $repo->get_file_source_info($thefile['url']);
+        $record->source = repository::build_source_field($sourcefield);
         try {
             $info = repository::move_to_filepool($thefile['path'], $record);
             redirect($home_url, get_string('downloadsucc', 'repository'));
index db4e45b..db99489 100644 (file)
@@ -1233,6 +1233,9 @@ abstract class repository {
      * @return string|null
      */
     public function get_file_source_info($url) {
+        if ($this->has_moodle_files()) {
+            return $this->get_reference_details($url, 0);
+        }
         return $url;
     }
 
index 45f0662..6919b3e 100644 (file)
@@ -241,6 +241,10 @@ switch ($action) {
                 }
             }
 
+            // {@link repository::build_source_field()}
+            $sourcefield = $repo->get_file_source_info($source);
+            $record->source = $repo::build_source_field($sourcefield);
+
             if ($usefilereference) {
                 $reference = $repo->get_file_reference($source);
                 // get reference life time from repo
@@ -266,10 +270,6 @@ switch ($action) {
                     $event['existingfile']->url      = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();;
                 } else {
 
-                    // {@link repository::build_source_field()}
-                    $sourcefield = $repo->get_file_source_info($source);
-                    $record->source = $repo::build_source_field($sourcefield);
-
                     $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
                     $event = array(
                         'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(),
@@ -307,10 +307,6 @@ switch ($action) {
                     throw new file_exception('maxbytes');
                 }
 
-                // {@link repository::build_source_field()}
-                $sourcefield = $repo->get_file_source_info($source);
-                $record->source = $repo::build_source_field($sourcefield);
-
                 $info = repository::move_to_filepool($downloadedfile['path'], $record);
                 if (empty($info)) {
                     $info['e'] = get_string('error', 'moodle');
index 5558620..c25dfc6 100644 (file)
@@ -126,7 +126,7 @@ class repository_s3 extends repository {
      * @return string
      */
     public function get_file_source_info($filepath) {
-        return 'Amazon S3:' . $filepath;
+        return 'Amazon S3: ' . $filepath;
     }
 
     /**
index cf023dd..028c641 100644 (file)
@@ -144,7 +144,8 @@ class repository_upload extends repository {
         @chmod($_FILES[$elname]['tmp_name'], $permissions);
 
         // {@link repository::build_source_field()}
-        $record->source = self::build_source_field($_FILES[$elname]['name']);
+        $sourcefield = $this->get_file_source_info($_FILES[$elname]['name']);
+        $record->source = self::build_source_field($sourcefield);
 
         if (empty($saveas_filename)) {
             $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);