View source
<?php
namespace Drupal\opigno_module;
use Drupal\group\Entity\Group;
class OpignoModuleBadges {
public static function opignoModuleSaveBadge($uid, $gid, $typology, $entity_id) {
$table_name = 'opigno_module_badges';
$badges = self::opignoModuleGetBadges($uid, $gid, $typology, $entity_id);
$keys = [
'uid' => $uid,
'gid' => $gid,
'entity_id' => $entity_id,
'typology' => $typology,
];
$fields = [
'uid' => $uid,
'gid' => $gid,
'entity_id' => $entity_id,
'typology' => $typology,
'badges' => $badges ? $badges + 1 : 1,
];
$query = \Drupal::database()
->merge($table_name)
->keys($keys)
->fields($fields);
$query
->execute();
}
public static function opignoModuleGetBadges($uid, $gid, $typology, $entity_id) {
$query = \Drupal::database()
->select('opigno_module_badges', 'mb')
->fields('mb', [
'badges',
])
->condition('uid', $uid)
->condition('gid', $gid)
->condition('typology', $typology)
->condition('entity_id', $entity_id);
$result = $query
->execute()
->fetchField();
return $result ? $result : FALSE;
}
public static function opignoModuleGetUserActiveBadgesModules($uid) {
$output = [];
$query = \Drupal::database()
->select('opigno_learning_path_step_achievements', 'sa');
$query
->join('opigno_module_field_data', 'omfd', 'omfd.id = sa.entity_id AND sa.typology = :typology', [
':typology' => 'Module',
]);
$query
->join('opigno_learning_path_achievements', 'pa', 'pa.gid = sa.gid');
$query
->join('media__field_media_image', 'mi', 'mi.entity_id = omfd.badge_media_image');
$query
->fields('sa', [
'entity_id',
'typology',
'name',
'score',
'status',
'time',
'completed',
]);
$query
->fields('omfd', [
'badge_name',
'badge_description',
'badge_criteria',
]);
$query
->fields('mi', [
'field_media_image_target_id',
]);
$query
->fields('pa', [
'gid',
]);
$query
->addField('pa', 'name', 'training');
$query
->condition('sa.typology', 'Module');
$query
->condition('sa.uid', $uid);
$query
->condition('omfd.badge_active', 1);
$query
->orderBy('sa.completed', 'DESC');
$query
->distinct();
$modules = $query
->execute()
->fetchAll();
if ($modules) {
$modules_keyed = [];
foreach ($modules as $module) {
$modules_keyed[$module->entity_id . '-' . $module->gid] = $module;
}
$modules = $modules_keyed;
}
$query = \Drupal::database()
->select('opigno_learning_path_step_achievements', 'sa');
$query
->join('opigno_learning_path_achievements', 'pa', 'pa.gid = sa.gid');
$query
->join('group__badge_active', 'ba', 'ba.entity_id = sa.entity_id');
$query
->join('group__badge_media_image', 'bmi', 'bmi.entity_id = sa.entity_id');
$query
->join('media__field_media_image', 'mi', 'mi.entity_id = bmi.badge_media_image_target_id');
$query
->fields('sa', [
'entity_id',
'typology',
'name',
'score',
'status',
'time',
'completed',
]);
$query
->fields('mi', [
'field_media_image_target_id',
]);
$query
->fields('pa', [
'gid',
]);
$query
->addField('pa', 'name', 'training');
$query
->condition('sa.typology', 'Course');
$query
->condition('sa.uid', $uid);
$query
->condition('ba.badge_active_value', 1);
$query
->orderBy('sa.completed', 'DESC');
$query
->distinct();
$courses = $query
->execute()
->fetchAll();
if (!empty($courses)) {
foreach ($courses as &$course) {
$entity = Group::load($course->entity_id);
$course->badge_name = $entity->badge_name->value;
$course->badge_description = $entity->badge_description->value;
$course->badge_criteria = $entity->badge_criteria->value;
}
$courses_keyed = [];
foreach ($courses as $course) {
$courses_keyed[$course->entity_id . '-' . $course->gid] = $course;
}
$courses = $courses_keyed;
}
if (!empty($courses) && !empty($modules)) {
$output = array_merge($modules, $courses);
usort($output, 'self::opignoModuleResultsSortByDate');
}
elseif (!empty($modules)) {
$output = $modules;
}
elseif (!empty($courses)) {
$output = $courses;
}
return $output;
}
public static function opignoModuleResultsSortByDate($a, $b) {
return strtotime($a->completed) < strtotime($b->completed);
}
}