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;
class CourseEnrollment extends ContentEntityBase implements EntityChangedInterface, RevisionLogInterface {
use RevisionLogEntityTrait;
use EntityChangedTrait;
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);
}
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);
}
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,
));
}
public function preSave(EntityStorageInterface $storage) {
$course = $this
->getCourse();
if ($this
->get('enroll_end')
->isEmpty() && !$course
->get('duration')
->isEmpty()) {
$this
->set('enroll_end', \Drupal::time()
->getRequestTime() + $course
->get('duration')
->getString());
}
$this
->evaluate();
$this
->setNewRevision();
parent::preSave($storage);
}
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;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$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;
}
function getCourse() {
return Course::load($this
->get('cid')
->getString());
}
function getUser() {
return User::load($this
->get('uid')
->getString());
}
public function track() {
$this
->evaluate();
$this
->save();
}
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());
}
$required += (int) $courseObject
->get('required')
->getString();
$required_complete += $courseObject
->get('required')
->getString() && $courseObject
->getFulfillment($account)
->isComplete();
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) {
$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());
}
}
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
foreach ($entities as $course_enrollment) {
$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";
$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);
}
}
function isComplete() {
return (bool) $this
->get('complete')
->getString();
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
\Drupal::entityTypeManager()
->getAccessControlHandler('course')
->resetCache();
\Drupal::entityTypeManager()
->getAccessControlHandler('course_object')
->resetCache();
}
public function label() {
return "{$this->getUser()->getDisplayName()}'s enrollment in {$this->getCourse()->getTitle()}";
}
}