Skip to content

Commit

Permalink
Adding Support for Scorm
Browse files Browse the repository at this point in the history
  • Loading branch information
its-ChaTTy committed Aug 2, 2024
1 parent 912c7df commit 979652b
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 1 deletion.
3 changes: 2 additions & 1 deletion _sources/lectures/TWP52_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ Review
+ OptionMenu()



.. raw:: html
:file: ../../scorm_package/index.html

.. disqus::
:shortname: pyzombis
Expand Down
2 changes: 2 additions & 0 deletions _templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@
{% endraw %}
{% endif %}

<script src="../scorm_package/scripts/scorm_functions.js"></script>

{% endblock %}

{# Silence the sidebar's, relbar's #}
Expand Down
21 changes: 21 additions & 0 deletions scorm_package/imsmanifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<manifest identifier="com.example.scorm" version="1.2">
<metadata>
<schema>ADL SCORM</schema>
<schemaversion>1.2</schemaversion>
</metadata>
<organizations default="ORG1">
<organization identifier="ORG1">
<title>SCORM Course</title>
<item identifier="ITEM1" identifierref="RES1">
<title>Lesson 1</title>
</item>
</organization>
</organizations>
<resources>
<resource identifier="RES1" type="webcontent" href="index.html">
<file href="index.html"/>
<file href="scripts/scorm_functions.js"/>
</resource>
</resources>
</manifest>
32 changes: 32 additions & 0 deletions scorm_package/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>SCORM Course</title>

<style>
.center {
display: flex;
justify-content: center;
align-items: center;
height: 10vh; /* Optional: to center vertically within the viewport */
}
</style>
</head>
<body>
<h1>Welcome to the SCORM Course</h1>
<p id="studentName"></p>
<div class="center">
<button onclick="completeLesson()">Complete Lesson</button>
</div>
<script>
window.onload = function() {
initialize().then(() => {
scorm.getValue("cmi.core.student_name").then(studentName => {
document.getElementById("studentName").innerText = "Student Name: " + studentName;
});
});
};
</script>
<script src="scripts/scorm_functions.js"></script>
</body>
</html>
76 changes: 76 additions & 0 deletions scorm_package/scripts/scorm_functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var scorm = pipwerks.SCORM;
scorm.version = "1.2";

function initialize() {
return new Promise((resolve, reject) => {
var result = scorm.init();
if (result) {
console.log("SCORM initialized");
resolve();
} else {
console.log("SCORM initialization failed");
reject();
}
});
}

function completeLesson() {
console.log("Complete Lesson button clicked");
scorm.setValue("cmi.core.lesson_status", "completed").then(() => {
fetch('/LMSSetValue', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ element: "cmi.core.lesson_status", value: "completed" }),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
});
}

scorm.setValue = function(element, value) {
return new Promise((resolve, reject) => {
fetch('/LMSSetValue', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ element: element, value: value }),
})
.then(response => {
if (response.ok) {
resolve();
} else {
reject();
}
})
.catch(error => {
console.error('Error:', error);
reject();
});
});
};

scorm.getValue = function(element) {
return new Promise((resolve, reject) => {
fetch(`/LMSGetValue?element=${element}`)
.then(response => response.json())
.then(data => {
resolve(data[element]);
})
.catch(error => {
console.error('Error:', error);
reject();
});
});
};

// Expose functions to global scope
window.completeLesson = completeLesson;
window.initialize = initialize;
38 changes: 38 additions & 0 deletions scorm_package/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from flask import Flask, request, jsonify

app = Flask(__name__)

# Dummy storage for demonstration
scorm_data = {
"student_name": "John Doe", # Example student name
"lesson_status": "",
"lesson_location": ""
}

@app.route('/LMSInitialize', methods=['GET'])
def initialize():
print("SCORM initialized")
return 'true', 200

@app.route('/LMSGetValue', methods=['GET'])
def get_value():
element = request.args.get('element')
print(f"Getting value for: {element}")
return jsonify({element: scorm_data.get(element, '')})

@app.route('/LMSSetValue', methods=['POST'])
def set_value():
data = request.get_json()
element = data.get('element')
value = data.get('value')
scorm_data[element] = value
print(f"Set {element} to {value}")
return 'true', 200

@app.route('/LMSFinish', methods=['GET'])
def finish():
print("SCORM finished")
return 'true', 200

if __name__ == '__main__':
app.run(debug=True)

0 comments on commit 979652b

Please sign in to comment.