|
2 | 2 |
|
3 | 3 | namespace Packback\Lti1p3;
|
4 | 4 |
|
5 |
| -use Packback\Lti1p3\Interfaces\ILtiServiceConnector; |
6 |
| - |
7 |
| -class LtiAssignmentsGradesService |
| 5 | +class LtiAssignmentsGradesService extends LtiAbstractService |
8 | 6 | {
|
9 |
| - private $service_connector; |
10 |
| - private $service_data; |
| 7 | + public const CONTENTTYPE_SCORE = 'application/vnd.ims.lis.v1.score+json'; |
| 8 | + public const CONTENTTYPE_LINEITEM = 'application/vnd.ims.lis.v2.lineitem+json'; |
| 9 | + public const CONTENTTYPE_RESULTCONTAINER = 'application/vnd.ims.lis.v2.resultcontainer+json'; |
11 | 10 |
|
12 |
| - public function __construct(ILtiServiceConnector $service_connector, array $service_data) |
| 11 | + public function getScope(): array |
13 | 12 | {
|
14 |
| - $this->service_connector = $service_connector; |
15 |
| - $this->service_data = $service_data; |
| 13 | + return $this->getServiceData()['scope']; |
16 | 14 | }
|
17 | 15 |
|
18 | 16 | public function putGrade(LtiGrade $grade, LtiLineitem $lineitem = null)
|
19 | 17 | {
|
20 |
| - if (!in_array(LtiConstants::AGS_SCOPE_SCORE, $this->service_data['scope'])) { |
| 18 | + if (!in_array(LtiConstants::AGS_SCOPE_SCORE, $this->getScope())) { |
21 | 19 | throw new LtiException('Missing required scope', 1);
|
22 | 20 | }
|
23 | 21 | if ($lineitem !== null && empty($lineitem->getId())) {
|
24 | 22 | $lineitem = $this->findOrCreateLineitem($lineitem);
|
25 |
| - $score_url = $lineitem->getId(); |
26 |
| - } elseif ($lineitem === null && !empty($this->service_data['lineitem'])) { |
27 |
| - $score_url = $this->service_data['lineitem']; |
| 23 | + $scoreUrl = $lineitem->getId(); |
| 24 | + } elseif ($lineitem === null && !empty($this->getServiceData()['lineitem'])) { |
| 25 | + $scoreUrl = $this->getServiceData()['lineitem']; |
28 | 26 | } else {
|
29 | 27 | $lineitem = LtiLineitem::new()
|
30 | 28 | ->setLabel('default')
|
31 | 29 | ->setScoreMaximum(100);
|
32 | 30 | $lineitem = $this->findOrCreateLineitem($lineitem);
|
33 |
| - $score_url = $lineitem->getId(); |
| 31 | + $scoreUrl = $lineitem->getId(); |
34 | 32 | }
|
35 | 33 |
|
36 | 34 | // Place '/scores' before url params
|
37 |
| - $pos = strpos($score_url, '?'); |
38 |
| - $score_url = $pos === false ? $score_url.'/scores' : substr_replace($score_url, '/scores', $pos, 0); |
39 |
| - |
40 |
| - return $this->service_connector->makeServiceRequest( |
41 |
| - $this->service_data['scope'], |
42 |
| - LtiServiceConnector::METHOD_POST, |
43 |
| - $score_url, |
44 |
| - $grade, |
45 |
| - 'application/vnd.ims.lis.v1.score+json' |
46 |
| - ); |
| 35 | + $pos = strpos($scoreUrl, '?'); |
| 36 | + $scoreUrl = $pos === false ? $scoreUrl.'/scores' : substr_replace($scoreUrl, '/scores', $pos, 0); |
| 37 | + |
| 38 | + $request = new ServiceRequest(LtiServiceConnector::METHOD_POST, $scoreUrl); |
| 39 | + $request->setBody($grade); |
| 40 | + $request->setContentType(static::CONTENTTYPE_SCORE); |
| 41 | + |
| 42 | + return $this->makeServiceRequest($request); |
47 | 43 | }
|
48 | 44 |
|
49 |
| - public function findOrCreateLineitem(LtiLineitem $new_line_item) |
| 45 | + public function findOrCreateLineitem(LtiLineitem $newLineItem) |
50 | 46 | {
|
51 |
| - $line_items = $this->getLineItems(); |
| 47 | + $lineitems = $this->getLineItems(); |
52 | 48 |
|
53 |
| - foreach ($line_items as $line_item) { |
| 49 | + foreach ($lineitems as $lineitem) { |
54 | 50 | if (
|
55 |
| - (empty($new_line_item->getResourceId()) && empty($new_line_item->getResourceLinkId())) || |
56 |
| - (isset($line_item['resourceId']) && $line_item['resourceId'] == $new_line_item->getResourceId()) || |
57 |
| - (isset($line_item['resourceLinkId']) && $line_item['resourceLinkId'] == $new_line_item->getResourceLinkId()) |
| 51 | + (empty($newLineItem->getResourceId()) && empty($newLineItem->getResourceLinkId())) || |
| 52 | + (isset($lineitem['resourceId']) && $lineitem['resourceId'] == $newLineItem->getResourceId()) || |
| 53 | + (isset($lineitem['resourceLinkId']) && $lineitem['resourceLinkId'] == $newLineItem->getResourceLinkId()) |
58 | 54 | ) {
|
59 |
| - if (empty($new_line_item->getTag()) || $line_item['tag'] == $new_line_item->getTag()) { |
60 |
| - return new LtiLineitem($line_item); |
| 55 | + if (empty($newLineItem->getTag()) || $lineitem['tag'] == $newLineItem->getTag()) { |
| 56 | + return new LtiLineitem($lineitem); |
61 | 57 | }
|
62 | 58 | }
|
63 | 59 | }
|
64 |
| - $created_line_item = $this->service_connector->makeServiceRequest( |
65 |
| - $this->service_data['scope'], |
66 |
| - LtiServiceConnector::METHOD_POST, |
67 |
| - $this->service_data['lineitems'], |
68 |
| - $new_line_item, |
69 |
| - 'application/vnd.ims.lis.v2.lineitem+json', |
70 |
| - 'application/vnd.ims.lis.v2.lineitem+json' |
71 |
| - ); |
| 60 | + $request = new ServiceRequest(LtiServiceConnector::METHOD_POST, $this->getServiceData()['lineitems']); |
| 61 | + $request->setBody($newLineItem) |
| 62 | + ->setContentType(static::CONTENTTYPE_LINEITEM) |
| 63 | + ->setAccept(static::CONTENTTYPE_LINEITEM); |
| 64 | + $createdLineItems = $this->makeServiceRequest($request); |
72 | 65 |
|
73 |
| - return new LtiLineitem($created_line_item['body']); |
| 66 | + return new LtiLineitem($createdLineItems['body']); |
74 | 67 | }
|
75 | 68 |
|
76 | 69 | public function getGrades(LtiLineitem $lineitem)
|
77 | 70 | {
|
78 | 71 | $lineitem = $this->findOrCreateLineitem($lineitem);
|
79 | 72 | // Place '/results' before url params
|
80 | 73 | $pos = strpos($lineitem->getId(), '?');
|
81 |
| - $results_url = $pos === false ? $lineitem->getId().'/results' : substr_replace($lineitem->getId(), '/results', $pos, 0); |
82 |
| - $scores = $this->service_connector->makeServiceRequest( |
83 |
| - $this->service_data['scope'], |
84 |
| - LtiServiceConnector::METHOD_GET, |
85 |
| - $results_url, |
86 |
| - null, |
87 |
| - null, |
88 |
| - 'application/vnd.ims.lis.v2.resultcontainer+json' |
89 |
| - ); |
| 74 | + $resultsUrl = $pos === false ? $lineitem->getId().'/results' : substr_replace($lineitem->getId(), '/results', $pos, 0); |
| 75 | + $request = new ServiceRequest(LtiServiceConnector::METHOD_GET, $resultsUrl); |
| 76 | + $request->setAccept(); |
| 77 | + $scores = $this->makeServiceRequest($request); |
90 | 78 |
|
91 | 79 | return $scores['body'];
|
92 | 80 | }
|
93 | 81 |
|
94 |
| - public function getLineItems() |
| 82 | + public function getLineItems(): array |
95 | 83 | {
|
96 |
| - if (!in_array(LtiConstants::AGS_SCOPE_LINEITEM, $this->service_data['scope'])) { |
| 84 | + if (!in_array(LtiConstants::AGS_SCOPE_LINEITEM, $this->getScope())) { |
97 | 85 | throw new LtiException('Missing required scope', 1);
|
98 | 86 | }
|
99 |
| - $line_items = []; |
100 |
| - |
101 |
| - $next_page = $this->service_data['lineitems']; |
102 |
| - |
103 |
| - while ($next_page) { |
104 |
| - $page = $this->service_connector->makeServiceRequest( |
105 |
| - $this->service_data['scope'], |
106 |
| - LtiServiceConnector::METHOD_GET, |
107 |
| - $next_page, |
108 |
| - null, |
109 |
| - null, |
110 |
| - 'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json' |
111 |
| - ); |
112 |
| - |
113 |
| - $line_items = array_merge($line_items, $page['body']); |
114 |
| - $next_page = false; |
115 |
| - |
116 |
| - // If the "Next" Link is not in the request headers, we can break the loop here. |
117 |
| - if (!isset($page['headers']['Link'])) { |
118 |
| - break; |
119 |
| - } |
120 | 87 |
|
121 |
| - $link = $page['headers']['Link']; |
| 88 | + $request = new ServiceRequest( |
| 89 | + LtiServiceConnector::METHOD_GET, |
| 90 | + $this->getServiceData()['lineitems'] |
| 91 | + ); |
| 92 | + $request->setAccept(static::CONTENTTYPE_RESULTCONTAINER); |
122 | 93 |
|
123 |
| - if (preg_match(LtiServiceConnector::NEXT_PAGE_REGEX, $link, $matches)) { |
124 |
| - $next_page = $matches[1]; |
125 |
| - } |
126 |
| - } |
| 94 | + $lineitems = $this->getAll($request, 'lineitems'); |
127 | 95 |
|
128 | 96 | // If there is only one item, then wrap it in an array so the foreach works
|
129 |
| - if (isset($line_items['body']['id'])) { |
130 |
| - $line_items['body'] = [$line_items['body']]; |
| 97 | + if (isset($lineitems['body']['id'])) { |
| 98 | + $lineitems['body'] = [$lineitems['body']]; |
131 | 99 | }
|
132 | 100 |
|
133 |
| - return $line_items; |
| 101 | + return $lineitems; |
134 | 102 | }
|
135 | 103 | }
|
0 commit comments