/**
 * Marks a unit as complete for the specified user. No error checking is made to check that the user
 * is allowed to update the record, it's assumed that the permission checking has been done before this step.
 * 
 * @param Integer $userID The ID of the user that's completed the unit.
 * @param Integer $unitID The ID of the unit that's been completed.
 */
function WPCW_units_saveUserProgress_Complete($userID, $unitID)
{
    global $wpdb, $wpcwdb;
    $wpdb->show_errors();
    $keyColumns = array('user_id', 'unit_id');
    $data = array();
    $data['unit_completed_status'] = 'complete';
    $data['unit_completed_date'] = current_time('mysql');
    $data['user_id'] = $userID;
    $data['unit_id'] = $unitID;
    $progress = doesRecordExistAlready($wpcwdb->user_progress, $keyColumns, array($userID, $unitID));
    if ($progress) {
        // Has it been marked as complete? If so, we don't want to do that again to preserve the date.
        // We generally shouldn't get here, but protect anyway.
        if ($progress->unit_completed_status == 'complete') {
            return false;
        }
        $SQL = arrayToSQLUpdate($wpcwdb->user_progress, $data, $keyColumns);
    } else {
        $SQL = arrayToSQLInsert($wpcwdb->user_progress, $data);
    }
    $wpdb->query($SQL);
}
Exemple #2
0
/**
* Updates the database to generate a certificate entry. If a certificate already exists for the user/course ID,
* then no new entry is created.
* 
* @param Integer $userID The ID of the user that the certificate is being generated for.
* @param Integer $courseID The ID of the associated course.
*/
function WPCW_certificate_generateCertificateEntry($userID, $courseID)
{
    if (!$userID || !$courseID) {
        return;
    }
    global $wpdb, $wpcwdb;
    $wpdb->show_errors();
    // Already have a record for this certificate.
    if ($certificateDetails = doesRecordExistAlready($wpcwdb->certificates, array('cert_user_id', 'cert_course_id'), array($userID, $courseID))) {
        return $certificateDetails;
    }
    // Create anonymous entry to allow users to access a certificate when they've completed a course.  Means that certificates
    // stay existing even if units are added to a course.
    $data = array();
    $data['cert_user_id'] = $userID;
    $data['cert_course_id'] = $courseID;
    $data['cert_generated'] = current_time('mysql');
    $data['cert_access_key'] = md5(serialize($data));
    // Unique key based on data we've just added
    $SQL = arrayToSQLInsert($wpcwdb->certificates, $data);
    $wpdb->query($SQL);
    // Return details of the added certificate
    return getRecordDetails($wpcwdb->certificates, array('cert_user_id', 'cert_course_id'), array($userID, $courseID));
}