You are here

class ContentTypeILT in Opigno Instructor-led Trainings 3.x

Same name and namespace in other branches
  1. 8 src/Plugin/OpignoGroupManagerContentType/ContentTypeILT.php \Drupal\opigno_ilt\Plugin\OpignoGroupManagerContentType\ContentTypeILT

Class ContentTypeILT.

Plugin annotation


@OpignoGroupManagerContentType(
  id = "ContentTypeILT",
  entity_type = "opigno_ilt",
  readable_name = "Instructor-Led Training",
  description = "Contains the Instructor-Led Trainings",
  allowed_group_types = {
    "learning_path"
  },
  group_content_plugin_id = "opigno_ilt_group"
)

Hierarchy

Expanded class hierarchy of ContentTypeILT

File

src/Plugin/OpignoGroupManagerContentType/ContentTypeILT.php, line 26

Namespace

Drupal\opigno_ilt\Plugin\OpignoGroupManagerContentType
View source
class ContentTypeILT extends ContentTypeBase {

  /**
   * {@inheritdoc}
   */
  public function shouldShowNext() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserScore($user_id, $entity_id) {

    /** @var \Drupal\opigno_ilt\ILTResultInterface[] $results */
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function getViewContentUrl($entity_id) {
    return Url::fromRoute('entity.opigno_ilt.canonical', [
      'opigno_ilt' => $entity_id,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getStartContentUrl($content_id, $group_id = NULL) {
    return Url::fromRoute('entity.opigno_ilt.canonical', [
      'opigno_ilt' => $content_id,
    ]);
  }

  /**
   * {@inheritdoc}
   *
   * @param int|\Drupal\opigno_ilt\ILTInterface $entity
   *   The entity ID or entity instance.
   */
  public function getContent($entity) {

    // If the value is the ILT ID, load the ILT.
    if (is_numeric($entity) || is_string($entity)) {

      /** @var \Drupal\opigno_ilt\ILTInterface $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'));
  }

  /**
   * Returns 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';
  }

  /**
   * {@inheritdoc}
   */
  public function getContentFromRequest(Request $request) {
    $entity = $request
      ->get('opigno_ilt');
    if ($entity === NULL || $entity === FALSE) {
      return FALSE;
    }
    return $this
      ->getContent($entity);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllContents() {
    try {

      /** @var \Drupal\opigno_ilt\ILTInterface[] $entities */
      $entities = \Drupal::entityTypeManager()
        ->getStorage('opigno_ilt')
        ->loadMultiple();
    } catch (InvalidPluginDefinitionException $e) {

      // TODO: Log the error.
      return FALSE;
    }
    $contents = [];
    foreach ($entities as $entity) {
      $contents[] = $this
        ->getContent($entity);
    }
    return $contents;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContents() {
    return $this
      ->getAllContents();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTypeBase::getAllowedGroupTypes public function Returns allowed group types.
ContentTypeBase::getDescription public function Returns description.
ContentTypeBase::getEntityType public function Returns entity type.
ContentTypeBase::getGroupContentPluginId public function Returns plugin id.
ContentTypeBase::getId public function Returns ID.
ContentTypeBase::getReadableName public function Returns readable name.
ContentTypeBase::shouldShowFinish public function Answer if the current page should show the "finish" button.
ContentTypeILT::getAllContents public function Get all the entities in an array of LearningPathContent. Overrides ContentTypeInterface::getAllContents
ContentTypeILT::getAvailableContents public function Get all the published entities in an array of LearningPathContent. Overrides ContentTypeInterface::getAvailableContents
ContentTypeILT::getContent public function Overrides ContentTypeInterface::getContent
ContentTypeILT::getContentFromRequest public function Try to get the content from a Request object. Overrides ContentTypeInterface::getContentFromRequest
ContentTypeILT::getDefaultModuleImageUrl public function Returns default image url.
ContentTypeILT::getFormObject public function Get the form object based on the entity ID. Overrides ContentTypeInterface::getFormObject
ContentTypeILT::getStartContentUrl public function Get the URL object for starting the quiz. Overrides ContentTypeBase::getStartContentUrl
ContentTypeILT::getUserScore public function Get the score of the user for a specific entity. Overrides ContentTypeInterface::getUserScore
ContentTypeILT::getViewContentUrl public function Get the URL object of the main view page of a specific entity. Overrides ContentTypeInterface::getViewContentUrl
ContentTypeILT::shouldShowNext public function Return TRUE if the page should show the "next" action button. Overrides ContentTypeInterface::shouldShowNext
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.