function course_tokens in Course 8.3
Same name and namespace in other branches
- 8.2 course.module \course_tokens()
- 7.2 course.module \course_tokens()
- 7 course.module \course_tokens()
- 3.x course.module \course_tokens()
Implements hook_tokens().
File
- ./
course.module, line 626 - course.module Core functionality for Courses.
Code
function course_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node']) && course_node_is_course($data['node'])) {
$course_tokens = token_find_with_prefix($tokens, 'course');
$node = $data['node'];
foreach ($course_tokens as $name => $original) {
$replacements[$course_tokens[$name]] = $node->course[$name];
}
}
/** @todo: replace with how course_enrollment does it, but tokens would change due to underscore/dash usage */
if ($type == 'course-report' && !empty($data['node']) && course_node_is_course($data['node']) && !empty($data['user'])) {
// Node and user context, we can lookup the user's records.
if ($report = course_enrollment_load($data['node'], $data['user'])) {
$report->data = empty($report->data) ? NULL : unserialize($report->data);
foreach ($tokens as $name => $original) {
// Handle date formats
if ((strpos($name, 'date_completed') === 0 || strpos($name, 'updated') === 0) && strpos($name, ':') !== FALSE) {
$name_parsed = explode(':', $name, 3);
$replacements[$tokens[$name]] = format_date($report->{$name_parsed[0]}, $name_parsed[1], $name_parsed[1] == 'custom' && count($name_parsed) == 3 ? $name_parsed[2] : '');
}
else {
if (isset($report->{$name})) {
$replacements[$tokens[$name]] = $report->{$name};
}
}
}
}
}
if ($type == 'course_enrollment' && !isset($data['course_enrollment']) && isset($data['node'], $data['user']) && ($course_enrollment = course_enrollment_load($data['node'], $data['user']))) {
$data['course_enrollment'] = $course_enrollment;
// Load enrollment from a passed node/user.
return entity_token_tokens('course_enrollment', $tokens, $data, $options);
}
return $replacements;
}