Skip to content

Commit

Permalink
Merge pull request #42 from cbs-software/test-and-cleanup-learner-report
Browse files Browse the repository at this point in the history
fix: test and cleanup learner report
  • Loading branch information
brianreichtcs authored May 29, 2024
2 parents 29ff371 + a2a8f66 commit a8a576d
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 147 deletions.
86 changes: 86 additions & 0 deletions example/GetEnrollmentReportLiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* A live Enrollment Report test.
*
* This test expects the SMARTERU_ACCOUNT_KEY and SMARTERU_USER_KEY environment
* variables to be set. It will query the SMARTERU API for a list of all users
* in the SANDBOX group that are active and output their progress in the
* courses they are enrolled in, in CSV format.
*
* @copyright $year$ Core Business Solutions
* @license MIT
* @version $version$
*/

declare(strict_types=1);

namespace CBS\SmarterU\Tests\Usability;

require_once(__DIR__ . '/../vendor/autoload.php');

use CBS\SmarterU\Client;
use CBS\SmarterU\Exceptions\SmarterUException;
use CBS\SmarterU\Queries\GetLearnerReportQuery;

$accountKey = getenv('SMARTERU_ACCOUNT_KEY') ?? null;
$userKey = getenv('SMARTERU_USER_KEY') ?? null;
$groupName = $argv[1] ?? null;

function printHelp() {
echo <<< END_OF_HELP
Prints a CSV Enrollment Report for all active users in the specified group.
The script expects the SMARTERU_ACCOUNT_KEY and SMARTERU_USER_KEY environment
variables to be set.
Usage: php GetEnrollmentReportLiveTest.php <group-name>
Example: php GetEnrollmentReportLiveTest.php "Widgets Inc."
END_OF_HELP;
}
// Make sure we have the necessary environment variables to perform the test.
if (empty($accountKey) || empty($userKey) || empty($groupName)) {
printHelp();
exit(1);
}

try {
// Create the Client for speaking to the API
$client = new Client($accountKey, $userKey);

// Query for an Enrollment report for all users in our SANDBOX group
// that are active. Add some optional fields that we care about.
$results = $client->getLearnerReport(
(new GetLearnerReportQuery())
->setGroupNames([$groupName])
->setUserStatus('Active')
->setColumns(['PROGRESS', 'COURSE_DURATION', 'DUE_DATE', ])
);

fputcsv(STDOUT, [
'ID',
'Given Name',
'Surname',
'Course Name',
'Course Duration',
'Is Completed',
'Completed Date',
'Progress'
]);

foreach ($results as $learnerReport) {
fputcsv(STDOUT, [
$learnerReport->getId(),
$learnerReport->getGivenName(),
$learnerReport->getSurname(),
$learnerReport->getCourseName(),
$learnerReport->getCourseDuration(),
$learnerReport->getProgress() === '100' ? 'Yes' : 'No',
$learnerReport->getCompletedDate(),
$learnerReport->getProgress()
]);
}
} catch (SmarterUException $error) {
var_dump($error);
}
4 changes: 3 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class Client {
*/
public const SMARTERU_EXCEPTION_MESSAGE = 'SmarterU rejected the request.';

public const ERROR_GENERIC_REQUEST_FAILURE = 'Failed to make request to SmarterU API. See context for request/response details.';

#endregion constants

#region properties
Expand Down Expand Up @@ -1536,7 +1538,7 @@ public function sanitizeRequestXML(string $request): string {
private function logFailedRequest(string $request, string $response) {
// Log the request and response.
$this->logger->error(
'Failed to make request to SmarterU API. See context for request/response details.',
self::ERROR_GENERIC_REQUEST_FAILURE,
[
'request' => $this->sanitizeRequestXML($request),
'response' => $response
Expand Down
11 changes: 3 additions & 8 deletions src/Queries/GetLearnerReportQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class GetLearnerReportQuery {
/**
* The system-generated identifier for the user's course enrollment.
*/
protected string $enrollmentId;
protected ?string $enrollmentId = null;

/**
* The status of groups to return. Acceptable values are "Active",
Expand Down Expand Up @@ -284,20 +284,15 @@ public function setPageSize(int $pageSize): self {

/**
* Get the system-generated identifier for the user's course enrollment.
*
* @return string The system-generated identifier.
*/
public function getEnrollmentId(): string {
public function getEnrollmentId(): ?string {
return $this->enrollmentId;
}

/**
* Set the system-generated identifier for the user's course enrollment.
*
* @param string $enrollmentId The system-generated identifier.
* @return self
*/
public function setEnrollmentId(string $enrollmentId): self {
public function setEnrollmentId(?string $enrollmentId): self {
$this->enrollmentId = $enrollmentId;
return $this;
}
Expand Down
Loading

0 comments on commit a8a576d

Please sign in to comment.