/**
  * Check the tags that have been provided, and get the details for each tag.
  * 
  * @param unknown_type $dataToDecode The data to decode.
  * 
  * @return Array The validated tags as tag_[number] => array (count => $count, 'name' => $name).
  */
 static function decodeTagSelection($dataToDecode)
 {
     $decodedData = json_decode($dataToDecode, true);
     $newDataToReturn = false;
     // Validate that the data is indeed JSON encoded.
     if (json_last_error() === JSON_ERROR_NONE) {
         if (!empty($decodedData)) {
             foreach ($decodedData as $key => $value) {
                 // Have we got the whole pool tag? If so, process and abort.
                 // Should be no further tags in this section.
                 if ('whole_pool' == $key) {
                     $newDataToReturn[$key] = array('count' => $value, 'name' => __('Entire Question Pool', 'wp_courseware'), 'tag_id' => false, 'tag_usage' => WPCW_questions_getQuestionCount());
                     break;
                 } else {
                     if (preg_match('/^tag_([0-9]+)$/', $key, $matches)) {
                         // Extract details for this tag.
                         $tagDetails = WPCW_questions_tags_getTagDetails($matches[1]);
                         if ($tagDetails) {
                             $newDataToReturn[$key] = array('count' => $value, 'name' => $tagDetails->question_tag_name, 'tag_id' => $tagDetails->question_tag_id, 'tag_usage' => $tagDetails->question_tag_usage);
                         }
                         // end tag detail check
                     }
                     // end preg_match
                 }
                 // end if whole_pool
             }
             // end foreach
         }
         // end if (!empty($decodedData))
     }
     return $newDataToReturn;
 }
Example #2
0
 /**
  * Export custom feedback messages for a specific quiz.
  */
 function export_content_handleQuizzes_customFeedbackMessages($quizObj, $feedbackParentPath)
 {
     $xml = false;
     $feedbackPath = $feedbackParentPath . '/custom_feedback_msgs';
     global $fieldsToProcess_quiz_custom_feedback;
     // Check for messages and render in the XML
     $messageList = WPCW_quizzes_feedback_getFeedbackMessagesForQuiz($quizObj->quiz_id);
     if (!empty($messageList)) {
         // Start msgs block
         $xml .= $this->export_startBlock($feedbackPath, 'custom_feedback_msgs');
         // Show each single message
         foreach ($messageList as $singleMessage) {
             // Add the name of the tag rather than the ID, so that this can be matched
             // up later on import.
             $tagDetails = WPCW_questions_tags_getTagDetails($singleMessage->qfeedback_tag_id);
             $singleMessage->qfeedback_tag_name = $tagDetails->question_tag_name;
             $xml .= $this->export_objectToXML('custom_feedback_msg', false, $singleMessage, $fieldsToProcess_quiz_custom_feedback, $feedbackPath . '/', '/custom_feedback_msg');
         }
         // End msgs block
         $xml .= $this->export_endBlock($feedbackPath, 'custom_feedback_msgs');
     }
     return $xml;
 }
/**
 * 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'));
}