/** * Does this quiz contain any random questions. * * @param unknown_type $quizDetails The quiz details to check. * @return Boolean True if there are random questions, false otherwise. */ function WPCW_quizzes_randomQuestions_fullyExpand($quizDetails) { if (empty($quizDetails->questions)) { return $quizDetails->questions; } // Just need the first question to confirm if this is the case. $newQuizList = array(); foreach ($quizDetails->questions as $questionID => $singleQuestion) { // Got a random selection, so we need to get all question variations. if ('random_selection' == $singleQuestion->question_type) { // Need tags for this question $tagDetails = WPCW_quiz_RandomSelection::decodeTagSelection($singleQuestion->question_question); // Ignore limits, just get all questions $expandedQuestions = WPCW_quiz_RandomSelection::questionSelection_getRandomQuestionsFromTags($tagDetails); if (!empty($expandedQuestions)) { $newQuizList += $expandedQuestions; } } else { $newQuizList[$questionID] = $singleQuestion; } } return $newQuizList; }
/** * Given a user ID, get the selection of questions for this random question selection. This * will lock the selection to the user if the selection has not been locked already. * * @param Integer $userID The ID of the user to get the selection of questions for. * @param Integer $parentUnitID The ID of the parent unit to show this random question on. * * @return Array The list of questions that have been locked. */ public function questionSelection_getLockedQuestionSelection($userID, $parentUnitID) { global $wpcwdb, $wpdb; $wpdb->show_errors(); // #### 1 - See if the question is locked for a user? If so, return locked selection. $lockedSelection = $wpdb->get_var($wpdb->prepare("\n\t\t\t\tSELECT question_selection_list \n\t\t\t\tFROM {$wpcwdb->question_rand_lock}\n\t\t\t\tWHERE question_user_id = %d\t\t\t\t \n\t\t\t\t AND rand_question_id = %d\n\t\t\t\t AND parent_unit_id = %d \n\t\t\t", $userID, $this->quizItem->question_id, $parentUnitID)); // #### 2 - Got a selection, turn this into question IDs. if ($lockedSelection) { // Quickly validate that it's just numbers and commas, then use directly in SQL // for an array search of question IDs that match the selection. If the string // does not validate as a string of numbers and commas, then we'll generate a new // selection using the code below. if (preg_match('/^([0-9]+,?)+$/', $lockedSelection)) { $questionsToUseWithIDKey = array(); // ORDER BY FIELD is a function that will sort the questions in the order they appear in // the comma-separated list. $questionsToUse = $wpdb->get_results("\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM {$wpcwdb->quiz_qs}\n\t\t\t\t\tWHERE question_id IN ({$lockedSelection})\n\t\t\t\t\t AND question_type != 'random_selection'\n\t\t\t\t\tORDER BY FIELD(question_id, {$lockedSelection})\n\t\t\t\t"); if (!empty($questionsToUse)) { // Need to remap to use ID => question foreach ($questionsToUse as $questionsToUse_single) { $questionsToUseWithIDKey[$questionsToUse_single->question_id] = $questionsToUse_single; } } return $questionsToUseWithIDKey; } } // #### 3 - Selection is not locked, so we generate a new selection for the user and then lock/save it to the database. $tagDetails = WPCW_quiz_RandomSelection::decodeTagSelection($this->quizItem->question_question); $questionsToUse = WPCW_quiz_RandomSelection::questionSelection_getRandomQuestionsFromTags($tagDetails); if (!empty($questionsToUse)) { // Need to get the IDs of this question to lock it. Don't need any other data. $questionIDList = array(); foreach ($questionsToUse as $questionDetails) { $questionIDList[] = $questionDetails->question_id; } // Now insert this list into the database to create the lock. Handling // duplicates elegantly here by using REPLACE INTO intentionally $wpdb->query($wpdb->prepare("\n\t\t\t\tREPLACE INTO {$wpcwdb->question_rand_lock}\n\t\t\t\t(question_user_id, rand_question_id, question_selection_list, parent_unit_id) \n\t\t\t\tVALUES (%d, %d, %s, %d)\n\t\t\t", $userID, $this->quizItem->question_id, implode(',', $questionIDList), $parentUnitID)); } return $questionsToUse; }