Example #1
1
File: lib.php Project: dg711/moodle
 /**
  * Delete plugin specific information.
  *
  * @param stdClass $instance
  * @return void
  */
 public function delete_instance($instance)
 {
     global $DB;
     // Get the tool associated with this instance.
     $tool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), 'id', MUST_EXIST);
     // Delete any users associated with this tool.
     $DB->delete_records('enrol_lti_users', array('toolid' => $tool->id));
     // Get tool and consumer mappings.
     $rsmapping = $DB->get_recordset('enrol_lti_tool_consumer_map', array('toolid' => $tool->id));
     // Delete consumers that are linked to this tool and their related data.
     $dataconnector = new data_connector();
     foreach ($rsmapping as $mapping) {
         $consumer = new ToolConsumer(null, $dataconnector);
         $consumer->setRecordId($mapping->consumerid);
         $dataconnector->deleteToolConsumer($consumer);
     }
     $rsmapping->close();
     // Delete mapping records.
     $DB->delete_records('enrol_lti_tool_consumer_map', array('toolid' => $tool->id));
     // Delete the lti tool record.
     $DB->delete_records('enrol_lti_tools', array('id' => $tool->id));
     // Time for the parent to do it's thang, yeow.
     parent::delete_instance($instance);
 }
Example #2
0
 /**
  * Performs the synchronisation of members.
  */
 public function execute()
 {
     if (!is_enabled_auth('lti')) {
         mtrace('Skipping task - ' . get_string('pluginnotenabled', 'auth', get_string('pluginname', 'auth_lti')));
         return;
     }
     // Check if the enrolment plugin is disabled - isn't really necessary as the task should not run if
     // the plugin is disabled, but there is no harm in making sure core hasn't done something wrong.
     if (!enrol_is_enabled('lti')) {
         mtrace('Skipping task - ' . get_string('enrolisdisabled', 'enrol_lti'));
         return;
     }
     $this->dataconnector = new data_connector();
     // Get all the enabled tools.
     $tools = helper::get_lti_tools(array('status' => ENROL_INSTANCE_ENABLED, 'membersync' => 1));
     foreach ($tools as $tool) {
         mtrace("Starting - Member sync for published tool '{$tool->id}' for course '{$tool->courseid}'.");
         // Variables to keep track of information to display later.
         $usercount = 0;
         $enrolcount = 0;
         $unenrolcount = 0;
         // Fetch consumer records mapped to this tool.
         $consumers = $this->dataconnector->get_consumers_mapped_to_tool($tool->id);
         // Perform processing for each consumer.
         foreach ($consumers as $consumer) {
             mtrace("Requesting membership service for the tool consumer '{$consumer->getRecordId()}'");
             // Get members through this tool consumer.
             $members = $this->fetch_members_from_consumer($consumer);
             // Check if we were able to fetch the members.
             if ($members === false) {
                 mtrace("Skipping - Membership service request failed.\n");
                 continue;
             }
             // Fetched members count.
             $membercount = count($members);
             mtrace("{$membercount} members received.\n");
             // Process member information.
             list($usercount, $enrolcount) = $this->sync_member_information($tool, $consumer, $members);
         }
         // Now we check if we have to unenrol users who were not listed.
         if ($this->should_sync_unenrol($tool->membersyncmode)) {
             $unenrolcount = $this->sync_unenrol($tool);
         }
         mtrace("Completed - Synced members for tool '{$tool->id}' in the course '{$tool->courseid}'. " . "Processed {$usercount} users; enrolled {$enrolcount} members; unenrolled {$unenrolcount} members.\n");
     }
     // Sync the user profile photos.
     mtrace("Started - Syncing user profile images.");
     $countsyncedimages = $this->sync_profile_images();
     mtrace("Completed - Synced {$countsyncedimages} profile images.");
 }
Example #3
0
 /**
  * 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);
 }
 /**
  * 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));
 }