feat: SCORM

This commit is contained in:
Jannat Patel
2024-11-11 18:25:56 +05:30
parent c45da4313e
commit 2e1aac4931
8 changed files with 230 additions and 57 deletions

View File

@@ -1,5 +1,5 @@
<template> <template>
<span v-if="instructors.length == 1"> <span v-if="instructors?.length == 1">
<router-link <router-link
:to="{ :to="{
name: 'Profile', name: 'Profile',
@@ -9,7 +9,7 @@
{{ instructors[0].full_name }} {{ instructors[0].full_name }}
</router-link> </router-link>
</span> </span>
<span v-if="instructors.length == 2"> <span v-if="instructors?.length == 2">
<router-link <router-link
:to="{ :to="{
name: 'Profile', name: 'Profile',
@@ -28,7 +28,7 @@
{{ instructors[1].first_name }} {{ instructors[1].first_name }}
</router-link> </router-link>
</span> </span>
<span v-if="instructors.length > 2"> <span v-if="instructors?.length > 2">
<router-link <router-link
:to="{ :to="{
name: 'Profile', name: 'Profile',
@@ -37,7 +37,7 @@
> >
{{ instructors[0].first_name }} {{ instructors[0].first_name }}
</router-link> </router-link>
and {{ instructors.length - 1 }} others and {{ instructors?.length - 1 }} others
</span> </span>
</template> </template>
<script setup> <script setup>

View File

@@ -89,6 +89,7 @@
</Draggable> </Draggable>
<div v-if="allowEdit" class="flex mt-2 mb-4 pl-8"> <div v-if="allowEdit" class="flex mt-2 mb-4 pl-8">
<router-link <router-link
v-if="!chapter.is_scorm_package"
:to="{ :to="{
name: 'LessonForm', name: 'LessonForm',
params: { params: {

View File

@@ -9,7 +9,7 @@
allowfullscreen allowfullscreen
></iframe> ></iframe>
</div> </div>
<div v-for="block in content.split('\n\n')"> <div v-for="block in content?.split('\n\n')">
<div v-if="block.includes('{{ YouTubeVideo')"> <div v-if="block.includes('{{ YouTubeVideo')">
<iframe <iframe
class="youtube-video" class="youtube-video"

View File

@@ -15,20 +15,66 @@
}" }"
> >
<template #body-content> <template #body-content>
<FormControl <div class="space-y-4">
ref="chapterInput" <FormControl ref="chapterInput" label="Title" v-model="chapter.title" />
label="Title" <FormControl
v-model="chapter.title" :label="__('Is SCORM Package')"
class="mb-4" v-model="chapter.is_scorm_package"
/> type="checkbox"
/>
<div v-if="chapter.is_scorm_package">
<FileUploader
v-if="!chapter.scorm_package"
:fileTypes="['.zip']"
:validateFile="validateFile"
@success="(file) => (chapter.scorm_package = file)"
>
<template v-slot="{ file, progress, uploading, openFileSelector }">
<div class="mb-4">
<Button @click="openFileSelector" :loading="uploading">
{{
uploading ? `Uploading ${progress}%` : 'Upload an zip file'
}}
</Button>
</div>
</template>
</FileUploader>
<div v-else class="">
<div class="flex items-center">
<div class="border rounded-md p-2 mr-2">
<FileText class="h-5 w-5 stroke-1.5 text-gray-700" />
</div>
<div class="flex flex-col">
<span>
{{ chapter.scorm_package.file_name }}
</span>
<span class="text-sm text-gray-500 mt-1">
{{ getFileSize(chapter.scorm_package.file_size) }}
</span>
</div>
<X
@click="() => (chapter.scorm_package = null)"
class="bg-gray-200 rounded-md cursor-pointer stroke-1.5 w-5 h-5 p-1 ml-4"
/>
</div>
</div>
</div>
</div>
</template> </template>
</Dialog> </Dialog>
</template> </template>
<script setup> <script setup>
import { Dialog, FormControl, createResource } from 'frappe-ui' import {
Button,
createResource,
Dialog,
FileUploader,
FormControl,
} from 'frappe-ui'
import { defineModel, reactive, watch, ref } from 'vue' import { defineModel, reactive, watch, ref } from 'vue'
import { createToast } from '@/utils/' import { showToast, getFileSize } from '@/utils/'
import { capture } from '@/telemetry' import { capture } from '@/telemetry'
import { FileText, X } from 'lucide-vue-next'
const show = defineModel() const show = defineModel()
const outline = defineModel('outline') const outline = defineModel('outline')
@@ -46,18 +92,18 @@ const props = defineProps({
const chapter = reactive({ const chapter = reactive({
title: '', title: '',
is_scorm_package: 0,
scorm_package: null,
}) })
const chapterResource = createResource({ const chapterResource = createResource({
url: 'frappe.client.insert', url: 'lms.lms.api.add_chapter',
makeParams(values) { makeParams(values) {
return { return {
doc: { title: chapter.title,
doctype: 'Course Chapter', course: props.course,
title: chapter.title, is_scorm_package: chapter.is_scorm_package,
description: chapter.description, scorm_package: chapter.scorm_package,
course: props.course,
},
} }
}, },
}) })
@@ -89,13 +135,16 @@ const chapterReference = createResource({
}, },
}) })
const addChapter = (close) => { const addChapter = async (close) => {
chapterResource.submit( chapterResource.submit(
{}, {},
{ {
validate() { validate() {
if (!chapter.title) { if (!chapter.title) {
return 'Title is required' return __('Title is required')
}
if (chapter.is_scorm_package && !chapter.scorm_package) {
return __('Please upload a SCORM package')
} }
}, },
onSuccess: (data) => { onSuccess: (data) => {
@@ -104,28 +153,34 @@ const addChapter = (close) => {
{ name: data.name }, { name: data.name },
{ {
onSuccess(data) { onSuccess(data) {
chapter.title = '' cleanChapter()
outline.value.reload() outline.value.reload()
createToast({ showToast(
text: 'Chapter added successfully', __('Success'),
icon: 'check', __('Chapter added successfully'),
iconClasses: 'bg-green-600 text-white rounded-md p-px', 'check'
}) )
}, },
onError(err) { onError(err) {
showError(err) showToast(__('Error'), err.messages?.[0] || err, 'x')
}, },
} }
) )
close() close()
}, },
onError(err) { onError(err) {
showError(err) showToast(__('Error'), err.messages?.[0] || err, 'x')
}, },
} }
) )
} }
const cleanChapter = () => {
chapter.title = ''
chapter.is_scorm_package = 0
chapter.scorm_package = null
}
const editChapter = (close) => { const editChapter = (close) => {
chapterEditResource.submit( chapterEditResource.submit(
{}, {},
@@ -137,31 +192,16 @@ const editChapter = (close) => {
}, },
onSuccess() { onSuccess() {
outline.value.reload() outline.value.reload()
createToast({ showToast(__('Success'), __('Chapter updated successfully'), 'check')
text: 'Chapter updated successfully',
icon: 'check',
iconClasses: 'bg-green-600 text-white rounded-md p-px',
})
close() close()
}, },
onError(err) { onError(err) {
showError(err) showToast(__('Error'), err.messages?.[0] || err, 'x')
}, },
} }
) )
} }
const showError = (err) => {
createToast({
title: 'Error',
text: err.messages?.[0] || err,
icon: 'x',
iconClasses: 'bg-red-600 text-white rounded-md p-px',
position: 'top-center',
timeout: 10,
})
}
watch( watch(
() => props.chapterDetail, () => props.chapterDetail,
(newChapter) => { (newChapter) => {
@@ -169,11 +209,18 @@ watch(
} }
) )
watch(show, () => { /* watch(show, () => {
if (show.value) { if (show.value) {
setTimeout(() => { setTimeout(() => {
chapterInput.value.$el.querySelector('input').focus() chapterInput.value.$el.querySelector('input').focus()
}, 100) }, 100)
} }
}) }) */
const validateFile = (file) => {
let extension = file.name.split('.').pop().toLowerCase()
if (extension !== 'zip') {
return __('Only zip files are allowed')
}
}
</script> </script>

View File

@@ -103,7 +103,7 @@
<span <span
class="h-6 mr-1" class="h-6 mr-1"
:class="{ :class="{
'avatar-group overlap': lesson.data.instructors.length > 1, 'avatar-group overlap': lesson.data.instructors?.length > 1,
}" }"
> >
<UserAvatar <UserAvatar
@@ -111,7 +111,10 @@
:user="instructor" :user="instructor"
/> />
</span> </span>
<CourseInstructors :instructors="lesson.data.instructors" /> <CourseInstructors
v-if="lesson.data?.instructors"
:instructors="lesson.data.instructors"
/>
</div> </div>
<div <div
v-if=" v-if="
@@ -146,6 +149,7 @@
class="ProseMirror prose prose-table:table-fixed prose-td:p-2 prose-th:p-2 prose-td:border prose-th:border prose-td:border-gray-300 prose-th:border-gray-300 prose-td:relative prose-th:relative prose-th:bg-gray-100 prose-sm max-w-none !whitespace-normal mt-5" class="ProseMirror prose prose-table:table-fixed prose-td:p-2 prose-th:p-2 prose-td:border prose-th:border prose-td:border-gray-300 prose-th:border-gray-300 prose-td:relative prose-th:relative prose-th:bg-gray-100 prose-sm max-w-none !whitespace-normal mt-5"
> >
<LessonContent <LessonContent
v-if="lesson.data?.body"
:content="lesson.data.body" :content="lesson.data.body"
:youtube="lesson.data.youtube" :youtube="lesson.data.youtube"
:quizId="lesson.data.quiz_id" :quizId="lesson.data.quiz_id"
@@ -369,13 +373,13 @@ const checkIfDiscussionsAllowed = () => {
const allowEdit = () => { const allowEdit = () => {
if (user.data?.is_moderator) return true if (user.data?.is_moderator) return true
if (lesson.data?.instructors.includes(user.data?.name)) return true if (lesson.data?.instructors?.includes(user.data?.name)) return true
return false return false
} }
const allowInstructorContent = () => { const allowInstructorContent = () => {
if (user.data?.is_moderator) return true if (user.data?.is_moderator) return true
if (lesson.data?.instructors.includes(user.data?.name)) return true if (lesson.data?.instructors?.includes(user.data?.name)) return true
return false return false
} }

View File

@@ -3,6 +3,9 @@
import json import json
import frappe import frappe
import zipfile
import os
import xml.etree.ElementTree as ET
from frappe.translate import get_all_translations from frappe.translate import get_all_translations
from frappe import _ from frappe import _
from frappe.query_builder import DocType from frappe.query_builder import DocType
@@ -10,6 +13,7 @@ from frappe.query_builder.functions import Count
from frappe.utils import time_diff, now_datetime, get_datetime, flt from frappe.utils import time_diff, now_datetime, get_datetime, flt
from typing import Optional from typing import Optional
from lms.lms.utils import get_average_rating, get_lesson_count from lms.lms.utils import get_average_rating, get_lesson_count
from xml.dom.minidom import parseString
@frappe.whitelist() @frappe.whitelist()
@@ -876,3 +880,70 @@ def give_dicussions_permission():
"delete": 1, "delete": 1,
} }
).save(ignore_permissions=True) ).save(ignore_permissions=True)
@frappe.whitelist()
def add_chapter(title, course, is_scorm_package, scorm_package):
values = frappe._dict(
{"title": title, "course": course, "is_scorm_package": is_scorm_package}
)
scorm_package = frappe._dict(scorm_package)
if is_scorm_package:
package = frappe.get_doc("File", scorm_package.name)
zip_path = package.get_full_path()
# Extract the zip file
extract_path = frappe.get_site_path("public", "files", "scorm", course, title)
zipfile.ZipFile(zip_path).extractall(extract_path)
values.update(
{
"scorm_package": scorm_package.name,
"scorm_package_path": extract_path,
"manifest_file": get_manifest_file(extract_path),
"launch_file": get_launch_file(extract_path),
}
)
chapter = frappe.new_doc("Course Chapter")
print(values.title)
chapter.update(values)
print(chapter.title)
chapter.insert()
return chapter
def get_manifest_file(extract_path):
manifest_file = None
for root, dirs, files in os.walk(extract_path):
for file in files:
if file == "imsmanifest.xml":
manifest_file = os.path.join(root, file)
break
if manifest_file:
break
return manifest_file
def get_launch_file(extract_path):
launch_file = None
manifest_file = get_manifest_file(extract_path)
print(extract_path)
if manifest_file:
with open(manifest_file) as file:
data = file.read()
print(data)
dom = parseString(data)
resource = dom.getElementsByTagName("resource")
for res in resource:
if res.getAttribute("adlcp:scormtype") == "sco":
launch_file = res.getAttribute("href")
break
if launch_file:
launch_file = os.path.join(os.path.dirname(manifest_file), launch_file)
return launch_file

View File

@@ -8,9 +8,16 @@
"editable_grid": 1, "editable_grid": 1,
"engine": "InnoDB", "engine": "InnoDB",
"field_order": [ "field_order": [
"course",
"column_break_3",
"title", "title",
"column_break_3",
"course",
"scorm_section",
"is_scorm_package",
"scorm_package",
"scorm_package_path",
"column_break_dlnw",
"manifest_file",
"launch_file",
"section_break_5", "section_break_5",
"lessons" "lessons"
], ],
@@ -43,6 +50,49 @@
"fieldtype": "Table", "fieldtype": "Table",
"label": "Lessons", "label": "Lessons",
"options": "Lesson Reference" "options": "Lesson Reference"
},
{
"default": "0",
"fieldname": "is_scorm_package",
"fieldtype": "Check",
"label": "Is SCORM Package"
},
{
"depends_on": "is_scorm_package",
"fieldname": "manifest_file",
"fieldtype": "Code",
"label": "Manifest File",
"read_only": 1
},
{
"depends_on": "is_scorm_package",
"fieldname": "launch_file",
"fieldtype": "Code",
"label": "Launch File",
"read_only": 1
},
{
"fieldname": "scorm_section",
"fieldtype": "Section Break",
"label": "SCORM"
},
{
"fieldname": "scorm_package",
"fieldtype": "Link",
"label": "SCORM Package",
"options": "File",
"read_only": 1
},
{
"fieldname": "column_break_dlnw",
"fieldtype": "Column Break"
},
{
"depends_on": "is_scorm_package",
"fieldname": "scorm_package_path",
"fieldtype": "Code",
"label": "SCORM Package Path",
"read_only": 1
} }
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
@@ -53,7 +103,7 @@
"link_fieldname": "chapter" "link_fieldname": "chapter"
} }
], ],
"modified": "2024-10-29 16:54:20.904683", "modified": "2024-11-11 16:25:45.586160",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "LMS", "module": "LMS",
"name": "Course Chapter", "name": "Course Chapter",

View File

@@ -1128,7 +1128,7 @@ def get_course_outline(course, progress=False):
chapter_details = frappe.db.get_value( chapter_details = frappe.db.get_value(
"Course Chapter", "Course Chapter",
chapter.chapter, chapter.chapter,
["name", "title"], ["name", "title", "is_scorm_package", "launch_file"],
as_dict=True, as_dict=True,
) )
chapter_details["idx"] = chapter.idx chapter_details["idx"] = chapter.idx