From: Stefan C. <sch...@ea...> - 2003-09-29 04:55:28
|
Here is the source code of my modified version. (sorry for being angry last time, but damn, it was just toooo harsh :)) :) stF <?php # $Id: questions.inc,v 1.13 2003/03/05 19:26:38 jimmerman Exp $ // Written by James Flemer // For eGrad2000.com // <jf...@al...> // Almost entirely rewritten by Stefan Champailler (for clarity) /* echo("\$HTTP_POST_VARS['q']=".$HTTP_POST_VARS['q']); // selected question number (1 is the first, n is the last) echo(", \$HTTP_POST_VARS['id'] =".$HTTP_POST_VARS['id']); // question id ? // id is retransmitted each time the question edit is // submitted. what happens, if we're creating a new question // with errors in it, that is, one that hasn't any id. echo(", \$HTTP_POST_VARS['type_id']=".$HTTP_POST_VARS['type_id']); // question type id echo "<br>"; echo(", \$HTTP_SESSION_VARS['survey_id'] =".$HTTP_SESSION_VARS['survey_id']); echo(", \$HTTP_SESSION_VARS['last_tab'] =". $HTTP_SESSION_VARS['last_tab']); echo "<br>"; echo "\$updated ? $updated"; // survey id. // In case of "edit a survey", survey_id is made out of newid in // tab.inc */ /* Some constants that will soon be used */ $FIELDS = array('name','type_id','length','precise','required','content','position'); // Scary: name is not shown, but kept anyway... I made that to // avoid disrupting other code parts (esp. survey_update). $NEW_QUESTION_ID = "new_question_id"; $NEW_QUESTION_BUTTON = _("New Field"); // Sanitize if( empty( $HTTP_SESSION_VARS['survey_id'])) trigger_error( _("Empty survey id. This can't happen."), E_USER_ERROR); /* $sid is a /shortcut/ to $HTTP_SE..., it shall not be modified. */ $sid =& $HTTP_SESSION_VARS['survey_id']; /* We collect all the questions id's of the survey. This will soon be helpful. */ $sql = "SELECT id FROM question WHERE survey_id='$sid' AND deleted='N' AND type_id != 99 ORDER BY position"; // stF: 99 is page break $result = mysql_query($sql); $q_ids = array(); while(list($qi) = mysql_fetch_row($result)) array_push($q_ids, $qi); mysql_free_result($result); /* $HTTP_POST_VARS['id'] is the question we WERE working on at submission time and we'll set up $id which the question we ARE working on.*/ $id = "!!!unset!!!"; /* We define a little shortcut here ($q). Plese note that it preserves the nullity of POST_VAR[q] if necessary. */ $q=''; $q_post = $HTTP_POST_VARS['q']; if( $q_post > 0 && $q_post <= count( $q_ids)) $q = $q_post; elseif( !empty( $q_post)) $q = $NEW_QUESTION_BUTTON; unset($q_post); /* So, now the whole job is to get $id from $HTTP_POST_VARS['q'] and other things... When we say "were working on", it means that it was the id of the question that was displayed when the user submitted its modifications. The number of ways a page is submitted is given by the number of buttons on the page multiplied by the fact that the submission has or has not been accepted (because invallid values). Globally the schema is this : - we were editing $id, we clicked on something, our edition is valid and we can fall on the somthign that was selected. - we were not editing and we fall back where we should. - we were editing $id, we clicked on something, our edition is NOT valid and we must stay here. */ /* So first question, where are we coming from */ $coming_from_outside = $HTTP_SESSION_VARS['last_tab'] != 'questions'; $submitted_data_are_valid = (empty($HTTP_POST_VARS['content']) && empty($HTTP_POST_VARS['name'])) || $updated;; $request_for_new_question = $q == $NEW_QUESTION_BUTTON; $request_for_edit_question = $q > 0; $request_for_extra_choice = !empty($HTTP_POST_VARS['extra_choices']); $outside_requested_qid = -1; if( !$request_for_new_question && $q > 0) // So we avoid empty(q) $outside_requested_qid = $q_ids[ $q - 1]; /* echo "\$coming_from_outside = $coming_from_outside "; if( $coming_from_outside) echo "\$outside_requested_qid = $outside_requested_qid"; echo "<br/>"; echo "request_for_extra_choice = $request_for_extra_choice"; echo "<br/>"; */ /* Now that we know where we're coming from, we can see if we can fulfill user's request (that is, go where he wants to go). */ /* If true, then it's the first time that we display the form for the edited question. If false, it's not the first time, it means that the user has just submitted the question. */ $ignore_post_var = false; if( $coming_from_outside) { /* As we're not coming from outside, we're not working on any particular question. Therefore, we have several possibilities : - There's no question to work on (creating a survey) - There's a question and we want to edit it. So, are we working with a survey that has some questions in it ? */ if( count( $q_ids) > 0) { // Yes, at least one question. // Outside may have requested to work on a special question. if( $outside_requested_qid >= 0) $id = $outside_requested_qid; else // we decide to edit the first of the available questions. $id = $q_ids[0]; } else { // No, we're creating a new question $id = $NEW_QUESTION_ID; } $ignore_post_var = true; } else { // Coming from the inside if( $submitted_data_are_valid) { if( $request_for_new_question) { $id = $NEW_QUESTION_ID; $ignore_post_var = true; } elseif( $request_for_edit_question) { $id = $q_ids[$HTTP_POST_VARS['q'] - 1]; $ignore_post_var = true; } elseif( $request_for_extra_choice) $id = $HTTP_POST_VARS['id']; } else $id = $HTTP_POST_VARS['id']; } /* At this point, $id is in a defined state. That is, it is either a integer, representing an existing id, or it is set to $NEW_QUESTION_ID which means that we're editing a question that has still no ID (i.e. a new question) */ if( $id != $NEW_QUESTION_ID && !in_array( $id, $q_ids)) trigger_error( _("Unrecognized question id ($id)"), E_USER_ERROR); /* Now we are going to display the form. The job is to figure out the value of each of the field. The fields value are set/read in the HTPP_POST array. - If we're editing an existing question, then the values are to be taken from the HTTP_POST array. - If we're arriving on the edit screen for the first time (for an exisiting question), then the values are to be taken from the database. - If we're creating a new question, then all the fields are empty (and so are the HTTP_POST) - If we're editing a new question, then all the fields come from the HTTP_POST) */ $multiple_choices = array(); if( $ignore_post_var && $id == $NEW_QUESTION_ID) { // We clear, just to make sure that there's no garbage // in the POST vars. foreach($FIELDS as $f) unset( $HTTP_POST_VARS[$f]); for($i=0; $i<$GLOBALS['ESPCONFIG']['default_num_choices']; $i++) array_push( $multiple_choices, array( "id" => '', "content" => '')); // We set some defaults $HTTP_POST_VARS['required'] = 'N'; $HTTP_POST_VARS['type_id'] = 2; } elseif( $ignore_post_var && $id != $NEW_QUESTION_ID) { /* We load everything from the DB. */ $sql = "SELECT * FROM question WHERE id='${id}' AND deleted='N'"; $result = mysql_query($sql); $question = mysql_fetch_array($result,MYSQL_ASSOC); mysql_free_result($result); foreach($FIELDS as $f) $HTTP_POST_VARS[$f] = _stripslashes( $question[$f]); /* Load the multiple choices if it's relevant to this question. */ if( esp_type_has_choices2( $HTTP_POST_VARS[type_id])) { $sql = "SELECT id,content FROM question_choice WHERE question_id='${id}' ORDER BY id"; $result = mysql_query($sql); while( $a = mysql_fetch_assoc($result)) { $a['content'] = _stripslashes( $a['content']); array_push( $multiple_choices, $a); } } } else { /* Here, we have $ignore_post_var == false, so we have to take care of what the user submitted. */ for( $i = 1; $i <= $HTTP_POST_VARS['num_choices']; $i++) array_push( $multiple_choices, array( 'id' => $HTTP_POST_VARS["choice_id_$i"], 'content' => $HTTP_POST_VARS["choice_content_$i"])); if( $request_for_extra_choice) array_push( $multiple_choices, array( "id" => '', "content" => '')); } /* Now, we'd like to know what's the "number" of the question from the user point of view. If the question is new, we'll set it to false. Since it is user point of view, $q_number will be 1-based. */ $q_number = false; if( $id != $NEW_QUESTION_ID) $q_number = array_search( $id, $q_ids) + 1; /* Now we set up an array with all the question types in it. 99 is the page break that can be added in the Order tab. */ $sql = 'SELECT id, type FROM question_type WHERE id != 99'; $result = mysql_query($sql); $arr = array(); while(list($key, $val) = mysql_fetch_row($result)) { $arr["$key"] = _($val); } ?> <script language="javascript"> <!-- // comment function clearTextInputs() { var i = 1; while (document.phpesp.elements["choice_content_" + i]) { document.phpesp.elements["choice_content_" + i].value = ""; i++; } } // comment --> </script> <input type="hidden" name="id" value="<?php echo($id); ?>"> <p> <?php //echo(_('Edit this field, or click the number of the field you would like to edit:')); ?> </p> <table> <tr> <td colspan="2"> <?php // If == 0 it means we're working on a question that is not // in the DB, therefore a "new question" if( $id == $NEW_QUESTION_ID ) echo _('You are currently editing a new question '); else echo _('You are currently editing question ') . "<b>$q_number</b>."; ?> </td> </tr> <tr> <td halign="left"> <?php if( count( $q_ids) > 0) { if( count( $q_ids) == 1) echo _("Question you can edit:"); else echo _("Questions you can edit:"); ?> </td> <td> <?php for( $i=1; $i<=count( $q_ids); ++$i) if( $i != $q_number) { ?> <input type="submit" name="q" value="<?php echo($i); ?>"> <?php } else echo(" ".$i." "); } ?> <input type="submit" name="q" value="<?php echo($NEW_QUESTION_BUTTON); ?>"> </td> </tr> </table> <hr> <table> <tr> <th><?php echo _('Type');?></th> <td><?php echo( mkselect('type_id',$arr, null, false)); ?></td> </tr> <tr> <th><?php echo _('Required');?></th> <td> <?php echo( mkselect( "required", array("Y" => _('Yes'),"N" => _('No')), null, false) ); ?></td> </tr> <tr> <th><?php echo _('Text');?></th> <td colspan="5" valign="top"><?php echo(mktextarea("content",4,60,"VIRTUAL")); ?></td> </tr> <?php // has answer options ... so show that part of the form if( $id == $NEW_QUESTION_ID || esp_type_has_choices2( $HTTP_POST_VARS[type_id])) { ?> <th> <?php echo _('Possible answers') ?> </th> <td> <input type="hidden" name="num_choices" value="<?php echo(count($multiple_choices)); ?>"> <?php for($i=0; $i<count( $multiple_choices); $i++) { ?> <input type="hidden" name="choice_id_<?php echo($i+1); ?>" value="<?php echo( $multiple_choices[$i]['id']); ?>"> <input type="text" size="60" name="choice_content_<?php echo($i+1); ?>" value="<?php echo( $multiple_choices[$i]['content']); ?>"> <br/> <?php } ?> <br/> <input type="submit" name="extra_choices" value="<?php echo(_('Add another answer line')); ?>"> <input type="button" name="clear" value="Clear all answer lines" onClick="clearTextInputs()"> </td> <?php } ?> </table> <hr/> |