$value) $conditions[] = explode(",",$value); //for each condition, check the response table for($i_condition = 0;$i_condition < $num_conditions; $i_condition++) { unset($condition_test_response); //find the form number that we want to query the previous responses from //if form number field was negative, go back that many forms, if positive, the form number indicates the form order in the experiment //reference to the entire condition string (used for setting flags on this condition) $condition_form_occurrence = $conditions[$i_condition][0]; $condition_form_id = $conditions[$i_condition][1]; $condition_trial_form = $conditions[$i_condition][2]; $condition_question_id = $conditions[$i_condition][3]; $condition_form_question_num = $conditions[$i_condition][4]; $condition_question_iteration = $conditions[$i_condition][5]; $condition_subquestion = $conditions[$i_condition][6]; if($condition_trial_form) $condition_trial_order = $_SESSION['trial_order'] - $conditions[$i_condition][7]; else $condition_trial_order = 0; $condition_stim = $conditions[$i_condition][8]; $condition_type = $conditions[$i_condition][9]; for($i_response = 10;$i_response < count($conditions[$i_condition]);$i_response++) $condition_test_response[] = $conditions[$i_condition][$i_response]; //check the condref for the form that this condition depends on //if condref is false, the parent form was not visited the last time around $condition_reference = 'condref_'.$condition_form_id; if(isset($_SESSION[$condition_reference]) && !($_SESSION[$condition_reference])) { $condition_met = FALSE; break; } $check_same_stim = ($condition_stim == 'same_stim' && $_SESSION['stimulus_id'] != NULL) ? sprintf("AND stimulus_id = %d ",$_SESSION['stimulus_id']) : ""; //select the last response given by the subject. //this may be either a trial response or a regular questionnaire response //The session_id and subject_id are selected and either the fields (question_id, etc) associated with the question, or the trial_id //The parenthesis before trial_order and at the end indicate that the OR clause should be given priority in the selection logic if($_SESSION['encrypted_response_table']) { $enc_key = subinfo_encryption_key(); $select_fields = sprintf("aes_decrypt(response_text,'%s') as response_text, aes_decrypt(response_enum,'%s') as response_enum",$enc_key,$enc_key); } else { $select_fields = "response_text,response_enum"; } $sql_conditional_response = sprintf("SELECT %s from %s left join form_x_trial using (form_id,trial_id) ",$select_fields,$_SESSION['response_table']). sprintf("WHERE session_id = %d and subject_id = %s and %s.form_id = %d ", GetSQLValueString($_SESSION['session_id'],"int"), GetSQLValueString($_SESSION['subject_id'],"text"), $_SESSION['response_table'], GetSQLValueString($condition_form_id,"int")). $check_same_stim. sprintf("AND (trial_order = %d ",$condition_trial_order). sprintf("OR question_id = %d AND form_question_num = %d ", GetSQLValueString($condition_question_id,"int"), GetSQLValueString($condition_form_question_num,"int")). sprintf("AND question_iteration = %d AND subquestion = %d) order by response_order", GetSQLValueString($condition_question_iteration,"int"), GetSQLValueString($condition_subquestion,"int")); mysql_select_db($database_subject,$subject); $conditional_response = mysql_query($sql_conditional_response,$subject) or report_error_form(mysql_error()); if($condition_form_occurrence < 0) $num_response = mysql_num_rows($conditional_response) + $condition_form_occurence; else $num_response = $condition_form_occurence; for($i_response = 0; $i_response < $num_response; $i_response++) { $row_conditional_response = mysql_fetch_assoc($conditional_response); } //check all responses that apply to this condition //if the condition is met by any one response, the condition is considered met for($i_response = 0;$i_response < count($condition_test_response);$i_response++) { switch($condition_type) { case "response_enum": $bitmask = pow(2,((int)($condition_test_response[$i_response] - 1))); //since the response can contain more than one enum answer (submitted via checkbox) //we are performing a bitwise AND with $bitmask, which only corresponds to a single answer //this means that we don't need an exact match for the condition to pass //As long as the subject matched at least one of their enums to a single enum necessary to pass //the condition, then the condition passes. if((boolean) ($row_conditional_response[$condition_type] & $bitmask)) $condition_met = TRUE; else $condition_met = FALSE; break; case "response_text": if($row_conditional_response[$condition_type] != $condition_test_response[$i_response]) $condition_met = FALSE; else $condition_met = TRUE; break; } //break out of inner for loop if we found a match, then $condition_met == TRUE for that response if($condition_met) { break; } } //break out of condition checking if a condition has not been met if(!($condition_met)) { break; } } //for loop } if($condition_met && ($condition_matlab != "")) { $pass_matlab_command = parse_matlab_arguments($condition_matlab); $matrpc_recv = matrpc_message($pass_matlab_command); if($matrpc_recv == $COND_MATLAB_FALSE_STRING) $condition_met = FALSE; elseif($matrpc_recv == $COND_MATLAB_TRUE_STRING) $condition_met = TRUE; else report_error_form(sprintf("Matlab conditional function \"%s\" returned \"%s\" when \"%s\" or \"%s\" was expected.", $pass_matlab_command,$matrpc_recv,$COND_MATLAB_TRUE_STRING,$COND_MATLAB_FALSE_STRING),"form_processing/check_conditions.php"); } //if the handler needs to break out of a loop due to condition(s) not passing, then skip forms if(!($condition_met) && isset($exit_loop_if_cond_not_met) && $exit_loop_if_cond_not_met) { if(isset($_SESSION['loop_end']) && isset($_SESSION['loop_goto'])) { $_SESSION['num_skip_forms'] = $_SESSION['loop_end'] - $_SESSION['last_visited'] + 1; } else { //loop end hasn't been recorded yet so see if we can find the next end of loop //if it is found, skip that many forms, otherwise, just skip one form. $sql_find_loop = sprintf("select form_order,goto from experiment_x_form where experiment_id = %d and form_order >= %d and goto is not null order by form_order",$_SESSION['experiment_id'],$_SESSION['last_visited']); $loop_end = mysql_select($sql_find_loop); if(!is_null($loop_end['goto'])) $_SESSION['num_skip_forms'] = $loop_end['form_order'] - $_SESSION['last_visited'] + 1; else $_SESSION['num_skip_forms'] = 1; } } //don't display this form and move on to the next if condition(s) were not met. if(!($condition_met)) { include($questionnaire_dir."form_processing/update_session.php"); } ?>