Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- Moodle Credly Integration script.
- Instructions:
- 1. Enter your Credly credentials below under username & password
- 2. Customize any output that your students will see, and enter your Moodle installation's URL
- 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
- 4. Under the Advanced URL settings, set the following parameters:
- firstname -> User First Name
- lastname -> User Lastname
- email -> Email Address
- 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!".
- 6. Test it out!
- NOTE: THIS SCRIPT IS IN ALPHA. HERE'S WHAT TO KNOW...
- A. It works, but if any little thing breaks, it looks like it works but doesn't, causing annoyance.
- B. If it breaks, you'll want to read below and check each "debug" step to see what's going on.
- C. Eventually the script should be written to include error messages and such.
- Author: Jon Westfall (jon@jonwestfall.com)
- Version: 0.1
- Portions from http://bavotasan.com/2011/post-url-using-curl-php/
- */
- $username = "xyz@com"; // Your Credly Username
- $password = "password"; // Your Credly Password
- $message = "Congratulations! Your badge is on its way to your inbox!";
- $url = "http://mymoodle.org"; // Your Moodle Location
- // You should not need to modify below this line.
- // First a security check - we're using GET here which would allow savvy users to try to issue
- // badges by guessing IDs. This adds a small layer of protection, although it's not perfect either
- // (i.e. someone could fake the referrer).
- $pos = strpos($_SERVER['HTTP_REFERER'],$url);
- if ($pos === false) {
- die("Called From Outside Moodle");
- }
- $api = 'https://api.credly.com/v0.2/';
- $authurl = $api . 'authenticate/';
- $url = $api . 'authenticate/';
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
- curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
- curl_setopt($curl, CURLOPT_SSLVERSION,3);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($curl, CURLOPT_HEADER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $params );
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
- curl_setopt($curl, CURLOPT_URL, $url);
- $response = curl_exec($curl);
- // Get just the API Key from the output
- $api_key = substr($response,-131);
- $api_key = substr($api_key,0,-3);
- // DEBUG NOTE: FIRST CHECK THAT YOU'RE GETTING AN API KEY. THE CODE HERE
- // IS CRUDE - IT JUST USES SUBSTR TO GET THE KEY. IF CREDLY CHANGES THEIR API
- // TO EXPORT MORE ON THE AUTHENTICATE REQUEST, THIS WILL BREAK AND YOU'LL
- // HAVE TO ADJUST LINES 50 AND 51 TO ISOLATE THE KEY. OR DO IT WITH JSON_DECODE (I.E. THE RIGHT WAY)
- // OK, now we have the API Key. Now we just need to give the badge
- $badge = $_GET['badge'];
- $first_name = $_GET['firstname'];
- $last_name = $_GET['lastname'];
- $email = $_GET['email'];
- $url = $api . "member_badges/";
- // DEBUG NOTE: IF MOODLE ISN'T PASSING THE RIGHT PARAMETERS, IT WILL MUCK THINGS UP. MIGHT WANT TO
- // UNCOMMENT THE LINE BELOW TO SEE WHAT'S PASSED
- //die(var_dump($_GET));
- $data = array(
- "badge_id" => $badge,
- "first_name" => $first_name,
- "last_name" => $last_name,
- "access_token" => $api_key,
- "email" => $email
- );
- $fields = '';
- foreach($data as $key => $value) {
- $fields .= $key . '=' . $value . '&';
- }
- rtrim($fields, '&');
- $post = curl_init();
- curl_setopt($post, CURLOPT_URL, $url);
- curl_setopt($post, CURLOPT_POST, count($data));
- curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
- curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($post);
- curl_close($post);
- // DEBUG NOTE: IF EVERYTHING ABOVE LOOKS OK, BUT YOU'RE STILL NOT ISSUING BADGES, CHECK
- // THE RESULT OF THIS CURL REQUEST BY UNCOMMENTING THE LINE BELOW
- //echo $result;
- // Everything is done, show the message.
- echo $message;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement