function CourseObjectAccessConditional::take 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::take()
- 8.2 src/Plugin/course/CourseObjectAccess/CourseObjectAccessConditional.php \Drupal\course\Plugin\course\CourseObjectAccess\CourseObjectAccessConditional::take()
Can the user take the object?
Parameters
type $account:
Return value
boolean
Overrides CourseObjectAccessPluginBase::take
1 call to CourseObjectAccessConditional::take()
- CourseObjectAccessConditional::view in src/
Plugin/ course/ CourseObjectAccess/ CourseObjectAccessConditional.php - Can the user view the object but not interact?
File
- src/
Plugin/ course/ CourseObjectAccess/ CourseObjectAccessConditional.php, line 28
Class
- CourseObjectAccessConditional
- Plugin annotation @CourseObjectAccess( id = "conditional", label = @Translation("Conditional"), )
Namespace
Drupal\course\Plugin\course\CourseObjectAccessCode
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;
}