You are here

public static function OpignoTinCanApiStatements::setObjectActivity in Opigno TinCan API 3.x

Same name and namespace in other branches
  1. 8 src/OpignoTinCanApiStatements.php \Drupal\opigno_tincan_api\OpignoTinCanApiStatements::setObjectActivity()

Set activity.

Parameters

\TinCan\Statement $statement: The statement to add the object.

string $activity_definition_type: An array containing the activity name and type (url). Use TinCanActivityDefinitions.

object $activity_object: An object: Group or Module or Activity.

Return value

bool|\TinCan\Statement Statement.

Throws

\Exception

1 call to OpignoTinCanApiStatements::setObjectActivity()
OpignoTinCanApiStatements::statementBaseCreation in src/OpignoTinCanApiStatements.php
Adds the current user as the actor.

File

src/OpignoTinCanApiStatements.php, line 39

Class

OpignoTinCanApiStatements
Class OpignoTinCanApiStatements.

Namespace

Drupal\opigno_tincan_api

Code

public static function setObjectActivity(Statement &$statement, $activity_definition_type, $activity_object) {

  // opigno_tincan_api_set_object_activity.
  if (empty($activity_definition_type)) {
    throw new Exception('The definition type given is empty.');
  }
  if (empty($activity_object)) {
    \Drupal::logger('opigno_tincan_api')
      ->notice('The entity given is not loaded or is not a entity: <pre>' . print_r($activity_object, TRUE) . '</pre>', []);
    return FALSE;
  }
  $entity_type_id = $activity_object
    ->getEntityTypeId();
  switch ($entity_type_id) {
    case 'group':
      $url = Url::fromRoute('entity.group.canonical', [
        'group' => $activity_object
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();
      $title = $activity_object
        ->label();
      break;
    case 'opigno_module':
      $url = Url::fromRoute('entity.opigno_module.canonical', [
        'opigno_module' => $activity_object
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();
      $title = $activity_object
        ->getName();
      break;
    case 'opigno_activity':
      $url = Url::fromRoute('entity.opigno_activity.canonical', [
        'opigno_activity' => $activity_object
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();
      $title = $activity_object
        ->getName();
      break;
    case 'opigno_moxtra_meeting':
      $url = Url::fromRoute('entity.opigno_moxtra_meeting.canonical', [
        'opigno_moxtra_meeting' => $activity_object
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();
      $title = $activity_object
        ->getTitle();
      break;
    case 'opigno_ilt':
      $url = Url::fromRoute('entity.opigno_ilt.canonical', [
        'opigno_ilt' => $activity_object
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();
      $title = $activity_object
        ->getTitle();
      break;
    case 'opigno_certificate':
      $url = Url::fromRoute('entity.opigno_certificate.canonical', [
        'opigno_certificate' => $activity_object
          ->id(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString();
      $title = $activity_object
        ->label();
      break;
  }
  $object = new Activity([
    'id' => $url,
    'definition' => [
      'name' => [
        'en-US' => $title,
      ],
      'type' => $activity_definition_type,
    ],
  ]);
  $statement
    ->setObject($object);
  return $statement;
}