/** * Get the user ID (which may be a compound of the tool consumer and resource link IDs). * * @param int $idScope Scope to use for user ID (optional, default is null for consumer default setting) * * @return string User ID value */ public function getId($idScope = null) { if (empty($idScope)) { if (!is_null($this->resourceLink)) { $idScope = $this->resourceLink->getConsumer()->idScope; } else { $idScope = ToolProvider::ID_SCOPE_ID_ONLY; } } switch ($idScope) { case ToolProvider::ID_SCOPE_GLOBAL: $id = $this->getResourceLink()->getKey() . ToolProvider::ID_SCOPE_SEPARATOR . $this->ltiUserId; break; case ToolProvider::ID_SCOPE_CONTEXT: $id = $this->getResourceLink()->getKey(); if ($this->resourceLink->ltiContextId) { $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->resourceLink->ltiContextId; } $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->ltiUserId; break; case ToolProvider::ID_SCOPE_RESOURCE: $id = $this->getResourceLink()->getKey(); if ($this->resourceLink->ltiResourceLinkId) { $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->resourceLink->ltiResourceLinkId; } $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->ltiUserId; break; default: $id = $this->ltiUserId; break; } return $id; }
/** * Class constructor. * * @param ResourceLink $resourceLink Resource_Link object * @param string $id Value of share key (optional, default is null) */ public function __construct($resourceLink, $id = null) { $this->initialize(); $this->dataConnector = $resourceLink->getDataConnector(); $this->resourceLinkId = $resourceLink->getRecordId(); $this->id = $id; if (!empty($id)) { $this->load(); } }
/** * Test for enrol_lti_plugin::delete_instance(). */ public function test_delete_instance() { global $DB; // Create tool enrolment instance. $data = new stdClass(); $data->enrolstartdate = time(); $data->secret = 'secret'; $tool = $this->getDataGenerator()->create_lti_tool($data); // Create consumer and related data. $dataconnector = new data_connector(); $consumer = new ToolConsumer('testkey', $dataconnector); $consumer->secret = $tool->secret; $consumer->ltiVersion = ToolProvider::LTI_VERSION1; $consumer->name = 'TEST CONSUMER NAME'; $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME'; $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID'; $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION'; $consumer->enabled = true; $consumer->protected = true; $consumer->save(); $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid'); $resourcelink->save(); $ltiuser = User::fromResourceLink($resourcelink, ''); $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId'; $ltiuser->ltiUserId = 'testuserid'; $ltiuser->email = '*****@*****.**'; $ltiuser->save(); $tp = new tool_provider($tool->id); $tp->user = $ltiuser; $tp->resourceLink = $resourcelink; $tp->consumer = $consumer; $tp->map_tool_to_consumer(); $mappingparams = ['toolid' => $tool->id, 'consumerid' => $tp->consumer->getRecordId()]; // Check first that the related records exist. $this->assertTrue($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams)); $this->assertTrue($DB->record_exists('enrol_lti_lti2_consumer', ['id' => $consumer->getRecordId()])); $this->assertTrue($DB->record_exists('enrol_lti_lti2_resource_link', ['id' => $resourcelink->getRecordId()])); $this->assertTrue($DB->record_exists('enrol_lti_lti2_user_result', ['id' => $ltiuser->getRecordId()])); // Perform deletion. $enrollti = new enrol_lti_plugin(); $instance = $DB->get_record('enrol', ['id' => $tool->enrolid]); $enrollti->delete_instance($instance); // Check that the related records have been deleted. $this->assertFalse($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams)); $this->assertFalse($DB->record_exists('enrol_lti_lti2_consumer', ['id' => $consumer->getRecordId()])); $this->assertFalse($DB->record_exists('enrol_lti_lti2_resource_link', ['id' => $resourcelink->getRecordId()])); $this->assertFalse($DB->record_exists('enrol_lti_lti2_user_result', ['id' => $ltiuser->getRecordId()])); // Check that the enrolled users and the tool instance has been deleted. $this->assertFalse($DB->record_exists('enrol_lti_users', ['toolid' => $tool->id])); $this->assertFalse($DB->record_exists('enrol_lti_tools', ['id' => $tool->id])); $this->assertFalse($DB->record_exists('enrol', ['id' => $instance->id])); }
public function setUp() { $this->resetAfterTest(); // Set this user as the admin. $this->setAdminUser(); $this->task = new dummy_sync_members_task(); $generator = $this->getDataGenerator(); $course = $generator->create_course(); $tooldata = ['courseid' => $course->id, 'membersyncmode' => helper::MEMBER_SYNC_ENROL_AND_UNENROL, 'membersync' => 1]; $tool = $generator->create_lti_tool((object) $tooldata); $this->tool = helper::get_lti_tool($tool->id); $dataconnector = $this->task->get_dataconnector(); $this->consumer = new ToolConsumer('Consumer1Key', $dataconnector); $this->consumer->name = 'Consumer1'; $this->consumer->secret = 'Consumer1Secret'; $this->consumer->save(); $toolprovider = new tool_provider($this->tool->id); $toolprovider->consumer = $this->consumer; $toolprovider->map_tool_to_consumer(); $imageurl = $this->getExternalTestFileUrl('test.jpg'); $count = 10; $this->members = []; for ($i = 1; $i <= $count; $i++) { $user = new User(); $user->firstname = 'Firstname' . $i; $user->lastname = 'Lastname' . $i; $user->ltiUserId = 'user' . $i; // Set user image values for some users. if ($i % 3 == 0) { $user->image = $imageurl; } $this->members[] = $user; } $this->context = Context::fromConsumer($this->consumer, 'testlticontextid'); $this->context->save(); $this->resourcelink = ResourceLink::fromContext($this->context, 'testresourcelinkid'); $this->resourcelink->save(); }
/** * Load the resource link from the database. * * @param int $id Record ID of resource link * @param DataConnector $dataConnector Database connection object * * @return ResourceLink ResourceLink object */ public static function fromRecordId($id, $dataConnector) { $resourceLink = new ResourceLink(); $resourceLink->dataConnector = $dataConnector; $resourceLink->load($id); return $resourceLink; }
/** * Performs membership service request using ResourceLink::doMembershipsService() method. * * @param ResourceLink $resourcelink * @return bool|User[] Array of User objects upon successful membership service request. False, otherwise. */ protected function do_resourcelink_membership_request(ResourceLink $resourcelink) { $members = false; $membershipsurl = $resourcelink->getSetting('ext_ims_lis_memberships_url'); $membershipsid = $resourcelink->getSetting('ext_ims_lis_memberships_id'); if ($membershipsurl && $membershipsid) { mtrace("Performing membership service request from resource link with membership URL: " . $membershipsurl); $members = $resourcelink->doMembershipsService(true); } return $members; }
/** * Get array of shares defined for this resource link. * * @param ResourceLink $resourceLink Resource_Link object * * @return array Array of ResourceLinkShare objects */ public function getSharesResourceLink($resourceLink) { $shares = array(); $sql = sprintf('SELECT consumer_pk, resource_link_pk, share_approved ' . "FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' . 'WHERE (primary_resource_link_pk = %d) ' . 'ORDER BY consumer_pk', $resourceLink->getRecordId()); $rsShare = mysql_query($sql); if ($rsShare) { while ($row = mysql_fetch_object($rsShare)) { $share = new ToolProvider\ResourceLinkShare(); $share->resourceLinkId = intval($row->resource_link_pk); $share->approved = intval($row->share_approved) === 1; $shares[] = $share; } } return $shares; }
/** * Test for data_connector::deleteUser(). */ public function test_delete_user() { $dc = new data_connector(); $consumer = new ToolConsumer(null, $dc); $consumer->name = 'TestName'; $consumer->setKey('TestKey'); $consumer->secret = 'TestSecret'; $consumer->save(); $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid'); $resourcelink->save(); $user = User::fromResourceLink($resourcelink, ''); $user->ltiResultSourcedId = 'testLtiResultSourcedId'; $user->firstname = 'First'; $user->lastname = 'Last'; $user->fullname = 'Full name'; $user->email = '*****@*****.**'; $user->roles = ['a', 'b']; $user->groups = ['1', '2']; $user->save(); // Delete user. $this->assertTrue($dc->deleteUser($user)); // User record should have been deleted from the DB. $this->assertFalse($dc->loadUser($user)); // User object should have been initialised(). $this->assertEmpty($user->firstname); $this->assertEmpty($user->lastname); $this->assertEmpty($user->fullname); $this->assertEmpty($user->email); $this->assertEmpty($user->roles); $this->assertEmpty($user->groups); $this->assertNull($user->ltiResultSourcedId); $this->assertNull($user->created); $this->assertNull($user->updated); }
/** * Check if a share arrangement is in place. * * @return boolean True if no error is reported */ private function checkForShare() { $ok = true; $doSaveResourceLink = true; $id = $this->resourceLink->primaryResourceLinkId; $shareRequest = isset($_POST['custom_share_key']) && !empty($_POST['custom_share_key']); if ($shareRequest) { if (!$this->allowSharing) { $ok = false; $this->reason = 'Your sharing request has been refused because sharing is not being permitted.'; } else { // Check if this is a new share key $shareKey = new ResourceLinkShareKey($this->resourceLink, $_POST['custom_share_key']); if (!is_null($shareKey->primaryConsumerKey) && !is_null($shareKey->primaryResourceLinkId)) { // Update resource link with sharing primary resource link details $key = $shareKey->primaryConsumerKey; $id = $shareKey->primaryResourceLinkId; $ok = $key !== $this->consumer->getKey() || $id != $this->resourceLink->getId(); if ($ok) { $this->resourceLink->primaryConsumerKey = $key; $this->resourceLink->primaryResourceLinkId = $id; $this->resourceLink->shareApproved = $shareKey->autoApprove; $ok = $this->resourceLink->save(); if ($ok) { $doSaveResourceLink = false; $this->user->getResourceLink()->primaryConsumerKey = $key; $this->user->getResourceLink()->primaryResourceLinkId = $id; $this->user->getResourceLink()->shareApproved = $shareKey->autoApprove; $this->user->getResourceLink()->updated = time(); // Remove share key $shareKey->delete(); } else { $this->reason = 'An error occurred initialising your share arrangement.'; } } else { $this->reason = 'It is not possible to share your resource link with yourself.'; } } if ($ok) { $ok = !is_null($key); if (!$ok) { $this->reason = 'You have requested to share a resource link but none is available.'; } else { $ok = !is_null($this->user->getResourceLink()->shareApproved) && $this->user->getResourceLink()->shareApproved; if (!$ok) { $this->reason = 'Your share request is waiting to be approved.'; } } } } } else { // Check no share is in place $ok = is_null($id); if (!$ok) { $this->reason = 'You have not requested to share a resource link but an arrangement is currently in place.'; } } // Look up primary resource link if ($ok && !is_null($id)) { $consumer = new ToolConsumer($key, $this->dataConnector); $ok = !is_null($consumer->created); if ($ok) { $resourceLink = ResourceLink::fromConsumer($consumer, $id); $ok = !is_null($resourceLink->created); } if ($ok) { if ($doSaveResourceLink) { $this->resourceLink->save(); } $this->resourceLink = $resourceLink; } else { $this->reason = 'Unable to load resource link being shared.'; } } return $ok; }
/** * Get array of shares defined for this resource link. * * @param ResourceLink $resourcelink ResourceLink object * @return array Array of ResourceLinkShare objects */ public function getSharesResourceLink($resourcelink) { global $DB; $shares = []; $params = ['primaryresourcelinkid' => $resourcelink->getRecordId()]; $fields = 'id, shareapproved, consumerid'; $records = $DB->get_records($this->resourcelinktable, $params, 'consumerid', $fields); foreach ($records as $record) { $share = new ResourceLinkShare(); $share->resourceLinkId = $record->id; $share->approved = $record->shareapproved == 1; $shares[] = $share; } return $shares; }
/** * Delete resource link object. * * @param ResourceLink $resourceLink Resource_Link object * * @return boolean True if the resource link object was successfully deleted */ public function deleteResourceLink($resourceLink) { $resourceLink->initialize(); return true; }
/** * Get array of shares defined for this resource link. * * @param ResourceLink $resourceLink Resource_Link object * * @return array Array of ResourceLinkShare objects */ public function getSharesResourceLink($resourceLink) { $id = $resourceLink->getRecordId(); $shares = array(); $sql = 'SELECT consumer_pk, resource_link_pk, share_approved ' . "FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' . 'WHERE (primary_resource_link_pk = :id) ' . 'ORDER BY consumer_pk'; $query = $this->db->prepare($sql); $query->bindValue('id', $id, PDO::PARAM_INT); if ($query->execute()) { while ($row = $query->fetch(PDO::FETCH_ASSOC)) { $row = array_change_key_case($row); $share = new ToolProvider\ResourceLinkShare(); $share->resourceLinkId = intval($row['resource_link_pk']); $share->approved = intval($row['share_approved']) === 1; $shares[] = $share; } } return $shares; }
/** * Builds a dummy tool provider object. * * @param string $secret Consumer secret. * @param array|stdClass $proxy Tool proxy data. * @param null $resourcelinksettings Key-value array for resource link settings. * @return dummy_tool_provider */ protected function build_dummy_tp($secret = null, $proxy = null, $resourcelinksettings = null) { $tool = $this->tool; $dataconnector = new data_connector(); $consumer = new ToolConsumer('testkey', $dataconnector); $ltiversion = ToolProvider::LTI_VERSION2; if ($secret === null && $proxy === null) { $consumer->secret = $tool->secret; $ltiversion = ToolProvider::LTI_VERSION1; } else { $consumer->secret = $secret; } $consumer->ltiVersion = $ltiversion; $consumer->name = 'TEST CONSUMER NAME'; $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME'; $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID'; $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION'; $consumer->enabled = true; $consumer->protected = true; if ($proxy !== null) { $consumer->toolProxy = json_encode($proxy); } $consumer->save(); $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid'); if (!empty($resourcelinksettings)) { foreach ($resourcelinksettings as $setting => $value) { $resourcelink->setSetting($setting, $value); } } $resourcelink->save(); $ltiuser = User::fromResourceLink($resourcelink, ''); $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId'; $ltiuser->ltiUserId = 'testuserid'; $ltiuser->email = '*****@*****.**'; $ltiuser->save(); $tp = new dummy_tool_provider($tool->id); $tp->user = $ltiuser; $tp->resourceLink = $resourcelink; $tp->consumer = $consumer; return $tp; }
/** * Test for data_connector::get_resourcelink_from_context() */ public function test_get_resourcelink_from_context() { $dc = new data_connector(); $consumer = new ToolConsumer(null, $dc); $consumer->name = 'TestName'; $consumer->setKey('TestKey'); $consumer->secret = 'TestSecret'; $consumer->save(); $settings = ['a', 'b', 'c']; $lticontextid = 'testlticontextid'; $context = Context::fromConsumer($consumer, $lticontextid); $context->settings = $settings; $context->save(); // No ResourceLink associated with the Context yet. $this->assertNull($dc->get_resourcelink_from_context($context)); // Create and save ResourceLink from the Context. $resourcelink = ResourceLink::fromContext($context, 'testresourcelinkid'); $resourcelink->save(); $dc->loadResourceLink($resourcelink); // Assert that the resource link and the one fetched by get_resourcelink_from_context() are the same. $this->assertEquals($resourcelink, $dc->get_resourcelink_from_context($context)); }
/** * Fetches a resource link record that is associated with a Context object. * * @param Context $context * @return ResourceLink */ public function get_resourcelink_from_context(Context $context) { global $DB; $resourcelink = null; if ($resourcelinkrecord = $DB->get_record($this->resourcelinktable, ['contextid' => $context->getRecordId()], 'ltiresourcelinkkey')) { $resourcelink = ResourceLink::fromContext($context, $resourcelinkrecord->ltiresourcelinkkey); } return $resourcelink; }