Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #9 from nebgor/MDL-27449

MDL-27449 SCORM module 1.9 converter
  • Loading branch information...
commit 193407a0968138891780e678557c3cadc1f4a101 2 parents ba1e221 + a1cac90
David Mudrák authored May 30, 2011

Showing 1 changed file with 163 additions and 0 deletions. Show diff stats Hide diff stats

  1. 163  mod/scorm/backup/moodle1/lib.php
163  mod/scorm/backup/moodle1/lib.php
... ...
@@ -0,0 +1,163 @@
  1
+<?php
  2
+
  3
+// This file is part of Moodle - http://moodle.org/
  4
+//
  5
+// Moodle is free software: you can redistribute it and/or modify
  6
+// it under the terms of the GNU General Public License as published by
  7
+// the Free Software Foundation, either version 3 of the License, or
  8
+// (at your option) any later version.
  9
+//
  10
+// Moodle is distributed in the hope that it will be useful,
  11
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
  12
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13
+// GNU General Public License for more details.
  14
+//
  15
+// You should have received a copy of the GNU General Public License
  16
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17
+
  18
+/**
  19
+ * Provides support for the conversion of moodle1 backup to the moodle2 format
  20
+ *
  21
+ * @package    mod
  22
+ * @subpackage scorm
  23
+ * @copyright  2011 Aparup Banerjee <nebgor@gmail.com>
  24
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25
+ */
  26
+
  27
+defined('MOODLE_INTERNAL') || die();
  28
+
  29
+/**
  30
+ * Scorm conversion handler
  31
+ */
  32
+class moodle1_mod_scorm_handler extends moodle1_mod_handler {
  33
+    /** @var array in-memory cache for the course module information  */
  34
+    protected $currentcminfo = null;
  35
+    /** @var moodle1_file_manager instance for the current scorm */
  36
+    protected $fileman = null;
  37
+
  38
+    /**
  39
+     * Declare the paths in moodle.xml we are able to convert
  40
+     *
  41
+     * The method returns list of {@link convert_path} instances.
  42
+     * For each path returned, the corresponding conversion method must be
  43
+     * defined.
  44
+     *
  45
+     * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM does not
  46
+     * actually exist in the file. The last element with the module name was
  47
+     * appended by the moodle1_converter class.
  48
+     *
  49
+     * @return array of {@link convert_path} instances
  50
+     */
  51
+    public function get_paths() {
  52
+        return array(
  53
+            new convert_path('scorm', '/MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM',
  54
+                array(
  55
+                    'newfields' => array(
  56
+                        'whatgrade' => 0,
  57
+                        'scormtype' => 'local',
  58
+                        'sha1hash' => null,
  59
+                        'revision' => '0',
  60
+                        'forcecompleted' => 1,
  61
+                        'forcenewattempt' => 0,
  62
+                        'lastattemptlock' => 0,
  63
+                        'displayattemptstatus' => 1,
  64
+                        'displaycoursestructure' => 1,
  65
+                        'timeopen' => '0',
  66
+                        'timeclose' => '0',
  67
+                        'introformat' => '0',
  68
+                        'launch' => null,
  69
+                    ),
  70
+                    'renamefields' => array(
  71
+                        'summary' => 'intro'
  72
+                    )
  73
+                )
  74
+            ),
  75
+            new convert_path('scorm_sco', '/MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM/SCOES/SCO')
  76
+        );
  77
+    }
  78
+
  79
+    /**
  80
+     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM
  81
+     * data available
  82
+     */
  83
+    public function process_scorm($data) {
  84
+        global $CFG;
  85
+        // get the course module id and context id
  86
+        $instanceid             = $data['id'];
  87
+        $this->currentcminfo    = $this->get_cminfo($instanceid);
  88
+        $moduleid               = $this->currentcminfo['id'];
  89
+        $contextid              = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  90
+
  91
+        // conditionally migrate to html format in intro
  92
+        if ($CFG->texteditors !== 'textarea' && $data['introformat'] == FORMAT_MOODLE ) {
  93
+            $data['intro'] = text_to_html($data['intro'], false, false, true);
  94
+            $data['introformat'] = FORMAT_HTML;
  95
+        }
  96
+
  97
+        // check 1.9 version where backup was created
  98
+        $backupinfo = $this->converter->get_stash('backup_info');
  99
+        if ($backupinfo['moodle_version'] < 2007110503) {
  100
+            // as we have no module version data, assume $currmodule->version <= $module->version
  101
+            // - fix data as the source 1.9 build hadn't yet at time of backing up.
  102
+            $data['grademethod'] = $data['grademethod']%10;
  103
+        }
  104
+
  105
+        // update scormtype (logic is consistent as done in scorm/db/upgrade.php)
  106
+        $ismanifest = preg_match('/imsmanifest\.xml$/', $data['reference']);
  107
+        $iszippif = preg_match('/.(zip|pif)$/', $data['reference']);
  108
+        $isurl = preg_match('/^((http|https):\/\/|www\.)/', $data['reference']);
  109
+        if ($isurl) {
  110
+            if ($ismanifest) {
  111
+                $data['scormtype'] = 'external';
  112
+            } else if ($iszippif) {
  113
+                $data['scormtype'] = 'localtype';
  114
+            }
  115
+        }
  116
+
  117
+        // we now have all information needed to start writing into the file
  118
+        $this->open_xml_writer("activities/scorm_{$moduleid}/scorm.xml");
  119
+        $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  120
+            'modulename' => 'scorm', 'contextid' => $contextid));
  121
+        $this->xmlwriter->begin_tag('scorm', array('id' => $instanceid));
  122
+
  123
+        unset($data['id']); // we already write it as attribute, do not repeat it as child element
  124
+        foreach ($data as $field => $value) {
  125
+            $this->xmlwriter->full_tag($field, $value);
  126
+        }
  127
+        $this->xmlwriter->begin_tag('scoes');
  128
+
  129
+        // prepare file manager for migrating scorm package file.
  130
+        $this->fileman = $this->converter->get_file_manager($contextid, 'mod_scorm', 'package');
  131
+        $this->fileman->migrate_file('course_files/'.$data['reference']);
  132
+    }
  133
+
  134
+    /**
  135
+     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM/SCOES/SCO
  136
+     * data available
  137
+     */
  138
+    public function process_scorm_sco($data) {
  139
+        $this->write_xml('sco', $data, array('/sco/id'));
  140
+    }
  141
+
  142
+    /**
  143
+     * This is executed when we reach the closing </MOD> tag of our 'scorm' path
  144
+     */
  145
+    public function on_scorm_end() {
  146
+        //close scorm.xml
  147
+        $this->xmlwriter->end_tag('scoes');
  148
+        $this->xmlwriter->end_tag('scorm');
  149
+        $this->xmlwriter->end_tag('activity');
  150
+        $this->close_xml_writer();
  151
+
  152
+        // write inforef.xml for migrated scorm zip file.
  153
+        $this->open_xml_writer("activities/scorm_{$this->currentcminfo['id']}/inforef.xml");
  154
+        $this->xmlwriter->begin_tag('inforef');
  155
+        $this->xmlwriter->begin_tag('fileref');
  156
+        foreach ($this->fileman->get_fileids() as $fileid) {
  157
+            $this->write_xml('file', array('id' => $fileid));
  158
+        }
  159
+        $this->xmlwriter->end_tag('fileref');
  160
+        $this->xmlwriter->end_tag('inforef');
  161
+        $this->close_xml_writer();
  162
+    }
  163
+}

0 notes on commit 193407a

Please sign in to comment.
Something went wrong with that request. Please try again.