Merge branch 'MDL-42714-master' of git://github.com/sammarshallou/moodle
[moodle.git] / badges / backpack.js
blobd20ad17d6f3b90519889bdeb300f3163a2ef37cb
1 /**
2  * Push badges to backpack.
3  */
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) { });
12     } else {
13         // Add error div if it doesn't exist yet.
14         if (!errordiv) {
15             var badgerror = Y.Node.create(errorhtml);
16             badgetable.insert(badgerror, 'before');
17         }
18     }
21 /**
22  * Check if website is externally accessible from the backpack.
23  */
24 function check_site_access() {
25     var add = Y.one('#check_connection');
27     var callback = {
28             method: "GET",
29             on: {
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');
35                             }
36                         },
37                 failure: function(o) { }
38             }
39         };
41     Y.use('io-base', function(Y) {
42         Y.io('ajax.php', callback);
43     });
45     return false;
49  * Update the status indicator to show connection progress.
50  */
51 function badges_set_connection_progress(status) {
52     switch (status) {
53     case 'connecting':
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 + '&nbsp;<img src="'+imageurl+'" width="16" height="16" alt="'+connecting+'"/>');
57         Y.one('#connection-status').removeClass('notconnected').addClass('connecting').setHTML(loading);
58         break;
59     case 'notconnected':
60         var notconnected = M.util.get_string('notconnected', 'badges');;
61         Y.one('#connection-status').removeClass('connecting').addClass('notconnected').setHTML(notconnected);
62         break;
63     default:
64         // Unknown status, do nothing.
65     }
69  * Print an error message at the top of the page.
70  */
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.
78  */
79 function badges_handle_assertion(assertion) {
81     if (!assertion) {
82         var noassertionstr = M.util.get_string('error:noassertion', 'badges');
83         badges_set_error_message('error:backpackloginfailed', noassertionstr);
84         return;
85     }
87     badges_set_connection_progress('connecting');
89     Y.io("backpackconnect.php", {
90         method: "POST",
91         data: "assertion="+assertion+"&sesskey="+M.cfg.sesskey,
92         on: {
93             success: function (id, result) {
94                 // Reload page to display connected email address.
95                 window.location.href = "mybackpack.php";
96             },
97             failure: function (id, result) {
98                 try {
99                     var parsedResponse = Y.JSON.parse(result.response);
100                 } catch (e) {
101                     badges_set_connection_progress('notconnected');
102                     var badjsonstr = M.util.get_string('error:badjson', 'badges');
103                     badges_set_error_message('error:backpackloginfailed', badjsonstr);
104                     return;
105                 }
106                 badges_set_connection_progress('notconnected');
107                 badges_set_error_message('error:backpackloginfailed', parsedResponse.reason);
108                 return;
109             }
110         }
111     });
115  * Create and bind the persona login button.
116  */
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);
128     });