MDL-26452 fix resource mod regression breaking upgrade on Windows
authorPetr Skoda <commits@skodak.org>
Wed, 16 Feb 2011 19:49:59 +0000 (20:49 +0100)
committerPetr Skoda <commits@skodak.org>
Wed, 16 Feb 2011 20:41:04 +0000 (21:41 +0100)
mod/resource/db/upgrade.php
mod/resource/view.php

index f5478ba..2b19c9b 100644 (file)
@@ -228,24 +228,22 @@ function xmldb_resource_upgrade($oldversion) {
 
         $instances = $DB->get_recordset_sql($sql);
         foreach ($instances as $instance) {
-            $context  = get_context_instance(CONTEXT_MODULE, $instance->cmid);
-            $component = 'mod_resource';
-            $filearea = 'content';
-            $itemid   = 0;
-            $filepath = file_correct_filepath(dirname($instance->mainfile));
-            $filename = basename($instance->mainfile);
-            file_set_sortorder($context->id, $component, $filearea, $itemid, $filepath, $filename, 1);
+            if (empty($instance->mainfile)) {
+                // weird
+                continue;
+            }
+            $context   = get_context_instance(CONTEXT_MODULE, $instance->cmid, MUST_EXIST);
+            $parts     = explode('/', $instance->mainfile);
+            $filename  = array_pop($parts);
+            $filepath  = implode('/', $parts);
+            file_set_sortorder($context->id, 'mod_resource', 'content', 0, $filepath, $filename, 1);
         }
         $instances->close();
 
      /// Define field mainfile to be dropped from resource
         $table = new xmldb_table('resource');
         $field = new xmldb_field('mainfile');
-
-    /// Conditionally launch drop field mainfile
-        if ($dbman->field_exists($table, $field)) {
-            $dbman->drop_field($table, $field);
-        }
+        $dbman->drop_field($table, $field);
 
     /// resource savepoint reached
         upgrade_mod_savepoint(true, 2009080501, 'resource');
index c534e35..ee3c694 100644 (file)
@@ -67,12 +67,13 @@ if ($resource->tobemigrated) {
 }
 
 $fs = get_file_storage();
-$files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder');
+$files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
 if (count($files) < 1) {
     resource_print_filenotfound($resource, $cm, $course);
     die;
 } else {
-    $file = array_pop($files);
+    $file = reset($files);
+    unset($files);
 }
 
 if ($redirect) {