/** * Class constructor. * * @param ToolProvider $toolProvider Tool Provider instance * @param string $secret Shared secret */ function __construct($toolProvider, $secret) { $tcContexts = array(); foreach ($toolProvider->consumer->profile->{'@context'} as $context) { if (is_object($context)) { $tcContexts = array_merge(get_object_vars($context), $tcContexts); } } $this->shared_secret = $secret; $toolServices = array(); foreach ($toolProvider->requiredServices as $requiredService) { foreach ($requiredService->formats as $format) { $service = $toolProvider->findService($format, $requiredService->actions); if ($service !== false && !array_key_exists($service->{'@id'}, $toolServices)) { $id = $service->{'@id'}; $parts = explode(':', $id, 2); if (count($parts) > 1) { if (array_key_exists($parts[0], $tcContexts)) { $id = "{$tcContexts[$parts[0]]}{$parts[1]}"; } } $toolService = new \stdClass(); $toolService->{'@type'} = 'RestServiceProfile'; $toolService->service = $id; $toolService->action = $requiredService->actions; $toolServices[$service->{'@id'}] = $toolService; } } } foreach ($toolProvider->optionalServices as $optionalService) { foreach ($optionalService->formats as $format) { $service = $toolProvider->findService($format, $optionalService->actions); if ($service !== false && !array_key_exists($service->{'@id'}, $toolServices)) { $id = $service->{'@id'}; $parts = explode(':', $id, 2); if (count($parts) > 1) { if (array_key_exists($parts[0], $tcContexts)) { $id = "{$tcContexts[$parts[0]]}{$parts[1]}"; } } $toolService = new \stdClass(); $toolService->{'@type'} = 'RestServiceProfile'; $toolService->service = $id; $toolService->action = $optionalService->actions; $toolServices[$service->{'@id'}] = $toolService; } } } $this->tool_service = array_values($toolServices); }
/** * Create a new instance of tool_provider to handle all the LTI tool provider interactions. * * @param int $toolid The id of the tool to be provided. */ public function __construct($toolid) { global $CFG, $SITE; $token = helper::generate_proxy_token($toolid); $tool = helper::get_lti_tool($toolid); $this->tool = $tool; $dataconnector = new data_connector(); parent::__construct($dataconnector); // Override debugMode and set to the configured value. $this->debugMode = $CFG->debugdeveloper; $this->baseUrl = $CFG->wwwroot; $toolpath = helper::get_launch_url($toolid); $toolpath = $this->strip_base_url($toolpath); $vendorid = $SITE->shortname; $vendorname = $SITE->fullname; $vendordescription = trim(html_to_text($SITE->summary)); $this->vendor = new Item($vendorid, $vendorname, $vendordescription, $CFG->wwwroot); $name = helper::get_name($tool); $description = helper::get_description($tool); $icon = helper::get_icon($tool)->out(); $icon = $this->strip_base_url($icon); $this->product = new Item($token, $name, $description, helper::get_proxy_url($tool), '1.0'); $requiredmessages = [new Message('basic-lti-launch-request', $toolpath, ['Context.id', 'CourseSection.title', 'CourseSection.label', 'CourseSection.sourcedId', 'CourseSection.longDescription', 'CourseSection.timeFrame.begin', 'ResourceLink.id', 'ResourceLink.title', 'ResourceLink.description', 'User.id', 'User.username', 'Person.name.full', 'Person.name.given', 'Person.name.family', 'Person.email.primary', 'Person.sourcedId', 'Person.name.middle', 'Person.address.street1', 'Person.address.locality', 'Person.address.country', 'Person.address.timezone', 'Person.phone.primary', 'Person.phone.mobile', 'Person.webaddress', 'Membership.role', 'Result.sourcedId', 'Result.autocreate'])]; $optionalmessages = []; $this->resourceHandlers[] = new ResourceHandler(new Item($token, helper::get_name($tool), $description), $icon, $requiredmessages, $optionalmessages); $this->requiredServices[] = new ServiceDefinition(['application/vnd.ims.lti.v2.toolproxy+json'], ['POST']); $this->requiredServices[] = new ServiceDefinition(['application/vnd.ims.lis.v2.membershipcontainer+json'], ['GET']); }
/** * Perform a Memberships service request. * * The user table is updated with the new list of user objects. * * @param boolean $withGroups True is group information is to be requested as well * * @return mixed Array of User objects or False if the request was not successful */ public function doMembershipsService($withGroups = false) { $users = array(); $oldUsers = $this->getUserResultSourcedIDs(true, ToolProvider::ID_SCOPE_RESOURCE); $this->extResponse = null; $url = $this->getSetting('ext_ims_lis_memberships_url'); $params = array(); $params['id'] = $this->getSetting('ext_ims_lis_memberships_id'); $ok = false; if ($withGroups) { $ok = $this->doService('basic-lis-readmembershipsforcontextwithgroups', $url, $params); } if ($ok) { $this->groupSets = array(); $this->groups = array(); } else { $ok = $this->doService('basic-lis-readmembershipsforcontext', $url, $params); } if ($ok) { if (!isset($this->extNodes['memberships']['member'])) { $members = array(); } else { if (!isset($this->extNodes['memberships']['member'][0])) { $members = array(); $members[0] = $this->extNodes['memberships']['member']; } else { $members = $this->extNodes['memberships']['member']; } } for ($i = 0; $i < count($members); $i++) { $user = User::fromResourceLink($this, $members[$i]['user_id']); // Set the user name $firstname = isset($members[$i]['person_name_given']) ? $members[$i]['person_name_given'] : ''; $lastname = isset($members[$i]['person_name_family']) ? $members[$i]['person_name_family'] : ''; $fullname = isset($members[$i]['person_name_full']) ? $members[$i]['person_name_full'] : ''; $user->setNames($firstname, $lastname, $fullname); // Set the user email $email = isset($members[$i]['person_contact_email_primary']) ? $members[$i]['person_contact_email_primary'] : ''; $user->setEmail($email, $this->getConsumer()->defaultEmail); /// Set the user roles if (isset($members[$i]['roles'])) { $user->roles = ToolProvider::parseRoles($members[$i]['roles']); } // Set the user groups if (!isset($members[$i]['groups']['group'])) { $groups = array(); } else { if (!isset($members[$i]['groups']['group'][0])) { $groups = array(); $groups[0] = $members[$i]['groups']['group']; } else { $groups = $members[$i]['groups']['group']; } } for ($j = 0; $j < count($groups); $j++) { $group = $groups[$j]; if (isset($group['set'])) { $set_id = $group['set']['id']; if (!isset($this->groupSets[$set_id])) { $this->groupSets[$set_id] = array('title' => $group['set']['title'], 'groups' => array(), 'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0); } $this->groupSets[$set_id]['num_members']++; if ($user->isStaff()) { $this->groupSets[$set_id]['num_staff']++; } if ($user->isLearner()) { $this->groupSets[$set_id]['num_learners']++; } if (!in_array($group['id'], $this->groupSets[$set_id]['groups'])) { $this->groupSets[$set_id]['groups'][] = $group['id']; } $this->groups[$group['id']] = array('title' => $group['title'], 'set' => $set_id); } else { $this->groups[$group['id']] = array('title' => $group['title']); } $user->groups[] = $group['id']; } // If a result sourcedid is provided save the user if (isset($members[$i]['lis_result_sourcedid'])) { $user->ltiResultSourcedId = $members[$i]['lis_result_sourcedid']; $user->save(); } $users[] = $user; // Remove old user (if it exists) unset($oldUsers[$user->getId(ToolProvider::ID_SCOPE_RESOURCE)]); } // Delete any old users which were not in the latest list from the tool consumer foreach ($oldUsers as $id => $user) { $user->delete(); } } else { $users = false; } return $users; }
/** * Get the memberships. * * @param string $role Role for which memberships are to be requested (optional, default is all roles) * @param int $limit Limit on the number of memberships to be returned (optional, default is all) * * @return mixed The array of User objects if successful, otherwise false */ public function get($role = null, $limit = 0) { $isLink = is_a($this->source, 'IMSGlobal\\LTI\\ToolProvider\\ResourceLink'); $parameters = array(); if (!empty($role)) { $parameters['role'] = $role; } if ($limit > 0) { $parameters['limit'] = strval($limit); } if ($isLink) { $parameters['rlid'] = $this->source->getId(); } $http = $this->send('GET', $parameters); if (!$http->ok) { $users = false; } else { $users = array(); if ($isLink) { $oldUsers = $this->source->getUserResultSourcedIDs(true, ToolProvider\ToolProvider::ID_SCOPE_RESOURCE); } foreach ($http->responseJson->pageOf->membershipSubject->membership as $membership) { $member = $membership->member; if ($isLink) { $user = ToolProvider\User::fromResourceLink($this->source, $member->userId); } else { $user = new ToolProvider\User(); $user->ltiUserId = $member->userId; } // Set the user name $firstname = isset($member->givenName) ? $member->givenName : ''; $lastname = isset($member->familyName) ? $member->familyName : ''; $fullname = isset($member->name) ? $member->name : ''; $user->setNames($firstname, $lastname, $fullname); // Set the user email $email = isset($member->email) ? $member->email : ''; $user->setEmail($email, $this->source->getConsumer()->defaultEmail); // Set the user roles if (isset($membership->role)) { $user->roles = ToolProvider\ToolProvider::parseRoles($membership->role); } // If a result sourcedid is provided save the user if ($isLink) { if (isset($member->message)) { foreach ($member->message as $message) { if (isset($message->message_type) && $message->message_type === 'basic-lti-launch-request') { if (isset($message->lis_result_sourcedid)) { $user->ltiResultSourcedId = $message->lis_result_sourcedid; $user->save(); } break; } } } } $users[] = $user; // Remove old user (if it exists) if ($isLink) { unset($oldUsers[$user->getId(ToolProvider\ToolProvider::ID_SCOPE_RESOURCE)]); } } // Delete any old users which were not in the latest list from the tool consumer if ($isLink) { foreach ($oldUsers as $id => $user) { $user->delete(); } } } return $users; }