Permalink
Browse files

MDL-38837 badges: Check for http or https in badges backpack support

  • Loading branch information...
1 parent c2ad901 commit 2e64d85668c3dfaea320b3221613dd3669d4f556 @ybozhko ybozhko committed Jul 16, 2013
Showing with 42 additions and 14 deletions.
  1. +14 −1 badges/backpack.js
  2. +2 −4 badges/badge.php
  3. +2 −5 badges/mybadges.php
  4. +7 −4 badges/renderer.php
  5. +1 −0 lang/en/badges.php
  6. +16 −0 lib/badgeslib.php
View
@@ -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 = '<div id="addtobackpack-error" class="box boxaligncenter notifyproblem">' + errortext + '</div>';
+
+ 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');
+ }
+ }
}
/**
View
@@ -56,10 +56,8 @@
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();
View
@@ -90,11 +90,8 @@
$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);
View
@@ -282,6 +282,7 @@ protected function render_issued_badge(issued_badge $ibadge) {
$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 @@ protected function render_issued_badge(issued_badge $ibadge) {
$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 @@ protected function render_badge_user_collection(badge_user_collection $badges) {
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) {
View
@@ -198,6 +198,7 @@
$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.';
View
@@ -1348,3 +1348,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);
+ }
+}

0 comments on commit 2e64d85

Please sign in to comment.