ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilObjLTIConsumer.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
15{
16 const DB_TABLE_NAME = 'lti_consumer_settings';
17
25
29 protected $providerId = 0;
30
34 protected $provider = null;
35
36 const LAUNCH_METHOD_OWN_WIN = 'ownWin';
37 const LAUNCH_METHOD_NEW_WIN = 'newWin';
38 const LAUNCH_METHOD_EMBEDDED = 'embedded';
39
40 protected $use_xapi = false;
41 protected $custom_activity_id = '';
42 protected $statementsReportEnabled = false;
43
44 protected $mastery_score = 0.5;
45
50
54 protected $customLaunchKey = '';
58 protected $customLaunchSecret = '';
59
65 public function __construct(int $a_id = 0, bool $a_reference = true)
66 {
67 parent::__construct($a_id, $a_reference);
68 }
69
70 protected function initType()
71 {
72 $this->type = "lti";
73 }
74
78 public function isActivationLimited()
79 {
81 }
82
87 {
88 $this->activationLimited = $activationLimited;
89 }
90
94 public function getActivationStartingTime()
95 {
97 }
98
103 {
104 $this->activationStartingTime = $activationStartingTime;
105 }
106
110 public function getActivationEndingTime()
111 {
113 }
114
119 {
120 $this->activationEndingTime = $activationEndingTime;
121 }
122
126 public function getActivationVisibility()
127 {
129 }
130
135 {
136 $this->activationVisibility = $activationVisibility;
137 }
138
142 public function getMasteryScore() : float
143 {
145 }
146
150 public function setMasteryScore(float $mastery_score)
151 {
152 $this->mastery_score = $mastery_score;
153 }
154
158 public function getMasteryScorePercent() : float
159 {
160 return $this->mastery_score * 100;
161 }
162
166 public function setMasteryScorePercent(float $mastery_score_percent)
167 {
168 $this->mastery_score = $mastery_score_percent / 100;
169 }
170
174 public function getProviderId() : int
175 {
176 return $this->providerId;
177 }
178
182 public function setProviderId(int $providerId)
183 {
184 $this->providerId = $providerId;
185 }
186
187 public function initProvider()
188 {
189 $this->provider = new ilLTIConsumeProvider($this->getProviderId());
190 }
191
196 {
197 return $this->provider;
198 }
199
204 {
205 $this->provider = $provider;
206 }
207
211 public function isLaunchMethodOwnWin() : bool
212 {
213 return $this->launchMethod == self::LAUNCH_METHOD_OWN_WIN;
214 }
215
219 public function isLaunchMethodEmbedded() : bool
220 {
221 return $this->launchMethod == self::LAUNCH_METHOD_EMBEDDED;
222 }
223
227 public function getLaunchMethod() : string
228 {
229 return $this->launchMethod;
230 }
231
235 public function setLaunchMethod(string $launchMethod)
236 {
237 $this->launchMethod = $launchMethod;
238 }
239
243 public function getCustomLaunchKey() : string
244 {
246 }
247
251 public function setCustomLaunchKey(string $customLaunchKey)
252 {
253 $this->customLaunchKey = $customLaunchKey;
254 }
255
259 public function getCustomLaunchSecret() : string
260 {
262 }
263
268 {
269 $this->customLaunchSecret = $customLaunchSecret;
270 }
271
275 public function getLaunchKey()
276 {
277 if ($this->getProvider()->isProviderKeyCustomizable()) {
278 return $this->getCustomLaunchKey();
279 }
280
281 return $this->getProvider()->getProviderKey();
282 }
283
287 public function getLaunchSecret()
288 {
289 if ($this->getProvider()->isProviderKeyCustomizable()) {
290 return $this->getCustomLaunchSecret();
291 }
292
293 return $this->getProvider()->getProviderSecret();
294 }
295
299 public function getUseXapi() : bool
300 {
301 return $this->use_xapi;
302 }
303
307 public function setUseXapi(bool $use_xapi)
308 {
309 $this->use_xapi = $use_xapi;
310 }
311
315 public function getCustomActivityId() : string
316 {
318 }
319
324 {
325 $this->custom_activity_id = $custom_activity_id;
326 }
327
331 public function getActivityId() : string
332 {
333 if (strlen($this->getProvider()->getXapiActivityId())) {
334 return $this->getProvider()->getXapiActivityId();
335 }
336
338 }
339
344 {
346 }
347
352 {
353 $this->statementsReportEnabled = $statementsReportEnabled;
354 }
355
359 private function getCustomParams() : array
360 {
361 $paramsAsArray = [];
362
363 $params = $this->getProvider()->getCustomParams();
364 // allows foo=bar;foo2=baz2; foo3=baz3
365 $params = preg_split('/; ?/', $params);
366
367 foreach ($params as $param) {
368 $param = explode('=', $param);
369 // empty field, duplicate/leading/trailing semicolon?
370 if ($param[0] != '') {
371 $value = isset($param[1]) ? $param[1] : '';
372 $paramsAsArray[$param[0]] = $value;
373 }
374 }
375
376 return $paramsAsArray;
377 }
378
379
380 public function doRead()
381 {
382 $this->load();
383 }
384
385 public function load()
386 {
387 global $DIC; /* @var \ILIAS\DI\Container $DIC */
388
389 $query = "SELECT * FROM {$this->dbTableName()} WHERE obj_id = %s";
390 $res = $DIC->database()->queryF($query, ['integer'], [$this->getId()]);
391
392 while ($row = $DIC->database()->fetchAssoc($res)) {
393 // if ($row['provider_id']) { //always set
394 $this->setProviderId((int) $row['provider_id']);
395 $this->setProvider(new ilLTIConsumeProvider((int) $row['provider_id']));
396 // }
397
398 $this->setLaunchMethod($row['launch_method']);
399
400 $this->setCustomLaunchKey((string) $row['launch_key']);
401 $this->setCustomLaunchSecret((string) $row['launch_secret']);
402
403 $this->setUseXapi((bool) $row['use_xapi']);
404 $this->setCustomActivityId((string) $row['activity_id']);
405 $this->setStatementsReportEnabled((bool) $row['show_statements']);
406 $this->setHighscoreEnabled((bool) $row['highscore_enabled']);
407 $this->setHighscoreAchievedTS((bool) $row['highscore_achieved_ts']);
408 $this->setHighscorePercentage((bool) $row['highscore_percentage']);
409 $this->setHighscoreWTime((bool) $row['highscore_wtime']);
410 $this->setHighscoreOwnTable((bool) $row['highscore_own_table']);
411 $this->setHighscoreTopTable((bool) $row['highscore_top_table']);
412 $this->setHighscoreTopNum((int) $row['highscore_top_num']);
413
414 $this->setMasteryScore((float) $row['mastery_score']);
415 }
416
418 }
419
420 public function doUpdate()
421 {
422 $this->save();
423 }
424
425 public function save()
426 {
427 global $DIC; /* @var \ILIAS\DI\Container $DIC */
428
429 $DIC->database()->replace($this->dbTableName(), [
430 'obj_id' => ['integer', $this->getId()]
431 ], [
432 'provider_id' => ['integer', $this->getProviderId()],
433 'launch_method' => ['text', $this->getLaunchMethod()],
434 'launch_key' => ['text', $this->getCustomLaunchKey()],
435 'launch_secret' => ['text', $this->getCustomLaunchSecret()],
436 'use_xapi' => ['integer',$this->getUseXapi()],
437 'activity_id' => ['text',$this->getCustomActivityId()],
438 'show_statements' => ['integer',$this->isStatementsReportEnabled()],
439 'highscore_enabled' => ['integer', (int) $this->getHighscoreEnabled()],
440 'highscore_achieved_ts' => ['integer', (int) $this->getHighscoreAchievedTS()],
441 'highscore_percentage' => ['integer', (int) $this->getHighscorePercentage()],
442 'highscore_wtime' => ['integer', (int) $this->getHighscoreWTime()],
443 'highscore_own_table' => ['integer', (int) $this->getHighscoreOwnTable()],
444 'highscore_top_table' => ['integer', (int) $this->getHighscoreTopTable()],
445 'highscore_top_num' => ['integer', (int) $this->getHighscoreTopNum()],
446 'mastery_score' => ['float', (float) $this->getMasteryScore()]
447 ]);
448
450 }
451
453 {
454 if ($this->ref_id) {
455 include_once "./Services/Object/classes/class.ilObjectActivation.php";
456 $activation = ilObjectActivation::getItem($this->ref_id);
457 switch ($activation["timing_type"]) {
459 $this->setActivationLimited(true);
460 $this->setActivationStartingTime($activation["timing_start"]);
461 $this->setActivationEndingTime($activation["timing_end"]);
462 $this->setActivationVisibility($activation["visible"]);
463 break;
464
465 default:
466 $this->setActivationLimited(false);
467 break;
468 }
469 }
470 }
471
473 {
474 if ($this->ref_id) {
475 include_once "./Services/Object/classes/class.ilObjectActivation.php";
476 ilObjectActivation::getItem($this->ref_id);
477
478 $item = new ilObjectActivation;
479 if (!$this->isActivationLimited()) {
481 } else {
482 $item->setTimingType(ilObjectActivation::TIMINGS_ACTIVATION);
483 $item->setTimingStart($this->getActivationStartingTime());
484 $item->setTimingEnd($this->getActivationEndingTime());
485 $item->toggleVisible($this->getActivationVisibility());
486 }
487
488 $item->update($this->ref_id);
489 }
490 }
491
492 protected function dbTableName()
493 {
494 return self::DB_TABLE_NAME;
495 }
496
499
503 protected $_highscore_enabled = 0;
504
508 protected $anonymity = 0;
509
514
519
523 protected $_highscore_wtime = 1;
524
529
534
538 protected $_highscore_top_num = 10;
539
543
544
545
551 public function setHighscoreEnabled($a_enabled)
552 {
553 $this->_highscore_enabled = (bool) $a_enabled;
554 }
555
561 public function getHighscoreEnabled()
562 {
563 return (bool) $this->_highscore_enabled;
564 }
565
566
572 public function setHighscoreAchievedTS($a_achieved_ts)
573 {
574 $this->_highscore_achieved_ts = (bool) $a_achieved_ts;
575 }
576
582 public function getHighscoreAchievedTS()
583 {
584 return (bool) $this->_highscore_achieved_ts;
585 }
586
592 public function setHighscorePercentage($a_percentage)
593 {
594 $this->_highscore_percentage = (bool) $a_percentage;
595 }
596
602 public function getHighscorePercentage()
603 {
604 return (bool) $this->_highscore_percentage;
605 }
606
612 public function setHighscoreWTime($a_wtime)
613 {
614 $this->_highscore_wtime = (bool) $a_wtime;
615 }
616
622 public function getHighscoreWTime()
623 {
624 return (bool) $this->_highscore_wtime;
625 }
626
632 public function setHighscoreOwnTable($a_own_table)
633 {
634 $this->_highscore_own_table = (bool) $a_own_table;
635 }
636
642 public function getHighscoreOwnTable()
643 {
644 return (bool) $this->_highscore_own_table;
645 }
646
652 public function setHighscoreTopTable($a_top_table)
653 {
654 $this->_highscore_top_table = (bool) $a_top_table;
655 }
656
662 public function getHighscoreTopTable()
663 {
664 return (bool) $this->_highscore_top_table;
665 }
666
673 public function setHighscoreTopNum($a_top_num)
674 {
675 $this->_highscore_top_num = (int) $a_top_num;
676 }
677
686 public function getHighscoreTopNum($a_retval = 10)
687 {
688 $retval = $a_retval;
689 if ((int) $this->_highscore_top_num != 0) {
691 }
692
693 return $retval;
694 }
695
699 public function getHighscoreMode()
700 {
701 switch (true) {
702 case $this->getHighscoreOwnTable() && $this->getHighscoreTopTable():
704 break;
705
706 case $this->getHighscoreTopTable():
708 break;
709
710 case $this->getHighscoreOwnTable():
711 default:
713 break;
714 }
715 }
716
720 public function setHighscoreMode($mode)
721 {
722 switch ($mode) {
724 $this->setHighscoreTopTable(1);
725 $this->setHighscoreOwnTable(1);
726 break;
727
729 $this->setHighscoreTopTable(1);
730 $this->setHighscoreOwnTable(0);
731 break;
732
734 default:
735 $this->setHighscoreTopTable(0);
736 $this->setHighscoreOwnTable(1);
737 break;
738 }
739 }
740 /* End GET/SET for highscore feature*/
741
742 public function buildLaunchParameters(ilCmiXapiUser $cmixUser, $token, $contextType, $contextId, $contextTitle, $returnUrl = '')
743 {
744 global $DIC; /* @var \ILIAS\DI\Container $DIC */
745
746 $roles = $DIC->access()->checkAccess('write', '', $this->getRefId()) ? "Instructor" : "Learner";
747 if ($this->getProvider()->getAlwaysLearner() == true) {
748 $roles = "Learner";
749 }
750
751 $resource_link_id = $this->getRefId();
752 if ($this->getProvider()->getUseProviderId() == true) {
753 $resource_link_id = 'p' . $this->getProvider()->getId();
754 }
755
756 $usrImage = '';
757 if ($this->getProvider()->getIncludeUserPicture()) {
758 $usrImage = ILIAS_HTTP_PATH . "/" . $DIC->user()->getPersonalPicturePath("small");
759 }
760
761 $documentTarget = "window";
762 if ($this->getLaunchMethod() == self::LAUNCH_METHOD_EMBEDDED) {
763 $documentTarget = "iframe";
764 }
765
766 $nameGiven = '-';
767 $nameFamily = '-';
768 $nameFull = '-';
769 switch ($this->getProvider()->getPrivacyName()) {
771 $nameGiven = $DIC->user()->getFirstname();
772 $nameFull = $DIC->user()->getFirstname();
773 break;
775 $usrName = $DIC->user()->getUTitle() ? $DIC->user()->getUTitle() . ' ' : '';
776 $usrName .= $DIC->user()->getLastname();
777 $nameFamily = $usrName;
778 $nameFull = $usrName;
779 break;
781 $nameGiven = $DIC->user()->getFirstname();
782 $nameFamily = $DIC->user()->getLastname();
783 $nameFull = $DIC->user()->getFullname();
784 break;
785 }
786
787 $userIdLTI = ilCmiXapiUser::getIdentAsId($this->getProvider()->getPrivacyIdent(), $DIC->user());
788
789 $emailPrimary = $cmixUser->getUsrIdent();
790
791 ilLTIConsumerResult::getByKeys($this->getId(), $DIC->user()->getId(), true);
792
793 $custom_params = $this->getCustomParams();
794
795 $launch_vars = [
796 "lti_message_type" => "basic-lti-launch-request",
797 "lti_version" => "LTI-1p0",
798 "resource_link_id" => $resource_link_id,
799 "resource_link_title" => $this->getTitle(),
800 "resource_link_description" => $this->getDescription(),
801 "user_id" => $userIdLTI,
802 "user_image" => $usrImage,
803 "roles" => $roles,
804 "lis_person_name_given" => $nameGiven,
805 "lis_person_name_family" => $nameFamily,
806 "lis_person_name_full" => $nameFull,
807 "lis_person_contact_email_primary" => $emailPrimary,
808 "context_id" => $contextId,
809 "context_type" => $contextType,
810 "context_title" => $contextTitle,
811 "context_label" => $contextType . " " . $contextId,
812 "launch_presentation_locale" => $this->lng->getLangKey(),
813 "launch_presentation_document_target" => $documentTarget,
814 "launch_presentation_width" => "",//recommended
815 "launch_presentation_height" => "",//recommended
816 "launch_presentation_return_url" => $returnUrl,
817 "tool_consumer_instance_guid" => $value = CLIENT_ID . "." . implode(".", array_reverse(explode("/", parse_url(ILIAS_HTTP_PATH)["path"]))) . parse_url(ILIAS_HTTP_PATH)["host"],
818 "tool_consumer_instance_name" => $DIC->settings()->get("short_inst_name") ? $DIC->settings()->get("short_inst_name") : CLIENT_ID,
819 "tool_consumer_instance_description" => ilObjSystemFolder::_getHeaderTitle(),
820 "tool_consumer_instance_url" => ilLink::_getLink(ROOT_FOLDER_ID, "root"),//ToDo? "https://vb52p70.example.com/release_5-3/goto.php?target=root_1&client_id=inno",
821 "tool_consumer_instance_contact_email" => $DIC->settings()->get("admin_email"),
822 "launch_presentation_css_url" => "",
823 "tool_consumer_info_product_family_code" => "ilias",
824 "tool_consumer_info_version" => $DIC->settings()->get("ilias_version"),
825 "lis_result_sourcedid" => $token,
826 "lis_outcome_service_url" => ILIAS_HTTP_PATH . "/Modules/LTIConsumer/result.php?client_id=" . CLIENT_ID,
827 "role_scope_mentor" => ""
828 ];
829
830 $OAuthParams = [
831 "url" => $this->getProvider()->getProviderUrl(),
832 "key" => $this->getLaunchKey(),
833 "secret" => $this->getLaunchSecret(),
834 "callback" => "about:blank",
835 "http_method" => "POST",
836 "sign_method" => "HMAC_SHA1",
837 "token" => "",
838 "data" => ($launch_vars + $custom_params)
839 ];
840
841 $launchParameters = ilLTIConsumerLaunch::signOAuth($OAuthParams);
842
843 return $launchParameters;
844 }
845}
An exception for terminatinating execution or to throw for unit testing.
static getIdentAsId($userIdentMode, ilObjUser $user)
static signOAuth($a_params)
sign request data with OAuth
static getByKeys($a_obj_id, $a_usr_id, $a_create=false)
Get a result by object and user key.
setActivationLimited($activationLimited)
setCustomLaunchSecret(string $customLaunchSecret)
setHighscorePercentage($a_percentage)
Sets if the percentages of the scores pass should be shown.
setActivationVisibility($activationVisibility)
setHighscoreTopTable($a_top_table)
Sets if the top-rankings table should be shown.
getHighscoreEnabled()
Gets the setting which determines if the highscore feature is enabled.
buildLaunchParameters(ilCmiXapiUser $cmixUser, $token, $contextType, $contextId, $contextTitle, $returnUrl='')
$activationLimited
repository object activation settings (handled by ilObject)
getHighscoreTopTable()
Gets, if the top-rankings table should be shown.
setHighscoreTopNum($a_top_num)
Sets the number of entries which are to be shown in the top-rankings table.
setProvider(ilLTIConsumeProvider $provider)
setHighscoreEnabled($a_enabled)
Sets if the highscore feature should be enabled.
setActivationStartingTime($activationStartingTime)
setHighscoreAchievedTS($a_achieved_ts)
Sets if the date and time of the scores achievement should be displayed.
setActivationEndingTime($activationEndingTime)
setHighscoreWTime($a_wtime)
Sets if the workingtime of the scores should be shown.
setLaunchMethod(string $launchMethod)
getHighscoreOwnTable()
Gets if the own rankings table should be shown.
getHighscorePercentage()
Gets if the percentage column should be shown.
setStatementsReportEnabled($statementsReportEnabled)
setHighscoreOwnTable($a_own_table)
Sets if the table with the own ranking should be shown.
setMasteryScorePercent(float $mastery_score_percent)
getHighscoreTopNum($a_retval=10)
Gets the number of entries which are to be shown in the top-rankings table.
setCustomLaunchKey(string $customLaunchKey)
setProviderId(int $providerId)
getHighscoreAchievedTS()
Returns if date and time of the scores achievement should be displayed.
setMasteryScore(float $mastery_score)
getHighscoreWTime()
Gets if the column with the workingtime should be shown.
__construct(int $a_id=0, bool $a_reference=true)
ilObjLTIConsumer constructor.
setCustomActivityId(string $custom_activity_id)
setUseXapi(bool $use_xapi)
Class ilObject2 This is an intermediate progress of ilObject class.
getRefId()
get reference id @access public
getDescription()
get object description
getTitle()
get object title @access public
Class ilObjectActivation.
setTimingType($a_type)
Set timing type.
static getItem($a_ref_id)
Get item data.
getId()
get object id @access public
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$query
foreach($_POST as $key=> $value) $res
$token
Definition: xapitoken.php:57
$DIC
Definition: xapitoken.php:46
$param
Definition: xapitoken.php:31