View source
<?php
namespace Drupal\opigno_learning_path;
use Drupal\opigno_learning_path\Entity\LPManagedContent;
class LearningPathContent {
private $learningPathContentTypeId;
private $entityType;
private $entityId;
private $title;
private $imageUrl;
private $imageAlt;
public function __construct($learning_path_content_type_id, $entity_type, $entity_id, $title, $image_url, $image_alt) {
$this
->setLearningPathContentTypeId($learning_path_content_type_id);
$this
->setEntityType($entity_type);
$this
->setEntityId($entity_id);
$this
->setTitle($title);
$this
->setImageUrl($image_url);
$this
->setImageAlt($image_alt);
}
public function __toString() {
return implode('.', [
self::class,
$this->entityType,
$this->entityId,
]);
}
public function toManagerArray(LPManagedContent $content = NULL) {
if ($content === NULL) {
$cid = '';
$is_mandatory = FALSE;
$success_score_min = 0;
$parents_links = [];
}
else {
$cid = $content
->id();
$is_mandatory = $content
->isMandatory();
$success_score_min = $content
->getSuccessScoreMin();
$parents_links = $content
->getParentsLinks();
}
$this_array = [
'cid' => $cid,
'entityId' => $this
->getEntityId(),
'entityType' => $this
->getLearningPathContentTypeId(),
'entityBundle' => $this
->getLearningPathContentTypeId(),
'contentType' => $this
->getLearningPathContentTypeId(),
'title' => $this
->getTitle(),
'imageUrl' => $this
->getImageUrl(),
'imageAlt' => $this
->getImageAlt(),
'isMandatory' => $is_mandatory,
'successScoreMin' => $success_score_min,
];
$parents = [];
foreach ($parents_links as $link) {
if (get_class($link) != 'Drupal\\opigno_learning_path\\Entity\\LPManagedLink') {
continue;
}
$parents[] = [
'cid' => $link
->getParentContentId(),
'minScore' => $link
->getRequiredScore(),
];
}
$this_array['parents'] = $parents;
return $this_array;
}
public function getEntityType() {
return $this->entityType;
}
public function setEntityType($entity_type) {
$this->entityType = $entity_type;
}
public function getEntityId() {
return $this->entityId;
}
public function setEntityId($entity_id) {
$this->entityId = $entity_id;
}
public function getTitle() {
return $this->title;
}
public function setTitle($entity_title) {
$this->title = $entity_title;
}
public function getLearningPathContentTypeId() {
return $this->learningPathContentTypeId;
}
public function setLearningPathContentTypeId($learning_path_content_type_id) {
$this->learningPathContentTypeId = $learning_path_content_type_id;
}
public function getImageUrl() {
return $this->imageUrl;
}
public function setImageUrl($image_url) {
$this->imageUrl = $image_url;
}
public function getImageAlt() {
return $this->imageAlt;
}
public function setImageAlt($image_alt) {
$this->imageAlt = $image_alt;
}
public static function getClassGroups($id) {
$db_connection = \Drupal::service('database');
$parents = $db_connection
->select('group_content_field_data', 'g_c_f_d')
->fields('g_c_f_d', [
'gid',
])
->condition('entity_id', $id)
->condition('type', 'group_content_type_27efa0097d858')
->execute()
->fetchAll();
return $parents;
}
public static function getGroupMembershipTimestamp($gid, $uid) {
$db_connection = \Drupal::service('database');
$timestamp = $db_connection
->select('group_content_field_data', 'g_c_f_d')
->fields('g_c_f_d', [
'created',
])
->condition('gid', $gid)
->condition('entity_id', $uid)
->execute()
->fetchField();
return $timestamp;
}
public static function getGroupMembershipIdsByType($gid, $types) {
$db_connection = \Drupal::service('database');
$ids = $db_connection
->select('group_content_field_data', 'g_c_f_d')
->fields('g_c_f_d', [
'entity_id',
])
->condition('gid', $gid)
->condition('type', $types, 'IN')
->execute()
->fetchCol();
return $ids;
}
public static function getAllStepsOnlyModules($group_id, $uid = NULL, $all = FALSE, $onlyCM = NULL, $latest_cert_date = NULL) {
if (!$uid) {
$uid = \Drupal::currentUser()
->id();
}
if ($all) {
$group_steps = opigno_learning_path_get_all_steps($group_id, $uid, $onlyCM, $latest_cert_date);
}
else {
$group_steps = opigno_learning_path_get_steps($group_id, $uid, $onlyCM, $latest_cert_date);
}
$steps = [];
array_walk($group_steps, function ($step, $key) use ($uid, &$steps, $onlyCM, $latest_cert_date) {
$step['position'] = $key;
if (!LearningPathAccess::checkStepValidation($step, $uid)) {
return;
}
if ($step['typology'] === 'Course') {
$course_steps = opigno_learning_path_get_steps($step['id'], $uid, $onlyCM, $latest_cert_date);
$last_course_step = end($course_steps);
$course_steps = array_map(function ($course_step, $key) use ($step, $last_course_step) {
$course_step['parent'] = $step;
$course_step['position'] = $key;
$course_step['is last child'] = $course_step['cid'] === $last_course_step['cid'];
return $course_step;
}, $course_steps, array_keys($course_steps));
$steps = array_merge($steps, $course_steps);
}
else {
$steps[] = $step;
}
});
return $steps;
}
}