ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilLTIAppEventListener Class Reference

Class ilLTIAppEventListener. More...

+ Inheritance diagram for ilLTIAppEventListener:
+ Collaboration diagram for ilLTIAppEventListener:

Static Public Member Functions

static handleEvent (string $a_component, string $a_event, array $a_parameter)
 Handle an event in a listener.
Parameters
string$a_componentcomponent, e.g. "components/ILIAS/Forum" or "components/ILIAS/User"
string$a_eventevent e.g. "createUser", "updateUser", "deleteUser", ...
array<string,mixed>$a_parameter parameter array (assoc), array("name" => ..., "phone_office" => ...)
More...
 
static handleCronUpdate (ilDateTime $since)
 
static handleOutcomeWithoutLP (int $a_obj_id, int $a_usr_id, ?float $a_percentage)
 
static handleEvent (string $a_component, string $a_event, array $a_parameter)
 Handle an event in a listener. More...
 

Protected Member Functions

 __construct ()
 ilLTIAppEventListener constructor. More...
 
 handleUpdateStatus (int $a_obj_id, int $a_usr_id, int $a_status, int $a_percentage)
 Handle update status. More...
 
 doCronUpdate (ilDateTime $since)
 
 definePercentageByObjectId (int|null $status, string $obj_id, int|null $percentage)
 
 isLTIAuthMode (string $auth_mode)
 
 tryOutcomeService (int $resource, string $ext_account, int $a_status, int $a_percentage)
 try outcome service More...
 

Static Protected Member Functions

static getInstance ()
 

Private Attributes

ilLogger $logger = null
 
ilLTIDataConnector $connector = null
 

Static Private Attributes

static ilLTIAppEventListener $instance = null
 

Detailed Description

Class ilLTIAppEventListener.

Definition at line 29 of file class.ilLTIAppEventListener.php.

Constructor & Destructor Documentation

◆ __construct()

ilLTIAppEventListener::__construct ( )
protected

ilLTIAppEventListener constructor.

Definition at line 41 of file class.ilLTIAppEventListener.php.

42 {
43 global $DIC;
44
45 $this->logger = ilLoggerFactory::getLogger('ltis');
46 $this->connector = new ilLTIDataConnector();
47 }
static getLogger(string $a_component_id)
Get component logger.
global $DIC
Definition: shib_login.php:26

References $DIC, ilLoggerFactory\getLogger(), and ILIAS\Repository\logger().

+ Here is the call graph for this function:

Member Function Documentation

◆ definePercentageByObjectId()

ilLTIAppEventListener::definePercentageByObjectId ( int|null  $status,
string  $obj_id,
int|null  $percentage 
)
protected
Exceptions
ilObjectNotFoundException
ilDatabaseException

Definition at line 133 of file class.ilLTIAppEventListener.php.

133 : int
134 {
135 global $DIC;
136 $logger = $DIC->logger()->root();
137 $logger->debug('definePercentageByObjectId');
138 $indentifier = ilObjectFactory::getInstanceByRefId((int) $obj_id)->getType();
139 $logger->info('Object type: ' . $indentifier . " for object id: " . $obj_id);
140 if (in_array($indentifier, ['crs', 'grp'])) {
142 $percentage = 100;
143 }
144 }
145 return $percentage;
146 }
const LP_STATUS_COMPLETED_NUM
const LP_STATUS_FAILED_NUM
info(string $message, array $context=[])
debug(string $message, array $context=[])
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id

References $DIC, $logger, ilLogger\debug(), ilObjectFactory\getInstanceByRefId(), ilLogger\info(), ilLPStatus\LP_STATUS_COMPLETED_NUM, and ilLPStatus\LP_STATUS_FAILED_NUM.

Referenced by doCronUpdate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doCronUpdate()

ilLTIAppEventListener::doCronUpdate ( ilDateTime  $since)
protected
Parameters
ilDateTime$since
Exceptions
ilDateTimeException

Definition at line 96 of file class.ilLTIAppEventListener.php.

96 : void
97 {
98 $this->logger->info('Starting cron update for lti outcome service');
99
100 $resources = $this->connector->lookupResourcesForAllUsersSinceDate($since);
101 foreach ($resources as $consumer_ext_account => $user_resources) {
102 list($consumer, $ext_account) = explode('__', $consumer_ext_account, 2);
103
104 $login = ilObjUser::_checkExternalAuthAccount('lti_' . $consumer, $ext_account);
105 if (!$login) {
106 $this->logger->info('No user found for lti_' . $consumer . ' -> ' . $ext_account);
107 continue;
108 }
109 $usr_id = ilObjUser::_lookupId($login);
110 foreach ($user_resources as $resource_info) {
111 list($resource_id, $resource_ref_id) = explode('__', $resource_info);
112 $this->logger->info('Found resource: ' . $resource_info . " for user: " . $usr_id . " resource_id: " . $resource_id . " resource_ref_id: " . $resource_ref_id);
113
114 // lookup lp status
116 ilObject::_lookupObjId((int) $resource_ref_id),
117 $usr_id
118 );
119 $percentage = ilLPStatus::_lookupPercentage(
120 ilObject::_lookupObjId((int) $resource_ref_id),
121 $usr_id
122 );
123 $percentage = $this->definePercentageByObjectId($status, $resource_ref_id, $percentage);
124 $this->tryOutcomeService((int) $resource_id, $ext_account, $status, $percentage);
125 }
126 }
127 }
static _lookupPercentage(int $a_obj_id, int $a_user_id)
Lookup percentage.
static _lookupStatus(int $a_obj_id, int $a_user_id, bool $a_create=true)
Lookup status.
definePercentageByObjectId(int|null $status, string $obj_id, int|null $percentage)
tryOutcomeService(int $resource, string $ext_account, int $a_status, int $a_percentage)
try outcome service
static _lookupId(string|array $a_user_str)
static _checkExternalAuthAccount(string $a_auth, string $a_account, bool $tryFallback=true)
check whether external account and authentication method matches with a user
static _lookupObjId(int $ref_id)
$resources
Definition: ltiservices.php:68

References $resources, ilObjUser\_checkExternalAuthAccount(), ilObjUser\_lookupId(), ilObject\_lookupObjId(), ilLPStatus\_lookupPercentage(), ilLPStatus\_lookupStatus(), definePercentageByObjectId(), ILIAS\Repository\logger(), and tryOutcomeService().

+ Here is the call graph for this function:

◆ getInstance()

static ilLTIAppEventListener::getInstance ( )
staticprotected

Definition at line 49 of file class.ilLTIAppEventListener.php.

50 {
51 if (!self::$instance instanceof \ilLTIAppEventListener) {
52 self::$instance = new self();
53 }
54 return self::$instance;
55 }
Class ilLTIAppEventListener.
static ilLTIAppEventListener $instance

References $instance.

Referenced by handleCronUpdate(), and handleEvent().

+ Here is the caller graph for this function:

◆ handleCronUpdate()

static ilLTIAppEventListener::handleCronUpdate ( ilDateTime  $since)
static
Parameters
ilDateTime$since
Returns
bool
Exceptions
ilDateTimeException

Definition at line 220 of file class.ilLTIAppEventListener.php.

220 : bool
221 {
222 $listener = self::getInstance();
223 $listener->doCronUpdate($since);
224 return true;
225 }

References getInstance().

Referenced by ilLTICronOutcomeService\run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleEvent()

static ilLTIAppEventListener::handleEvent ( string  $a_component,
string  $a_event,
array  $a_parameter 
)
static

Handle an event in a listener.

Parameters
string$a_componentcomponent, e.g. "components/ILIAS/Forum" or "components/ILIAS/User"
string$a_eventevent e.g. "createUser", "updateUser", "deleteUser", ...
array<string,mixed>$a_parameter parameter array (assoc), array("name" => ..., "phone_office" => ...)

Implements ilAppEventListener.

Definition at line 197 of file class.ilLTIAppEventListener.php.

197 : void
198 {
200 $logger->info('Handling event: ' . $a_event . ' from ' . $a_component);
201
202 if ($a_component == 'components/ILIAS/Tracking') {
203 if ($a_event == 'updateStatus') {
204 $listener = self::getInstance();
205 $listener->handleUpdateStatus(
206 $a_parameter['obj_id'],
207 $a_parameter['usr_id'],
208 $a_parameter['status'],
209 $a_parameter['percentage']
210 );
211 }
212 }
213 }

References $logger, getInstance(), ilLoggerFactory\getLogger(), and ilLogger\info().

+ Here is the call graph for this function:

◆ handleOutcomeWithoutLP()

static ilLTIAppEventListener::handleOutcomeWithoutLP ( int  $a_obj_id,
int  $a_usr_id,
?float  $a_percentage 
)
static

Definition at line 228 of file class.ilLTIAppEventListener.php.

228 : void
229 {
230 global $DIC;
231 $score = 0;
233
234 $auth_mode = ilObjUser::_lookupAuthMode($a_usr_id);
235 if (strpos($auth_mode, 'lti_') === false) {
236 $logger->debug('Ignoring outcome for non-LTI-user.');
237 return;
238 }
239 //check if LearningPress enabled
240 $olp = ilObjectLP::getInstance($a_obj_id);
241 if (ilLPObjSettings::LP_MODE_DEACTIVATED != $olp->getCurrentMode()) {
242 $logger->debug('Ignoring outcome if LP is activated.');
243 return;
244 }
245
246 if ($a_percentage && $a_percentage > 0) {
247 $score = round($a_percentage / 100, 4);
248 }
249
251 $ext_account = ilObjUser::_lookupExternalAccount($a_usr_id);
252 list($lti, $consumer) = explode('_', $auth_mode);
253
254 // iterate through all references
255 $refs = ilObject::_getAllReferences($a_obj_id);
256 foreach ((array) $refs as $ref_id) {
258 $ref_id,
259 $ext_account,
260 (int) $consumer
261 );
262
263 $logger->debug('Resources for update: ' . dump($resources));
264
265 foreach ($resources as $resource) {
266 // $this->tryOutcomeService($resource, $ext_account, $a_status, $a_percentage);
267 $resource_link = ResourceLink::fromRecordId($resource, $connector);
268 if ($resource_link->hasOutcomesService()) {
269 $user = UserResult::fromResourceLink($resource_link, $ext_account);
270 $logger->debug('Sending score: ' . (string) $score);
271 $outcome = new Outcome((string) $score);
272
273 $resource_link->doOutcomesService(
274 ServiceAction::Write,
275 $outcome,
276 $user
277 );
278 }
279 }
280 }
281 }
lookupResourcesForUserObjectRelation(int $a_ref_id, string $a_lti_user, int $a_ext_consumer, ?ilDateTime $since=null)
Lookup resources for user object relation.
static _lookupExternalAccount(int $a_user_id)
static _lookupAuthMode(int $a_usr_id)
static getInstance(int $obj_id)
static _getAllReferences(int $id)
get all reference ids for object ID
$ref_id
Definition: ltiauth.php:66

References $connector, $DIC, $logger, $ref_id, $resources, ilObject\_getAllReferences(), ilObjUser\_lookupAuthMode(), ilObjUser\_lookupExternalAccount(), ilLogger\debug(), ilObjectLP\getInstance(), ilLoggerFactory\getLogger(), ilLTIDataConnector\lookupResourcesForUserObjectRelation(), and ilLPObjSettings\LP_MODE_DEACTIVATED.

Referenced by ilObjSCORMTracking\storeJsApiCmi().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleUpdateStatus()

ilLTIAppEventListener::handleUpdateStatus ( int  $a_obj_id,
int  $a_usr_id,
int  $a_status,
int  $a_percentage 
)
protected

Handle update status.

Definition at line 61 of file class.ilLTIAppEventListener.php.

61 : void
62 {
63 $this->logger->debug('Handle update status');
64 $auth_mode = ilObjUser::_lookupAuthMode($a_usr_id);
65 if (!$this->isLTIAuthMode($auth_mode)) {
66 $this->logger->debug('Ignoring update for non-LTI-user.');
67 return;
68 }
69 $ext_account = ilObjUser::_lookupExternalAccount($a_usr_id);
70 list($lti, $consumer) = explode('_', $auth_mode);
71
72 // iterate through all references
73 $refs = ilObject::_getAllReferences($a_obj_id);
74 $this->logger->debug('Refs for : ' . $a_obj_id . ': ' . count($refs));
75 foreach ((array) $refs as $ref_id) {
76 $resources = $this->connector->lookupResourcesForUserObjectRelation(
77 $ref_id,
78 $ext_account,
79 (int) $consumer
80 );
81
82 $this->logger->debug('Resources for update:');
83 $this->logger->dump($resources, ilLogLevel::DEBUG);
84
85 foreach ($resources as $resource) {
86 $this->tryOutcomeService((int) $resource, $ext_account, $a_status, $a_percentage);
87 }
88 }
89 }

References $ref_id, $resources, ilObject\_getAllReferences(), ilObjUser\_lookupAuthMode(), ilObjUser\_lookupExternalAccount(), ilLogLevel\DEBUG, isLTIAuthMode(), ILIAS\Repository\logger(), and tryOutcomeService().

+ Here is the call graph for this function:

◆ isLTIAuthMode()

ilLTIAppEventListener::isLTIAuthMode ( string  $auth_mode)
protected

Definition at line 148 of file class.ilLTIAppEventListener.php.

148 : bool
149 {
150 return strpos($auth_mode, 'lti_') === 0;
151 }

Referenced by handleUpdateStatus().

+ Here is the caller graph for this function:

◆ tryOutcomeService()

ilLTIAppEventListener::tryOutcomeService ( int  $resource,
string  $ext_account,
int  $a_status,
int  $a_percentage 
)
protected

try outcome service

Definition at line 157 of file class.ilLTIAppEventListener.php.

157 : void
158 {
159 $resource_link = ResourceLink::fromRecordId($resource, $this->connector);
160 if (!$resource_link->hasOutcomesService()) {
161 $this->logger->info('No outcome service available for resource id: ' . $resource);
162 return;
163 }
164 $this->logger->info('Trying outcome service with status ' . $a_status . ' and percentage ' . $a_percentage);
165 $user = UserResult::fromResourceLink($resource_link, $ext_account);
166
167 if (!$a_percentage && $a_status != ilLPStatus::LP_STATUS_NOT_ATTEMPTED_NUM) {
168 $score = 0;
169 } else {
171 $score = $a_percentage / 100;
172 } elseif (
174 ) {
175 $score = null;
176 } else {
177 $score = 0;
178 }
179 }
180
181 $this->logger->info('Sending score: ' . (string) $score);
182
183 $outcome = new Outcome((string) $score);
184
185 $status = $resource_link->doOutcomesService(
186 ServiceAction::Write,
187 $outcome,
188 $user
189 );
190 $this->logger->info('Outcome service request status: ' . $status);
191 }
const LP_STATUS_NOT_ATTEMPTED_NUM

References ILIAS\Repository\logger(), ilLPStatus\LP_STATUS_COMPLETED_NUM, ilLPStatus\LP_STATUS_FAILED_NUM, and ilLPStatus\LP_STATUS_NOT_ATTEMPTED_NUM.

Referenced by doCronUpdate(), and handleUpdateStatus().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $connector

ilLTIDataConnector ilLTIAppEventListener::$connector = null
private

Definition at line 35 of file class.ilLTIAppEventListener.php.

Referenced by handleOutcomeWithoutLP().

◆ $instance

ilLTIAppEventListener ilLTIAppEventListener::$instance = null
staticprivate

Definition at line 31 of file class.ilLTIAppEventListener.php.

Referenced by getInstance().

◆ $logger

ilLogger ilLTIAppEventListener::$logger = null
private

The documentation for this class was generated from the following file: