View source
<?php
namespace Drupal\opigno_module\Plugin\OpignoGroupManagerContentType;
use Drupal\file\Entity\File;
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_module\Entity\OpignoModule;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Drupal\image\Entity\ImageStyle;
class ContentTypeModule extends ContentTypeBase {
public function getViewContentUrl($entity_id) {
return Url::fromRoute('entity.opigno_module.canonical', [
'opigno_module' => $entity_id,
]);
}
public function getStartContentUrl($entity_id, $group_id = NULL) {
return Url::fromRoute('opigno_module.take_module', [
'group' => $group_id,
'opigno_module' => $entity_id,
]);
}
public function getUserScore($user_id, $entity_id) {
$opigno_module = OpignoModule::load($entity_id);
$user = User::load($user_id);
$user_attempts = $opigno_module
->getModuleAttempts($user);
$best_score = 0;
foreach ($user_attempts as $user_attempt) {
$score = $user_attempt
->getScore();
$actual_score = $score / 100;
if ($actual_score > $best_score) {
$best_score = $actual_score;
}
}
return $best_score;
}
public function getContent($entity_id) {
$module = OpignoModule::load($entity_id);
$request = \Drupal::request();
return new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $entity_id, $module
->label(), ($image_url = $this
->getModuleImageUrl($module)) ? $image_url : $this
->getDefaultModuleImageUrl(), $image_url ? $this
->getModuleImageAlt($module) : t('Default image'));
}
public function getAvailableContents() {
try {
$modules = \Drupal::entityTypeManager()
->getStorage('opigno_module')
->loadByProperties([
'status' => 1,
]);
} catch (InvalidPluginDefinitionException $e) {
return FALSE;
}
$request = \Drupal::request();
$contents = [];
foreach ($modules as $module) {
$contents[] = new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $module
->id(), $module
->getName(), ($image_url = $this
->getModuleImageUrl($module)) ? $image_url : $this
->getDefaultModuleImageUrl(), $image_url ? $this
->getModuleImageAlt($module) : t('Default image'));
}
return $contents;
}
public function getAllContents() {
try {
$modules = \Drupal::entityTypeManager()
->getStorage('opigno_module')
->loadByProperties([]);
} catch (InvalidPluginDefinitionException $e) {
return FALSE;
}
$request = \Drupal::request();
$contents = [];
foreach ($modules as $module) {
$contents[] = new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $module
->id(), $module
->getName(), ($image_url = $this
->getModuleImageUrl($module)) ? $image_url : $this
->getDefaultModuleImageUrl(), $image_url ? $this
->getModuleImageAlt($module) : t('Default image'));
}
return $contents;
}
public function getContentFromRequest(Request $request) {
$route_name = $request->attributes
->get('_route');
if (!in_array($route_name, [
'entity.opigno_module.canonical',
'opigno_module.take_module',
'opigno_module.group.answer_form',
'opigno_module.my_results',
'opigno_module.group.my_results',
'opigno_module.module_result',
'opigno_module.module_result_form',
]) || $request->attributes
->has('opigno_module') == FALSE) {
return FALSE;
}
$opigno_module = $request->attributes
->get('opigno_module');
if (empty($opigno_module) || $opigno_module
->getEntityTypeId() != 'opigno_module') {
return FALSE;
}
return new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $opigno_module
->id(), $opigno_module
->getName(), ($image_url = $this
->getModuleImageUrl($opigno_module)) ? $image_url : $this
->getDefaultModuleImageUrl(), $image_url ? $this
->getModuleImageAlt($opigno_module) : t('Default image'));
}
public function getFormObject($entity_id = NULL) {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'default');
if (empty($entity_id)) {
$entity = OpignoModule::create();
}
else {
$entity = OpignoModule::load($entity_id);
}
$form
->setEntity($entity);
return $form;
}
public function shouldShowNext() {
if (\Drupal::routeMatch()
->getRouteName() == 'entity.opigno_module.canonical') {
return TRUE;
}
return FALSE;
}
public function getModuleImageAlt($module) {
$media = $module
->get('module_media_image')->entity;
if ($image = $media
->get('field_media_image')
->getValue()) {
return isset($image[0]['alt']) ? $image[0]['alt'] : NULL;
}
return NULL;
}
public function getModuleImageUrl($module) {
$image_url = NULL;
if (!$module) {
return $image_url;
}
$media = $module
->get('module_media_image')->entity;
if ($media) {
$image = $media
->get('field_media_image')
->getValue();
}
if (isset($image)) {
$image = File::load($image[0]['target_id']);
$style = ImageStyle::load('learning_path_thumbnail');
if ($style) {
$image_url = $style
->buildUrl($image
->getFileUri());
}
}
return $image_url;
}
public function getDefaultModuleImageUrl() {
$request = \Drupal::request();
$path = \Drupal::service('module_handler')
->getModule('opigno_module')
->getPath();
return $request
->getBasePath() . '/' . $path . '/img/img_module.svg';
}
}