function wpcw_tag_cleanup() { global $wpdb, $wpcwdb; $wpcwdb = new WPCW_Database(); $tags = array(); $SQL = "SELECT *\n\t\t\tFROM {$wpcwdb->question_tag_mapping}"; $tags = $wpdb->get_results($SQL); foreach ($tags as $tag) { $questionDetails = WPCW_questions_getQuestionDetails($tag->question_id); if (!$questionDetails) { WPCW_questions_tags_removeTag($tag->question_id, $tag->tag_id); } WPCW_questions_tags_updatePopularity($tag->tag_id); } }
/** * Process the action form to change tags for the selected questions. */ function WPCW_showPage_QuestionPool_processActionForm($page) { global $wpdb, $wpcwdb; $wpdb->show_errors(); if (!isset($_POST['wpcw_bulk_action_actions'])) { return; } // #### #1 - Get a list of the questions to update $questionListToUpdate = array(); foreach ($_POST as $key => $value) { // We're looking for these to get the question ID // [question_162] => on // [question_149] => on if (preg_match('/^question_([0-9]+)$/', $key, $matches)) { $questionListToUpdate[] = $matches[1]; } } // Appears there's nothing to do. if (empty($questionListToUpdate)) { $page->showMessage(__('Error. Please select some questions to update.', 'wp_courseware'), true); return; } // #### #2 - Validate that the questions do indeed exist // Direct SQL is ok here, as IDs have been validated with the regex previously. $questionListStr = implode(',', $questionListToUpdate); $validatedQuestions = $wpdb->get_col("\n\t\tSELECT * \n\t\tFROM {$wpcwdb->quiz_qs}\n\t\tWHERE question_id IN ({$questionListStr}) \n\t"); // Appears there's nothing to do, as questions do not validate. if (empty($questionListToUpdate)) { $page->showMessage(__('Error. Those questions no longer exist. Please select some more questions to update.', 'wp_courseware'), true); return; } // #### #3 - Check that the action is what we're expecting. $actionToProcess = WPCW_arrays_getValue($_POST, 'wpcw_bulk_action_actions'); switch ($actionToProcess) { case 'add_tag': case 'remove_tag': case 'replace_tag': break; default: $page->showMessage(__('Error. Did not recognise action to apply to selected questions.', 'wp_courseware'), true); return; break; } // #### #4 - Check that we have the tags that we're expecting. $tagID_first = WPCW_arrays_getValue($_POST, 'wpcw_bulk_action_select_tag_a', 0); $tagID_second = WPCW_arrays_getValue($_POST, 'wpcw_bulk_action_select_tag_b', 0); $tagDetails_first = false; $tagDetails_second = false; if (!($tagDetails_first = WPCW_questions_tags_getTagDetails($tagID_first))) { $page->showMessage(__('Error. The first tag does not exist. Please select another tag.', 'wp_courseware'), true); return; } // Check replace tag requirements if ('replace_tag' == $actionToProcess) { // No 2nd tag if (!($tagDetails_second = WPCW_questions_tags_getTagDetails($tagID_second))) { $page->showMessage(__('Error. The second tag does not exist. Please select another tag.', 'wp_courseware'), true); return; } // 1st and 2nd tags match if ($tagDetails_first->question_tag_id == $tagDetails_second->question_tag_id) { $page->showMessage(__('Error. The first and second tag should be different.', 'wp_courseware'), true); return; } } // #### #5 - By this point, everything is validated, so just execute the SQL. foreach ($validatedQuestions as $questionID) { switch ($actionToProcess) { case 'add_tag': $wpdb->query($wpdb->prepare("\n\t\t\t\t\tINSERT IGNORE {$wpcwdb->question_tag_mapping}\n\t\t\t\t\t(question_id, tag_id) \n\t\t\t\t\tVALUES (%d, %d) \n\t\t\t\t", $questionID, $tagDetails_first->question_tag_id)); break; case 'remove_tag': $wpdb->query($wpdb->prepare("\n\t\t\t\t\tDELETE FROM {$wpcwdb->question_tag_mapping}\n\t\t\t\t\tWHERE question_id = %d\n\t\t\t\t\t AND tag_id = %d \n\t\t\t\t", $questionID, $tagDetails_first->question_tag_id)); break; case 'replace_tag': $wpdb->query($wpdb->prepare("\n\t\t\t\t\tUPDATE {$wpcwdb->question_tag_mapping}\n\t\t\t\t\t SET tag_id = %d\n\t\t\t\t\tWHERE question_id = %d\n\t\t\t\t\t AND tag_id = %d \n\t\t\t\t", $tagDetails_second->question_tag_id, $questionID, $tagDetails_first->question_tag_id)); break; } } // Need to update tag counts WPCW_questions_tags_updatePopularity($tagDetails_first->question_tag_id); // 2nd is optional, so just need to check it exists first before trying update to prevent // an error message. if ($tagDetails_second) { WPCW_questions_tags_updatePopularity($tagDetails_second->question_tag_id); } // #### #6 Finally show message $page->showMessage(__('Questions successfully updated.', 'wp_courseware')); }
/** * Given a list of tags, try to add them to the specified question. * * @param Integer $questionID The ID of the question that we're adding the tag for. * @param Array $tagList The list of tags to add. * @return Array The list of tags to be rendered again. */ function WPCW_questions_tags_addTags($questionID, $tagList) { if (empty($tagList)) { return; } global $wpdb, $wpcwdb; $wpdb->show_errors(); $taglistToReturn = WPCW_questions_tags_addTags_withoutQuestion($tagList); // Now we need to work through and associate each tag with the question foreach ($taglistToReturn as $tagID => $tagText) { // Create association with a question $wpdb->query($wpdb->prepare("\n\t\t\tINSERT IGNORE INTO {$wpcwdb->question_tag_mapping} \n\t\t\t(question_id, tag_id)\n\t\t\tVALUES (%d, %d)\n\t\t", $questionID, $tagID)); WPCW_questions_tags_updatePopularity($tagID); } return $taglistToReturn; }