diff --git a/mod/scorm/datamodels/aicc.js.php b/mod/scorm/datamodels/aicc.php
similarity index 76%
rename from mod/scorm/datamodels/aicc.js.php
rename to mod/scorm/datamodels/aicc.php
index 9a73b20cb0e..17839acee34 100644
--- a/mod/scorm/datamodels/aicc.js.php
+++ b/mod/scorm/datamodels/aicc.php
@@ -14,33 +14,24 @@
 // You should have received a copy of the GNU General Public License
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
-if (isset($userdata->status)) {
-    if ($userdata->status == '') {
-        $userdata->entry = 'ab-initio';
-    } else {
-        if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
-            $userdata->entry = 'resume';
-        } else {
-            $userdata->entry = '';
-        }
-    }
-}
+require_once($CFG->dirroot.'/mod/scorm/locallib.php');
+
+$userdata = new stdClass();
+$def = get_scorm_default($userdata, $scorm, $scoid, $attempt, $mode);
+
 if (!isset($currentorg)) {
     $currentorg = '';
 }
 
-$def = get_scorm_default($userdata);
-
 $cmiobj = '';
-$current_objective = '';
+$currentobj = '';
 $count = 0;
-$objectives = '';
 foreach ($userdata as $element => $value) {
     if (substr($element, 0, 14) == 'cmi.objectives') {
         $element = preg_replace('/\.(\d+)\./', "_\$1.", $element);
         preg_match('/\_(\d+)\./', $element, $matches);
-        if (count($matches) > 0 && $current_objective != $matches[1]) {
-            $current_objective = $matches[1];
+        if (count($matches) > 0 && $currentobj != $matches[1]) {
+            $currentobj = $matches[1];
             $count++;
             $end = strpos($element, $matches[1])+strlen($matches[1]);
             $subelement = substr($element, 0, $end);
@@ -58,4 +49,5 @@ if ($count > 0) {
     $cmiobj .= '    cmi.objectives._count = '.$count.";\n";
 }
 
-$PAGE->requires->js_init_call('M.scorm_api.init', array($def, $cmiobj, $scorm->auto, $CFG->wwwroot, $scorm->id, $scoid, $attempt, $mode, $currentorg, sesskey(), $id);
+$PAGE->requires->js_init_call('M.scorm_api.init', array($def, $cmiobj, $scorm->auto, $CFG->wwwroot, $scorm->id, $scoid, $attempt,
+                                                         $mode, $currentorg, sesskey(), $id));
diff --git a/mod/scorm/datamodels/aicclib.php b/mod/scorm/datamodels/aicclib.php
index 855a885bb2c..59997b51668 100644
--- a/mod/scorm/datamodels/aicclib.php
+++ b/mod/scorm/datamodels/aicclib.php
@@ -479,7 +479,54 @@ function scorm_aicc_generate_simple_sco($scorm) {
     return $id;
 }
 
-function get_scorm_default ($userdata) {
+function get_scorm_default (&$userdata, $scorm, $scoid, $attempt, $mode) {
+    global $USER;
+    
+    $userdata->student_id = $USER->username;
+    $userdata->student_name = $USER->lastname .', '. $USER->firstname;
+
+    if ($usertrack = scorm_get_tracks($scoid, $USER->id, $attempt)) {
+        foreach ($usertrack as $key => $value) {
+            $userdata->$key = $value;
+        }
+    } else {
+        $userdata->status = '';
+        $userdata->score_raw = '';
+    }
+
+    if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
+        foreach ($scodatas as $key => $value) {
+            $userdata->$key = $value;
+        }
+    } else {
+        print_error('cannotfindsco', 'scorm');
+    }
+    if (!$sco = scorm_get_sco($scoid)) {
+        print_error('cannotfindsco', 'scorm');
+    }
+
+    $userdata->mode = 'normal';
+    if (!empty($mode)) {
+        $userdata->mode = $mode;
+    }
+    if ($userdata->mode == 'normal') {
+        $userdata->credit = 'credit';
+    } else {
+        $userdata->credit = 'no-credit';
+    }
+
+    if (isset($userdata->status)) {
+        if ($userdata->status == '') {
+            $userdata->entry = 'ab-initio';
+        } else {
+            if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
+                $userdata->entry = 'resume';
+            } else {
+                $userdata->entry = '';
+            }
+        }
+    }
+
     $def = array();
     $def['cmi.core.student_id'] = $userdata->student_id;
     $def['cmi.core.student_name'] = $userdata->student_name;
diff --git a/mod/scorm/datamodels/scorm_12.js.php b/mod/scorm/datamodels/scorm_12.php
similarity index 84%
rename from mod/scorm/datamodels/scorm_12.js.php
rename to mod/scorm/datamodels/scorm_12.php
index dbe5f9ce2b4..189c95e5182 100644
--- a/mod/scorm/datamodels/scorm_12.js.php
+++ b/mod/scorm/datamodels/scorm_12.php
@@ -16,17 +16,10 @@
 
 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
 
-if (isset($userdata->status)) {
-    if ($userdata->status == '') {
-        $userdata->entry = 'ab-initio';
-    } else {
-        if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
-            $userdata->entry = 'resume';
-        } else {
-            $userdata->entry = '';
-        }
-    }
-}
+// Set some vars to use as default values.
+$userdata = new stdClass();
+$def = get_scorm_default($userdata, $scorm, $scoid, $attempt, $mode);
+
 if (!isset($currentorg)) {
     $currentorg = '';
 }
@@ -40,9 +33,6 @@ if (intval(get_config("scorm", "scorm12standard"))) {
     $cmistring4096 = $cmistring256;
 }
 
-// Set some vars to use as default values.
-$def = get_scorm_default($userdata);
-
 // reconstitute objectives
 $cmiobj = scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score'));
 $cmiint = scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.interactions', array('objectives', 'correct_responses'));
diff --git a/mod/scorm/datamodels/scorm_12lib.php b/mod/scorm/datamodels/scorm_12lib.php
index a81caa22e5f..59bbe9f34a6 100644
--- a/mod/scorm/datamodels/scorm_12lib.php
+++ b/mod/scorm/datamodels/scorm_12lib.php
@@ -121,7 +121,54 @@ function scorm_eval_prerequisites($prerequisites, $usertracks) {
     return eval('return '.implode($stack).';');
 }
 
-function get_scorm_default ($userdata) {
+function get_scorm_default (&$userdata, $scorm, $scoid, $attempt, $mode) {
+    global $USER;
+
+    $userdata->student_id = $USER->username;
+    $userdata->student_name = $USER->lastname .', '. $USER->firstname;
+
+    if ($usertrack = scorm_get_tracks($scoid, $USER->id, $attempt)) {
+        foreach ($usertrack as $key => $value) {
+            $userdata->$key = $value;
+        }
+    } else {
+        $userdata->status = '';
+        $userdata->score_raw = '';
+    }
+    
+    if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
+        foreach ($scodatas as $key => $value) {
+            $userdata->$key = $value;
+        }
+    } else {
+        print_error('cannotfindsco', 'scorm');
+    }
+    if (!$sco = scorm_get_sco($scoid)) {
+        print_error('cannotfindsco', 'scorm');
+    }
+
+    if (isset($userdata->status)) {
+        if ($userdata->status == '') {
+            $userdata->entry = 'ab-initio';
+        } else {
+            if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
+                $userdata->entry = 'resume';
+            } else {
+                $userdata->entry = '';
+            }
+        }
+    }
+
+    $userdata->mode = 'normal';
+    if (!empty($mode)) {
+        $userdata->mode = $mode;
+    }
+    if ($userdata->mode == 'normal') {
+        $userdata->credit = 'credit';
+    } else {
+        $userdata->credit = 'no-credit';
+    }
+
     $def = array();
     $def['cmi.core.student_id'] = $userdata->student_id;
     $def['cmi.core.student_name'] = $userdata->student_name;
diff --git a/mod/scorm/datamodels/scorm_13.js.php b/mod/scorm/datamodels/scorm_13.php
similarity index 79%
rename from mod/scorm/datamodels/scorm_13.js.php
rename to mod/scorm/datamodels/scorm_13.php
index 2c6a39aeb5e..fbc76ed7f6b 100644
--- a/mod/scorm/datamodels/scorm_13.js.php
+++ b/mod/scorm/datamodels/scorm_13.php
@@ -16,23 +16,13 @@
 
 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
 
-if (isset($userdata->status)) {
-    if (!isset($userdata->{'cmi.exit'}) || (($userdata->{'cmi.exit'} == 'time-out') || ($userdata->{'cmi.exit'} == 'normal'))) {
-            $userdata->entry = 'ab-initio';
-    } else {
-        if (isset($userdata->{'cmi.exit'}) && (($userdata->{'cmi.exit'} == 'suspend') || ($userdata->{'cmi.exit'} == 'logout'))) {
-            $userdata->entry = 'resume';
-        } else {
-            $userdata->entry = '';
-        }
-    }
-}
+$userdata = new stdClass();
+$def = get_scorm_default($userdata, $scorm, $scoid, $attempt, $mode);
+
 if (!isset($currentorg)) {
     $currentorg = '';
 }
 
-$def = get_scorm_default($userdata);
-
 // reconstitute objectives, comments_from_learner and comments_from_lms
 $cmiobj = scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score'));
 $cmiint = scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.interactions', array('objectives', 'correct_responses'));
diff --git a/mod/scorm/datamodels/scorm_13lib.php b/mod/scorm/datamodels/scorm_13lib.php
index c4703b702fe..605c0d6d92a 100644
--- a/mod/scorm/datamodels/scorm_13lib.php
+++ b/mod/scorm/datamodels/scorm_13lib.php
@@ -1181,7 +1181,72 @@ function scorm_seq_flow ($activity, $direction, $seq, $childrenflag, $userid) {
     }
 }
 
-function get_scorm_default ($userdata) {
+function get_scorm_default (&$userdata, $scorm, $scoid, $attempt, $mode) {
+    global $DB, $USER;
+
+    $userdata->student_id = $USER->username;
+    $userdata->student_name = $USER->lastname .', '. $USER->firstname;
+
+    if ($usertrack = scorm_get_tracks($scoid, $USER->id, $attempt)) {
+        // According to SCORM 2004(RTE V1, 4.2.8), only cmi.exit==suspend should allow previous datamodel elements on re-launch.
+        if (isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend')) {
+            foreach ($usertrack as $key => $value) {
+                $userdata->$key = $value;
+            }
+        } else {
+            $userdata->status = '';
+            $userdata->score_raw = '';
+        }
+    } else {
+        $userdata->status = '';
+        $userdata->score_raw = '';
+    }
+
+    if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
+        foreach ($scodatas as $key => $value) {
+            $userdata->$key = $value;
+        }
+    } else {
+        print_error('cannotfindsco', 'scorm');
+    }
+    if (!$sco = scorm_get_sco($scoid)) {
+        print_error('cannotfindsco', 'scorm');
+    }
+
+    if (isset($userdata->status)) {
+        if (!isset($userdata->{'cmi.exit'}) || $userdata->{'cmi.exit'} == 'time-out' || $userdata->{'cmi.exit'} == 'normal') {
+                $userdata->entry = 'ab-initio';
+        } else {
+            if (isset($userdata->{'cmi.exit'}) && ($userdata->{'cmi.exit'} == 'suspend' || $userdata->{'cmi.exit'} == 'logout')) {
+                $userdata->entry = 'resume';
+            } else {
+                $userdata->entry = '';
+            }
+        }
+    }
+    
+    $userdata->mode = 'normal';
+    if (!empty($mode)) {
+        $userdata->mode = $mode;
+    }
+    if ($userdata->mode == 'normal') {
+        $userdata->credit = 'credit';
+    } else {
+        $userdata->credit = 'no-credit';
+    }
+
+    $objectives = $DB->get_records('scorm_seq_objective', array('scoid' => $scoid));
+    $index = 0;
+    foreach ($objectives as $objective) {
+        if (!empty($objective->minnormalizedmeasure)) {
+            $userdata->{'cmi.scaled_passing_score'} = $objective->minnormalizedmeasure;
+        }
+        if (!empty($objective->objectiveid)) {
+            $userdata->{'cmi.objectives.N'.$index.'.id'} = $objective->objectiveid;
+            $index++;
+        }
+    }
+
     $def = array();
     $def['cmi.learner_id'] = $userdata->student_id;
     $def['cmi.learner_name'] = $userdata->student_name;
diff --git a/mod/scorm/loaddatamodel.php b/mod/scorm/loaddatamodel.php
deleted file mode 100644
index d2191862818..00000000000
--- a/mod/scorm/loaddatamodel.php
+++ /dev/null
@@ -1,112 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * This page loads the correct JS file based on package type
- *
- * @package mod_scorm
- * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-require_once("../../config.php");
-require_once('locallib.php');
-
-$id = optional_param('id', 0, PARAM_INT);       // Course Module ID, or
-$a = optional_param('a', 0, PARAM_INT);         // scorm ID.
-$scoid = required_param('scoid', PARAM_INT);     // sco ID.
-$mode = optional_param('mode', '', PARAM_ALPHA); // navigation mode.
-$currentorg = optional_param('currentorg', '', PARAM_RAW); // Selected organization.
-$attempt = required_param('attempt', PARAM_INT); // new attempt.
-
-if (!empty($id)) {
-    $cm = get_coursemodule_from_id('scorm', $id, 0, false, MUST_EXIST);
-    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
-    $scorm = $DB->get_record('scorm', array('id' => $cm->instance), '*', MUST_EXIST);
-} else if (!empty($a)) {
-    $scorm = $DB->get_record('scorm', array('id' => $a), '*', MUST_EXIST);
-    $course = $DB->get_record('course', array('id' => $scorm->course), '*', MUST_EXIST);
-    $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id, false, MUST_EXIST);
-} else {
-    print_error('missingparameter');
-}
-
-$PAGE->set_url('/mod/scorm/loaddatamodel.php', array('scoid' => $scoid, 'id' => $cm->id));
-
-require_login($course, false, $cm);
-
-$userdata = new stdClass();
-if ($usertrack = scorm_get_tracks($scoid, $USER->id, $attempt)) {
-    // According to SCORM 2004 spec(RTE V1, 4.2.8), only cmi.exit==suspend should allow previous datamodel elements on re-launch.
-    if (!scorm_version_check($scorm->version, SCORM_13) ||
-        (isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend'))) {
-        foreach ($usertrack as $key => $value) {
-            $userdata->$key = addslashes_js($value);
-        }
-    } else {
-        $userdata->status = '';
-        $userdata->score_raw = '';
-    }
-} else {
-    $userdata->status = '';
-    $userdata->score_raw = '';
-}
-$userdata->student_id = addslashes_js($USER->username);
-$userdata->student_name = addslashes_js($USER->lastname .', '. $USER->firstname);
-$userdata->mode = 'normal';
-if (!empty($mode)) {
-    $userdata->mode = $mode;
-}
-if ($userdata->mode == 'normal') {
-    $userdata->credit = 'credit';
-} else {
-    $userdata->credit = 'no-credit';
-}
-if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
-    foreach ($scodatas as $key => $value) {
-        $userdata->$key = addslashes_js($value);
-    }
-} else {
-    print_error('cannotfindsco', 'scorm');
-}
-if (!$sco = scorm_get_sco($scoid)) {
-    print_error('cannotfindsco', 'scorm');
-}
-if (scorm_version_check($scorm->version, SCORM_13)) {
-    $objectives = $DB->get_records('scorm_seq_objective', array('scoid' => $scoid));
-    $index = 0;
-    foreach ($objectives as $objective) {
-        if (!empty($objective->minnormalizedmeasure)) {
-            $userdata->{'cmi.scaled_passing_score'} = $objective->minnormalizedmeasure;
-        }
-        if (!empty($objective->objectiveid)) {
-            $userdata->{'cmi.objectives.N'.$index.'.id'} = $objective->objectiveid;
-            $index++;
-        }
-    }
-}
-
-header('Content-Type: text/javascript; charset=UTF-8');
-
-// Set the start time of this SCO.
-scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());
-?>
-
-var errorCode = "0";
-function underscore(str) {
-    str = String(str).replace(/.N/g,".");
-    return str.replace(/\./g,"__");
-}
diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php
index 619e7e5cef3..3bdf394bba4 100644
--- a/mod/scorm/locallib.php
+++ b/mod/scorm/locallib.php
@@ -1033,8 +1033,8 @@ function scorm_reconstitute_array_element($sversion, $userdata, $elementname, $c
             preg_match('/\_(\d+)\./', $element, $matches);
         }
         if (count($matches) > 0 && $current != $matches[1]) {
-            if ($count_sub > 0) {
-                $return .= '    '.$element_name.$scormseperator.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
+            if ($countsub > 0) {
+                $return .= '    '.$elementname.$scormseperator.$current.'.'.$currentsubelement.'._count = '.$countsub.";\n";
             }
             $current = $matches[1];
             $count++;
@@ -1061,9 +1061,9 @@ function scorm_reconstitute_array_element($sversion, $userdata, $elementname, $c
         }
 
         // Check the sub element type.
-        if (count($matches) > 0 && $current_subelement != $matches[1]) {
-            if ($count_sub > 0) {
-                $return .= '    '.$element_name.$scormseperator.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
+        if (count($matches) > 0 && $currentsubelement != $matches[1]) {
+            if ($countsub > 0) {
+                $return .= '    '.$elementname.$scormseperator.$current.'.'.$currentsubelement.'._count = '.$countsub.";\n";
             }
             $currentsubelement = $matches[1];
             $currentsub = '';
@@ -1084,11 +1084,11 @@ function scorm_reconstitute_array_element($sversion, $userdata, $elementname, $c
 
         $return .= '    '.$element.' = \''.$value."';\n";
     }
-    if ($count_sub > 0) {
-        $return .= '    '.$element_name.$scormseperator.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
+    if ($countsub > 0) {
+        $return .= '    '.$elementname.$scormseperator.$current.'.'.$currentsubelement.'._count = '.$countsub.";\n";
     }
     if ($count > 0) {
-        $return .= '    '.$element_name.'._count = '.$count.";\n";
+        $return .= '    '.$elementname.'._count = '.$count.";\n";
     }
     return $return;
 }
diff --git a/mod/scorm/module.js b/mod/scorm/module.js
index 2c92908bffa..8f45f4291e3 100644
--- a/mod/scorm/module.js
+++ b/mod/scorm/module.js
@@ -27,6 +27,11 @@ mod_scorm_activate_item = null;
 mod_scorm_parse_toc_tree = null;
 scorm_layout_widget = null;
 
+function underscore(str) {
+    str = String(str).replace(/.N/g,".");
+    return str.replace(/\./g,"__");
+}
+
 M.mod_scorm = {};
 
 M.mod_scorm.init = function(Y, nav_display, navposition_left, navposition_top, hide_toc, collapsetocwinsize, toc_title, window_name, launch_sco, scoes_nav) {
@@ -139,18 +144,6 @@ M.mod_scorm.init = function(Y, nav_display, navposition_left, navposition_top, h
                 el_old_api.parentNode.removeChild(el_old_api);
             }
 
-            if (node.title) {
-                var el_scorm_api = document.getElementById("external-scormapi");
-                el_scorm_api.parentNode.removeChild(el_scorm_api);
-                el_scorm_api = document.createElement('script');
-                el_scorm_api.setAttribute('id','external-scormapi');
-                el_scorm_api.setAttribute('type','text/javascript');
-                var pel_scorm_api = document.getElementById('scormapi-parent');
-                pel_scorm_api.appendChild(el_scorm_api);
-                var api_url = M.cfg.wwwroot + '/mod/scorm/loaddatamodel.php?' + node.title;
-                document.getElementById('external-scormapi').src = api_url;
-            }
-
             var content = Y.one('#scorm_content');
             var obj = document.createElement('iframe');
             obj.setAttribute('id', 'scorm_object');
diff --git a/mod/scorm/player.php b/mod/scorm/player.php
index 1dc6108f72c..d51e977c605 100644
--- a/mod/scorm/player.php
+++ b/mod/scorm/player.php
@@ -254,10 +254,10 @@ if (!empty($forcejs)) {
     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
 }
 
-if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php')) {
-    include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php');
+if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php')) {
+    include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php');
 } else {
-    include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.js.php');
+    include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.php');
 }
 
 // Add the checknet system to keep checking for a connection.
@@ -266,3 +266,6 @@ $PAGE->requires->yui_module('moodle-core-checknet', 'M.core.checknet.init', arra
     'message' => array('networkdropped', 'mod_scorm'),
 )));
 echo $OUTPUT->footer();
+
+// Set the start time of this SCO.
+scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());