class ContentTypeCourse in Opigno course 8
Same name and namespace in other branches
- 3.x src/Plugin/OpignoGroupManagerContentType/ContentTypeCourse.php \Drupal\opigno_course\Plugin\OpignoGroupManagerContentType\ContentTypeCourse
Class ContentTypeCourse.
@package Drupal\opigno_course\Plugin\OpignoGroupManagerContentType
@OpignoGroupManagerContentType ( id = "ContentTypeCourse", entity_type = "group", readable_name = "Course", description = "A course is a group of learning path contents", allowed_group_types = { "learning_path", }, group_content_plugin_id = "subgroup:opigno_course" )
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_course\Plugin\OpignoGroupManagerContentType\ContentTypeCourse
- class \Drupal\opigno_group_manager\ContentTypeBase implements ContentTypeInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ContentTypeCourse
File
- src/
Plugin/ OpignoGroupManagerContentType/ ContentTypeCourse.php, line 29
Namespace
Drupal\opigno_course\Plugin\OpignoGroupManagerContentTypeView source
class ContentTypeCourse 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.group.canonical', [
'group' => $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) {
// TODO: Implement getUserScore() method.
return 1;
}
/**
* 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) {
if (!is_numeric($entity_id)) {
return FALSE;
}
/** @var \Drupal\group\Entity\Group $group */
$group = Group::load($entity_id);
if (empty($group)) {
return FALSE;
}
if ($group
->getGroupType()
->id() != 'opigno_course') {
return FALSE;
}
// Load image.
$image = $this
->getCourseImage($group);
if (!$image) {
// If no image set, put default image.
$img_url = $this
->getDefaultCourseImageUrl();
$img_alt = 'Default course image';
}
else {
// If there is an image set, get the URL of it.
$image = $image
->getValue();
$file = File::load($image[0]['target_id']);
$uri = $file
->getFileUri();
// I use this function `file_create_url` because all the others
// methods ($file->toUrl(), URL::fromUri($uri)->toString(), ...)
// doesn't work...
$url = file_create_url($uri);
$img_url = $url;
$img_alt = $image[0]['alt'];
}
return new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $entity_id, $group
->label(), $img_url, $img_alt);
}
/**
* 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\group\Entity\Group[] $groups */
$groups = \Drupal::entityTypeManager()
->getStorage('group')
->loadByProperties([
'type' => 'opigno_course',
]);
} catch (InvalidPluginDefinitionException $e) {
// TODO: Log the error.
return FALSE;
}
$contents = [];
foreach ($groups as $group) {
// Load image.
$image = $this
->getCourseImage($group);
if (!$image) {
// If no image set, put default image.
$img_url = $this
->getDefaultCourseImageUrl();
$img_alt = 'Default course image';
}
else {
// If there is an image set, get the URL of it.
$image = $image
->getValue();
$file = File::load($image[0]['target_id']);
$uri = $file
->getFileUri();
// I use this function `file_create_url` because all the others
// methods ($file->toUrl(), URL::fromUri($uri)->toString(), ...)
// doesn't work...
$url = file_create_url($uri);
$img_url = $url;
$img_alt = $image[0]['alt'];
}
$contents[] = new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $group
->id(), $group
->label(), $img_url, $img_alt);
}
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\group\Entity\Group[] $groups */
$groups = \Drupal::entityTypeManager()
->getStorage('group')
->loadByProperties([
'type' => 'opigno_course',
]);
} catch (InvalidPluginDefinitionException $e) {
// TODO: Log the error.
return FALSE;
}
$contents = [];
foreach ($groups as $group) {
// Load image.
$image = $this
->getCourseImage($group);
if (!$image) {
// If no image set, put default image.
$img_url = $this
->getDefaultCourseImageUrl();
$img_alt = 'Default course image';
}
else {
// If there is an image set, get the URL of it.
$image = $image
->getValue();
$file = File::load($image[0]['target_id']);
$uri = $file
->getFileUri();
// I use this function `file_create_url` because all the others
// methods ($file->toUrl(), URL::fromUri($uri)->toString(), ...)
// doesn't work...
$url = file_create_url($uri);
$img_url = $url;
$img_alt = $image[0]['alt'];
}
$contents[] = new OpignoGroupContent($this
->getPluginId(), $this
->getEntityType(), $group
->id(), $group
->label(), $img_url, $img_alt);
}
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) {
$group = $request
->get('group', FALSE);
if ($group === FALSE) {
return FALSE;
}
// If the value is the group ID, load the group.
if (is_numeric($group)) {
/* @var \Drupal\group\Entity\Group $group */
$group = Group::load($group);
}
if ($group
->getGroupType()
->id() != 'opigno_course') {
return FALSE;
}
// Load image.
$image = $this
->getCourseImage($group);
if (!$image) {
// If no image set, put default image.
$img_url = $this
->getDefaultCourseImageUrl();
$img_alt = 'Default course image';
}
else {
// If there is an image set, get the URL of it.
$image = $image
->getValue();
$file = File::load($image[0]['target_id']);
$uri = $file
->getFileUri();
// I use this function `file_create_url` because all the others
// methods ($file->toUrl(), URL::fromUri($uri)->toString(), ...)
// doesn't work...
$url = file_create_url($uri);
$img_url = $url;
$img_alt = $image[0]['alt'];
}
return new OpignoGroupContent($this
->getPluginId(), $this, $group
->id(), $group
->label(), $img_url, $img_alt);
}
/**
* 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) {
if (empty($entity_id)) {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'add');
$entity = Group::create([
'type' => 'opigno_course',
]);
}
else {
$form = \Drupal::entityTypeManager()
->getFormObject($this
->getEntityType(), 'edit');
$entity = Group::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
* Show next.
*/
public function shouldShowNext() {
// For now, show the next button only if the page is a "view" page
// from a group of type "opigno_course".
$route_name = \Drupal::routeMatch()
->getRouteName();
if ($route_name != 'entity.group.canonical') {
return FALSE;
}
$group = \Drupal::request()
->get('group');
if (empty($group) || $group
->getGroupType()
->id() != 'opigno_course') {
return FALSE;
}
return TRUE;
}
/**
* Returns default image url.
*/
public function getDefaultCourseImageUrl() {
$request = \Drupal::request();
$path = \Drupal::service('module_handler')
->getModule('opigno_course')
->getPath();
return $request
->getBasePath() . '/' . $path . '/img/img_course.svg';
}
/**
* Method for getting image from Course.
*/
protected function getCourseImage($group) {
if (!$group
->hasField('field_course_media_image')) {
return FALSE;
}
$image = FALSE;
// Get media.
if ($media = $group
->get('field_course_media_image')->entity) {
// Get image.
$image = $media
->get('field_media_image');
}
return $image;
}
}
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 | Get the URL object for starting the quiz. | |
ContentTypeBase:: |
public | function | Answer if the current page should show the "finish" button. | |
ContentTypeCourse:: |
public | function |
Get all the entities in an array of LearningPathContent. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
public | function |
Get all the published entities in an array of LearningPathContent. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
public | function |
Get the entity as a LearningPathContent. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
public | function |
Try to get the content from a Request object. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
protected | function | Method for getting image from Course. | |
ContentTypeCourse:: |
public | function | Returns default image url. | |
ContentTypeCourse:: |
public | function |
Get the form object based on the entity ID. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
public | function |
Get the score of the user for a specific entity. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
public | function |
Get the URL object of the main view page of a specific entity. Overrides ContentTypeInterface:: |
|
ContentTypeCourse:: |
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. |