2 * Push badges to backpack.
4 function addtobackpack(event, args) {
5 var badgetable = Y.one('#issued-badge-table');
6 var errordiv = Y.one('#addtobackpack-error');
7 var errortext = M.util.get_string('error:backpackproblem', 'badges');
8 var errorhtml = '<div id="addtobackpack-error" class="box boxaligncenter notifyproblem">' + errortext + '</div>';
10 if (typeof OpenBadges !== 'undefined') {
11 OpenBadges.issue([args.assertion], function(errors, successes) { });
13 // Add error div if it doesn't exist yet.
15 var badgerror = Y.Node.create(errorhtml);
16 badgetable.insert(badgerror, 'before');
22 * Check if website is externally accessible from the backpack.
24 function check_site_access() {
25 var add = Y.one('#check_connection');
30 success: function(id, o, args) {
31 var data = Y.JSON.parse(o.responseText);
32 if (data.code == 'http-unreachable') {
33 add.setHTML(data.response);
34 add.removeClass('hide');
37 failure: function(o) { }
41 Y.use('io-base', function(Y) {
42 Y.io('ajax.php', callback);
49 * Update the status indicator to show connection progress.
51 function badges_set_connection_progress(status) {
54 var connecting = M.util.get_string('connecting', 'badges');
55 var imageurl = M.util.image_url('i/loading_small', 'moodle');
56 var loading = Y.Node.create(connecting + ' <img src="'+imageurl+'" width="16" height="16" alt="'+connecting+'"/>');
57 Y.one('#connection-status').removeClass('notconnected').addClass('connecting').setHTML(loading);
60 var notconnected = M.util.get_string('notconnected', 'badges');;
61 Y.one('#connection-status').removeClass('connecting').addClass('notconnected').setHTML(notconnected);
64 // Unknown status, do nothing.
69 * Print an error message at the top of the page.
71 function badges_set_error_message(messagekey, param) {
72 var errortext = M.util.get_string(messagekey, 'badges', param);
73 Y.one('#connection-error').setHTML(errortext);
77 * Handle the assertion generated by the Persona dialog.
79 function badges_handle_assertion(assertion) {
82 var noassertionstr = M.util.get_string('error:noassertion', 'badges');
83 badges_set_error_message('error:backpackloginfailed', noassertionstr);
87 badges_set_connection_progress('connecting');
89 Y.io("backpackconnect.php", {
91 data: "assertion="+assertion+"&sesskey="+M.cfg.sesskey,
93 success: function (id, result) {
94 // Reload page to display connected email address.
95 window.location.href = "mybackpack.php";
97 failure: function (id, result) {
99 var parsedResponse = Y.JSON.parse(result.response);
101 badges_set_connection_progress('notconnected');
102 var badjsonstr = M.util.get_string('error:badjson', 'badges');
103 badges_set_error_message('error:backpackloginfailed', badjsonstr);
106 badges_set_connection_progress('notconnected');
107 badges_set_error_message('error:backpackloginfailed', parsedResponse.reason);
115 * Create and bind the persona login button.
117 function badges_init_persona_login_button() {
118 // Create the login button and add to the page via Javascript.
119 var imageurl = M.util.image_url('i/persona_sign_in_black', 'moodle');
120 var imagealt = M.util.get_string('signinwithyouremail', 'badges');
121 var button = Y.Node.create('<img id="persona_signin" src="'+imageurl+'" width="202" height="25" alt="'+imagealt+'"/>');
122 Y.one('#persona-container').append(button);
124 // Bind a click event to trigger login when clicked.
125 button.on('click', function() {
126 Y.one('#connection-error').empty();
127 navigator.id.get(badges_handle_assertion);