/** * 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])); }
/** * 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; }
/** * Delete user object. * * @param User $user User object * * @return boolean True if the user object was successfully deleted */ public function deleteUser($user) { $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' . 'WHERE (user_pk = %d)', $user->getRecordId()); $ok = mysql_query($sql); if ($ok) { $user->initialize(); } return $ok; }
/** * 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; }
/** * Delete user object. * * @param User $user User object * @return boolean True if the user object was successfully deleted */ public function deleteUser($user) { global $DB; $DB->delete_records($this->userresulttable, ['id' => $user->getRecordId()]); $user->initialize(); return true; }
/** * Delete user object. * * @param User $user User object * * @return boolean True if the user object was successfully deleted */ public function deleteUser($user) { $user->initialize(); return true; }
/** * Delete user object. * * @param User $user User object * * @return boolean True if the user object was successfully deleted */ public function deleteUser($user) { $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' . 'WHERE (user_pk = :id)'; $query = $this->db->prepare($sql); $query->bindValue('id', $user->getRecordId(), PDO::PARAM_INT); $ok = $query->execute(); if ($ok) { $user->initialize(); } return $ok; }
/** * 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; }
/** * Class constructor from resource link. * * @param ResourceLink $resourceLink Resource_Link object * @param string $ltiUserId User ID value * @return User */ public static function fromResourceLink($resourceLink, $ltiUserId) { $user = new User(); $user->resourceLink = $resourceLink; if (!is_null($resourceLink)) { $user->resourceLinkId = $resourceLink->getRecordId(); $user->dataConnector = $resourceLink->getDataConnector(); } $user->ltiUserId = $ltiUserId; if (!empty($ltiUserId)) { $user->load(); } return $user; }