/** * Function called when a new question tag is added. */ function WPCW_AJAX_handleQuestionNewTag() { $ajaxResults = array('success' => true, 'errormsg' => __('Unfortunately there was a problem adding the tag.', 'wp_courseware'), 'html' => false); // Assume that we may have multiple tags, separated by commas. $potentialTagList = explode(',', WPCW_arrays_getValue($_POST, 'tagtext')); $cleanTagList = array(); // Check if question is expected to have been saved. $hasQuestionBeenSaved = 'yes' == WPCW_arrays_getValue($_POST, 'isquestionsaved'); // Got potential tags if (!empty($potentialTagList)) { // Clean up each tag, and add to a list. foreach ($potentialTagList as $potentialTag) { $cleanTagList[] = sanitize_text_field(stripslashes($potentialTag)); } // Check that cleaned tags are ok too if (!empty($cleanTagList)) { // Do this if the question exists and we're adding tags. if ($hasQuestionBeenSaved) { // Get the ID of the question we're adding this tag to. $questionID = intval(WPCW_arrays_getValue($_POST, 'questionid')); // Validate that the question exists before we tag it. $questionDetails = WPCW_questions_getQuestionDetails($questionID); if (!$questionDetails) { $ajaxResults['errormsg'] = __('Unfortunately that question could not be found, so the tag could not be added.', 'wp_courseware'); $ajaxResults['success'] = false; } else { // Add the tag to the database, get a list of the tag details now that they have been added. $tagDetailList = WPCW_questions_tags_addTags($questionID, $cleanTagList); foreach ($tagDetailList as $tagAddedID => $tagAddedText) { // Create the HTML to show the new tag. $ajaxResults['html'] .= sprintf('<span><a data-questionid="%d" data-tagid="%d" class="ntdelbutton">X</a> %s</span>', $questionID, $tagAddedID, $tagAddedText); } } // else question found } else { $tagDetailList = WPCW_questions_tags_addTags_withoutQuestion($cleanTagList); // For a new question, the ID is a string, not a value. $questionIDStr = WPCW_arrays_getValue($_POST, 'questionid'); // Create a hidden form entry plus the little tag, so that we can add the tag to the question when we save. foreach ($tagDetailList as $tagAddedID => $tagAddedText) { // Create the HTML to show the new tag. We'll add the full string to the hidden field so that we can // add the tags later. $ajaxResults['html'] .= sprintf(' <span> <a data-questionid="%d" data-tagid="%d" class="ntdelbutton">X</a> %s <input type="hidden" name="tags_to_add%s[]" value="%s" /> </span> ', 0, $tagAddedID, $tagAddedText, $questionIDStr, addslashes($tagAddedText)); } // end foreach } } } header('Content-Type: application/json'); echo json_encode($ajaxResults); die; }
/** * 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; }