You are here

class ContentTypeCourse in Opigno course 8

Same name and namespace in other branches
  1. 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

Expanded class hierarchy of ContentTypeCourse

File

src/Plugin/OpignoGroupManagerContentType/ContentTypeCourse.php, line 29

Namespace

Drupal\opigno_course\Plugin\OpignoGroupManagerContentType
View 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

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::getStartContentUrl public function Get the URL object for starting the quiz.
ContentTypeBase::shouldShowFinish public function Answer if the current page should show the "finish" button.
ContentTypeCourse::getAllContents public function Get all the entities in an array of LearningPathContent. Overrides ContentTypeInterface::getAllContents
ContentTypeCourse::getAvailableContents public function Get all the published entities in an array of LearningPathContent. Overrides ContentTypeInterface::getAvailableContents
ContentTypeCourse::getContent public function Get the entity as a LearningPathContent. Overrides ContentTypeInterface::getContent
ContentTypeCourse::getContentFromRequest public function Try to get the content from a Request object. Overrides ContentTypeInterface::getContentFromRequest
ContentTypeCourse::getCourseImage protected function Method for getting image from Course.
ContentTypeCourse::getDefaultCourseImageUrl public function Returns default image url.
ContentTypeCourse::getFormObject public function Get the form object based on the entity ID. Overrides ContentTypeInterface::getFormObject
ContentTypeCourse::getUserScore public function Get the score of the user for a specific entity. Overrides ContentTypeInterface::getUserScore
ContentTypeCourse::getViewContentUrl public function Get the URL object of the main view page of a specific entity. Overrides ContentTypeInterface::getViewContentUrl
ContentTypeCourse::shouldShowNext public function Return TRUE if the page should show the "next" action button. Overrides ContentTypeInterface::shouldShowNext
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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 3
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. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.