Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions local/custom_service/lang/en/local_custom_service.php

This file was deleted.

30 changes: 0 additions & 30 deletions local/custom_service/version.php

This file was deleted.

34 changes: 34 additions & 0 deletions local/scormresource/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# local_scormresource

The plugin will help to insert scorm resource data from external source.

## Features

- The plugin accept the corse id ,sectionid, scorm name , folder name and base64zip of scorm package and creates scorm under the specified course.


## Requirements

- Moodle version: 4.5 or higher
- PHP version: 8.3 or higher

## Installation

1. Place the plugin folder into `moodle/local/scormresource/` or install the plugin using the zip
2. Visit the **Site administration > Notifications** page to complete installation.

## Configuration

Add the funtion mod_scorm_insert_scorm_resource into the external service.

## Usage

Use the end point mod_scorm_insert_scorm_resource to insert the scorm data. Input parameters are courseid,section,scormname,foldername,base64Zip

## Support

For bugs or feature requests, open an issue in the repository or contact the maintainer.

## License

NHS England.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace local_custom_service\privacy;

namespace local_scormresource\privacy;
defined('MOODLE_INTERNAL') || die;


class provider implements \core_privacy\local\metadata\null_provider {
public static function get_reason() : string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

defined('MOODLE_INTERNAL') || die();
$functions = array(

'mod_scorm_insert_scorm_resource' => array(
'classname' => 'insert_scorm_resource',
'methodname' => 'insert_scorm_resource',
'classpath' => 'local/custom_service/externallib.php',
'classpath' => 'local/scormresource/externallib.php',
'description' => 'Create a scorm resource under a course',
'type' => 'write',
'ajax' => true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use core_completion\progress;
use core\http_client;
require_once($CFG->libdir.'/externallib.php');
require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/course/lib.php');
Expand All @@ -9,10 +9,8 @@
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir . '/filestorage/file_storage.php');
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
class insert_scorm_resource extends external_api {



class insert_scorm_resource extends external_api {
public static function insert_scorm_resource_parameters() {
return new external_function_parameters(
array(
Expand All @@ -31,17 +29,14 @@ public static function insert_scorm_resource($courseids,$section,$scormname,$fol
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->libdir . '/formslib.php');

require_once(__DIR__ . '/../../config.php');
require_login();
global $DB, $USER;
// zip file
$savedPath = self::saveBase64ToZip($base64Zip, $foldername . '.zip');

//zip end here
$courseid = $courseids; // Course ID where the SCORM package will be uploaded
$scormfile =$savedPath;//$path . '' . $file. '.zip'; // Path to the SCORM .zip file

$scormfile =$savedPath;// Path to the SCORM .zip file
$zip = new ZipArchive;

if ($zip->open($scormfile) === TRUE) {
// Check if imsmanifest.xml exists in the ZIP archive
if ($zip->locateName('imsmanifest.xml', ZipArchive::FL_NODIR) !== false) {
Expand All @@ -55,7 +50,6 @@ public static function insert_scorm_resource($courseids,$section,$scormname,$fol
echo 'Failed to open SCORM package.';
return;
}

// Get course and context
try{
$course = get_course($courseid);
Expand All @@ -65,49 +59,39 @@ public static function insert_scorm_resource($courseids,$section,$scormname,$fol
echo 'Course not found';
return;
}

$context = context_course::instance($courseid);

// Check permissions
require_capability('mod/scorm:addinstance', $context);

// Create SCORM instance (if needed)
$scorm = new stdClass();
$scorm->course = $courseid;
$scorm->name = $scormname;
$scorm->reference='Test Ref.zip';
$scorm->reference=$foldername . '.zip';
$scorm->intro = 'Intro to SCORM';
$scorm->maxattempt=3;
$scorm->maxattempt=0;
$scorm->introformat = FORMAT_HTML;
$scorm->timemodified = time();

// Insert the SCORM instance into the database and get the instance ID
$scorm->id = $DB->insert_record('scorm', $scorm);

// Create a new course module record
$cm = new stdClass();
$cm->course = $courseid;
$cm->module = $DB->get_field('modules', 'id', array('name' => 'scorm'));
$cm->instance = $scorm->id;
$cm->visible = 1;
$cm->section = $section; // You can set the section if needed

// Insert the course module
$cm->id = add_course_module($cm); //$DB->insert_record('course_modules', $cm);

$cm->id = add_course_module($cm);
$sectionid=course_add_cm_to_section($courseid,$cm->id,$cm->section);

// Update the record
$data = new stdClass();
$data->id = $cm->id; // The ID of the course module to update
$data->section = $sectionid; // The new section value

// // Update the record in the course_modules table
$data->id = $cm->id;
$data->section = $sectionid;
//Update the record in the course_modules table
$DB->update_record('course_modules', $data);
// Upload the SCORM package to Moodle file storage
$fs = get_file_storage();
$context = context_module::instance($cm->id);

// Add the SCORM .zip package to the file area
$fileinfo = array(
'contextid' => $context->id,
Expand All @@ -117,46 +101,36 @@ public static function insert_scorm_resource($courseids,$section,$scormname,$fol
'filepath' => '/',
'filename' => $file. '.zip'
);

$file = $fs->create_file_from_pathname($fileinfo, $scormfile);

$packer = get_file_packer('application/zip');
;
if ($file) {
$extracted_files = $file->extract_to_storage($packer,$context->id, 'mod_scorm', 'content', 0, '/');
} else {
}

//new code for reading imsmanifest.xml
$fs = get_file_storage();

// Locate the extracted directory in Moodle file storage (adjust as needed)
$contextid = $context->id; // The course/module context ID
$component = 'mod_scorm'; // Change this to match your module (e.g., mod_scorm, mod_lti, etc.)
$filearea = 'content'; // File area for SCORM or Common Cartridge
$itemid = 0; // Usually 0 unless specified
$filename = 'imsmanifest.xml';

// Get the manifest file
$file = $fs->get_file($contextid, $component, $filearea, $itemid, '/', $filename);
$manifest = $fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml');


scorm_parse_scorm($scorm, $manifest);


$lti_updated[] = array(
'id' => $scorm->id,
'name' => $scormname,
);
$lti_updated[] = array(
'scormid' => $scorm->id,
'sectionid' => $sectionid
);
return $lti_updated;
}
public static function insert_scorm_resource_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'new scorm id'),
'name' => new external_value(PARAM_RAW, 'new scorm name'),
'scormid' => new external_value(PARAM_INT, 'new scorm id'),
'sectionid' => new external_value(PARAM_INT, 'new section id')
)
)
);
Expand All @@ -167,14 +141,11 @@ public static function saveBase64ToZip($base64Data, $filename, $saveDir = 'uploa
if (!file_exists($saveDir)) {
mkdir($saveDir, 0777, true);
}

// Sanitize filename
$safeFilename = preg_replace('/[^a-zA-Z0-9_\.-]/', '_', $filename);
$zipPath = rtrim($saveDir, '/') . '/' . $safeFilename;

// Decode base64
$binaryData = base64_decode($base64Data);

// Save the file
if (file_put_contents($zipPath, $binaryData) !== false) {
return realpath($zipPath); // return full path
Expand Down
3 changes: 3 additions & 0 deletions local/scormresource/lang/en/local_scormresource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$string['pluginname'] = 'Scorm Resource Service';
$string['privacy:metadata'] = 'The local_scormresource plugin does not store any personal data.';
9 changes: 9 additions & 0 deletions local/scormresource/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024100990;
$plugin->requires = 2024100290;
$plugin->component = 'local_scormresource';
$plugin->privacy = ['provider' => 'local_scormresource\privacy\provider'];
$plugin->release = 'v1.0.0';
$plugin->maturity = MATURITY_STABLE;