\n");
printf("Condition Editor\n");
$sql_get_exf_info = sprintf("select experiment_title, form_order, form_name from experiment_x_form left join form using (form_id) left join experiment on experiment_x_form.experiment_id = experiment.experiment_id ").
sprintf("where experiment.experiment_id = %d and form.form_id = %d and form_order = %d", $_SESSION['editor_experiment_id'],$_SESSION['form_id'],$_SESSION['form_order']);
$get_exf_info = mysql_select($sql_get_exf_info);
printf("Editing conditions for experiment '%s,' form '%s,' order %d",$get_exf_info['experiment_title'],$get_exf_info['form_name'],$get_exf_info['form_order']);
//if a form or question was changed in a pulldown, checkbox, or textbox, this code updates the condition array
//and the listed enums
if(!isset($_POST['submit_condition']) && isset($_POST['change_condition'])) {
//initialize the posted condition to all 0s
$posted_condition = array(0,0,0,0,0,0,0,0,0);
//parse the posted fields and copy the submitted field values to the $conditions array
foreach($_POST as $key=>$value) {
if(preg_match("|([[:digit:]+]),([[:digit:]+])|",$key,$test_match)) {
//only assign $test_match to $matches
//if the preg_match matched the pattern
//preg_match assigns results to $test_match regardless of whether the pattern was matched
//this is different behavior than ereg(), now deprecated
$matches = $test_match;
if ($matches[2] == 0 || $matches[2] == 7)
$value *= -1;
$posted_condition[$matches[2]] = $value;
}
}
$condition_idx = $matches[1];
foreach($_POST as $key=>$value) {
if(preg_match("|([[:digit:]+]),enum[[:digit:]+]|",$key,$test_match)) {
$matches = $test_match;
$posted_condition[] = $value;
}
}
$old_condition = $_SESSION['editor_conditions'][$condition_idx];
//if a new form id was posted, initialize the question_id, form_question_num, and subquestion
if ($posted_condition[1] != $old_condition[1]) {
$sql_get_valid_question_ids = sprintf("select question_id, form_question_num from form_x_question where form_id = %d group by question_id",$posted_condition[1]);
$valid_question_id = mysql_select($sql_get_valid_question_ids);
$posted_condition[3] = $valid_question_id['question_id'];
$posted_condition[4] = $valid_form_question_num['form_question_num'];
$posted_condition[5] = 1;
$posted_condition[6] = 1;
}
//if a new question id was posted, clear out the selected enum values and reset form_question_num
if($posted_condition[3] != $old_condition[3]) {
$posted_condition = array_slice($posted_condition,0,9);
$sql_form_quest_num = sprintf("select form_question_num from form_x_question where form_id = %d and question_id =%d",$posted_condition[1],$posted_condition[3]);
$form_quest_num = mysql_select($sql_form_quest_num);
$posted_condition[4] = $form_quest_num['form_question_num'];
$posted_condition[5] = 1;
$posted_condition[6] = 1;
}
//set the modified $posted_condition to it's place in $_SESSION['editor_conditions']
$_SESSION['editor_conditions'][$matches[1]] = $posted_condition;
$_SESSION['editor_condition_submit_mask'][$condition_idx] = 0;
}
//a new condition was submitted (after having added the form) or an old one was changed
if(isset($_POST['submit_condition']) && ($_POST['submit_condition'] == 'submit')) {
//clear all rows in $conditions that refer to posted condition clauses. These are simply set to an empty array
foreach($_POST as $key=>$value) {
if(ereg("([[:digit:]+]),0",$key,$matches)) {
$_SESSION['editor_conditions'][$matches[1]] = array();
}
}
//parse the posted fields and copy the submitted field values to the $conditions array
foreach($_POST as $key=>$value) {
if(preg_match("|([[:digit:]+]),([[:digit:]+])|",$key,$test_match)) {
//only assign $test_match to $matches
//if the preg_match matched the pattern
//preg_match assigns results to $test_match regardless of whether the pattern was matched
//this is different behavior than ereg(), now deprecated
$matches = $test_match;
if ($matches[2] == 0 || $matches[2] == 7) {
//if this is the first value (form occurrence) or seventh value (trial order)
//then multiply the value by -1 to indicate this many forms or trials in the past
$value *= -1;
}
$_SESSION['editor_conditions'][$matches[1]][$matches[2]] = $value;
}
}
//obtain number of enums so that we can ensure that checked enum values are valid
//this is necessary when changing question_ids or subquestions
$matched_question_id = $_SESSION['editor_conditions'][$matches[1]][3];
$matched_subquestion = $_SESSION['editor_conditions'][$matches[1]][6];
$sql_get_enum_values = sprintf("select enum_values from question_x_data_format left join data_format on (question_x_data_format.answer_format_id = data_format.data_format_id) where ").
sprintf("question_id = %d and subquestion = %d",$matched_question_id,$matched_subquestion);
$get_enum_values = mysql_select($sql_get_enum_values);
$enum_value_array = explode(",",$get_enum_values['enum_values']);
$num_enums = sizeof($enum_value_array);
//the explode function returns a single value of null when exploding an empty string, so need to set num_enums=0 (it would be 1 otherwise)
if($enum_value_array[0] == NULL)
$num_enums = 0;
foreach($_POST as $key=>$value) {
if(preg_match("|([[:digit:]+]),enum[[:digit:]+]|",$key,$matches) && ($value <= $num_enums)) {
$_SESSION['editor_conditions'][$matches[1]][] = $value;
}
}
//after copying the conditions to the $_SESSION['editor_conditions'] array, submit the conditions to MySQL
$numrows = sizeof($_SESSION['editor_conditions']);
for($irow = 0; $irow < $numrows; $irow++) {
$condition_clauses[] = implode(",",$_SESSION['editor_conditions'][$irow]);
}
$all_conditions = implode("\n",$condition_clauses);
$sql_submit_conditions = sprintf("update experiment_x_form set `condition` = '%s' where experiment_id = %d and form_id = %d and form_order = %d",
$all_conditions,$_SESSION['editor_experiment_id'],$_SESSION['form_id'],$_SESSION['form_order']);
mysql_update($sql_submit_conditions);
fetch_conditions();
}
//delete button was pressed for a condition
if(isset($_POST['delete_condition']) && ($_POST['delete_condition'] == 'delete')) {
//remove the row corresponding to the condition
foreach($_POST as $key=>$value) {
if(preg_match("|([[:digit:]+]),0|",$key,$matches))
array_splice($_SESSION['editor_conditions'],$matches[1],1);
}
//after copying the conditions to the $_SESSION['editor_conditions'] array, submit the conditions to MySQL
$numrows = sizeof($_SESSION['editor_conditions']);
for($irow = 0; $irow < $numrows; $irow++) {
$condition_clauses[] = implode(",",$_SESSION['editor_conditions'][$irow]);
}
($numrows > 0)? $all_conditions = implode("\n",$condition_clauses) : $all_conditions = NULL;
$sql_submit_conditions = sprintf("update experiment_x_form set `condition` = %s where experiment_id = %d and form_id = %d and form_order = %d",
GetSQLValueString($all_conditions,"text"),$_SESSION['editor_experiment_id'],$_SESSION['form_id'],$_SESSION['form_order']);
mysql_update($sql_submit_conditions);
fetch_conditions();
}
//if a form was added to the list of conditions, add it to the conditions array with default values
if(isset($_POST['add_form_to_conditions']) && ($_POST['add_form_to_conditions'])) {
//obtain the list of questions that belong to this form
$sql_get_form_question = sprintf("select question.question_id, form_question_num, question_iteration from form_x_question left join question using (question_id) where form_id = %d",$_POST['add_form_id']);
$row_form_question = mysql_select($sql_get_form_question);
$new_condition[0] = -1;
$new_condition[1] = $_POST['add_form_id'];
$new_condition[2] = 0;
$new_condition[3] = $row_form_question['question_id'];
$new_condition[4] = $row_form_question['form_question_num'];
$new_condition[5] = $row_form_question['question_iteration'];
$new_condition[6] = 1;
$new_condition[7] = 0;
$new_condition[8] = 1;
$new_condition[9] = "response_enum";
//add the new_conditions array to conditions
$_SESSION['editor_conditions'][] = $new_condition;
$_SESSION['editor_num_conditions']++;
$_SESSION['editor_condition_submit_mask'][] = 0;
}
for($i_condition = 0; $i_condition < $_SESSION['editor_num_conditions']; $i_condition++) {
$condition_form_occurrence = $_SESSION['editor_conditions'][$i_condition][0];
$condition_form_id = $_SESSION['editor_conditions'][$i_condition][1];
$condition_trial_form = $_SESSION['editor_conditions'][$i_condition][2];
$condition_question_id = $_SESSION['editor_conditions'][$i_condition][3];
$condition_form_question_num = $_SESSION['editor_conditions'][$i_condition][4];
$condition_question_iteration = $_SESSION['editor_conditions'][$i_condition][5];
$condition_subquestion = $_SESSION['editor_conditions'][$i_condition][6];
$condition_trial_order = $_SESSION['editor_conditions'][$i_condition][7];
$condition_stim = $_SESSION['editor_conditions'][$i_condition][8];
$condition_type = $_SESSION['editor_conditions'][$i_condition][9];
unset($condition_test_response);
for($i_response = 10;$i_response < count($_SESSION['editor_conditions'][$i_condition]);$i_response++)
$condition_test_response[] = $_SESSION['editor_conditions'][$i_condition][$i_response];
$sql_get_subquestion_info = sprintf("select question_text, heading, subquestion, type, enum_values from question_x_data_format left join question using (question_id) ").
sprintf("left join data_format on (question_x_data_format.answer_format_id = data_format.data_format_id) ").
sprintf("where question_x_data_format.question_id = %d and subquestion = %d",$condition_question_id,$condition_subquestion);
$get_subquestion_info = mysql_select($sql_get_subquestion_info);
printf(" | \n");
printf("\n");
printf("\n");
//display a table of available forms with options to preview them
printf("\n");
$sql_get_forms = sprintf("select experiment_x_form.form_id, form_name from experiment_x_form left join form using (form_id) where ").
sprintf("form_order < %d and experiment_id = %d order by form_order",$_SESSION['form_order'],$_SESSION['editor_experiment_id']);
$get_forms = mysql_query($sql_get_forms) or die(mysql_error());
while($row_get_forms = mysql_fetch_assoc($get_forms)) {
printf("\n");
printf("%s | \n",$row_get_forms['form_name']);
printf("\n");
printf(" | \n");
printf("\n");
printf(" | \n");
printf(" \n");
}
printf("\n");
printf(" \n");
?>
|