daily pastebin goal
69%
SHARE
TWEET

Credly-Moodle Integration Script

jonwestfall Apr 26th, 2013 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  
  4. Moodle Credly Integration script.
  5. Instructions:
  6. 1. Enter your Credly credentials below under username & password
  7. 2. Customize any output that your students will see, and enter your Moodle installation's URL
  8. 3. Create a URL object in Moodle pointing to this script, along with the badge ID number. You can find badge ID numbers by logging into Credly, finding the badge, and clicking "Give". The URL of the page you're taken to will read "/give/XXXX" - those X's are the ID number of that badge. In Moodle, your URL's link will look something like http://mymoodle/credly_moodle.php?badge=XXXX
  9. 4. Under the Advanced URL settings, set the following parameters:
  10.   firstname -> User First Name
  11.   lastname -> User Lastname
  12.   email -> Email Address
  13. 5. Set up the conditions for this URL to be visible (i.e. a certain grade, completion of a course, etc...). Let Students know that the URL will be visible and they need to click on it to get their badge. Perhaps say "Claim your XXXX Badge!".
  14. 6. Test it out!
  15.  
  16. NOTE: THIS SCRIPT IS IN ALPHA. HERE'S WHAT TO KNOW...
  17. A. It works, but if any little thing breaks, it looks like it works but doesn't, causing annoyance.
  18. B. If it breaks, you'll want to read below and check each "debug" step to see what's going on.
  19. C. Eventually the script should be written to include error messages and such.
  20.  
  21.  
  22. Author: Jon Westfall (jon@jonwestfall.com)
  23. Version: 0.1
  24. Portions from http://bavotasan.com/2011/post-url-using-curl-php/
  25. */
  26.  
  27. $username = "xyz@com"; // Your Credly Username
  28. $password = "password"; // Your Credly Password
  29. $message = "Congratulations! Your badge is on its way to your inbox!";
  30. $url = "http://mymoodle.org"; // Your Moodle Location
  31.  
  32.  
  33. // You should not need to modify below this line.
  34.  
  35. // First a security check - we're using GET here which would allow savvy users to try to issue
  36. // badges by guessing IDs. This adds a small layer of protection, although it's not perfect either
  37. // (i.e. someone could fake the referrer).
  38.  
  39. $pos = strpos($_SERVER['HTTP_REFERER'],$url);
  40.  
  41. if ($pos === false) {
  42. die("Called From Outside Moodle");
  43. }
  44.  
  45. $api = 'https://api.credly.com/v0.2/';
  46. $authurl = $api . 'authenticate/';
  47. $url = $api . 'authenticate/';
  48. $curl = curl_init();
  49.  
  50. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
  51. curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
  52. curl_setopt($curl, CURLOPT_SSLVERSION,3);
  53. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  54. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  55. curl_setopt($curl, CURLOPT_HEADER, true);
  56. curl_setopt($curl, CURLOPT_POST, true);
  57. curl_setopt($curl, CURLOPT_POSTFIELDS, $params );
  58. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  59. curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  60. curl_setopt($curl, CURLOPT_URL, $url);
  61. $response = curl_exec($curl);
  62. // Get just the API Key from the output
  63. $api_key = substr($response,-131);
  64. $api_key = substr($api_key,0,-3);
  65. // DEBUG NOTE: FIRST CHECK THAT YOU'RE GETTING AN API KEY. THE CODE HERE
  66. // IS CRUDE - IT JUST USES SUBSTR TO GET THE KEY. IF CREDLY CHANGES THEIR API
  67. // TO EXPORT MORE ON THE AUTHENTICATE REQUEST, THIS WILL BREAK AND YOU'LL
  68. // HAVE TO ADJUST LINES 50 AND 51 TO ISOLATE THE KEY. OR DO IT WITH JSON_DECODE (I.E. THE RIGHT WAY)
  69.  
  70. // OK, now we have the API Key. Now we just need to give the badge
  71. $badge = $_GET['badge'];
  72. $first_name = $_GET['firstname'];
  73. $last_name = $_GET['lastname'];
  74. $email = $_GET['email'];
  75. $url = $api . "member_badges/";
  76.  
  77. // DEBUG NOTE: IF MOODLE ISN'T PASSING THE RIGHT PARAMETERS, IT WILL MUCK THINGS UP. MIGHT WANT TO
  78. // UNCOMMENT THE LINE BELOW TO SEE WHAT'S PASSED
  79. //die(var_dump($_GET));
  80.  
  81. $data = array(
  82.    "badge_id" => $badge,
  83.    "first_name" => $first_name,
  84.    "last_name" => $last_name,
  85.    "access_token" => $api_key,
  86.    "email" => $email
  87. );
  88. $fields = '';
  89. foreach($data as $key => $value) {
  90. $fields .= $key . '=' . $value . '&';
  91. }
  92. rtrim($fields, '&');
  93. $post = curl_init();
  94.  
  95.    curl_setopt($post, CURLOPT_URL, $url);
  96.    curl_setopt($post, CURLOPT_POST, count($data));
  97.    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
  98.    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
  99.  
  100.    $result = curl_exec($post);
  101.    
  102.    curl_close($post);
  103. // DEBUG NOTE: IF EVERYTHING ABOVE LOOKS OK, BUT YOU'RE STILL NOT ISSUING BADGES, CHECK
  104. // THE RESULT OF THIS CURL REQUEST BY UNCOMMENTING THE LINE BELOW
  105. //echo $result;
  106. // Everything is done, show the message.
  107. echo $message;
  108. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top