You are here

function course_tokens in Course 7

Same name and namespace in other branches
  1. 8.3 course.module \course_tokens()
  2. 8.2 course.module \course_tokens()
  3. 7.2 course.module \course_tokens()
  4. 3.x course.module \course_tokens()

Implements hook_tokens().

File

./course.module, line 1829
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];
    }
  }
  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_report_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};
          }
        }
      }
    }
  }
  return $replacements;
}