You are here

CourseEnrollment.php in Course 3.x

File

src/Entity/CourseEnrollment.php
View source
<?php

namespace Drupal\course\Entity;

use Drupal;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionLogEntityTrait;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\User;
use function count;

/**
 * Defines the profile entity class.
 *
 * @ContentEntityType(
 *   id = "course_enrollment",
 *   label = @Translation("Course enrollment"),
 *   label_collection = @Translation("Course enrollments"),
 *   label_singular = @Translation("course_enrollment"),
 *   label_plural = @Translation("course_enrollments"),
 *   label_count = @PluralTranslation(
 *     singular = "@count enrollment",
 *     plural = "@count enrollments",
 *   ),
 *   bundle_label = @Translation("Course enrollment type"),
 *   bundle_entity_type = "course_enrollment_type",
 *   admin_permission = "administer course",
 *   permission_granularity = "bundle",
 *   base_table = "course_enrollment",
 *   revision_table = "course_enrollment_revision",
 *   fieldable = TRUE,
 *   field_ui_base_route = "entity.course_enrollment_type.edit_form",
 *   show_revision_ui = TRUE,
 *   entity_keys = {
 *     "id" = "eid",
 *     "bundle" = "type",
 *     "uid" = "uid",
 *     "uuid" = "uuid",
 *     "revision" = "revision_id",
 *   },
 *   revision_metadata_keys = {
 *     "revision_default" = "revision_default",
 *     "revision_user" = "revision_user",
 *     "revision_created" = "revision_created",
 *     "revision_log_message" = "revision_log_message",
 *   },
 *   handlers = {
 *     "access" = "Drupal\entity\UncacheableEntityAccessControlHandler",
 *     "permission_provider" = "Drupal\entity\UncacheableEntityPermissionProvider",
 *     "storage_schema" = "Drupal\course\Schema\CourseEnrollmentStorageSchema",
 *     "views_data" = "Drupal\entity\EntityViewsData",
 *     "form" = {
 *       "default" = "Drupal\course\Form\CourseEnrollmentEntityForm",
 *      }
 *   }
 * )
 */
class CourseEnrollment extends ContentEntityBase implements EntityChangedInterface, RevisionLogInterface {
  use RevisionLogEntityTrait;
  use EntityChangedTrait;

  /**
   * Use the Course's configured enrollment type if available.
   *
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    if (isset($values['cid'])) {
      $course = Course::load($values['cid']);
      $values['type'] = $course
        ->get('enrollment_type')
        ->getString();
    }
    return parent::preCreate($storage, $values);
  }

  /**
   * {@inheritdoc}
   */
  function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
    if (!$account) {
      $account = \Drupal::currentUser();
    }
    $course_access = $this
      ->getCourse()
      ->access('update', $account);
    $admin_course = $account
      ->hasPermission('administer course');
    $admin_enrollments = $account
      ->hasPermission('administer course enrollments');
    return AccessResult::allowedIf($course_access || $admin_course || $admin_enrollments);
  }

  /**
   * Enrollment entity label callback.
   */
  function defaultLabel() {
    $course = Course::load($this->cid);
    $account = User::load($this->uid);
    return t("@username's enrollment in @title", array(
      '@username' => format_username($account),
      '@title' => $course->title->value,
    ));
  }

  /**
   * If a duration is set on the course, apply it to this enrollment.
   *
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    $course = $this
      ->getCourse();
    if ($this
      ->get('enroll_end')
      ->isEmpty() && !$course
      ->get('duration')
      ->isEmpty()) {

      // Set enrollment end to now + the duration of the course.
      $this
        ->set('enroll_end', \Drupal::time()
        ->getRequestTime() + $course
        ->get('duration')
        ->getString());
    }

    // After enrollment, check for completion.
    $this
      ->evaluate();

    // Always create a new revision.
    $this
      ->setNewRevision();
    parent::preSave($storage);
  }

  /**
   * @kludge Reset the static lookup cache.
   */
  public function save() {
    $watchdog_variables = array(
      '@uid' => $this
        ->getUser()
        ->id(),
      '@cid' => $this
        ->getCourse()
        ->id(),
    );
    $ret = parent::save();
    if ($this
      ->isNew()) {
      Drupal::logger('course_enroll')
        ->notice('Enrolled user @uid into @cid.', $watchdog_variables);
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    // Add the revision metadata fields.
    $fields += static::revisionLogBaseFieldDefinitions($entity_type);
    $fields['cid'] = BaseFieldDefinition::create('entity_reference')
      ->setSetting('target_type', 'course')
      ->setRequired(TRUE)
      ->setLabel(t('Course'));
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setSetting('target_type', 'user')
      ->setRequired(TRUE)
      ->setLabel(t('User'));
    $fields['enrollmenttype'] = BaseFieldDefinition::create('string')
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'text_textfield',
    ])
      ->setLabel(t('Source'))
      ->setRevisionable(TRUE)
      ->setDescription(t('The creation source of the enrollment.'));
    $fields['status'] = BaseFieldDefinition::create('boolean')
      ->setDefaultValue(1)
      ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'datetime_default',
    ])
      ->setRevisionable(TRUE)
      ->setLabel(t('Status'));
    $fields['timestamp'] = BaseFieldDefinition::create('timestamp')
      ->setDefaultValue(NULL)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'nullable_datetime_timestamp',
    ])
      ->setRevisionable(TRUE)
      ->setLabel(t('Start'));
    $fields['enroll_end'] = BaseFieldDefinition::create('timestamp')
      ->setDefaultValue(NULL)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'nullable_datetime_timestamp',
    ])
      ->setRevisionable(TRUE)
      ->setLabel(t('End'));
    $fields['complete'] = BaseFieldDefinition::create('boolean')
      ->setDefaultValue(FALSE)
      ->setDisplayConfigurable('form', TRUE)
      ->setRevisionable(TRUE)
      ->setLabel(t('Complete'));
    $fields['date_completed'] = BaseFieldDefinition::create('timestamp')
      ->setRevisionable(TRUE)
      ->setDefaultValue(NULL)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'nullable_datetime_timestamp',
    ])
      ->setRevisionable(TRUE)
      ->setLabel(t('Date completed'));
    $fields['grade_result'] = BaseFieldDefinition::create('integer')
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
    ])
      ->setRevisionable(TRUE)
      ->setLabel(t('Grade result'));
    $fields['section'] = BaseFieldDefinition::create('string')
      ->setRevisionable(TRUE)
      ->setLabel(t('Section'));
    $fields['section_name'] = BaseFieldDefinition::create('string')
      ->setRevisionable(TRUE)
      ->setLabel(t('Section name'));
    $fields['coid'] = BaseFieldDefinition::create('entity_reference')
      ->setSetting('target_type', 'course_object')
      ->setRevisionable(TRUE)
      ->setLabel(t('Course object ID'));
    $fields['data'] = BaseFieldDefinition::create('map')
      ->setLabel(t('Data'));
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setRevisionable(TRUE)
      ->setLabel('Created');
    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setRevisionable(TRUE)
      ->setLabel('Changed');
    return $fields;
  }

  /**
   * Get the Course for this enrollment.
   *
   * @return Course
   */
  function getCourse() {
    return Course::load($this
      ->get('cid')
      ->getString());
  }

  /**
   * Get the User for this enrollment.
   *
   * @return AccountInterface
   */
  function getUser() {
    return User::load($this
      ->get('uid')
      ->getString());
  }
  public function track() {
    $this
      ->evaluate();
    $this
      ->save();
  }

  /**
   * Track the course (scan required objects, update progress, completion, etc.)
   * and mark progress/completion.
   */
  public function evaluate() {
    $required = 0;
    $required_complete = 0;
    $prev = NULL;
    $account = $this
      ->getUser();
    $grades = [];
    foreach ($this
      ->getCourse()
      ->getObjects() as $courseObject) {
      if (!$courseObject
        ->get('enabled')->value) {
        continue;
      }
      if (!$prev) {
        $this
          ->set('section_name', $courseObject
          ->getTitle());
        $this
          ->set('coid', $courseObject
          ->id());
      }

      // Count required objects.
      $required += (int) $courseObject
        ->get('required')
        ->getString();

      // Count completed required objects.
      $required_complete += $courseObject
        ->get('required')
        ->getString() && $courseObject
        ->getFulfillment($account)
        ->isComplete();

      // Log last grade.
      if ($courseObject
        ->isGraded() && $courseObject
        ->getOption('grade_include')) {
        $grades[$courseObject
          ->id()] = $courseObject
          ->getFulfillment($account)
          ->getOption('grade_result');
      }
      if (!$courseObject
        ->getFulfillment($account)
        ->isComplete() && $prev && $prev
        ->getFulfillment($account)
        ->isComplete()) {
        $this
          ->set('section_name', $courseObject
          ->getTitle());
        $this
          ->set('coid', $courseObject
          ->id());
      }
      $prev = clone $courseObject;
    }
    if (!empty($grades)) {
      $this
        ->set('grade_result', array_sum($grades) / count($grades));
    }
    if ($required_complete >= $required) {

      // Course requirements have been met.
      $this
        ->set('section', 'complete');
      $this
        ->set('section_name', 'Complete');
      $this
        ->set('complete', 1);
      if ($this
        ->get('date_completed')
        ->isEmpty()) {
        $this
          ->set('date_completed', \Drupal::time()
          ->getRequestTime());
      }
    }
  }

  /**
   * Clean up fulfillments after deleting an enrollment.
   *
   * {@inheritdoc}
   */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    foreach ($entities as $course_enrollment) {

      // Find all course objects in this course and delete the fulfillments.
      $coids = array();
      $result = Drupal::database()
        ->query("SELECT coid FROM {course_object} WHERE cid = :cid", array(
        ':cid' => $course_enrollment
          ->getCourse()
          ->id(),
      ));
      while ($row = $result
        ->fetch()) {
        $coids[] = $row->coid;
      }
      if (count($coids)) {
        $sql = "SELECT cofid FROM {course_object_fulfillment} WHERE coid IN (:coids[]) AND uid = :uid";

        // The user is already deleted, so we have to delete by the user ID.
        $cofIds = Drupal::database()
          ->query($sql, array(
          ':coids[]' => $coids,
          ':uid' => $course_enrollment
            ->get('uid')
            ->getString(),
        ))
          ->fetchAllKeyed(0, 0);
        $cofStorage = \Drupal::entityTypeManager()
          ->getStorage('course_object_fulfillment');
        $entities = $cofStorage
          ->loadMultiple($cofIds);
        foreach ($entities as $entity) {
          $entity
            ->delete();
        }
      }
      parent::preDelete($storage, $entities);
    }
  }

  /**
   * Check if the user has completed this course.
   *
   * @return bool
   */
  function isComplete() {
    return (bool) $this
      ->get('complete')
      ->getString();
  }

  /**
   * Reset enrollment access cache.
   *
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    \Drupal::entityTypeManager()
      ->getAccessControlHandler('course')
      ->resetCache();
    \Drupal::entityTypeManager()
      ->getAccessControlHandler('course_object')
      ->resetCache();
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return "{$this->getUser()->getDisplayName()}'s enrollment in {$this->getCourse()->getTitle()}";
  }

}

Classes

Namesort descending Description
CourseEnrollment Defines the profile entity class.