LPModuleAvailability.php in Opigno Learning path 3.x
File
src/Entity/LPModuleAvailability.php
View source
<?php
namespace Drupal\opigno_learning_path\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
class LPModuleAvailability extends ContentEntityBase {
public static function createWithValues($group_id, $entity_id, $availability = 0, $open_date = 0, $close_date = 0) {
$values = [
'group_id' => $group_id,
'entity_id' => $entity_id,
'availability' => $availability,
'open_date' => $open_date,
'close_date' => $close_date,
];
return parent::create($values);
}
public static function loadByProperties($properties) {
return \Drupal::entityTypeManager()
->getStorage('lp_module_availability')
->loadByProperties($properties);
}
public function getAvailability() {
$availability = $this
->get('availability')->value;
return isset($availability) ? $availability : 0;
}
public function getOpenDate() {
return $this
->get('open_date')->value;
}
public function getCloseDate() {
return $this
->get('close_date')->value;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['id']
->setLabel(t('Module availability ID'))
->setDescription(t('The LP module availability ID.'));
$fields['group_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Group ID')
->setDescription('The group ID')
->setSetting('target_type', 'group')
->setSetting('handler_settings', [
'target_bundles' => [
'learning_path' => 'learning_path',
],
]);
$fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Entity ID')
->setDescription('The entity ID')
->setSettings([
'target_type' => 'opigno_module',
'handler' => 'default',
]);
$options = [
0 => t('Always available'),
1 => t('Restrict to specific dates for that training'),
];
$fields['availability'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Module availability'))
->setDescription(t('Set module availability for particular training.'))
->setSetting('allowed_values', $options)
->setDefaultValue(0);
$fields['open_date'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Open date'))
->setDescription(t('The date this Module will become available.'));
$fields['close_date'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Open date'))
->setDescription(t('The date this Module will become unavailable.'));
return $fields;
}
}