View source
<?php
namespace Drupal\opigno_learning_path\Entity;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\group\Entity\Group;
class LPManagedContent extends ContentEntityBase {
public static function createWithValues($learning_path_id, $lp_content_type_id, $entity_id, $success_score_min = 0, $is_mandatory = 0, $coordinate_x = 0, $coordinate_y = 0) {
$values = [
'learning_path_id' => $learning_path_id,
'lp_content_type_id' => $lp_content_type_id,
'entity_id' => $entity_id,
'success_score_min' => $success_score_min,
'is_mandatory' => $is_mandatory,
'coordinate_x' => $coordinate_x,
'coordinate_y' => $coordinate_y,
];
return parent::create($values);
}
public function getLearningPathId() {
return $this
->get('learning_path_id')->target_id;
}
public function setLearningPathId($learning_path_id) {
$this
->set('learning_path_id', $learning_path_id);
return $this;
}
public function getLearningPath() {
return $this
->get('learning_path_id')->entity;
}
public function setLearningPath(Group $learning_path) {
if ($learning_path
->getGroupType()
->id() != 'learning_path') {
return FALSE;
}
$this
->setLearningPathId($learning_path
->id());
return $this;
}
public function getLearningPathContentTypeId() {
return $this
->get('lp_content_type_id')->value;
}
public function setLearningPathContentTypeId($lp_content_type_id) {
$this
->set('lp_content_type_id', $lp_content_type_id);
return $this;
}
public function getEntityId() {
return $this
->get('entity_id')->value;
}
public function setEntityId($entity_id) {
$this
->set('entity_id', $entity_id);
return $this;
}
public function getSuccessScoreMin() {
return $this
->get('success_score_min')->value;
}
public function setSuccessScoreMin($success_score_min) {
$this
->set('success_score_min', $success_score_min);
return $this;
}
public function isMandatory() {
return $this
->get('is_mandatory')->value;
}
public function setMandatory($is_mandatory) {
$this
->set('is_mandatory', $is_mandatory);
return $this;
}
public function getCoordinateX() {
return $this
->get('coordinate_x')->value;
}
public function setCoordinateX($coordinate_x) {
$this
->set('coordinate_x', $coordinate_x);
return $this;
}
public function getCoordinateY() {
return $this
->get('coordinate_y')->value;
}
public function setCoordinateY($coordinate_y) {
$this
->set('coordinate_y', $coordinate_y);
return $this;
}
public function getParentsLinks() {
return LPManagedLink::loadByProperties([
'learning_path_id' => $this
->getLearningPathId(),
'child_content_id' => $this
->id(),
]);
}
public function getChildrenLinks() {
return LPManagedLink::loadByProperties([
'learning_path_id' => $this
->getLearningPathId(),
'parent_content_id' => $this
->id(),
]);
}
public function hasChildLink() {
return !empty($this
->getChildrenLinks());
}
public function getLearningPathContentType() {
$manager = \Drupal::getContainer()
->get('opigno_learning_path.content_types.manager');
return $manager
->createInstance($this
->getLearningPathContentTypeId());
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['learning_path_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Learning Path')
->setCardinality(1)
->setSetting('target_type', 'group')
->setSetting('handler_settings', [
'target_bundles' => [
'learning_path' => 'learning_path',
],
]);
$fields['lp_content_type_id'] = BaseFieldDefinition::create('string')
->setLabel('Learning Path Content Type ID')
->setDescription('The content type ID (should be a plugin ID)');
$fields['entity_id'] = BaseFieldDefinition::create('integer')
->setLabel('Entity ID')
->setDescription('The entity ID');
$fields['success_score_min'] = BaseFieldDefinition::create('integer')
->setLabel('Success score minimum')
->setDescription('The minimum score to success this content')
->setDefaultValue(0);
$fields['is_mandatory'] = BaseFieldDefinition::create('boolean')
->setLabel('Is mandatory')
->setDescription('Indicate if this content is mandatory to succeed the learning path')
->setDefaultValue(FALSE);
$fields['coordinate_x'] = BaseFieldDefinition::create('integer')
->setLabel('Coordinate X')
->setDescription('The X coordinate in this learning path manager')
->setDefaultValue(0);
$fields['coordinate_y'] = BaseFieldDefinition::create('integer')
->setLabel('Coordinate Y')
->setDescription('The Y coordinate in this learning path manager')
->setDefaultValue(0);
return $fields;
}
public static function loadByProperties($properties) {
return \Drupal::entityTypeManager()
->getStorage('learning_path_content')
->loadByProperties($properties);
}
public static function loadByLearningPathId($learning_path_id) {
try {
return self::loadByProperties([
'learning_path_id' => $learning_path_id,
]);
} catch (InvalidPluginDefinitionException $e) {
return [];
}
}
public function delete() {
$as_child_links = LPManagedLink::loadByProperties([
'learning_path_id' => $this
->getLearningPathId(),
'child_content_id' => $this
->id(),
]);
$as_parent_links = LPManagedLink::loadByProperties([
'learning_path_id' => $this
->getLearningPathId(),
'parent_content_id' => $this
->id(),
]);
$all_links = array_merge($as_child_links, $as_parent_links);
foreach ($all_links as $link) {
$link
->delete();
}
parent::delete();
}
public static function getFirstStep($learning_path_id) {
$contents = self::loadByLearningPathId($learning_path_id);
foreach ($contents as $content) {
$parents = $content
->getParentsLinks();
if (empty($parents)) {
return $content;
}
}
return FALSE;
}
public function getNextStep($user_score) {
$query = \Drupal::entityTypeManager()
->getStorage('learning_path_link')
->getQuery();
$query
->condition('parent_content_id', $this
->id());
$query
->condition('required_score', $user_score, '<=');
$query
->sort('required_score', 'DESC');
$query
->range(0, 1);
$result = $query
->execute();
if (empty($result)) {
return FALSE;
}
$next_step_id = array_pop($result);
$next_step_link = LPManagedLink::load($next_step_id);
return $next_step_link
->getChildContent();
}
}