Commit 10bd8af0 authored by Plugins bot's avatar Plugins bot

PLUGIN-22415 local_obf: cibot precheck request

parent 535523f2
This diff is collapsed.
Open Badge Factory -plugin
=================
Open Badge Factory is a cloud platform that provides the tools your organization needs to implement a meaningful and sustainable Open Badges system.
With the local_obf plugin you can issue Open Badges created in Open Badge Factory. To use the plugin, you need an account on [https://openbadgefactory.com](https://openbadgefactory.com) (You can register for free, see [https://openbadgefactory.com/faq](https://openbadgefactory.com/faq) for details about different service levels).
For developers
--------
[See the project README](../../../README.md)
How to install
--------------
Moodle 2.7, 2.9, 3.0, 3.1 and up:
1. Install the zip via Moodle's plugin page. Select "local" as the type of the plugin. (alternative: unzip to moodle's local subdirectory)
2. Update the database using the notifications page
3. Complete the [Post install steps](README.md#post-install)
Totara 11.0 and greater
Totara Learn does not include an add-on installer, all additional plugins must be installed manually by server administrators.
1. Download plugin from https://moodle.org/plugins/local_obf
2. Unzip the file into the Totara installation directory.
3. By using a site administrator account, go to Site administration → Notifications and upgrade Totara database
4. Complete the [Post install steps](README.md#post-install)
Post install
------------------
To connect to Open Badge Factory, the plugin needs a request token or API key.
To generate the required API key, log in to Open Badge Factory. When logged in, navigate to `Admin tools > API key`. On the API key -page click on `Generate certificate signing request token`.
Copy the generated token into OBF Moodle plugin settings, in `Site administration > Open Badges > Settings`.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Script for fetching user's badges via Ajax.
*
* @package local_obf
* @copyright 2013-2015, Discendum Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/class/backpack.php');
require_login(); // TODO: Handle login requirement more gracefully for more useful error messages?
$userid = required_param('userid', PARAM_INT);
$provider = optional_param('provider', 0, PARAM_INT);
$context = context_user::instance($userid);
if ((int)$USER->id === $userid) {
require_capability('local/obf:viewownbackpack', $context);
} else {
// TODO: more specific capabilities?
require_capability('local/obf:viewbackpack', $context);
}
$backpack = obf_backpack::get_instance_by_userid($userid, $DB, $provider);
if ($backpack === false || count($backpack->get_group_ids()) == 0) {
die(json_encode(array('error' => 'nogroups')));
}
echo json_encode($backpack->get_assertions_as_array());
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin configuration page.
*
* @package local_obf
* @copyright 2013-2015, Discendum Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once(__DIR__ . '/class/backpack.php');
require_once(__DIR__ . '/form/backpackconfig.php');
$context = context_system::instance();
$msg = optional_param('msg', '', PARAM_TEXT);
$action = optional_param('action', 'list', PARAM_TEXT);
$providerid = optional_param('id', 0, PARAM_NUMBER);
$urlparams = $action == 'list' ? array() : array('action' => $action);
$url = new moodle_url('/local/obf/backpackconfig.php', $urlparams);
require_login();
require_capability('local/obf:configure', $context);
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_pagelayout('admin');
$content = '';
$backpacks = array();
if (!empty($msg)) {
$content .= $OUTPUT->notification($msg);
}
foreach (obf_backpack::get_providers() as $provider) {
$existing = obf_backpack::get_instance($USER, $provider);
$backpacks[] = $existing ? $existing : new obf_backpack(null, $provider);
}
function local_obf_backpackconfig_update_backpack_from_form($form, $backpack, &$content) {
if (!$form->is_cancelled()) {
if ($data = $form->get_data()) {
if (!empty($data->deletebutton)) {
obf_backpack::delete_provider_record($backpack);
if (obf_backpack::save_provider_record($backpack)) {
$redirecturl = new moodle_url('/local/obf/backpackconfig.php');
redirect($redirecturl);
}
} else {
$backpack = (object) array_merge((array) $backpack, (array) $data);
if (obf_backpack::save_provider_record($backpack)) {
$redirecturl = new moodle_url('/local/obf/backpackconfig.php');
redirect($redirecturl);
}
}
} else {
$form->set_data($backpack);
}
$content .= $form->render();
} else {
$redirecturl = new moodle_url('/local/obf/backpackconfig.php');
redirect($redirecturl);
}
}
switch ($action) {
case 'list':
$content .= $PAGE->get_renderer('local_obf')->print_heading('personalbadgecloudservices');
$content .= $PAGE->get_renderer('local_obf')->render_backpack_provider_list($backpacks);
break;
case 'create':
$backpack = new stdClass();
$formurl = new moodle_url('/local/obf/backpackconfig.php', array('action' => 'create'));
$form = new obf_backpack_config($formurl, array('backpack' => $backpack));
local_obf_backpackconfig_update_backpack_from_form($form, $backpack, $content);
break;
case 'edit':
$backpack = obf_backpack::get_provider_record($providerid);
$formurl = new moodle_url('/local/obf/backpackconfig.php', array('action' => 'edit', 'id' => $providerid));
$form = new obf_backpack_config($formurl, array('backpack' => $backpack));
local_obf_backpackconfig_update_backpack_from_form($form, $backpack, $content);
$providername = isset($backpack->fullname) ? $backpack->fullname : '';
$params = array(array('class' => 'delete',
'question' => get_string('confirmdelete', 'local_obf', $providername)));
$PAGE->requires->yui_module('moodle-local_obf-submitconfirm',
'M.local_obf.init_submitconfirm', $params);
break;
}
echo $OUTPUT->header();
echo $content;
echo $OUTPUT->footer();
\ No newline at end of file
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Page for displaying content closely related to badges.
*
* @package local_obf
* @copyright 2013-2015, Discendum Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/class/badge.php');
require_once($CFG->libdir . '/adminlib.php');
$badgeid = optional_param('id', '', PARAM_ALPHANUM);
$action = optional_param('action', 'list', PARAM_ALPHANUM);
$courseid = optional_param('courseid', null, PARAM_INT);
$criteriatype = optional_param('criteriatype', null, PARAM_INT);
$message = optional_param('msg', '', PARAM_TEXT);
$context = empty($courseid) ? context_system::instance() : context_course::instance($courseid);
$url = new moodle_url('/local/obf/badge.php', array('action' => $action));
$badge = empty($badgeid) ? null : obf_badge::get_instance($badgeid);
if (!empty($badgeid)) {
$url->param('id', $badgeid);
}
// Site context.
if (empty($courseid)) {
require_login();
} else { // Course context.
$url->param('courseid', $courseid);
require_login($courseid);
}
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_pagelayout(empty($courseid) ? 'admin' : 'course');
$PAGE->set_title(get_string('obf', 'local_obf'));
$PAGE->add_body_class('local-obf');
$content = '';
$hasissuecapability = has_capability('local/obf:issuebadge', $context);
switch ($action) {
// Show issuance history.
case 'history':
require_capability('local/obf:viewhistory', $context);
$page = optional_param('page', 0, PARAM_INT);
try {
$client = obf_client::get_instance();
$content .= $PAGE->get_renderer('local_obf')->print_badge_info_history(
$client, $badge, $context, $page);
} catch (Exception $e) {
$content .= $OUTPUT->notification($e->getMessage());
}
break;
// Show the list of badges.
case 'list':
require_capability('local/obf:viewallbadges', $context);
try {
$badges = obf_badge::get_badges();
if ($context instanceof context_system) {
$content .= $PAGE->get_renderer('local_obf')->render_badgelist($badges,
$hasissuecapability, $context, $message);
} else {
$content .= $PAGE->get_renderer('local_obf')->render_badgelist_course($badges,
$hasissuecapability, $context, $message);
}
} catch (Exception $e) {
$content .= $OUTPUT->notification($e->getMessage(), 'notifyproblem');
}
break;
// Display badge info.
case 'show':
require_capability('local/obf:viewdetails', $context);
$client = obf_client::get_instance();
$page = optional_param('page', 0, PARAM_INT);
$show = optional_param('show', 'details', PARAM_ALPHANUM);
$baseurl = new moodle_url('/local/obf/badge.php',
array('action' => 'show', 'id' => $badgeid));
if ($context instanceof context_system) {
navigation_node::override_active_url(new moodle_url('/local/obf/badge.php',
array('action' => 'list')));
$PAGE->navbar->add($badge->get_name(), $baseurl);
} else {
navigation_node::override_active_url(new moodle_url('/local/obf/badge.php',
array('action' => 'list', 'courseid' => $courseid)));
$coursebadgeurl = clone $baseurl;
$coursebadgeurl->param('courseid', $courseid);
$PAGE->navbar->add($badge->get_name(), $coursebadgeurl);
}
$renderer = $PAGE->get_renderer('local_obf', 'badge');
$content .= $PAGE->get_renderer('local_obf')->render_badge_heading($badge,
$context);
switch ($show) {
// Email template.
case 'email':
require_capability('local/obf:configure', $context);
$emailurl = new moodle_url(
'/local/obf/badge.php', array('id' => $badge->get_id(),
'action' => 'show', 'show' => 'email'));
$PAGE->navbar->add(
get_string('badgeemail', 'local_obf'), $emailurl);
$form = new obf_email_template_form(
$emailurl, array('badge' => $badge));
$html = '';
if (!empty($message)) {
$html .= $OUTPUT->notification($message, 'notifysuccess');
}
if (!is_null($data = $form->get_data())) {
global $DB;
$email = is_null($badge->get_email()) ? new obf_email() : $badge->get_email();
$email->set_badge_id($badge->get_id());
$email->set_subject($data->emailsubject);
$email->set_body($data->emailbody);
$email->set_footer($data->emailfooter);
$email->set_link_text($data->emaillinktext);
$email->save($DB);
$redirecturl = clone $emailurl;
$redirecturl->param(
'msg', get_string('emailtemplatesaved', 'local_obf'));
redirect($redirecturl);
}
$html .= $form->render();
$content .= $renderer->page($badge, 'email', $html);
break;
// Badge details.
case 'details':
$taburl = clone $baseurl;
$taburl->param('show', $show);
if ($context instanceof context_system) {
$PAGE->navbar->add(
get_string('badge' . $show, 'local_obf'), $taburl);
}
$content .= $PAGE->get_renderer('local_obf')->page_badgedetails(
$client, $badge, $context, $show, $page, $message);
$content .= $PAGE->get_renderer('local_obf')->render_button($badge,
$context, 'issue');
break;
// Badge criteria.
case 'criteria':
$content .= $PAGE->get_renderer('local_obf')->page_badgedetails(
$client, $badge, $context, $show, $page, $message);
break;
// Badge issuance history.
case 'history':
$content .= $PAGE->get_renderer('local_obf')->page_badgedetails(
$client, $badge, $context, $show, $page, $message);
break;
}
break;
}
echo $OUTPUT->header();
echo $content;
echo $OUTPUT->footer();
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Page for blacklisting individual badges from being displayed on the profile page.
*
* @package local_obf
* @copyright 2013-2015, Discendum Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/class/blacklist.php');
require_once(__DIR__ . '/form/blacklist.php');
require_once(__DIR__ . '/class/user_preferences.php');
$error = optional_param('error', '', PARAM_TEXT);
$msg = optional_param('msg', '', PARAM_TEXT);
$action = optional_param('action', 'edit', PARAM_TEXT);
$context = context_system::instance();
require_login();
require_capability('local/obf:configureuser', $context);
$url = new moodle_url('/local/obf/blacklist.php', array('action' => $action));
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_pagelayout('standard');
$content = $OUTPUT->header();
$obfuserpreferences = new obf_user_preferences($USER->id);
$formurl = new moodle_url('/local/obf/blacklist.php', array('action' => 'update'));
$form = new obf_blacklist_form($formurl,
array('user' => $USER,
'blacklist' => new obf_blacklist($USER->id)));
switch ($action) {
case 'edit':
if (!empty($msg)) {
$content .= $OUTPUT->notification($msg, 'notifysuccess');
}
$content .= $PAGE->get_renderer('local_obf')->render_blacklistconfig($form, $error);
break;
case 'addbadge':
$badgeid = required_param('badgeid', PARAM_ALPHANUM);
require_sesskey();
$blacklist = new obf_blacklist($USER->id);
$blacklist->add_to_blacklist($badgeid);
$blacklist->save();
$redirecturl = $url;
$redirecturl->param('msg', get_string('blacklistsaved', 'local_obf'));
$redirecturl->param('action', 'edit');
cache_helper::invalidate_by_event('obf_blacklist_changed', array($USER->id));
redirect($redirecturl);
break;
case 'update':
if ($data = $form->get_data()) {
$newblacklist = property_exists($data, 'blacklist') ? array_keys(array_filter($data->blacklist)) : array();
$blacklist = new obf_blacklist($USER->id);
$blacklist->save($newblacklist);
cache_helper::invalidate_by_event('obf_blacklist_changed', array($USER->id));
$redirecturl = $url;
$redirecturl->param('msg', get_string('blacklistsaved', 'local_obf'));
$redirecturl->param('action', 'edit');
redirect($redirecturl);
}
break;
}
$content .= $OUTPUT->footer();
echo $content;
This diff is collapsed.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Collection of assertions.
* @package local_obf
* @copyright 2013-2015, Discendum Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/assertion.php');
require_once(__DIR__ . '/backpack.php');
require_once(__DIR__ . '/blacklist.php');
/**
* Represents a collection of events in OBF.
*
* @author olli
* @copyright 2013-2015, Discendum Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class obf_assertion_collection implements Countable, IteratorAggregate {
/**
* @var obf_assertion[] The assertions in this collection.
*/
private $assertions = array();
/**
* Assertion recipients mapped as Moodle users
*
* @var array
*/
private $users = array();
/**
* Class constructor.
*
* @param obf_assertion[] $assertions The assertions.
*/
public function __construct(array $assertions = array()) {
$this->assertions = $assertions;
}
/**
* Adds an assertion to this collection.
*
* @param obf_assertion $assertion The assertion.
*/
public function add_assertion(obf_assertion $assertion) {
$this->assertions[] = $assertion;
}
/**
* Returns an array representing this collection.
*
* @return array The array.
*/
public function toArray() {
$ret = array();
foreach ($this->assertions as $assertion) {
$ret[] = $assertion->toArray();
}
return $ret;
}
/**
* Merges two collections.
*
* @param obf_assertion_collection $collection The other collection.
*/
public function add_collection(obf_assertion_collection $collection) {
for ($i = 0; $i < count($collection); $i++) {
$assertion = $collection->get_assertion($i);
// Skip duplicates.
if (!$this->has_assertion($assertion)) {
$this->add_assertion($assertion);
}
}
}
/**
* Checks whether this collection contains $assertion.
*
* @param obf_assertion $assertion The assertion to search for.
* @return boolean Returns true if found, false otherwise.
*/
public function has_assertion(obf_assertion $assertion) {
for ($i = 0; $i < count($this->assertions); $i++) {
if ($this->get_assertion($i)->equals($assertion)) {
return true;
}
}
return false;
}
/**
* Returns an assertion from index $index.
*
* @param int $index
* @return obf_assertion
*/
public function get_assertion($index) {
if (!isset($this->assertions[$index])) {
throw new Exception("Invalid array index.");
}
return $this->assertions[$index];
}
/**
* Returns an array of Moodle-users that are related to selected assertion.
*
* @param obf_assertion $assertion The assertion.
* @return stdClass[] An array of Moodle's user objects.
*/
public function get_assertion_users(obf_assertion $assertion) {
global $DB;
if (count($this->users) === 0) {
$emails = array();
foreach ($this->assertions as $a) {
$emails = array_merge($emails, $a->get_recipients());
}
$this->users = $DB->get_records_list('user', 'email', $emails);
}
$ret = array();
// TODO: check number of SQL-queries performed in this loop.
foreach ($assertion->get_recipients() as $recipient) {
// Try to find the user by email.
if (($user = $this->find_user_by_email($recipient)) !== false) {
$ret[] = $user;
} else {
// ... and then try to find the user by backpack email.
$backpack = obf_backpack::get_instance_by_backpack_email($recipient);
$ret[] = $backpack === false ? $recipient : $DB->get_record('user',
array('id' => $backpack->get_user_id()));
}
}
return $ret;
}
/**
* Remove badges from collection, that match those defined in users blacklist.
* @param obf_blacklist $blacklist Blacklist object used for filtering.
* @return obf_assertion_collection $this
*/
public function apply_blacklist(obf_blacklist $blacklist) {
$badgeids = $blacklist->get_blacklist();
$assertions = array();
foreach ($this->assertions as $assertion) {
if (!in_array($assertion->get_badge()->get_id(), $badgeids)) {
$assertions[] = $assertion;
}
}
$this->assertions = $assertions;
return $this;
}
/**
* Tries to find the Moodle user by email from collection's cache.