Skip to content

Commit

Permalink
Merge pull request #27 from YourAcclaim/feature/issue-badges-from-adh…
Browse files Browse the repository at this point in the history
…oc-task-scheduler

Feature/issue badges from adhoc task scheduler
  • Loading branch information
yancyribbens authored Aug 31, 2019
2 parents ea1d5e6 + 1600fd0 commit 84939c2
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace block_acclaim\event;
defined('MOODLE_INTERNAL') || die();

class block_acclaim_issue_badge extends \core\event\base {
class block_acclaim_create_pending_badge extends \core\event\base {
protected function init()
{
$this->data['crud'] = 'c'; // c(reate), r(ead), u(pdate), d(elete)
Expand Down
18 changes: 2 additions & 16 deletions classes/group_observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,9 @@
require_once($CFG->dirroot . '/blocks/acclaim/lib.php');

class group_observers {
public static function block_acclaim_issue_badge($event)
public static function block_acclaim_create_pending_badge($event)
{
global $DB;
$course = block_acclaim_get_block_course($event->courseid);
$expires_timestamp = "";

if($course->expiration){
$expires_timestamp = $course->expiration;
}

$data = block_acclaim_create_data_array($event, $course->badgeid, $expires_timestamp);
$url = block_acclaim_get_issue_badge_url();
$token = block_acclaim_get_request_token();
$return_code = block_acclaim_issue_badge_request($data, $url, $token);
if($return_code != 201){
error_log("failed to issue badge, return code: ".$return_code);
}
block_acclaim_create_pending_badge($event);
}
}

Expand Down
44 changes: 44 additions & 0 deletions classes/task/issue_badges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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/>.

/**
* Create request to issue a new credential
*
* @package block_acclaim
* @copyright 2014 Yancy Ribbens <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_acclaim\task;

defined('MOODLE_INTERNAL') || die();

class issue_badges extends \core\task\scheduled_task {
public function get_name() {
return get_string('issuecredentials', 'block_acclaim');
}

public function execute() {
global $CFG;
require_once($CFG->libdir. '/filelib.php');
require_once($CFG->dirroot . '/blocks/acclaim/lib.php');
$url = block_acclaim_get_issue_badge_url();
$token = block_acclaim_get_request_token();
$curl = new \curl;
block_acclaim_issue_badge($curl, time(), $url, $token);
}
}
2 changes: 1 addition & 1 deletion db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
$observers = array(
array(
'eventname' => '\core\event\course_completed',
'callback' => '\block_acclaim\group_observers::block_acclaim_issue_badge',
'callback' => '\block_acclaim\group_observers::block_acclaim_create_pending_badge',
),
);
17 changes: 16 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="block_acclaim" COMMENT="Map course contents to an Acclaim badge_template.">
<TABLE NAME="block_acclaim_courses" COMMENT="Map course contents to an Acclaim badge_template.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Course ID"/>
Expand All @@ -19,5 +19,20 @@
<INDEX NAME="courseid" UNIQUE="true" FIELDS="courseid"/>
</INDEXES>
</TABLE>

<TABLE NAME="block_acclaim_pending_badges" COMMENT="Store badges to be issued upon course complete">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="badgetemplateid" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Badge Template ID"/>
<FIELD NAME="firstname" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Issued to First Name"/>
<FIELD NAME="lastname" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Issued to Last Name"/>
<FIELD NAME="recipientemail" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Recipient Email "/>
<FIELD NAME="expiration" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Badge Expiration"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>

</TABLES>
</XMLDB>
37 changes: 37 additions & 0 deletions db/tasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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/>.

/**
* Scheduled task to issue badges
*
* @package block_acclaim
* @copyright 2014 Yancy Ribbens <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$tasks = [
[
'classname' => 'block_acclaim\task\issue_badges',
'blocking' => 0,
'minute' => '*/5',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
],
];
2 changes: 1 addition & 1 deletion lang/en/block_acclaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
$string['acclaim'] = 'Acclaim';
$string['acclaim:addinstance'] = 'Add a new Acclaim block';
$string['acclaim:myaddinstance'] = 'Add a new Acclaim block to the My Moodle page';
$string['privacy:metadata'] = 'The Acclaim block only only stores course and badge_template details';
$string['privacy:metadata'] = 'The Acclaim block only only stores course and badge_template details. User data is stored in a temporary table and removed once issued';
87 changes: 66 additions & 21 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function block_acclaim_get_badge_info($course_id,$field)
global $DB;
$return_val = "";

$course = $DB->get_record('block_acclaim', array('courseid' => $course_id));
$course = $DB->get_record('block_acclaim_courses', array('courseid' => $course_id));

if(!empty($course)){
$return_val = $course->$field;
Expand All @@ -66,18 +66,18 @@ function block_acclaim_get_badge_info($course_id,$field)
function block_acclaim_get_block_course($course_id)
{
global $DB;
$course = $DB->get_record('block_acclaim', array('courseid' => $course_id), '*', MUST_EXIST);
$course = $DB->get_record('block_acclaim_courses', array('courseid' => $course_id), '*', MUST_EXIST);
return $course;
}

function block_acclaim_write_badge_to_issue($fromform)
function block_acclaim_set_course_badge_template($fromform)
{
global $DB;

$fromform = block_acclaim_update_form_with_badge_name($fromform);
$DB->delete_records('block_acclaim', array('courseid' => $fromform->courseid));
$DB->delete_records('block_acclaim_courses', array('courseid' => $fromform->courseid));

return $DB->insert_record('block_acclaim', $fromform);
return $DB->insert_record('block_acclaim_courses', $fromform);
}

function block_acclaim_get_issue_badge_url()
Expand Down Expand Up @@ -124,33 +124,78 @@ function block_acclaim_update_form_with_badge_name($fromform)
return $fromform;
}

function block_acclaim_create_data_array($event,$badge_id,$timestamp)
function block_acclaim_create_pending_badge($event)
{
global $DB;
$course = block_acclaim_get_block_course($event->courseid);
$pending_badge = block_acclaim_create_pending_badge_obj($event, $course);
$DB->insert_record('block_acclaim_pending_badges', $pending_badge);
}

function block_acclaim_create_pending_badge_obj($event, $course)
{
$user_id = $event->relateduserid;
$badge_template_id = $course->badgeid;
$course_id = $event->courseid;
$user = block_acclaim_return_user($user_id);
$firstname = $user->firstname;
$lastname = $user->lastname;
$email = $user->email;
$expires_at = block_acclaim_convert_time_stamp($timestamp);
$date_time = block_acclaim_convert_time_stamp(time());
$expires_at = $course->expiration;

$data = array(
'badge_template_id' => $badge_id,
'issued_to_first_name' => $firstname,
'issued_to_last_name' => $lastname,
'expires_at' => $expires_at,
'recipient_email' => $email,
'issued_at' => $date_time
);
$pending_badge = new stdClass();
$pending_badge->badgetemplateid = $badge_template_id;
$pending_badge->firstname = $firstname;
$pending_badge->lastname = $lastname;
$pending_badge->expiration = $expires_at;
$pending_badge->recipientemail = $email;

return $data;
return $pending_badge;
}

function block_acclaim_issue_badge_request($data, $url, $username)
{
$curl = new curl;
$curl->post($url, $data, array( "CURLOPT_USERPWD" => $username . ":" ));
function block_acclaim_issue_badge($curl, $time, $url, $token){
global $DB;

$datetime = block_acclaim_convert_time_stamp($time);

$pending_badges = $DB->get_records('block_acclaim_pending_badges');

foreach ($pending_badges as &$badge) {

$payload = [
'badge_template_id' => $badge->badgetemplateid,
'issued_to_first_name' => $badge->firstname,
'issued_to_last_name' => $badge->lastname,
'recipient_email' => $badge->recipientemail,
'issued_at' => $datetime
];

if($badge->expiration){
$payload['expires_at'] =
block_acclaim_convert_time_stamp($badge->expiration);
}

$curl->post(
$url, $payload, array( "CURLOPT_USERPWD" => $token. ":" )
);

if ($curl->info["http_code"] == 201) {
// The badge has been issued so we remove it from pending.
$DB->delete_records('block_acclaim_pending_badges', array('id' => $badge->id));
} elseif ($curl->info["http_code"] == 422) {
// Acclaim can not issue the badge so we remove this from pending
// so it will not try again. This could happen for example if the
// user already has been issued a badge.
error_log(print_r($curl->response, true));
$DB->delete_records('block_acclaim_pending_badges', array('id' => $badge->id));
} else {
// some other issue is preventing the badge from being issued
// for example site down or token incorrectly entered. The
// record is left as pending to try again in the future.
error_log(print_r($curl->response, true));
}
};

return $curl->info["http_code"];
}

Expand Down
Loading

0 comments on commit 84939c2

Please sign in to comment.