[WatuPRO] How To Display Different Content Depending on Last / Not Last Quiz Attempt

WatuPRO has a setting to limit the number of quiz attempts by IP addres (or by username if your quiz requires user login). Sometimes you may want to display different message to the user depending on whether this is their last quiz attempt or not. For example you may want to conditionally display text like “Try again!” and “Sorry, you have no more attempts!”.

WatuPRO doesn’t yet have such configuration but it’s easy to build a custom filter using our barebone customization plugin.

If you are not interested in the code, read only the following section which also gives download link for the custom plugin. The third section of this article will show and explain the code.

Customized Filter Download and Usage

In order to use the new filter you need to download and install this plugin:

watupro-custom1.1

How to use it? Simply include your conditional content between “tags”:

{last} This content will be shown only after the last user’s attempt on the quiz. {/last}

{notlast} This content will be shown only if the user has more attempts. {/notlast}

You can include these tags in the “Final page / quiz output” tab or in the grade descriptions in case you are showing grade descriptions in your quiz output.

Here’s an example:


 

You completed quiz %%QUIZ-NAME%%.

You got %%PERCENTAGE%%% correct answers.

{last}You have no more attempts.

Here are your answers: %%ANSWERS%%{/last}

{notlast}You can try again{/notlast}


Programming Code

If you are interested in the code, here’s how we did it. In the watuprocustom_init() function we added a filter for the “watupro_content” filer:

add_filter(‘watupro_content’, array(‘WatuPROCustomFilters’, ‘last_attempt’));

And below is the filter function. It works only by IP but it’s easy to change it to work with quizzes restricted by “user_id” as well.

class WatuPROCustomFilters {
	// replace contents of  &  pseudo-tags
	//   & 
	// currently used for %%ANSWERS%% variable
   static function last_attempt($content) {
   	global $wpdb;
		if(empty($_POST['watupro_current_taking_id'])) return $content;   
		
		// now get the contents between last-attempts and not-last-attempts tags
		// in case tags are not there return content
		if(!strstr($content, '{last}') and !strstr($content, '{notlast}')) return $content; 	
   	
   	// select taking
		$taking = $wpdb->get_row($wpdb->prepare("SELECT exam_id, ip, user_id 
			FROM ".WATUPRO_TAKEN_EXAMS." WHERE ID=%d", $_POST['watupro_current_taking_id']));
			
		// select quiz allowed num takings	
		$quiz = $wpdb->get_row($wpdb->prepare("SELECT takings_by_ip FROM ".WATUPRO_EXAMS." 
			WHERE ID=%d", $taking->exam_id));
			
		$is_last_attempt = false;
			
		if($quiz->takings_by_ip) {
		
			// select num takings
			$num_takings = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM ".WATUPRO_TAKEN_EXAMS."
			WHERE exam_id=%d AND ip=%s", $taking->exam_id, $_SERVER['REMOTE_ADDR']));
			
			if($num_takings >= $quiz->takings_by_ip) $is_last_attempt = true;
		}		
		
		// now replace properly
		if($is_last_attempt) {
			// the content between  and  should be shown. The other should be removed
			$content = str_replace(array('{last}','{/last}'), '', $content);
			$content = preg_replace('#\{notlast\}(.+?)\{\/notlast\}#s', '', $content);
		}
		else {
			echo 'werehere';
			$content = str_replace(array('{notlast}','{/notlast}'), '', $content);
			$content = preg_replace('#\{last\}(.+?)\{\/last\}#s', '', $content);
		}
		
		return $content;		
			 
   }	// end last_attempt filter
}