Skip to content
Closed
11 changes: 11 additions & 0 deletions local/custom_service/classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace local_custom_service\privacy;

defined('MOODLE_INTERNAL') || die;


class provider implements \core_privacy\local\metadata\null_provider {
public static function get_reason() : string {
return 'privacy:metadata';
}
}
35 changes: 35 additions & 0 deletions local/custom_service/db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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',
'description' => 'Create a scorm resource under a course',
'type' => 'write',
'ajax' => true,
),

);

$services = array(
'Insert Scorm resource' => array(
'functions' => array(

'mod_scorm_insert_scorm_resource'
),
'restrictedusers' => 0,
'enabled' => 1,
// This field os optional, but requried if the `restrictedusers` value is
// set, so as to allow configuration via the Web UI.
'shortname' => 'InsertScorm',

// Whether to allow file downloads.
'downloadfiles' => 0,

// Whether to allow file uploads.
'uploadfiles' => 0,
)
);
172 changes: 172 additions & 0 deletions local/custom_service/externallib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

use core_completion\progress;
require_once($CFG->libdir.'/externallib.php');
require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/mod/scorm/lib.php');
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
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 {



public static function insert_scorm_resource_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_TEXT, 'Course Id'),
'section' => new external_value(PARAM_TEXT, 'section'),
'scormname' => new external_value(PARAM_TEXT, 'scorm name'),
'file' => new external_value(PARAM_TEXT, 'file'),
'path' => new external_value(PARAM_TEXT, 'path')

)
);
}
public static function insert_scorm_resource($courseids,$section,$scormname,$file,$path) {
global $DB,$CFG;
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->libdir . '/formslib.php');

require_login();
$courseid = $courseids; // Course ID where the SCORM package will be uploaded
$scormname = $scormname;// // Name for the SCORM module
$scormfile =$path . '' . $file. '.zip'; // 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) {
$zip->close();
} else {
$zip->close();
echo'imsmanifest.xml is missing from SCORM package.';
return;
}
} else {
echo 'Failed to open SCORM package.';
return;
}

// Get course and context
try{
$course = get_course($courseid);
}
catch(exception $ex)
{
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->intro = 'Intro to SCORM';
$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);

$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
$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,
'component' => 'mod_scorm',
'filearea' => 'package',
'itemid' => 0, // Item ID (could be used to reference a specific instance of the package)
'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, '/');
echo "Extraction complete!";
} else {
echo "ZIP file not found.";
}

//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);



$count = 0;
$lti_updated = [
'message'=>'Scorm package added to course',
];




return $lti_updated;
}
public static function insert_scorm_resource_returns() {
return new external_single_structure(
array(
'ids' => new external_value(PARAM_TEXT, 'course ids'),
'message'=> new external_value(PARAM_TEXT, 'success message'),
'updated'=>new external_value(PARAM_TEXT,'Items Updated')
)
);
}







}
3 changes: 3 additions & 0 deletions local/custom_service/lang/en/local_custom_service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$string['pluginname'] = 'Custom Web Services';
$string['privacy:metadata'] = 'The local_custom_service plugin does not store any personal data.';
9 changes: 9 additions & 0 deletions local/custom_service/tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../config.php"
backupGlobals="false"
colors="true"
verbose="true">
<testsuite name="local_custom_service">
<file>privacy_test.php</file>
</testsuite>
</phpunit>
17 changes: 17 additions & 0 deletions local/custom_service/tests/privacy_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/local/custom_service/classes/privacy/provider.php');

use core_privacy\tests\provider_testcase;
use local_custom_service\privacy\provider;

class local_custom_service_privacy_test extends provider_testcase {

public function test_privacy_provider() {
$this->assertInstanceOf(
\core_privacy\local\metadata\null_provider::class,
new provider()
);
}
}
30 changes: 30 additions & 0 deletions local/custom_service/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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/>.

/**
* 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'];
Loading