You are here

function course_report_save in Course 6

Same name and namespace in other branches
  1. 7.2 course.module \course_report_save()
  2. 7 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 enrolment action

File

./course.module, line 2322
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);
  $sql = "SELECT * FROM {course_report} WHERE nid = %d AND uid = %d";
  $result = db_query($sql, $entry->nid, $entry->uid);
  $old = db_fetch_object($result);
  $entry->updated = time();
  if ($entry->complete && empty($entry->date_completed)) {
    $entry->date_completed = 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) {
    drupal_write_record('course_report', $entry, array(
      'nid',
      'uid',
    ));
  }
  else {
    drupal_write_record('course_report', $entry);
  }

  // Notify modules that a course report has been saved.
  module_invoke_all('course_report_saved', $entry, $account, $old);
}