LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
Content/LineItem.php
1<?php
2
3namespace ceLTIc\LTI\Content;
4
6
15{
16
22 private $label = null;
23
29 private $scoreMaximum = null;
30
36 private $resourceId = null;
37
43 private $tag = null;
44
50 private $submissionReview = null;
51
61 function __construct($label, $scoreMaximum, $resourceId = null, $tag = null, $submissionReview = null)
62 {
63 $this->label = $label;
64 $this->scoreMaximum = $scoreMaximum;
65 $this->resourceId = $resourceId;
66 $this->tag = $tag;
67 $this->submissionReview = $submissionReview;
68 }
69
75 public function toJsonldObject()
76 {
77 $lineItem = new \stdClass();
78
79 $lineItem->{'@type'} = 'LineItem';
80 $lineItem->label = $this->label;
81 $lineItem->reportingMethod = 'http://purl.imsglobal.org/ctx/lis/v2p1/Result#normalScore';
82 if (!empty($this->resourceId)) {
83 $lineItem->assignedActivity = new \stdClass();
84 $lineItem->assignedActivity->activityId = $this->resourceId;
85 }
86 $lineItem->scoreConstraints = new \stdClass();
87 $lineItem->scoreConstraints->{'@type'} = 'NumericLimits';
88 $lineItem->scoreConstraints->normalMaximum = $this->scoreMaximum;
89 if (!empty($this->submissionReview)) {
90 $lineItem->submissionReview = $this->submissionReview->toJsonObject();
91 }
92
93 return $lineItem;
94 }
95
101 public function toJsonObject()
102 {
103 $lineItem = new \stdClass();
104
105 $lineItem->label = $this->label;
106 $lineItem->scoreMaximum = $this->scoreMaximum;
107 if (!empty($this->resourceId)) {
108 $lineItem->resourceId = $this->resourceId;
109 }
110 if (!empty($this->tag)) {
111 $lineItem->tag = $this->tag;
112 }
113 if (!empty($this->submissionReview)) {
114 $lineItem->submissionReview = $this->submissionReview->toJsonObject();
115 }
116
117 return $lineItem;
118 }
119
127 public static function fromJsonObject($item)
128 {
129 $obj = null;
130 $label = null;
131 $reportingMethod = null;
132 $scoreMaximum = null;
133 $activityId = null;
134 $tag = null;
135 $available = null;
136 $submission = null;
137 $submissionReview = null;
138 foreach (get_object_vars($item) as $name => $value) {
139 switch ($name) {
140 case 'label':
141 $label = $item->label;
142 break;
143 case 'reportingMethod':
144 $reportingMethod = $item->reportingMethod;
145 break;
146 case 'scoreConstraints':
147 $scoreConstraints = $item->scoreConstraints;
148 break;
149 case 'scoreMaximum':
150 $scoreMaximum = $item->scoreMaximum;
151 break;
152 case 'assignedActivity':
153 if (isset($item->assignedActivity->activityId)) {
154 $activityId = $item->assignedActivity->activityId;
155 }
156 break;
157 case 'resourceId':
158 $activityId = $item->resourceId;
159 break;
160 case 'tag':
161 $tag = $item->tag;
162 break;
163 case 'submissionReview':
164 $submissionReview = SubmissionReview::fromJsonObject($item->submissionReview);
165 break;
166 }
167 }
168 if (is_null($scoreMaximum) && $label && $reportingMethod && $scoreConstraints) {
169 foreach (get_object_vars($scoreConstraints) as $name => $value) {
170 $method = str_replace('Maximum', 'Score', $name);
171 if (substr($reportingMethod, -strlen($method)) === $method) {
172 $scoreMaximum = $value;
173 break;
174 }
175 }
176 }
177 if (!is_null($scoreMaximum)) {
178 $obj = new LineItem($label, $scoreMaximum, $activityId, $tag, $submissionReview);
179 }
180
181 return $obj;
182 }
183
184}
Class to represent a line-item object.
__construct($label, $scoreMaximum, $resourceId=null, $tag=null, $submissionReview=null)
Class constructor.
static fromJsonObject($item)
Generate a LineItem object from its JSON or JSON-LD representation.
toJsonObject()
Generate the JSON object representation of the line-item.
toJsonldObject()
Generate the JSON-LD object representation of the line-item.
Class to represent a submission review.