Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Resource module file API migration.

Development:Resource module file API migration

From MoodleDocs

Template:Moodle 2.0

Please use talk page for comments and proposals, thanks.

Current problems

  • plain text and html resource types separate
  • embedded images and files in general are stored in course files without appropriate access control
  • local files and Internet links mixed
  • HarvestRoad Hive repository type is not maintained.
  • embedded images are lost during backup & restore which moves the course to a new site. -- Matt Gibson 08:53, 18 May 2009 (UTC)
  • can't move a "whole" web page properly from one course to another (eg course import).

Types of resources

Plugin Moodle 1.9 Moodle 2.0
directory a directory of files on disk a collection of files in database
html A html page, containing media from course files or external A html page, containing associated media or external
text A plain text page in some format ?
file A single file, from course files or external URL A single file, associated file or external URL
ims An IMS resource package from course file or repository web root An IMS resource package from associated files
repository Links to external files in repositories (ie Hive) (na)


There are other third party plugins in contrib that need to be migrated:

  • mod/book - displays multiple pages of HTML etc
  • mod/resource/type/digitalnz - browser and add items listed in http://www.digitalnz.org/
  • mod/resource/type/globe - browser and add items in Ariadne repositories
  • mod/resource/type/jmol - display chemistry structures from a jmol format file
  • mod/resource/type/rss - display one RSS feed on a page
  • mod/resource/type/slideshow - display a directory of images as a slideshow

Automatic upgrade

Each resource type requires different upgrade code because the files are stored in different places that require special handling. They also have different settings and options with different text encoding in the table fields: reference, alltext and options.

Resources that can not be completely migrated during upgrade time will be flagged using a new field called legacyfiles:

  • 0 = new resource created in Moodle 2
  • 1 = upgraded resource that has completed migration
  • 2 = upgraded resource that has NOT been migrated yet
  • timestamp = last date of a migrated file within this resource


Directory

Recursively copy the chosen course directory to the resource area. No other upgrade steps are required.

A full filemanager will be required to manage the tree of files.

File

Files in the course directory

  1. Copy the linked file (image, sound, pdf, HTML, etc.) from course area to resource file area
  2. If the file is HTML then parse the text for absolute or relative links to other files within the same course (images, sounds, video, pdf, docs, css, javascript, java applets, flash applets) and copy those files to the resource file area as well (storing the old location in the files table)
  3. Repeat parsing of new html and css files until no new files are found
  4. If any of these are found in any of the HTML pages then flag those files for post-upgrade processing with legacyfiles = 2 (see next section).
    • Java applets
    • Flash applets
    • Javascript code


Files referenced by URL

(This includes links to other courses and site files)

This does not require any upgrade. The url is stored in reference field and will continue working.

Page (Web and Text)

  1. Convert to one plug-in called 'page' (editable via the new editor forms element)
  2. Parse the text for absolute or relative links to other files within the same course (images, sounds, video, pdf, docs, css, javascript, java applets, flash applets) and copy those files to the resource file area as well (storing the old location in the files table)
  3. Repeat parsing of new html and css files until no new files are found
  4. If any of these are found in any of the HTML pages then flag those files for post-upgrade processing with legacyfiles = 2 (see next section).
    • Java applets
    • Flash applets
    • Javascript code

IMS

  1. Copy files from moddata to new resource file area
  2. Copy original backup of package file to another new resource area

No other special upgrade steps required.

Repository

Work out the URL of the resource and convert it to a simple URL.

Others

(TODO: Add some docs here for each type)

Post-upgrade processing

After the Moodle 2.0 upgrade has completed we take the admin to a special report that:

  1. explains the issue of resources that contain "hidden" links to other files
  2. explains how it can be fixed by on-demand migration (see below)
  3. presents a list of flagged files as links that need to be visited manually in the browser

I imagine this interface might have a small frame on the left with the main pages being loaded in the larger frame on the right. The left frame has controls and info like:

  • total number of pages to process
  • next and previous buttons
  • some button to say "I have completely viewed this resource"

So the admin will have to sit there and go through all these resources and check them off to complete the upgrade. Until this process is finished none of these operations can be guaranteed to work so we might even think about disabling those operations:

  • Backup
  • Restore
  • Import of a course

Normal viewing of resources WILL work fine for teachers and students, because it will just trigger the normal on-demand migration of files.


On-demand migration of files

If any of the HTML files encountered during the upgrade process contained any of these:

  • flash applets containing relative links to media (eg sounds or images)
  • java applets containing relative links to media
  • javascript that loads media via relative links (eg imagine an auto-generated slideshow)

then it would be flagged with legacyfiles = 1 indicating that it may contain still relative links to legacy files.

(All other "simpler" upgraded resources and new Moodle 2.0 resources have legacyfiles = 0).

We still want to move all these legacy files into the correct resource filearea, so we do it like this:

  1. When a browser requests to view one of these "legacyfile" HTML files, it will load all the JS and Java and Flash it contains.
  2. These programs run, and make relative requests to media if they want to.
  3. These request to Moodle will hit the pluginfile.php script
  4. TODO - explain exactly how pluginfile can derive the original location of the file so it can copy the file into the resource filearea

Solution of caching problems

Teachers often do not understand that files may be cached in browsers, se the same mechanism already implemented in SCORM module.

  1. Internally store all files as itemid=0
  2. Add new revision db field into resource table
  3. When serving files always construct links with itemid==revision
  4. Ignore itemid for resource files in pluginfile.php

Code refactoring proposal

Technically it would be difficult to split current Resource into multiple modules. The idea of module subplugins was only implemented only partially, it should be much easier to split unmaintained plugins into separate modules in contrib. Considering long term maintenance and support costs it might be better to create a minimal resource-like module skeleton in contrib instead of encouraging people to create new unofficial resource plugins.

Subplugin related problems

  1. no standard subplugin APIs
  2. all internal module API frozen - no internal refactoring allowed
  3.  subplugin regression more likely
  4. sloppy db table structures - many universal columns with
  5. no standard way to upgrade subplugins - main version file needs to be bumped up
  6. no standard way to uninstall subplugin - db table leftovers
  7. capability in subplugins not supported
  8. subplugin upgrade is not integrated into main module upgrade - subplugin upgrade is executed after the main module upgrade (critical)
  9. subplugins can not have own lang packs
  10. subplugins do not have own log actions
  11. instead of reducing code duplication advanced plugins may duplicate a lot more core
  12.  it is not easy to move unmaintained subplugins into contrib
  13. unofficial subplugins not reported during upgrade
  14. no support for subplugins in file api

Option 1: split Resource into multiple modules

  • mod/resource - formatted text pages
  • mod/resourcefile - one or more uploaded files
  • mod/resourcedir - directory structure
  •  mod/resourcelink - links to external files
+1 to shorter names, like "rsrcfile, rsrcdir ..." due to DB restrictions mainly. --Eloy Lafuente (stronk7) 16:20, 19 May 2009 (UTC)

Activities block would have to be updated to group all resources onto one page instead of listing all resource modules separately. This can be accomplished easily by adding new field into core modules table.

Benefits:

  • easier maintenance in future
  • easier upgrade (no problems with new db fields)
  • showcase of good coding style for beginners
  • more contrib modules may behave like the real resources (from users' POV)
  • no need for solving of any subplugin problems

Option 2: keep Resource submodules/types

  • mod/resource/type/page - replaces Plain text, wiki and HTML resources
  • mod/resource/type/file - one or more uploaded files one is marked as the main file
  • mod/resource/type/directory - one or more uploaded files
  • mod/resource/type/link - links to external files
  • mod/resource/type/ims - IMS packages, maintained by Eloy

Problems:

  • each type needs different db structure
  • we should solve all subplugin problems above (a lot of work, hard to maintain)

Moving to contrib

  •  New module/type for special mod/resource/type/file/localfile.php (win32 only tweak) - originally intended for large files stored on local networks or CDs
  • (No idea what to do with hive repositories, we should also remove the obsoleted auth hive SSO support)

See also