Skip to content

Commit

Permalink
Merge pull request #47 from IMSGlobal/groups_and_sub_review
Browse files Browse the repository at this point in the history
Add support for the groups service and submission review
  • Loading branch information
MartinLenord authored Jun 3, 2020
2 parents 8a42ff7 + b124206 commit 3a192de
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 1 deletion.
110 changes: 110 additions & 0 deletions src/lti/LTI_Course_Groups_Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
namespace IMSGlobal\LTI;

class LTI_Course_Groups_Service {

private $service_connector;
private $service_data;

public function __construct(LTI_Service_Connector $service_connector, $service_data) {
$this->service_connector = $service_connector;
$this->service_data = $service_data;
}

public function get_groups() {

$groups = [];

$next_page = $this->service_data['context_groups_url'];

while ($next_page) {
$page = $this->service_connector->make_service_request(
$this->service_data['scope'],
'GET',
$next_page,
null,
null,
'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json'
);

$groups = array_merge($groups, $page['body']['groups']);

$next_page = false;
foreach($page['headers'] as $header) {
if (preg_match(LTI_Service_Connector::NEXT_PAGE_REGEX, $header, $matches)) {
$next_page = $matches[1];
break;
}
}
}
return $groups;

}

public function get_sets() {

$sets = [];

// Sets are optional.
if (!isset($this->service_data['context_group_sets_url'])) {
return [];
}

$next_page = $this->service_data['context_group_sets_url'];

while ($next_page) {
$page = $this->service_connector->make_service_request(
$this->service_data['scope'],
'GET',
$next_page,
null,
null,
'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json'
);

$sets = array_merge($sets, $page['body']['sets']);

$next_page = false;
foreach($page['headers'] as $header) {
if (preg_match(LTI_Service_Connector::NEXT_PAGE_REGEX, $header, $matches)) {
$next_page = $matches[1];
break;
}
}
}
return $sets;

}

public function get_groups_by_set() {
$groups = $this->get_groups();
$sets = $this->get_sets();

$groups_by_set = [];
$unsetted = [];

foreach ($sets as $key => $set) {
$groups_by_set[$set['id']] = $set;
$groups_by_set[$set['id']]['groups'] = [];
}

foreach ($groups as $key => $group) {
if (isset($group['set_id']) && isset($groups_by_set[$group['set_id']])) {
$groups_by_set[$group['set_id']]['groups'][$group['id']] = $group;
} else {
$unsetted[$group['id']] = $group;
}
}

if (!empty($unsetted)) {
$groups_by_set['none'] = [
"name" => "None",
"id" => "none",
"groups" => $unsetted,
];
}

return $groups_by_set;
}
}
?>
22 changes: 22 additions & 0 deletions src/lti/LTI_Grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
class LTI_Grade {
private $score_given;
private $score_maximum;
private $comment;
private $activity_progress;
private $grading_progress;
private $timestamp;
private $user_id;
private $submission_review;

/**
* Static function to allow for method chaining without having to assign to a variable first.
Expand All @@ -34,6 +36,15 @@ public function set_score_maximum($value) {
return $this;
}

public function get_comment() {
return $this->comment;
}

public function set_comment($comment) {
$this->comment = $comment;
return $this;
}

public function get_activity_progress() {
return $this->activity_progress;
}
Expand Down Expand Up @@ -70,14 +81,25 @@ public function set_user_id($value) {
return $this;
}

public function get_submission_review() {
return $this->submission_review;
}

public function set_submission_review($value) {
$this->submission_review = $value;
return $this;
}

public function __toString() {
return json_encode(array_filter([
"scoreGiven" => 0 + $this->score_given,
"scoreMaximum" => 0 + $this->score_maximum,
"comment" => $this->comment,
"activityProgress" => $this->activity_progress,
"gradingProgress" => $this->grading_progress,
"timestamp" => $this->timestamp,
"userId" => $this->user_id,
"submissionReview" => $this->submission_review,
]));
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/lti/LTI_Grade_Submission_Review.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace IMSGlobal\LTI;

class LTI_Grade_Submission_Review {
private $reviewable_status;
private $label;
private $url;
private $custom;

/**
* Static function to allow for method chaining without having to assign to a variable first.
*/
public static function new() {
return new LTI_Grade_Submission_Review();
}

public function get_reviewable_status() {
return $this->reviewable_status;
}

public function set_reviewable_status($value) {
$this->reviewable_status = $value;
return $this;
}

public function get_label() {
return $this->label;
}

public function set_label($value) {
$this->label = $value;
return $this;
}

public function get_url() {
return $this->url;
}

public function set_url($url) {
$this->url = $url;
return $this;
}

public function get_custom() {
return $this->custom;
}

public function set_custom($value) {
$this->custom = $value;
return $this;
}

public function __toString() {
return json_encode(array_filter([
"reviewableStatus" => $this->reviewable_status,
"label" => $this->label,
"url" => $this->url,
"custom" => $this->custom,
]));
}
}
?>
29 changes: 29 additions & 0 deletions src/lti/LTI_Message_Launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ public function get_nrps() {
$this->jwt['body']['https://purl.imsglobal.org/spec/lti-nrps/claim/namesroleservice']);
}

/**
* Returns whether or not the current launch can use the groups service.
*
* @return boolean Returns a boolean indicating the availability of groups.
*/
public function has_gs() {
return !empty($this->jwt['body']['https://purl.imsglobal.org/spec/lti-gs/claim/groupsservice']['context_groups_url']);
}

/**
* Fetches an instance of the groups service for the current launch.
*
* @return LTI_Course_Groups_Service An instance of the groups service that can be used to make calls within the scope of the current launch.
*/
public function get_gs() {
return new LTI_Course_Groups_Service(
new LTI_Service_Connector($this->registration),
$this->jwt['body']['https://purl.imsglobal.org/spec/lti-gs/claim/groupsservice']);
}

/**
* Returns whether or not the current launch can use the assignments and grades service.
*
Expand Down Expand Up @@ -149,6 +169,15 @@ public function is_deep_link_launch() {
return $this->jwt['body']['https://purl.imsglobal.org/spec/lti/claim/message_type'] === 'LtiDeepLinkingRequest';
}

/**
* Returns whether or not the current launch is a submission review launch.
*
* @return boolean Returns true if the current launch is a submission review launch.
*/
public function is_submission_review_launch() {
return $this->jwt['body']['https://purl.imsglobal.org/spec/lti/claim/message_type'] === 'LtiSubmissionReviewRequest';
}

/**
* Returns whether or not the current launch is a resource launch.
*
Expand Down
2 changes: 1 addition & 1 deletion src/lti/LTI_Names_Roles_Provisioning_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function get_members() {

$next_page = false;
foreach($page['headers'] as $header) {
if (preg_match("/^Link:.*<([^>]*)>; ?rel=\"next\"/i", $header, $matches)) {
if (preg_match(LTI_Service_Connector::NEXT_PAGE_REGEX, $header, $matches)) {
$next_page = $matches[1];
break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lti/LTI_Service_Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use Firebase\JWT\JWT;

class LTI_Service_Connector {

const NEXT_PAGE_REGEX = "/^Link:.*<([^>]*)>; ?rel=\"next\"/i";

private $registration;
private $access_tokens = [];

Expand Down
29 changes: 29 additions & 0 deletions src/lti/message_validators/submission_review_message_validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace IMSGlobal\LTI;

class Submission_Review_Message_Validator implements Message_Validator {
public function can_validate($jwt_body) {
return $jwt_body['https://purl.imsglobal.org/spec/lti/claim/message_type'] === 'LtiSubmissionReviewRequest';
}

public function validate($jwt_body) {
if (empty($jwt_body['sub'])) {
throw new LTI_Exception('Must have a user (sub)');
}
if ($jwt_body['https://purl.imsglobal.org/spec/lti/claim/version'] !== '1.3.0') {
throw new LTI_Exception('Incorrect version, expected 1.3.0');
}
if (!isset($jwt_body['https://purl.imsglobal.org/spec/lti/claim/roles'])) {
throw new LTI_Exception('Missing Roles Claim');
}
if (empty($jwt_body['https://purl.imsglobal.org/spec/lti/claim/resource_link']['id'])) {
throw new LTI_Exception('Missing Resource Link Id');
}
if (empty($jwt_body['https://purl.imsglobal.org/spec/lti/claim/for_user'])) {
throw new LTI_Exception('Missing For User');
}

return true;
}
}
?>

0 comments on commit 3a192de

Please sign in to comment.