ContentTypeILT.php in Opigno Instructor-led Trainings 3.x
File
src/Plugin/OpignoGroupManagerContentType/ContentTypeILT.php
View source
<?php
namespace Drupal\opigno_ilt\Plugin\OpignoGroupManagerContentType;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Url;
use Drupal\opigno_group_manager\ContentTypeBase;
use Drupal\opigno_group_manager\OpignoGroupContent;
use Drupal\opigno_ilt\Entity\ILT;
use Symfony\Component\HttpFoundation\Request;
class ContentTypeILT extends ContentTypeBase {
public function shouldShowNext() {
return FALSE;
}
public function getUserScore($user_id, $entity_id) {
$results = \Drupal::entityTypeManager()
->getStorage('opigno_ilt_result')
->loadByProperties([
'user_id' => $user_id,
'opigno_ilt' => $entity_id,
]);
$best_score = 0;
foreach ($results as $result) {
$score = $result
->getScore();
if ($score > $best_score) {
$best_score = $score;
}
}
return $best_score / 100;
}
public function getViewContentUrl($entity_id) {
return Url::fromRoute('entity.opigno_ilt.canonical', [
'opigno_ilt' => $entity_id,
]);
}
public function getStartContentUrl($content_id, $group_id = NULL) {
return Url::fromRoute('entity.opigno_ilt.canonical', [
'opigno_ilt' => $content_id,
]);
}
public function getContent($entity) {
if (is_numeric($entity) || is_string($entity)) {
$entity = ILT::load($entity);
}
if ($entity === NULL || $entity === FALSE) {
return FALSE;
}
return new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $entity
->id(), $entity
->label(), $this
->getDefaultModuleImageUrl(), t('Default image'));
}
public function getDefaultModuleImageUrl() {
$request = \Drupal::request();
$path = \Drupal::service('module_handler')
->getModule('opigno_module')
->getPath();
return $request
->getBasePath() . '/' . $path . '/img/img_module.svg';
}
public function getContentFromRequest(Request $request) {
$entity = $request
->get('opigno_ilt');
if ($entity === NULL || $entity === FALSE) {
return FALSE;
}
return $this
->getContent($entity);
}
public function getFormObject($entity_id = NULL) {
if (empty($entity_id)) {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'add');
$entity = ILT::create();
}
else {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'edit');
$entity = ILT::load($entity_id);
}
$form
->setEntity($entity);
return $form;
}
public function getAllContents() {
try {
$entities = \Drupal::entityTypeManager()
->getStorage('opigno_ilt')
->loadMultiple();
} catch (InvalidPluginDefinitionException $e) {
return FALSE;
}
$contents = [];
foreach ($entities as $entity) {
$contents[] = $this
->getContent($entity);
}
return $contents;
}
public function getAvailableContents() {
return $this
->getAllContents();
}
}