Commit 42c171f9 authored by Son Nguyen's avatar Son Nguyen Committed by Robert Lyon

Enhance the openbadgedisplayer plugin. Bug 1536393

Allow loading openbadgedisplayer block via ajax.
Dynamically load badge groups from sources.
Cache badge details in database for one day if $fromcache is true.

behatnotneeded

Change-Id: I36c8054fd6daf7ca1fcf1fe3a22672c9eb009c6e
parent 9a6a59e9
<?php
/**
*
* @package mahara
* @subpackage blocktype-openbadgedisplayer
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('blocktype', 'openbadgedisplayer');
$host = param_variable('host', null);
$email = param_variable('email', null);
if (!isset($host) || !isset($email)) {
json_reply('local', get_string('parameterexception', 'error'));
}
// Make sure the email belongs to the current user
$emails = get_column('artefact_internal_profile_email', 'email', 'owner', $USER->id, 'verified', 1);
if (!isset($emails) || !in_array($email, $emails)) {
json_reply('local', get_string('accessdeniedbadge', 'error'));
}
$uid = PluginBlocktypeOpenbadgedisplayer::get_backpack_id($host, $email);
json_reply(false, array(
'host' => $host,
'hosttitle' => get_string('title_' . $host, 'blocktype.openbadgedisplayer'),
'uid' => $uid,
'badgegroups' => PluginBlocktypeOpenbadgedisplayer::get_badgegroupnames($host, $uid),
));
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20060926" COMMENT="XMLDB file for core Mahara tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="blocktype_openbadgedisplayer_data">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" SEQUENCE="true" NOTNULL="true" />
<FIELD NAME="host" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="uid" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="badgegroupid" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="name" TYPE="text" NOTNULL="false" />
<FIELD NAME="html" TYPE="text" NOTNULL="false" />
<FIELD NAME="lastupdate" TYPE="datetime" NOTNULL="false" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
</KEYS>
<INDEXES>
<INDEX NAME="hostuidix" UNIQUE="false" FIELDS="host,uid"/>
<INDEX NAME="hostuidbadgegroupidix" UNIQUE="true" FIELDS="host,uid,badgegroupid"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
......@@ -31,5 +31,24 @@ function xmldb_blocktype_openbadgedisplayer_upgrade($oldversion = 0) {
}
}
if ($oldversion < 2016030200) {
// Add a new table blocktype_openbadgedisplayer_data for storing prefetch badges
$table = new XMLDBTable('blocktype_openbadgedisplayer_data');
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$table->addFieldInfo('host', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
$table->addFieldInfo('uid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL);
$table->addFieldInfo('badgegroupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL);
$table->addFieldInfo('name', XMLDB_TYPE_TEXT);
$table->addFieldInfo('html', XMLDB_TYPE_TEXT);
$table->addFieldInfo('lastupdate', XMLDB_TYPE_DATETIME);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addIndexInfo('hostuidix', XMLDB_INDEX_NOTUNIQUE, array('host', 'uid'));
$table->addIndexInfo('hostuidbadgegroupidix', XMLDB_INDEX_UNIQUE, array('host', 'uid', 'badgegroupid'));
create_table($table);
}
return true;
}
\ No newline at end of file
/**
* Asynchronous loading badges
*
* @package mahara
* @subpackage blocktype-openbadgedisplayer
* @author Discendum Oy
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*/
/* pieform_element_checkboxes_get_headdata() includes the javascript
needed by the "Select all/none" -links. That function isn't called
when the config form is rendered, so let's just copy the code here
and add it to window scope.*/
if (typeof pieform_element_checkboxes_update === 'undefined') {
window.pieform_element_checkboxes_update = function (p, v) {
forEach(getElementsByTagAndClassName('input', 'checkboxes', p), function(e) {
if (!e.disabled) {
e.checked = v;
}
});
if (typeof formchangemanager !== 'undefined') {
var form = jQuery('div#' + p).closest('form')[0];
formchangemanager.setFormState(form, FORM_CHANGED);
}
};
}
var badgegroups_hosts = JSON.parse(jQuery("input#instconf_hosts").val());
var badgegroups_emails = JSON.parse(jQuery("input#instconf_emails").val());
var selectedbadgegroups = JSON.parse(jQuery("input#instconf_selectedbadgegroups").val());
if ((badgegroups_hosts instanceof Array && badgegroups_hosts.length >= 1)
&& (badgegroups_emails instanceof Array && badgegroups_emails.length >= 1)) {
var count=0;
jQuery("div#instconf_loadinginfo_container > p.alert").removeClass('hidden');
for (var i=0; i < badgegroups_hosts.length; i++) {
var h = badgegroups_hosts[i];
for (var j=0; j < badgegroups_emails.length; j++) {
var e = badgegroups_emails[j];
var params = {'host': h, 'email': e};
count++;
/* Fetching the badge info via ajax and render the pieform checkbox element */
sendjsonrequest(config['wwwroot'] + '/blocktype/openbadgedisplayer/badgegroupnames.json.php', params, 'POST', function(data) {
if (!jQuery.isEmptyObject(data.badgegroups)) {
var htmlstr =
'<div id="instconf_' + data.host + '_container" class="checkboxes form-group">' +
'<span class="pseudolabel">' + data["hosttitle"] + '</span>' +
'<div class="btn-group">' +
'<a href="" class="btn btn-default btn-xs" onclick="pieform_element_checkboxes_update(\'instconf_' + data["host"] + '_container\', true); return false;">Select all</a>' +
'<a href="" class="btn btn-default btn-xs" onclick="pieform_element_checkboxes_update(\'instconf_' + data["host"] + '_container\', false); return false;">Select none</a>&nbsp;' +
'</div>';
for (var badgegroupid in data.badgegroups) {
var badgegroupname = data.badgegroups[badgegroupid];
var checkboxvalue = data["host"] + ':' + data["uid"] + ':' + badgegroupid;
var checkboxid = data["host"] + '_' + data["uid"] + '_' + badgegroupid;
var selected = '';
if (jQuery.inArray(checkboxvalue, selectedbadgegroups) != -1) {
selected = 'checked';
}
htmlstr +=
'<div class="checkboxes-option checkbox">' +
'<input type="checkbox" id="instconf_' + checkboxid + '"name="' + data["host"] + '[]" value="' + checkboxvalue + '" ' + selected + ' class="checkboxes">' +
'<label class="checkbox" for="instconf_' + checkboxid + '">' +
'<span class="accessible-hidden sr-only">' + data["hosttitle"] + ': </span>' +
badgegroupname +
'</label>' +
'</div>';
}
htmlstr +=
'<div class="cl"></div>' +
'</div>';
jQuery("div#instconf_loadinginfo_container > div").append(htmlstr);
count--;
if (count == 0) {
jQuery("div#instconf_loadinginfo_container > p.alert").addClass('hidden');
}
}
});
}
}
}
......@@ -64,3 +64,4 @@ $string['title_backpack'] = 'Mozilla Backpack';
$string['title_passport'] = 'Open Badge Passport';
$string['fetchingbadges'] = 'Fetching entries. This may take a while.';
This diff is collapsed.
......@@ -20,6 +20,7 @@
* @package mahara
* @subpackage blocktype-openbadgedisplayer
* @author Discendum Oy
* @author Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2012 Discedum Oy http://discendum.com
* @copyright (C) 2011 Catalyst IT Ltd http://catalyst.net.nz
......@@ -29,5 +30,5 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2015062301;
$config->release = '1.0.4';
$config->version = 2016030200;
$config->release = '1.0.5';
......@@ -91,6 +91,7 @@ $string['accessdenied'] = 'Access denied';
$string['accessdeniedobjection'] = 'Access denied. The objection has already been resolved by another administrator.';
$string['accessdeniedexception'] = 'You do not have access to view this page.';
$string['accessdeniednourlsecret'] = 'You do not have access to this functionality. Please provide the value for "urlsecret" from your config.php file as part of the URL.';
$string['accessdeniedbadge'] = 'You do not have access to view this badge.';
$string['viewnotfoundexceptiontitle'] = 'Page not found';
$string['viewnotfoundexceptionmessage'] = 'You tried to access a page that does not exist.';
......
......@@ -4405,5 +4405,12 @@ function xmldb_core_upgrade($oldversion=0) {
change_field_precision($table, $field);
}
if ($oldversion < 2016033100) {
log_debug('Upgrade openbadgedisplayer plugin');
if ($data = check_upgrades('blocktype.openbadgedisplayer')) {
upgrade_plugin($data);
}
}
return $status;
}
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2016032900;
$config->version = 2016033100;
$config->series = '16.04';
$config->release = '16.04dev';
$config->minupgradefrom = 2012080604;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment