/** * Function to add the specified list of course IDs for the specified user. * * @param Integer $user_id The ID of the user to update. * @param Mixed $courseIDs The ID or array of IDs of the course IDs to give the user access to. * @param Boolean $syncMode If 'sync', then remove access to any course IDs not mentioned in $courseIDs parameter. If 'add', then just add access for course IDs that have been specified. */ function WPCW_courses_syncUserAccess($user_id, $courseIDs, $syncMode = 'add') { // Not a valid user if (!get_userdata($user_id)) { return; } global $wpdb, $wpcwdb; $wpdb->show_errors(); // List of course IDs that actually exist. $courseList = array(); // Got a list of IDs? if (is_array($courseIDs)) { // List is empty, save a query if (count($courseIDs) > 0) { // Yep, this course actually exists foreach ($courseIDs as $potentialCourseID) { if ($courseDetails = WPCW_courses_getCourseDetails($potentialCourseID)) { $courseList[$potentialCourseID] = $courseDetails; } } } } else { if ($courseDetails = WPCW_courses_getCourseDetails($courseIDs)) { $courseList[$courseIDs] = $courseDetails; } } // Check if we want to remove access for courses that are not mentioned. // We'll add any they should have access to in a mo. if ($syncMode == 'sync') { $str_courseIDs = false; $courseIDCount = count(array_keys($courseList)); // Actually got some IDs to remove, so create an SQL string with all IDs if ($courseIDCount > 0) { $str_courseIDs = implode(",", array_keys($courseList)); // Remove meta for this user all previous courses. // Previous version deleteted all courses then re-created them. As a result, data was being lost about email being sent. $wpdb->query($wpdb->prepare("DELETE FROM {$wpcwdb->user_courses} WHERE user_id = %d AND course_id NOT IN ({$str_courseIDs})", $user_id)); } else { $wpdb->query($wpdb->prepare("DELETE FROM {$wpcwdb->user_courses} WHERE user_id = %d", $user_id)); } } // Only process valid course IDs if (count($courseList) > 0) { foreach ($courseList as $validCourseID => $courseDetails) { // See if this is already in the database. if (!$wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpcwdb->user_courses} WHERE user_id = %d AND course_id = %d", $user_id, $validCourseID))) { // Actually add reference in database as it doesn't exist. $wpdb->query($wpdb->prepare("INSERT INTO {$wpcwdb->user_courses} (user_id, course_id, course_progress) VALUES(%d, %d, 0)", $user_id, $validCourseID)); } // Get a total count of units in this course $SQL = $wpdb->prepare("\n\t\t \tSELECT COUNT(*) \n\t\t \tFROM {$wpcwdb->units_meta} \n\t\t \tWHERE parent_course_id = %d\n\t\t ", $validCourseID); $totalUnitCount = $wpdb->get_var($SQL); // Calculate the user's progress, in case they've still got completed progress // in the database. WPCW_users_updateUserUnitProgress($validCourseID, $user_id, $totalUnitCount); } } }
/** * This function removes the user progress for the specified list of users and units. * * @param Array $userList The list of users to reset. * @param Array $unitList The list of units to remove from their progress. * @param Object $courseDetails The details of the course. * @param Integer $totalUnitCount The total number of units in this course. */ function WPCW_users_resetProgress($userList, $unitList, $courseDetails, $totalUnitCount) { global $wpcwdb, $wpdb; $wpdb->show_errors(); // Nothing to do! if (empty($userList) || empty($unitList)) { return; } $SQL_units = '(' . implode(',', $unitList) . ')'; $SQL_users = '(' . implode(',', $userList) . ')'; // Delete all data in user progress in one hit $SQL = "DELETE FROM {$wpcwdb->user_progress}\n\t\t\tWHERE user_id IN {$SQL_users}\n\t\t\t AND unit_id IN {$SQL_units}\n\t\t\t"; $wpdb->query($SQL); // Delete all quiz data in one hit $SQL = "DELETE FROM {$wpcwdb->user_progress_quiz}\n\t\t\tWHERE user_id IN {$SQL_users}\n\t\t\t AND unit_id IN {$SQL_units}\n\t\t\t"; $wpdb->query($SQL); // Delete all user locks $SQL = "DELETE FROM {$wpcwdb->question_rand_lock}\n\t\t\tWHERE question_user_id IN {$SQL_users}\n\t\t\t AND parent_unit_id IN {$SQL_units}\n\t\t\t"; $wpdb->query($SQL); // Now update the user progress. foreach ($userList as $aUser) { $progressExists = $wpdb->get_row($wpdb->prepare("\n\t\t\tSELECT * \n\t\t\tFROM {$wpcwdb->user_courses} \n\t\t\tWHERE user_id = %d \n\t\t\t AND course_id = %d\n\t\t", $aUser, $courseDetails->course_id)); if ($progressExists) { // Update the progress with their actual progress count. WPCW_users_updateUserUnitProgress($courseDetails->course_id, $aUser, $totalUnitCount); } } }