Share and be happy!

New Year comes, it’s time to make gifts to our users and clients, so we decided to make some useful changes at still the only in the official Moodle repository, authentication plugin via OAuth social networks (Facebook, Google, Yahoo, Twitter, VKontakte, Yandex, Mail .ru). The changes are not so grandiose, but hopefully useful to the users. So, briefly about what we did:

  1. added automatically avatar upload of the social profile
  2. the “bug” with the re-authorization via Twitter fixed
  3. Update to version 5.27 API VKontakte

And now more

Retrieve OAuth avatar to Moodle profile LenAuthAdded new option & mdash; uploading / receiving avatar (picture) from the profile. Moodle / plugin LenAuth administrator can check this option to user avatar automatically loads to his Moodle profile. This option works is quite simple: in the LenAuth settings check «Retrieve avatar», now users who have in their social profiles avatars and not having still an avatar at their Moodle account, loggin/authorize yourself in Moodle through LenAuth will upload their avatars to their account automatically. Important:: during initial photo upload at top right of the profile avatar will be default, ie user will be able to see it (if only that is automatically loaded avatar) only by going to the Edit Profile or at the next login. If a user hasnt avatar in his social network profile, through which it is authorized, then avatar will not uploaded.

Download avatar from the point of view of plugin programming / development

    Algorithm of uploading:

  1. Check the option «Retrieve avatar»
  2. If this option is checked — get an image link from the OAuth response
  3. Check avatar available in OAuth response and avatar presence in the current Moodle profile
  4. If the response is confirmed by the presence of the image and profile Moodle not, then load

    So, each social network OAuth has its own method of get user profile image:

  1. Facebook: after receiving the internal user id his avatar is available here: http://graph.facebook.com/user_id/picture
  2. Google: link to the image contained in the webservice response, if we transform the answer in an associative array $array, then you can get a link in such a way: $array['image']['url']
  3. Yahoo: as well as Google’s link contained in the answer: $array['query']['results']['profile']['image']['imageUrl']
  4. Twitter: here, as usual, Twitter has a bit confusing algorithm to obtain a reference to the picture, let’s look at it. In Twitter OAuth response comes username or $array['screen_name'], and to get the image, you must substitute the screen_name a URL like: https://twitter.com/screen_name/profile_image?size=original. But here Twitter shows a picture with him giving header image/jpeg, which is not enough, since it needs a reference. Therefore, we used the php-based function get_headers, which returns all the inside information about the content on the page image. So the code is:
    //$array['screen_name'] - Twitter username received in OAuth response
    $image_url_pre = 'https://twitter.com/' . $array['screen_name'] . '/profile_image?size=original';
    $image_header = get_headers($image_url_pre, 1); //get all the information about the content on the URL $image_url_pre
    $image_url = $image_header['location']; //key location contains the very link to the image
    
  5. VK.com: in VK OAuth is also quite unusual to obtain additional data: after receiving the internal profile id, you need to make an additional request to the URL like http://api.vk.com/method/users.get?user_ids=profile_id&fields=fields_params. Parameters list of the method users.get you can see here.Request is presented at JSON format. It is better to look all together:
    //$curl - object of Moodle-based class curl, $id - id user's if from response, photo_200 - key for medium format photography
    $additional_fields_pre = $curl->get('http://api.vk.com/method/users.get?user_ids='. $id . '&fields = photo_200'); //GET-request to the URL
    $additional_fields = json_decode($additional_fields_pre, true); //convert the json to an associative array
    $image_url = $additional_fields['response'][0]['photo_200']; //link to the image
    
  6. Yandex: webservice returns only avatar internal id, and a link to it enough to just get by substituting the id in the URL of the form: https://avatars.yandex.net/get-yapic/avatar_id/needle_avatar_size. More on obtaining avatars in Yandex. Of all the available sizes, we have chosen size 200×200 with key islands-200.
  7. Mail.ru: here response comes with an OAuth array key pic_big with the URL of the image, in the end: $array[0]['pic_big'].

Ok, we got a link to a specific avatar of desired provider, now we need to turn it into an image of a user profile in Moodle. There is a special feature for it: the Moodle-based package GDlib (lib/gdlib.php), it is called process_new_icon($context, $component, $filearea, $itemid, $originalfile), in our case, the $context is the user context context_user::instance($newuser->id, MUST_EXIST), $component is user, $filearea is icon, $temid = 0, but $originalfile — this is just the file you need to process, but we has not a file, but only a link to the image, the function checks for the existence of a file, but if there is no file — let’s create the one. For this we use php-based function copy, this function copies the contents to file. We need to copy the image (link) to some temporary file and transfer it to the function process_new_icon as a parameter $originalfile. But where to copy a temporary file? Moodle configuration object $CFG has parameter tempdir — this is the path to the temporary folder. Important! This folder is not created when you just installing Moodle so necessary to check its existence and, in the absence of, create it using php-based function mkdir. Coined the name of the temporary file and determine its path to the temporary folder Moodle copy the image there and transfer a quality parameter $originalfile to function process_new_icon which returns us to the downloaded file id (all attachments and information they are stored in the database table mdl_files) from the database, you need to specify the id in the record in the database the user in the field picture of table mdl_user and everything. Code:

//Create a unique name for a temporary file
$tempfilename = substr( microtime(), 0, 10 ) . '.tmp';
//specify the path to the temporary folder to copy into it the temporary file
$templfolder = $CFG->tempdir . '/filestorage';
//check for the existence of the temporary folder in the absence - create
if ( !file_exists( $templfolder ) ) {
  mkdir( $templfolder, $CFG->directorypermissions );
}
//specify write permissions to this folder
@chmod( $templfolder, 0777 );
//the full path to a temporary file
$tempfile = $templfolder . '/' . $tempfilename;
//copy
if ( copy( $image_url, $tempfile ) ) {
  //connect GDlib Moodle-based Library
  require_once( $CFG->libdir . '/gdlib.php' );
  //upload avatar from the temporary file
  $usericonid = process_new_icon( context_user::instance( $newuser->id, MUST_EXIST ), 'user', 'icon', 0, $tempfile );
  //specify icon id for the desired user with id $newuser->id (in our case)
  if ( $usericonid ) {
    $DB->set_field( 'user', 'picture', $usericonid, array( 'id' => $newuser->id ) );
  }
  //delete temporary files
  unset( $tempfile );
}
//return the necessary permissions on the directory for temporary files
@chmod( $templfolder, $CFG->directorypermissions );

And now the trouble, we found ourselves in our “bug” in the calculation of the OAuth id profiles and quickly fixed it. VK.com became quite frequently update their API, we have also updated this version.

Write a comment

Your email address will not be published. Required fields are marked *

Waiting feedback for mutually beneficial cooperation

Our services are b2b-oriented. We strive for high-quality Moodle services supply as it leads to higher income and satisfaction of our clients. If you have any questions or desires, please contact us.

A pop-up window

Contact us
Programming technologies Moodle: CSS3, AngularJS, Bootstrap

© 2014–2016, «LMS-Service». All rights reserved.