View source
<?php
namespace Drupal\opigno_learning_path\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\Group;
use Drupal\user\Entity\User;
class LPResult extends ContentEntityBase {
public static function createWithValues($learning_path_id, $user_id, $has_passed, $finished) {
$values = [
'learning_path_id' => $learning_path_id,
'user_id' => $user_id,
'has_passed' => $has_passed,
'finished' => $finished,
];
return parent::create($values);
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
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) {
$this
->setLearningPathId($learning_path
->id());
return $this;
}
public function getUserId() {
return $this
->get('user_id')->target_id;
}
public function setUserId($user_id) {
$this
->set('user_id', $user_id);
return $this;
}
public function getUser() {
return $this
->get('user_id')->entity;
}
public function setUser(User $user) {
$this
->set('user_id', $user
->id());
return $this;
}
public function hasPassed() {
return $this
->get('has_passed')->value;
}
public function setHasPassed($has_passed) {
$this
->set('has_passed', $has_passed);
return $this;
}
public static function learningPathUserProgress(Group $group, $uid) {
$progress = 0;
$contents = LPManagedContent::loadByLearningPathId($group
->id());
if (!empty($contents)) {
$content_count = count($contents);
foreach ($contents as $content) {
$content_type = $content
->getLearningPathContentType();
$user_score = $content_type
->getUserScore($uid, $content
->getEntityId());
$progress += $user_score;
}
$progress = round($progress / $content_count * 100);
}
return $progress;
}
public function isStarted() {
return (bool) $this
->get('started')->value != 0;
}
public function setStarted($timestamp) {
$this
->set('started', $timestamp);
return $this;
}
public function isFinished() {
return (bool) $this
->get('finished')->value != 0;
}
public function setFinished($timestamp) {
$this
->set('finished', $timestamp);
return $this;
}
public static function getCurrentLPAttempt(Group $group, AccountInterface $user) {
$results_storage = \Drupal::entityTypeManager()
->getStorage('learning_path_result');
$query = $results_storage
->getQuery();
$results = $query
->condition('learning_path_id', $group
->id())
->condition('user_id', $user
->id())
->condition('finished', 0)
->execute();
return !empty($results) ? $results_storage
->load(key($results)) : FALSE;
}
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['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('User')
->setSetting('target_type', 'user');
$fields['has_passed'] = BaseFieldDefinition::create('boolean')
->setLabel('Has passed')
->setDescription('Define if the user has passed the learning path')
->setDefaultValue(FALSE);
$fields['started'] = BaseFieldDefinition::create('created')
->setLabel(t('Started'))
->setDescription(t('The time that the LP attempt has started.'));
$fields['finished'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Finished'))
->setDescription(t('The time that the LP attempt finished.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Creation datetime'))
->setDescription(t('The time that the result was saved.'));
return $fields;
}
public static function loadByProperties($properties) {
return \Drupal::entityTypeManager()
->getStorage('learning_path_result')
->loadByProperties($properties);
}
public static function loadByLearningPath(Group $learning_path) {
return self::loadByProperties([
'learning_path_id' => $learning_path
->id(),
]);
}
}