public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/pages/page-question-pool.php
/**
 * Process the action form to change tags for the selected questions.
 *
 * @since 1.0.0
 */
Process the action form to change tags for the selected questions.
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;
	}
	$questionListToUpdate = array();
	foreach ( $_POST as $key => $value ) {
		if ( preg_match( '/^question_([0-9]+)$/', $key, $matches ) ) {
			$questionListToUpdate[] = $matches[1];
		}
	}
	if ( empty( $questionListToUpdate ) ) {
		$page->showMessage( __( 'Error. Please select some questions to update.', 'wp-courseware' ), true );
		return;
	}
	$questionListStr    = implode( ',', $questionListToUpdate );
	$validatedQuestions = $wpdb->get_col( "
		SELECT *
		FROM $wpcwdb->quiz_qs
		WHERE question_id IN ($questionListStr)
	" );
	if ( empty( $questionListToUpdate ) ) {
		$page->showMessage( __( 'Error. Those questions no longer exist. Please select some more questions to update.', 'wp-courseware' ), true );
		return;
	}
	$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;
	}
	$bulk_question_counter = 0;
	foreach ( $validatedQuestions as $questionId ) {
		$canBulkEditQuestion = false;
		$current_user        = wp_get_current_user();
		if ( user_can( $current_user->ID, 'manage_wpcw_settings' ) ) {
			$canBulkEditQuestion = true;
		}
		$question_author_id = $wpdb->get_var( $wpdb->prepare( "SELECT question_author FROM $wpcwdb->quiz_qs WHERE question_id = %d", $questionId ) );
		if ( $question_author_id == $current_user->ID ) {
			$canBulkEditQuestion = true;
		}
		$canBulkEditQuestion = apply_filters( 'wpcw_back_permissions_user_can_bulkedit_question', $canBulkEditQuestion, $current_user->ID, $questionId );
		$canBulkEditQuestion     = apply_filters( 'wpcw_permissions_user_can_bulkedit_question', $canBulkEditQuestion, $current_user, $questionId );
		$cantBulkEditQuestionMsg = apply_filters( 'wpcw_permissions_user_can_bulkedit_question_msg',
			sprintf( esc_attr__( 'You are not permitted to bulk edit Question ID: %s.', 'wp-courseware' ), $questionId ),
			$current_user,
			$questionId
		);
		if ( ! $canBulkEditQuestion ) {
			$page->showMessage( $cantBulkEditQuestionMsg, true );
			unset( $validatedQuestions[ $bulk_question_counter ] );
		}
		$bulk_question_counter ++;
	}
	$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;
	}
	if ( 'replace_tag' == $actionToProcess ) {
		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;
		}
		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;
		}
	}
	foreach ( $validatedQuestions as $questionID ) {
		switch ( $actionToProcess ) {
			case 'add_tag':
				$wpdb->query( $wpdb->prepare( "
					INSERT IGNORE $wpcwdb->question_tag_mapping
					(question_id, tag_id)
					VALUES (%d, %d)
				", $questionID, $tagDetails_first->question_tag_id ) );
				break;
			case 'remove_tag':
				$wpdb->query( $wpdb->prepare( "
					DELETE FROM $wpcwdb->question_tag_mapping
					WHERE question_id = %d
					  AND tag_id = %d
				", $questionID, $tagDetails_first->question_tag_id ) );
				break;
			case 'replace_tag':
				$wpdb->query( $wpdb->prepare( "
					UPDATE $wpcwdb->question_tag_mapping
					  SET tag_id = %d
					WHERE question_id = %d
					  AND tag_id = %d
				",
					$tagDetails_second->question_tag_id,
					$questionID, $tagDetails_first->question_tag_id
				) );
				break;
		}
	}
	WPCW_questions_tags_updatePopularity( $tagDetails_first->question_tag_id );
	if ( $tagDetails_second ) {
		WPCW_questions_tags_updatePopularity( $tagDetails_second->question_tag_id );
	}
	$page->showMessage( __( 'Questions successfully updated.', 'wp-courseware' ) );
}
function WPCW_showPage_QuestionPool_processActionForm ( $ page ) { global $ wpdb , $ wpcwdb ; $ wpdb -> show_errors ( ) ; if ( ! isset ( $ _POST [ 'wpcw_bulk_action_actions' ] ) ) { return ; } $ questionListToUpdate = array ( ) ; foreach ( $ _POST as $ key => $ value ) { if ( preg_match ( '/^question_([0-9]+)$/' , $ key , $ matches ) ) { $ questionListToUpdate [ ] = $ matches [ 1 ] ; } } if ( empty ( $ questionListToUpdate ) ) { $ page -> showMessage ( __ ( 'Error. Please select some questions to update.' , 'wp-courseware' ) , true ) ; return ; } $ questionListStr = implode ( ',' , $ questionListToUpdate ) ; $ validatedQuestions = $ wpdb -> get_col ( " 
		SELECT *
		FROM  $ wpcwdb -> quiz_qs 
		WHERE question_id IN ( $ questionListStr )
	 " ) ; if ( empty ( $ questionListToUpdate ) ) { $ page -> showMessage ( __ ( 'Error. Those questions no longer exist. Please select some more questions to update.' , 'wp-courseware' ) , true ) ; return ; } $ 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 ; } $ bulk_question_counter = 0 ; foreach ( $ validatedQuestions as $ questionId ) { $ canBulkEditQuestion = false ; $ current_user = wp_get_current_user ( ) ; if ( user_can ( $ current_user -> ID , 'manage_wpcw_settings' ) ) { $ canBulkEditQuestion = true ; } $ question_author_id = $ wpdb -> get_var ( $ wpdb -> prepare ( " SELECT question_author FROM  $ wpcwdb -> quiz_qs  WHERE question_id = %d " , $ questionId ) ) ; if ( $ question_author_id == $ current_user -> ID ) { $ canBulkEditQuestion = true ; } $ canBulkEditQuestion = apply_filters ( 'wpcw_back_permissions_user_can_bulkedit_question' , $ canBulkEditQuestion , $ current_user -> ID , $ questionId ) ; $ canBulkEditQuestion = apply_filters ( 'wpcw_permissions_user_can_bulkedit_question' , $ canBulkEditQuestion , $ current_user , $ questionId ) ; $ cantBulkEditQuestionMsg = apply_filters ( 'wpcw_permissions_user_can_bulkedit_question_msg' , sprintf ( esc_attr__ ( 'You are not permitted to bulk edit Question ID: %s.' , 'wp-courseware' ) , $ questionId ) , $ current_user , $ questionId ) ; if ( ! $ canBulkEditQuestion ) { $ page -> showMessage ( $ cantBulkEditQuestionMsg , true ) ; unset ( $ validatedQuestions [ $ bulk_question_counter ] ) ; } $ bulk_question_counter ++ ; } $ 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 ; } if ( 'replace_tag' == $ actionToProcess ) { 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 ; } 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 ; } } foreach ( $ validatedQuestions as $ questionID ) { switch ( $ actionToProcess ) { case 'add_tag' : $ wpdb -> query ( $ wpdb -> prepare ( " 
					INSERT IGNORE  $ wpcwdb -> question_tag_mapping 
					(question_id, tag_id)
					VALUES (%d, %d)
				 " , $ questionID , $ tagDetails_first -> question_tag_id ) ) ; break ; case 'remove_tag' : $ wpdb -> query ( $ wpdb -> prepare ( " 
					DELETE FROM  $ wpcwdb -> question_tag_mapping 
					WHERE question_id = %d
					  AND tag_id = %d
				 " , $ questionID , $ tagDetails_first -> question_tag_id ) ) ; break ; case 'replace_tag' : $ wpdb -> query ( $ wpdb -> prepare ( " 
					UPDATE  $ wpcwdb -> question_tag_mapping 
					  SET tag_id = %d
					WHERE question_id = %d
					  AND tag_id = %d
				 " , $ tagDetails_second -> question_tag_id , $ questionID , $ tagDetails_first -> question_tag_id ) ) ; break ; } } WPCW_questions_tags_updatePopularity ( $ tagDetails_first -> question_tag_id ) ; if ( $ tagDetails_second ) { WPCW_questions_tags_updatePopularity ( $ tagDetails_second -> question_tag_id ) ; } $ page -> showMessage ( __ ( 'Questions successfully updated.' , 'wp-courseware' ) ) ; }
Process the action form to change tags for the selected questions.
Process the action form to change tags for the selected questions .
// #### #1 - Get a list of the questions to update // We're looking for these to get the question ID // 		[question_162] => on //		[question_149] => on // Appears there's nothing to do. // #### #2 - Validate that the questions do indeed exist // Direct SQL is ok here, as IDs have been validated with the regex previously. // Appears there's nothing to do, as questions do not validate. // #### #3 - Check that the action is what we're expecting. // #### #4 - check permissions and unset what questions should be updated. // Vars // Check permissions, this condition allows admins to view all modules even if they are not the author. // Question author // Check Author // Back compat filter // Add filters to override // Display message if no access. // Increment Counter // #### #4 - Check that we have the tags that we're expecting. // Check replace tag requirements // No 2nd tag // 1st and 2nd tags match // #### #5 - By this point, everything is validated, so just execute the SQL. // Need to update tag counts // 2nd is optional, so just need to check it exists first before trying update to prevent // an error message. // #### #6 Finally show message