ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
LineItem.php
Go to the documentation of this file.
1 <?php
2 
20 
28 class LineItem
29 {
35  private ?string $label = null;
36 
42  private ?int $scoreMaximum = null;
43 
49  private ?string $resourceId = null;
50 
56  private ?string $tag = null;
57 
65  public function __construct(string $label, int $scoreMaximum, string $resourceId = null, string $tag = null)
66  {
67  $this->label = $label;
68  $this->scoreMaximum = $scoreMaximum;
69  $this->resourceId = $resourceId;
70  $this->tag = $tag;
71  }
72 
78  public function toJsonldObject(): object
79  {
80  $lineItem = new \stdClass();
81 
82  $lineItem->{'@type'} = 'LineItem';
83  $lineItem->label = $this->label;
84  $lineItem->reportingMethod = 'http://purl.imsglobal.org/ctx/lis/v2p1/Result#normalScore';
85  if (!empty($this->resourceId)) {
86  $lineItem->assignedActivity = new \stdClass();
87  $lineItem->assignedActivity->activityId = $this->resourceId;
88  }
89  $lineItem->scoreConstraints = new \stdClass();
90  $lineItem->scoreConstraints->{'@type'} = 'NumericLimits';
91  $lineItem->scoreConstraints->normalMaximum = $this->scoreMaximum;
92 
93  return $lineItem;
94  }
95 
101  public function toJsonObject(): object
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 
114  return $lineItem;
115  }
116 
122  public static function fromJsonObject(object $item): ?LineItem
123  {
124  $obj = null;
125  $label = null;
126  $reportingMethod = null;
127  $scoreMaximum = null;
128  $activityId = null;
129  $tag = null;
130  $available = null;
131  $submission = null;
132  foreach (get_object_vars($item) as $name => $value) {
133  switch ($name) {
134  case 'label':
135  $label = $item->label;
136  break;
137  case 'reportingMethod':
138  $reportingMethod = $item->reportingMethod;
139  break;
140  case 'scoreConstraints':
141  $scoreConstraints = $item->scoreConstraints;
142  break;
143  case 'scoreMaximum':
144  $scoreMaximum = $item->scoreMaximum;
145  break;
146  case 'assignedActivity':
147  if (isset($item->assignedActivity->activityId)) {
148  $activityId = $item->assignedActivity->activityId;
149  }
150  break;
151  case 'resourceId':
152  $activityId = $item->resourceId;
153  break;
154  case 'tag':
155  $tag = $item->tag;
156  break;
157  }
158  }
159  if (is_null($scoreMaximum) && $label && $reportingMethod && $scoreConstraints) {
160  foreach (get_object_vars($scoreConstraints) as $name => $value) {
161  $method = str_replace('Maximum', 'Score', $name);
162  if (substr($reportingMethod, -strlen($method)) === $method) {
163  $scoreMaximum = $value;
164  break;
165  }
166  }
167  }
168  if (!is_null($scoreMaximum)) {
169  $obj = new LineItem($label, $scoreMaximum, $activityId, $tag);
170  }
171 
172  return $obj;
173  }
174 }
toJsonldObject()
Generate the JSON-LD object representation of the line-item.
Definition: LineItem.php:78
toJsonObject()
Generate the JSON object representation of the line-item.
Definition: LineItem.php:101
int $scoreMaximum
Maximum score of line-item.
Definition: LineItem.php:42
Class to represent a line-item object.
Definition: LineItem.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: FileItem.php:19
string $tag
Tag of line-item.
Definition: LineItem.php:56
if($format !==null) $name
Definition: metadata.php:247
string $label
Label of line-item.
Definition: LineItem.php:35
static fromJsonObject(object $item)
Generate a LineItem object from its JSON or JSON-LD representation.
Definition: LineItem.php:122
__construct(string $label, int $scoreMaximum, string $resourceId=null, string $tag=null)
Class constructor.
Definition: LineItem.php:65
string $resourceId
Resource ID associated with line-item.
Definition: LineItem.php:49