2023-05-17 21:19:14 +02:00
|
|
|
<?php
|
|
|
|
namespace local_treestudyplan;
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
class badgeinfo {
|
|
|
|
private $badge; // Holds database record
|
|
|
|
|
|
|
|
private const STATUSINFO = [
|
|
|
|
BADGE_STATUS_INACTIVE => 'inactive',
|
|
|
|
BADGE_STATUS_ACTIVE => 'active',
|
|
|
|
BADGE_STATUS_INACTIVE_LOCKED => 'inactive',
|
|
|
|
BADGE_STATUS_ACTIVE_LOCKED => 'active',
|
|
|
|
BADGE_STATUS_ARCHIVED => 'archived',
|
|
|
|
];
|
|
|
|
private const LOCKEDINFO = [
|
|
|
|
BADGE_STATUS_INACTIVE => 0,
|
|
|
|
BADGE_STATUS_ACTIVE => 0,
|
|
|
|
BADGE_STATUS_INACTIVE_LOCKED => 1,
|
|
|
|
BADGE_STATUS_ACTIVE_LOCKED => 1,
|
|
|
|
BADGE_STATUS_ARCHIVED => 1, // We don't want to edit archived badges anyway....
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(\core_badges\badge $badge) {
|
|
|
|
global $DB;
|
|
|
|
$this->badge = $badge;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function name(){
|
|
|
|
return $this->badge->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function id_from_name($name){
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
return $DB->get_field("badge", "id", ['name' => $name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exists($id){
|
|
|
|
global $DB;
|
|
|
|
return is_numeric($id) && $DB->record_exists('badge', array('id' => $id));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function editor_structure($value=VALUE_REQUIRED){
|
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of badge'),
|
2023-06-03 00:01:18 +02:00
|
|
|
"infolink" => new \external_value(PARAM_TEXT, 'badge issue information link', VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"name" => new \external_value(PARAM_TEXT, 'badge name'),
|
|
|
|
"status" => new \external_value(PARAM_TEXT, 'badge status'),
|
|
|
|
"locked" => new \external_value(PARAM_TEXT, 'badge lock status'),
|
2023-06-03 00:01:18 +02:00
|
|
|
"criteria" => new \external_multiple_structure(new \external_value(PARAM_RAW, 'criteria text'),'badge criteria',VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"description"=> new \external_value(PARAM_TEXT, 'badge description'),
|
|
|
|
"imageurl" => new \external_value(PARAM_TEXT, 'url of badge image'),
|
|
|
|
],"Badge info",$value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function editor_model()
|
|
|
|
{
|
|
|
|
$context = ($this->badge->type == BADGE_TYPE_SITE) ? \context_system::instance() : \context_course::instance($this->badge->courseid);
|
|
|
|
// If the user is viewing another user's badge and doesn't have the right capability return only part of the data.
|
2023-06-03 00:01:18 +02:00
|
|
|
|
|
|
|
$criteria = [];
|
|
|
|
foreach($this->badge->get_criteria() as $bc){
|
|
|
|
$criteria[] = $bc->get_title()." ".$bc->get_details();
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
$model = [
|
|
|
|
'id' => $this->badge->id,
|
2023-06-03 00:01:18 +02:00
|
|
|
'infolink' => (new \moodle_url('/badges/overview.php', ['id' => $this->badge->id]))->out(false),
|
2023-05-17 21:19:14 +02:00
|
|
|
'name' => $this->badge->name,
|
|
|
|
'status' => self::STATUSINFO[$this->badge->status],
|
|
|
|
'locked' => self::LOCKEDINFO[$this->badge->status],
|
2023-06-03 00:01:18 +02:00
|
|
|
'criteria' => $criteria,
|
2023-05-17 21:19:14 +02:00
|
|
|
'description' => $this->badge->description,
|
|
|
|
'imageurl' => \moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $this->badge->id, '/','f1')->out(false),
|
|
|
|
];
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function user_structure($value=VALUE_REQUIRED)
|
|
|
|
{
|
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of badge'),
|
2023-06-03 00:01:18 +02:00
|
|
|
"infolink" => new \external_value(PARAM_TEXT, 'badge issue information link', VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"name" => new \external_value(PARAM_TEXT, 'badge name'),
|
2023-06-03 00:01:18 +02:00
|
|
|
"criteria" => new \external_multiple_structure(new \external_value(PARAM_RAW, 'criteria text'),'badge criteria',VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"description"=> new \external_value(PARAM_TEXT, 'badge description'),
|
|
|
|
"imageurl" => new \external_value(PARAM_TEXT, 'url of badge image'),
|
|
|
|
"issued" => new \external_value(PARAM_BOOL, 'badge is issued'),
|
2023-06-03 00:01:18 +02:00
|
|
|
"dateissued" => new \external_value(PARAM_TEXT, 'date the badge was issued',VALUE_OPTIONAL),
|
|
|
|
"dateexpire" => new \external_value(PARAM_TEXT, 'date the badge will expire',VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"uniquehash" => new \external_value(PARAM_TEXT, 'badge issue hash', VALUE_OPTIONAL),
|
|
|
|
"issuedlink" => new \external_value(PARAM_TEXT, 'badge issue information link', VALUE_OPTIONAL),
|
|
|
|
],"Badge info",$value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user_model($userid)
|
|
|
|
{
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$context = ($this->badge->type == BADGE_TYPE_SITE) ? \context_system::instance() : \context_course::instance($this->badge->courseid);
|
|
|
|
$issued = $this->badge->is_issued($userid);
|
|
|
|
|
|
|
|
// If the user is viewing another user's badge and doesn't have the right capability return only part of the data.
|
2023-06-03 00:01:18 +02:00
|
|
|
$criteria = [];
|
|
|
|
foreach($this->badge->get_criteria() as $bc){
|
|
|
|
$criteria[] = $bc->get_title()."".$bc->get_details();
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
$badge = [
|
|
|
|
'id' => $this->badge->id,
|
|
|
|
'name' => $this->badge->name,
|
|
|
|
'description' => $this->badge->description,
|
|
|
|
'imageurl' => \moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $this->badge->id, '/','f1')->out(false),
|
2023-06-03 00:01:18 +02:00
|
|
|
'criteria' => $criteria,
|
2023-05-17 21:19:14 +02:00
|
|
|
'issued' => $issued,
|
2023-06-03 00:01:18 +02:00
|
|
|
'infolink' => (new \moodle_url('/badges/overview.php', ['id' => $this->badge->id]))->out(false),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if($issued) {
|
|
|
|
$issueinfo = $DB->get_record('badge_issued', array('badgeid' => $this->badge->id, 'userid' => $userid));
|
2023-06-03 00:01:18 +02:00
|
|
|
$badge['dateissued'] = userdate($issueinfo->dateissued,"%e %b %G");
|
|
|
|
if($issueinfo->expiredate){
|
|
|
|
$badge['dateexpire'] = userdate($issueinfo->dateexpire,"%e %b %G");
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
$badge['uniquehash'] = $issueinfo->uniquehash;
|
2023-06-03 00:01:18 +02:00
|
|
|
$badge['issuedlink'] = (new \moodle_url('/badges/badge.php', ['hash' => $issueinfo->uniquehash]))->out(false);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $badge;
|
|
|
|
}
|
|
|
|
|
2023-06-16 13:49:47 +02:00
|
|
|
function count_issued(array $student_ids){
|
|
|
|
$issuecount = 0;
|
|
|
|
|
|
|
|
foreach($student_ids as $uid){
|
|
|
|
if($this->badge->is_issued($userid)){
|
|
|
|
$issuecount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $issuecount;
|
|
|
|
}
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|