class CourseObjectAccessConditional in Course 3.x
Same name and namespace in other branches
- 8.3 src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessConditional
- 8.2 src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessConditional
Plugin annotation
@CourseObjectAccess(
id = "conditional",
label = @Translation("Conditional"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\course\Plugin\CourseObjectAccessPluginBase implements PluginFormInterface, CourseObjectAccessInterface
- class \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessConditional
- class \Drupal\course\Plugin\CourseObjectAccessPluginBase implements PluginFormInterface, CourseObjectAccessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CourseObjectAccessConditional
File
- src/
Plugin/ course/ CourseObjectAccess/ CourseObjectAccessConditional.php, line 16
Namespace
Drupal\course\Plugin\course\CourseObjectAccessView source
class CourseObjectAccessConditional extends CourseObjectAccessPluginBase {
function view($account) {
return $this
->take($account);
}
function see($account) {
if ($this
->getOption('conditional_hidden')) {
return $this
->view($account);
}
}
function take($account) {
$type = $this
->getOption('conditional_type');
$offset = (int) $this
->getOption('conditional_time');
$time = \Drupal::time()
->getRequestTime();
$access = TRUE;
$title = $this
->getCourseObject()
->getTitle();
$msg = t('%title is not yet available.', array(
'%title' => $title,
));
if ($type) {
foreach ($this
->getCourseObject()
->getCourse()
->getObjects() as $courseObject) {
if ($courseObject
->id() == $this
->getOption('conditional_object')) {
switch ($type) {
case 'completed':
$completed = $courseObject
->getFulfillment($account)
->getOption('date_completed');
if (!$completed) {
$access = FALSE;
}
else {
$date = $completed + $offset;
$access = $time >= $date;
$msg = t('%title will be available on %date', array(
'%title' => $title,
'%date' => \Drupal::service('date.formatter')
->format($date, 'short'),
));
}
break;
case 'started':
$started = $courseObject
->getFulfillment($account)
->getOption('date_started');
if (!$started) {
$access = FALSE;
}
else {
$date = $started + $offset;
$access = $time >= $date;
$msg = t('%title will be available on %date', array(
'%title' => $title,
'%date' => \Drupal::service('date.formatter')
->format($date, 'short'),
));
}
break;
}
}
}
}
if (!$access) {
$this
->getCourseObject()
->setAccessMessage('conditional', $msg);
}
return $access;
}
function optionsDefinition() {
$defaults = parent::optionsDefinition();
$defaults += array(
'conditional_type' => NULL,
'conditional_time' => NULL,
'conditional_object' => NULL,
'conditional_hidden' => NULL,
);
return $defaults;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$config = $this
->getOptions();
$form['conditional_type'] = array(
'#title' => t('Event'),
'#type' => 'select',
'#options' => array(
'',
'started' => t('User started course object'),
'completed' => t('User completed course object'),
),
'#description' => t('This sets the conditional behavior.'),
);
if (Drupal::moduleHandler()
->moduleExists('timeperiod')) {
$form['conditional_time'] = array(
'#title' => t('Time'),
'#type' => 'timeperiod_select',
'#units' => array(
'86400' => array(
'max' => 30,
'step size' => 1,
),
'3600' => array(
'max' => 24,
'step size' => 1,
),
'60' => array(
'max' => 60,
'step size' => 1,
),
),
'#description' => t('Length of time after the event happens when this course object should be accessible.'),
'#default_value' => $config['conditional_time'],
);
}
else {
$form['conditional_time'] = array(
'#title' => t('Time'),
'#type' => 'textfield',
'#size' => 8,
'#description' => t('Length of time in seconds after the event happens when this course object should be accessible.'),
'#default_value' => $config['conditional_time'],
);
}
$form['conditional_object'] = array(
'#title' => t('Course object'),
'#type' => 'select',
'#options' => $this
->getObjectOptions(),
'#description' => t('The course object to check for the type and time of conditional display.'),
);
$form['conditional_hidden'] = array(
'#title' => t('Hide object until ready'),
'#type' => 'checkbox',
'#description' => t('This will prevent the course object from appearing in the course outline until it is ready.'),
);
$conditional_dependent = array(
'#states' => array(
'visible' => array(
array(
'#edit-plugins-access-conditional-conditional-type' => array(
'value' => 'started',
),
),
array(
'#edit-plugins-access-conditional-conditional-type' => array(
'value' => 'completed',
),
),
),
),
);
foreach (Element::children($form) as $key) {
if ($key != 'conditional_time' && isset($config[$key])) {
$form[$key]['#default_value'] = $config[$key];
}
if ($key != 'conditional_type' && $key != 'conditional_time') {
$form[$key] += $conditional_dependent;
}
}
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CourseObjectAccessConditional:: |
public | function |
Define the form to be used in the object access settings area. Overrides CourseObjectAccessPluginBase:: |
|
CourseObjectAccessConditional:: |
function |
Overrides CourseObjectAccessPluginBase:: |
||
CourseObjectAccessConditional:: |
function |
Can the user see the object in the outline? Overrides CourseObjectAccessPluginBase:: |
||
CourseObjectAccessConditional:: |
function |
Can the user take the object? Overrides CourseObjectAccessPluginBase:: |
||
CourseObjectAccessConditional:: |
function |
Can the user view the object but not interact? Overrides CourseObjectAccessPluginBase:: |
||
CourseObjectAccessPluginBase:: |
private | property | ||
CourseObjectAccessPluginBase:: |
private | property | ||
CourseObjectAccessPluginBase:: |
public | function | Alter a property during display/tracking. Does not affect the stored options. This is called in CourseObject::getReadOnlyOptions(). | |
CourseObjectAccessPluginBase:: |
public | function | Evaluate the access conditions. | 1 |
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | Helper method to get possible objects. | 1 |
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function | ||
CourseObjectAccessPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
CourseObjectAccessPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |