diff --git a/badges/backpack.js b/badges/backpack.js index 68e736bee08..98678e8f29e 100644 --- a/badges/backpack.js +++ b/badges/backpack.js @@ -2,7 +2,20 @@ * Push badges to backpack. */ function addtobackpack(event, args) { - OpenBadges.issue([args.assertion], function(errors, successes) { }); + var badgetable = Y.one('#issued-badge-table'); + var errordiv = Y.one('#addtobackpack-error'); + var errortext = M.util.get_string('error:backpackproblem', 'badges'); + var errorhtml = '
' + errortext + '
'; + + if (typeof OpenBadges !== 'undefined') { + OpenBadges.issue([args.assertion], function(errors, successes) { }); + } else { + // Add error div if it doesn't exist yet. + if (!errordiv) { + var badgerror = Y.Node.create(errorhtml); + badgetable.insert(badgerror, 'before'); + } + } } /** diff --git a/badges/badge.php b/badges/badge.php index cd90239502c..339b147313e 100644 --- a/badges/badge.php +++ b/badges/badge.php @@ -56,10 +56,8 @@ if (isloggedin()) { navigation_node::override_active_url($url); } -// TODO: Better way of pushing badges to Mozilla backpack? -if (!empty($CFG->badges_allowexternalbackpack)) { - $PAGE->requires->js(new moodle_url('http://backpack.openbadges.org/issuer.js'), true); -} +// Include JS files for backpack support. +badges_setup_backpack_js(); echo $OUTPUT->header(); diff --git a/badges/mybadges.php b/badges/mybadges.php index 46f4c1dce2d..d944e736beb 100644 --- a/badges/mybadges.php +++ b/badges/mybadges.php @@ -90,11 +90,8 @@ $PAGE->set_title($title); $PAGE->set_heading($title); $PAGE->set_pagelayout('mydashboard'); -// TODO: Better way of pushing badges to Mozilla backpack? -if (!empty($CFG->badges_allowexternalbackpack)) { - $PAGE->requires->js(new moodle_url('http://backpack.openbadges.org/issuer.js'), true); - $PAGE->requires->js('/badges/backpack.js', true); -} +// Include JS files for backpack support. +badges_setup_backpack_js(); $output = $PAGE->get_renderer('core', 'badges'); $badges = badges_get_user_badges($USER->id); diff --git a/badges/renderer.php b/badges/renderer.php index e6e9d2d6fed..f6abcb699cd 100644 --- a/badges/renderer.php +++ b/badges/renderer.php @@ -282,6 +282,7 @@ class core_badges_renderer extends plugin_renderer_base { $today = strtotime($today_date); $table = new html_table(); + $table->id = 'issued-badge-table'; $imagetable = new html_table(); $imagetable->attributes = array('class' => 'clearfix badgeissuedimage'); @@ -294,11 +295,13 @@ class core_badges_renderer extends plugin_renderer_base { $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1; if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today) && badges_user_has_backpack($USER->id)) { $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash)); + $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false))); $attributes = array( - 'type' => 'button', - 'value' => get_string('addtobackpack', 'badges'), - 'onclick' => 'OpenBadges.issue(["' . $assertion->out(false) . '"], function(errors, successes) { })'); + 'type' => 'button', + 'id' => 'addbutton', + 'value' => get_string('addtobackpack', 'badges')); $tobackpack = html_writer::tag('input', '', $attributes); + $this->output->add_action_handler($action, 'addbutton'); $imagetable->data[] = array($tobackpack); } } @@ -459,7 +462,7 @@ class core_badges_renderer extends plugin_renderer_base { get_string('downloadall'), 'POST', array('class' => 'activatebadge')); // Local badges. - $localhtml = html_writer::start_tag('fieldset', array('class' => 'generalbox')); + $localhtml = html_writer::start_tag('fieldset', array('id' => 'issued-badge-table', 'class' => 'generalbox')); $localhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges')); if ($badges->badges) { diff --git a/lang/en/badges.php b/lang/en/badges.php index 437c5dd1ecf..9e5b147dabe 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -198,6 +198,7 @@ $string['donotaward'] = 'Currently, this badge is not active, so it cannot be aw $string['editsettings'] = 'Edit settings'; $string['enablebadges'] = 'Enable badges'; $string['error:backpacknotavailable'] = 'Your site is not accessible from the Internet, so any badges issued from this site cannot be verified by external backpack services.'; +$string['error:backpackproblem'] = 'There was a problem connecting to your backpack service provider. Please try again later.'; $string['error:cannotact'] = 'Cannot activate the badge. '; $string['error:cannotawardbadge'] = 'Cannot award badge to a user.'; $string['error:clone'] = 'Cannot clone the badge.'; diff --git a/lib/badgeslib.php b/lib/badgeslib.php index cf517646ced..98f30e6544b 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -1350,3 +1350,19 @@ function badges_handle_course_deletion($courseid) { $DB->update_record('badge', $toupdate); } } + +/** + * Loads JS files required for backpack support. + * + * @uses $CFG, $PAGE + * @return void + */ +function badges_setup_backpack_js() { + global $CFG, $PAGE; + if (!empty($CFG->badges_allowexternalbackpack)) { + $PAGE->requires->string_for_js('error:backpackproblem', 'badges'); + $protocol = (strpos($CFG->wwwroot, 'https://') === 0) ? 'https://' : 'http://'; + $PAGE->requires->js(new moodle_url($protocol . 'backpack.openbadges.org/issuer.js'), true); + $PAGE->requires->js('/badges/backpack.js', true); + } +}