/** * 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."); }
/** * Test for data_connector::get_consumers_mapped_to_tool(). */ public function test_get_consumers_mapped_to_tool() { $generator = $this->getDataGenerator(); // Create two tools belonging to the same course. $course1 = $generator->create_course(); $data = new stdClass(); $data->courseid = $course1->id; $tool = $generator->create_lti_tool($data); $tool2 = $generator->create_lti_tool($data); $dc = new data_connector(); $consumer = new ToolConsumer('key1', $dc); $consumer->name = 'testconsumername'; $consumer->secret = 'testsecret'; $consumer->save(); $tp = new \enrol_lti\tool_provider($tool->id); $tp->consumer = $consumer; $tp->map_tool_to_consumer(); $consumer2 = new ToolConsumer('key2', $dc); $consumer2->name = 'testconsumername2'; $consumer2->secret = 'testsecret2'; $consumer2->save(); $tp2 = new \enrol_lti\tool_provider($tool2->id); $tp2->consumer = $consumer2; $tp2->map_tool_to_consumer(); $consumers = $dc->get_consumers_mapped_to_tool($tool->id); $this->assertCount(1, $consumers); $this->assertEquals($consumer, $consumers[0]); $consumers2 = $dc->get_consumers_mapped_to_tool($tool2->id); $this->assertCount(1, $consumers2); $this->assertEquals($consumer2, $consumers2[0]); }