Skip to content

Commit

Permalink
Merge pull request #49 from 3iPunt/MOODLE_29_STABLE
Browse files Browse the repository at this point in the history
Allow force membership service
  • Loading branch information
jleyva authored Sep 1, 2017
2 parents c806947 + f0d11c1 commit e70e4b9
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 177 deletions.
4 changes: 4 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
$buttons[] = html_writer::link(new moodle_url('/local/ltiprovider/syncreport.php', array('id'=>$tool->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('i/grades'), 'alt'=>get_string('gradessyncreport', 'local_ltiprovider'), 'class'=>'iconsmall')));
}

if ($tool->syncmembers) {
$buttons[] = html_writer::link(new moodle_url('/local/ltiprovider/syncmembers.php', array('id'=>$tool->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('i/users'), 'alt'=>get_string('forcesyncmembers', 'local_ltiprovider'), 'class'=>'iconsmall')));
}

$line[] = implode(' ', $buttons);
} else {
$line[] = '';
Expand Down
4 changes: 3 additions & 1 deletion lang/en/local_ltiprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

$string['addtogroup'] = 'Add users to group (idnumber or request based)';
$string['addtogroup_help'] = 'Add new users to the indicated group (use the group idnumber). If you want to use or create groups based on a parameter from the request, you must add "request:" before parameter name in the setting field, for example, request:myrememotegroup';
$string['forcesendgradesselectedusers'] = 'Force sned grades for selected users';
$string['forcesendgradesselectedusers'] = 'Force send grades for selected users';
$string['youhavetoselectauser'] = 'Must select at least one user';

$string['outcomessettings'] = 'Outcomes service settings';
Expand All @@ -140,3 +140,5 @@
1: When the course or activity is completed<br/>
0: When the course or activity is not completed yet';

$string['forcesyncmembers'] = 'Force sync members';

178 changes: 6 additions & 172 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

defined('MOODLE_INTERNAL') or die;
require_once($CFG->dirroot.'/local/ltiprovider/ims-blti/blti_util.php');
require_once($CFG->dirroot.'/local/ltiprovider/locallib.php');

use moodle\local\ltiprovider as ltiprovider;

Expand Down Expand Up @@ -454,161 +455,9 @@ function local_ltiprovider_cron() {
$lastsync = 0;
}
if ($lastsync + $tool->syncperiod < $timenow) {
mtrace('Starting sync of tool: ' . $tool->id);
// We check for all the users, notice that users can access the same tool from different consumers.
if ($users = $DB->get_records('local_ltiprovider_user', array('toolid' => $tool->id), 'lastaccess DESC')) {
$response = "";

foreach ($users as $user) {
if (!$user->membershipsurl or !$user->membershipsid) {
continue;
}

$consumer = md5($user->membershipsurl . ':' . $user->membershipsid . ':' . $user->consumerkey . ':' . $user->consumersecret);
if (in_array($consumer, $consumers)) {
// We had syncrhonized with this consumer yet.
continue;
}
$consumers[] = $consumer;

$params = array(
'lti_message_type' => 'basic-lis-readmembershipsforcontext',
'id' => $user->membershipsid,
'lti_version' => 'LTI-1p0'
);

mtrace('Calling memberships url: ' . $user->membershipsurl . ' with body: ' . json_encode($params));

try {
$response = ltiprovider\sendOAuthParamsPOST('POST', $user->membershipsurl, $user->consumerkey, $user->consumersecret,
'application/x-www-form-urlencoded', $params);
} catch (Exception $e) {
mtrace("Exception: " . $e->getMessage());
$response = false;
}

if ($response) {
$data = new SimpleXMLElement($response);
if(!empty($data->statusinfo)) {
if(strpos(strtolower($data->statusinfo->codemajor), 'success') !== false) {
$members = $data->memberships->member;
mtrace(count($members) . ' members received');
$currentusers = array();
foreach ($members as $member) {
$username = local_ltiprovider_create_username($user->consumerkey, $member->user_id);

$userobj = $DB->get_record('user', array('username' => $username));
if (!$userobj) {
// Old format.
$oldusername = 'ltiprovider' . md5($user->consumerkey . ':' . $member->user_id);
$userobj = $DB->get_record('user', array('username' => $oldusername));
if ($userobj) {
$DB->set_field('user', 'username', $username, array('id' => $userobj->id));
}
$userobj = $DB->get_record('user', array('username' => $username));
}

if ($userobj) {
$currentusers[] = $userobj->id;
$userobj->firstname = clean_param($member->person_name_given, PARAM_TEXT);
$userobj->lastname = clean_param($member->person_name_family, PARAM_TEXT);
$userobj->email = clean_param($member->person_contact_email_primary, PARAM_EMAIL);
$userobj->timemodified = time();

$DB->update_record('user', $userobj);
$userphotos[$userobj->id] = $member->user_image;

// Trigger event.
$event = \core\event\user_updated::create(
array(
'objectid' => $userobj->id,
'relateduserid' => $userobj->id,
'context' => context_user::instance($userobj->id)
)
);
$event->trigger();

} else {
// New members.
if ($tool->syncmode == 1 or $tool->syncmode == 2) {
// We have to enrol new members so we have to create it.
$userobj = new stdClass();
// clean_param , email username text
$auth = get_config('local_ltiprovider', 'defaultauthmethod');
if ($auth) {
$userobj->auth = $auth;
} else {
$userobj->auth = 'nologin';
}

$username = local_ltiprovider_create_username($user->consumerkey, $member->user_id);
$userobj->username = $username;
$userobj->password = md5(uniqid(rand(), 1));
$userobj->firstname = clean_param($member->person_name_given, PARAM_TEXT);
$userobj->lastname = clean_param($member->person_name_family, PARAM_TEXT);
$userobj->email = clean_param($member->person_contact_email_primary, PARAM_EMAIL);
$userobj->city = $tool->city;
$userobj->country = $tool->country;
$userobj->institution = $tool->institution;
$userobj->timezone = $tool->timezone;
$userobj->maildisplay = $tool->maildisplay;
$userobj->mnethostid = $CFG->mnet_localhost_id;
$userobj->confirmed = 1;
$userobj->lang = $tool->lang;
$userobj->timecreated = time();
if (! $userobj->lang) {
// TODO: This should be changed for detect the course lang
$userobj->lang = current_language();
}

$userobj->id = $DB->insert_record('user', $userobj);
// Reload full user
$userobj = $DB->get_record('user', array('id' => $userobj->id));

$userphotos[$userobj->id] = $member->user_image;
// Trigger event.
$event = \core\event\user_created::create(
array(
'objectid' => $userobj->id,
'relateduserid' => $userobj->id,
'context' => context_user::instance($userobj->id)
)
);
$event->trigger();

$currentusers[] = $userobj->id;
}
}
// 1 -> Enrol and unenrol, 2 -> enrol
if ($tool->syncmode == 1 or $tool->syncmode == 2) {
// Enroll the user in the course. We don't know if it was previously unenrolled.
$roles = explode(',', strtolower($member->roles));
local_ltiprovider_enrol_user($tool, $userobj, $roles, true);
}
}
// Now we check if we have to unenrol users for keep both systems sync.
if ($tool->syncmode == 1 or $tool->syncmode == 3) {
// Unenrol users also.
$context = context_course::instance($tool->courseid);
$eusers = get_enrolled_users($context);
foreach ($eusers as $euser) {
if (!in_array($euser->id, $currentusers)) {
local_ltiprovider_unenrol_user($tool, $euser);
}
}
}
} else {
mtrace('Error recived from the remote system: ' . $data->statusinfo->codemajor . ' ' . $data->statusinfo->severity . ' ' . $data->statusinfo->codeminor);
}
} else {
mtrace('Error parsing the XML received' . substr($response, 0, 125) . '... (Displaying only 125 chars)');
}
} else {
mtrace('No response received from ' . $user->membershipsurl);
}
}
}
set_config('membershipslastsync-' . $tool->id, $timenow, 'local_ltiprovider');
$ret = local_ltiprovider_membership_service($tool, $timenow, $userphotos, $consumers);
$userphotos = $ret['userphotos'];
$consumers = $ret['consumers'];
} else {
$last = format_time((time() - $lastsync));
mtrace("Tool $tool->id synchronized $last ago");
Expand All @@ -617,23 +466,8 @@ function local_ltiprovider_cron() {
}
}

// Sync of user photos.
mtrace("Sync user profile images");
$counter = 0;
if ($userphotos) {
foreach ($userphotos as $userid => $url) {
if ($url) {
$result = local_ltiprovider_update_user_profile_image($userid, $url);
if ($result === true) {
$counter++;
mtrace("Profile image succesfully downloaded and created from $url");
} else {
mtrace($result);
}
}
}
}
mtrace("$counter profile images updated");
local_ltiprovider_membership_service_update_userphotos($userphotos);

}

/**
Expand Down
Loading

0 comments on commit e70e4b9

Please sign in to comment.