class ContentTypeModule in Opigno module 8
Same name and namespace in other branches
- 3.x src/Plugin/OpignoGroupManagerContentType/ContentTypeModule.php \Drupal\opigno_module\Plugin\OpignoGroupManagerContentType\ContentTypeModule
Class ContentTypeModule.
@package Drupal\opigno_module\Plugin\ContentTypeModule
Plugin annotation
@OpignoGroupManagerContentType(
id = "ContentTypeModule",
entity_type = "opigno_module",
readable_name = "Module",
description = "Contains the Opigno modules",
allowed_group_types = {
"opigno_course",
"learning_path"
},
group_content_plugin_id = "opigno_module_group"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\opigno_group_manager\ContentTypeBase implements ContentTypeInterface
- class \Drupal\opigno_module\Plugin\OpignoGroupManagerContentType\ContentTypeModule
- class \Drupal\opigno_group_manager\ContentTypeBase implements ContentTypeInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ContentTypeModule
1 string reference to 'ContentTypeModule'
- opigno_module_entity_delete in ./
opigno_module.module - Implements hook_entity_delete().
File
- src/
Plugin/ OpignoGroupManagerContentType/ ContentTypeModule.php, line 32
Namespace
Drupal\opigno_module\Plugin\OpignoGroupManagerContentTypeView source
class ContentTypeModule extends ContentTypeBase {
/**
* Get the URL object of the main view page of a specific entity.
*
* @param int $entity_id
* The entity ID.
*
* @return \Drupal\Core\Url
* The tool entity URL.
*/
public function getViewContentUrl($entity_id) {
return Url::fromRoute('entity.opigno_module.canonical', [
'opigno_module' => $entity_id,
]);
}
/**
* {@inheritdoc}
*/
public function getStartContentUrl($entity_id, $group_id = NULL) {
return Url::fromRoute('opigno_module.take_module', [
'group' => $group_id,
'opigno_module' => $entity_id,
]);
}
/**
* Get the score of the user for a specific entity.
*
* @param int $user_id
* The user ID.
* @param int $entity_id
* The entity ID.
*
* @return float|false
* The score between 0 and 1. FALSE if no score found.
*/
public function getUserScore($user_id, $entity_id) {
// Get the module and the concerned user.
$opigno_module = OpignoModule::load($entity_id);
$user = User::load($user_id);
// For each attempt, check the score and save the best one.
$user_attempts = $opigno_module
->getModuleAttempts($user);
$best_score = 0;
foreach ($user_attempts as $user_attempt) {
/* @var $user_attempt UserModuleStatus */
// Get the scores.
$score = $user_attempt
->getScore();
// Divide the score to receive value between 0 and 1
// instead of percents count.
$actual_score = $score / 100;
// Save the best score.
if ($actual_score > $best_score) {
$best_score = $actual_score;
}
}
// Finally, return the BEST !
return $best_score;
}
/**
* Get the entity as a LearningPathContent.
*
* @param int $entity_id
* The entity ID.
*
* @return \Drupal\opigno_group_manager\OpignoGroupContent|false
* The content loaded in a LearningPathContent.
* FALSE if not possible to load.
*/
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'));
}
/**
* Get all the published entities in an array of LearningPathContent.
*
* @return \Drupal\opigno_group_manager\OpignoGroupContent[]|false
* The published contents or FALSE in case of error.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getAvailableContents() {
try {
/** @var \Drupal\opigno_module\Entity\OpignoModule[] $modules */
$modules = \Drupal::entityTypeManager()
->getStorage('opigno_module')
->loadByProperties([
'status' => 1,
]);
} catch (InvalidPluginDefinitionException $e) {
// TODO: Log the error.
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;
}
/**
* Get all the entities in an array of LearningPathContent.
*
* @return \Drupal\opigno_group_manager\OpignoGroupContent[]|false
* The contents or FALSE in case of error.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getAllContents() {
try {
/** @var \Drupal\opigno_module\Entity\OpignoModule[] $modules */
$modules = \Drupal::entityTypeManager()
->getStorage('opigno_module')
->loadByProperties([]);
} catch (InvalidPluginDefinitionException $e) {
// TODO: Log the error.
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;
}
/**
* Try to get the content from a Request object.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @return \Drupal\opigno_group_manager\OpignoGroupContent|false
* The content if possible. FALSE otherwise.
*/
public function getContentFromRequest(Request $request) {
// If the route is not an Opigno module route, leave the function.
$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;
}
/* @var $opigno_module OpignoModule */
$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'));
}
/**
* Get the form object based on the entity ID.
*
* If no entity given in parameter, return the entity creation form object.
*
* @param int $entity_id
* The entity ID.
*
* @return \Drupal\Core\Entity\EntityFormInterface
* Form.
*/
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;
}
/**
* Return TRUE if the page should show the "next" action button.
*
* Even if the score does not permit the user to go next.
*
* Returning TRUE will not automatically show the button.
* The button will show up only if this method returns
* TRUE and if there is a next step available
* and if the user is able to go to this next content.
*
* @return bool
* Next flag.
*/
public function shouldShowNext() {
// If the route is the good one, show the next/finish button.
if (\Drupal::routeMatch()
->getRouteName() == 'entity.opigno_module.canonical') {
return TRUE;
}
return FALSE;
}
/**
* Returns module image alt.
*/
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;
}
/**
* Returns module image url.
*/
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;
}
/**
* Returns module default 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';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentTypeBase:: |
public | function | Returns allowed group types. | |
ContentTypeBase:: |
public | function | Returns description. | |
ContentTypeBase:: |
public | function | Returns entity type. | |
ContentTypeBase:: |
public | function | Returns plugin id. | |
ContentTypeBase:: |
public | function | Returns ID. | |
ContentTypeBase:: |
public | function | Returns readable name. | |
ContentTypeBase:: |
public | function | Answer if the current page should show the "finish" button. | |
ContentTypeModule:: |
public | function |
Get all the entities in an array of LearningPathContent. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function |
Get all the published entities in an array of LearningPathContent. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function |
Get the entity as a LearningPathContent. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function |
Try to get the content from a Request object. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function | Returns module default image url. | |
ContentTypeModule:: |
public | function |
Get the form object based on the entity ID. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function | Returns module image alt. | |
ContentTypeModule:: |
public | function | Returns module image url. | |
ContentTypeModule:: |
public | function |
Get the URL object for starting the quiz. Overrides ContentTypeBase:: |
|
ContentTypeModule:: |
public | function |
Get the score of the user for a specific entity. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function |
Get the URL object of the main view page of a specific entity. Overrides ContentTypeInterface:: |
|
ContentTypeModule:: |
public | function |
Return TRUE if the page should show the "next" action button. Overrides ContentTypeInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |