You are here

function course_report_save in Course 7

Same name and namespace in other branches
  1. 6 course.module \course_report_save()
  2. 7.2 course.module \course_report_save()

Inserts or updates a course report record.

@todo Check for missing fields.

Parameters

object $entry: The report entry to be saved into {course_report}, containing:

  • nid: Required. the node id.
  • uid: Required. the user id.
  • data: An array containing:
    • user: The serialized user object at the time of entry.
    • profile: The serialized user profile at the time of entry.
  • updated: Timestamp. The entry time.
1 call to course_report_save()
course_edit_enrollment_action in ./course.module
Edit enrollment action

File

./course.module, line 2387
course.module Core functionality for Courses.

Code

function course_report_save($entry) {

  // No shenanigans.
  if (!$entry->nid > 0 || !$entry->uid > 0) {
    $message = t('Report not entered because entry must have nid and uid.');
    watchdog('course_report', $message, WATCHDOG_ERROR);
    drupal_set_message(check_plain($message), 'error');
    return FALSE;
  }

  // Load user so we can serialize it.
  $account = user_load($entry->uid);
  $result = db_query("SELECT * FROM {course_report} WHERE nid = :nid AND uid = :uid", array(
    ':nid' => $entry->nid,
    ':uid' => $entry->uid,
  ));
  $old = $result
    ->fetch();
  $entry->updated = REQUEST_TIME;
  if ($entry->complete && empty($entry->date_completed)) {
    $entry->date_completed = REQUEST_TIME;
  }
  if ($old && $old->complete && !$entry->complete) {

    // Do not un-complete existing completed records.
    $entry->complete = 1;
  }

  // Allow modules to alter course reports before it goes in.
  drupal_alter('course_report', $entry, $account, $old);

  // Hello CE credit!
  if ($old) {
    $entry->crid = $old->crid;
  }
  entity_save('course_report', $entry);
}