diff --git a/local/custom_service/lang/en/local_custom_service.php b/local/custom_service/lang/en/local_custom_service.php deleted file mode 100644 index 6fdc0b44600..00000000000 --- a/local/custom_service/lang/en/local_custom_service.php +++ /dev/null @@ -1,3 +0,0 @@ -. - -/** - * Version details - * - * @package block_calendar_upcoming - * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -$plugin->version = 2024100990; // The current plugin version (Date: YYYYMMDDXX). -$plugin->requires = 2024100290; // Requires this Moodle version. -$plugin->component = 'local_custom_service'; // Full name of the plugin (used for diagnostics) -$plugin->privacy = ['provider' => 'local_custom_service\privacy\provider']; \ No newline at end of file diff --git a/local/scormresource/README.md b/local/scormresource/README.md new file mode 100644 index 00000000000..a804f9facd7 --- /dev/null +++ b/local/scormresource/README.md @@ -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. diff --git a/local/custom_service/classes/privacy/provider.php b/local/scormresource/classes/privacy/provider.php similarity index 82% rename from local/custom_service/classes/privacy/provider.php rename to local/scormresource/classes/privacy/provider.php index 6e0fc064f09..516f5fd9d0b 100644 --- a/local/custom_service/classes/privacy/provider.php +++ b/local/scormresource/classes/privacy/provider.php @@ -1,8 +1,6 @@ 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, diff --git a/local/custom_service/externallib.php b/local/scormresource/externallib.php similarity index 86% rename from local/custom_service/externallib.php rename to local/scormresource/externallib.php index b6a995d3852..766e2f32caa 100644 --- a/local/custom_service/externallib.php +++ b/local/scormresource/externallib.php @@ -1,6 +1,6 @@ libdir.'/externallib.php'); require_once($CFG->dirroot.'/user/lib.php'); require_once($CFG->dirroot.'/course/lib.php'); @@ -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( @@ -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) { @@ -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); @@ -65,25 +59,20 @@ 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; @@ -91,23 +80,18 @@ public static function insert_scorm_resource($courseids,$section,$scormname,$fol $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, @@ -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') ) ) ); @@ -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 diff --git a/local/scormresource/lang/en/local_scormresource.php b/local/scormresource/lang/en/local_scormresource.php new file mode 100644 index 00000000000..28ff80887eb --- /dev/null +++ b/local/scormresource/lang/en/local_scormresource.php @@ -0,0 +1,3 @@ +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; \ No newline at end of file