<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rustici-software / scormcloud-api-v2-client-php example snippets
// Note: This code is specifically designed to not modify any existing data
$dispatch_api = new RusticiSoftware\Cloud\V2\Api\DispatchApi();
$response = $dispatch_api->updateDispatchesWithHttpInfo(new \RusticiSoftware\Cloud\V2\Model\UpdateDispatchSchema(), null, new DateTime("now"));
print($response[2]['X-Total-Count'][0])
usticiSoftware\Cloud\V2 as ScormCloud;
// ScormCloud API credentials
// Note: These are not the same credentials used to log in to ScormCloud
const APP_ID = 'APP_ID';
const SECRET_KEY = 'SECRET_KEY';
// Sample values for data
const COURSE_PATH = '/PATH/TO/COURSE/RunTimeAdvancedCalls_SCORM20043rdEdition.zip';
const COURSE_ID = 'PHP_SAMPLE_COURSE';
const LEARNER_ID = 'PHP_SAMPLE_COURSE_LEARNER';
const REGISTRATION_ID = 'PHP_SAMPLE_COURSE_REGISTRATION';
// String used for output formatting
const OUTPUT_BORDER = "---------------------------------------------------------\n";
/**
* This sample will consist of:
* 1. Creating a course.
* 2. Registering a learner for the course.
* 3. Building a link for the learner to take the course.
* 4. Getting the learner's progress after having taken the course.
* 5. Viewing all courses and registrations.
* 6. Deleting all of the data created via this sample.
*
* All input variables used in this sample are defined up above.
*/
function main() {
// Configure HTTP basic authorization: APP_NORMAL
$config = new ScormCloud\Configuration();
$config->setUsername(APP_ID);
$config->setPassword(SECRET_KEY);
// Set the default configuration values for new configuration objects
ScormCloud\Configuration::setDefaultConfiguration($config);
$sc = new ScormCloud_Php_Sample();
try {
// Create a course and a registration
$courseDetails = $sc->createCourse(COURSE_ID, COURSE_PATH);
$sc->createRegistration(COURSE_ID, LEARNER_ID, REGISTRATION_ID);
// Show details of the newly imported course
echo 'Newly Imported Course Details: ', PHP_EOL;
echo $courseDetails, PHP_EOL;
// Create the registration launch link
$launchLink = $sc->buildLaunchLink(REGISTRATION_ID);
// Show the launch link
echo OUTPUT_BORDER, PHP_EOL;
echo "Launch Link: {$launchLink}", PHP_EOL;
echo 'Navigate to the url above to take the course. Hit enter once complete.', PHP_EOL;
readline();
// Get the results for the registration
$registrationProgress = $sc->getResultForRegistration(REGISTRATION_ID);
// Show details of the registration progress
echo OUTPUT_BORDER, PHP_EOL;
echo 'Registration Progess: ', PHP_EOL;
echo $registrationProgress, PHP_EOL;
// Get information about all the courses in ScormCloud
$courseList = $sc->getAllCourses();
// Show details of the courses
echo OUTPUT_BORDER, PHP_EOL;
echo 'Course List: ', PHP_EOL;
foreach ($courseList as $course) {
echo $course, PHP_EOL;
}
// Get information about all the registrations in ScormCloud
$registrationList = $sc->getAllRegistrations();
// Show details of the registrations
echo OUTPUT_BORDER, PHP_EOL;
echo 'Registration List: ', PHP_EOL;
foreach ($registrationList as $registration) {
echo $registration, PHP_EOL;
}
} catch (ScormCloud\ApiException | InvalidArgumentException $e) {
echo $e->getMessage(), PHP_EOL;
} finally {
// Delete all the data created by this sample
$sc->cleanUp(COURSE_ID, REGISTRATION_ID);
}
}
class ScormCloud_Php_Sample {
/**
* Sets the default OAuth token passed with all calls to the API.
*
* If a token is created with limited scope (i.e. read:registration),
* calls that >getImportJobStatus($jobId->getResult());
while ($jobResult->getStatus() == ScormCloud\Model\ImportJobResultSchema::STATUS_RUNNING) {
sleep(1);
$jobResult = $courseApi->getImportJobStatus($jobId->getResult());
}
if ($jobResult->getStatus() == ScormCloud\Model\ImportJobResultSchema::STATUS_ERROR)
throw new InvalidArgumentException('Course is not properly formatted: ' . $jobResult->getMessage());
return $jobResult->getImportResult()->getCourse();
}
/**
* Creates a registration allowing the learner to consume the course
* content. A registration is the link between a learner and a single
* course.
*
* @param string $courseId Id of the course to register the learner for.
* @param string $learnerId Id that will be used to identify the learner.
* @param string $registrationId Id that will be used to identify the registration.
*/
function createRegistration($courseId, $learnerId, $registrationId) {
// (Optional) Further authenticate via OAuth token access
// $this->configureOAuth([ 'write:registration' ]);
$registrationApi = new ScormCloud\Api\RegistrationApi();
$learner = new ScormCloud\Model\LearnerSchema([ 'id' => $learnerId ]);
$registration = new ScormCloud\Model\CreateRegistrationSchema([ 'course_id' => $courseId, 'learner' => $learner, 'registration_id' => $registrationId]);
$registrationApi->createRegistration($registration);
}
/**
* Builds a url allowing the learner to access the course.
*
* This sample will build the launch link and print it out. It will then
* pause and wait for user input, allowing you to navigate to the course
* to generate sample learner progress. Once this step has been reached,
* hitting the enter key will continue program execution.
*
* @param string $registrationId Id of the registration the link is being built for.
* @return string Link for the learner to launch the course.
*/
function buildLaunchLink($registrationId) {
// (Optional) Further authenticate via OAuth token access
// $this->configureOAuth([ 'read:registration' ]);
$registrationApi = new ScormCloud\Api\RegistrationApi();
$settings = new ScormCloud\Model\LaunchLinkRequestSchema([ 'redirect_on_exit_url' => 'Message' ]);
$launchLink = $registrationApi->buildRegistrationLaunchLink($registrationId, $settings);
return $launchLink->getLaunchLink();
}
/**
* Gets information about the progress of the registration.
*
* For the most up-to-date results, you should implement our postback
* mechanism. The basic premise is that any update to the registration
* would cause us to send the updated results to your system.
*
* More details can be found in the documentation:
* https://cloud.scorm.com/docs/v2/guides/postback/
*
* @param string Id of the registration to get results for.
* @return ScormCloud\Model\RegistrationSchema Detailed information about the registration's progress.
*/
function getResultForRegistration($registrationId) {
// (Optional) Further authenticate via OAuth token access
// $this->configureOAuth([ 'read:registration' ]);
$registrationApi = new ScormCloud\Api\RegistrationApi();
$progress = $registrationApi->getRegistrationProgress($registrationId);
return $progress;
}
/**
* Gets information about all courses. The result received from the API
* call is a paginated list, meaning that additional calls are
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.