Odyssey
cuquest.prg
1 <?php
2  $monLibrary= dirname(__FILE__) . "/../library";
3  $monIncludes= dirname(__FILE__) . "/../includes";
4  require_once("$monLibrary/cu_top.i");
5  require_once("$monLibrary/ck_hticket.i");
6  require_once("$monIncludes/cu_remote_top.prg");
7 
8  if (!CheckPerm($link, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
9  // ** Permissions failed
10  // ** redirect to new page
11  header("Location: /hcuadm/hcu_noperm.prg");
12  exit;
13  }
14 
15 $dms_ok=array('act'=>'digits','qid'=>'string','qlang'=>'string',
16 'form_qid'=>'digits','form_lang'=>'array','form_txt_'=>'prefix_s',
17 'form_ex_txt_'=>'prefix_s','btnSave'=>'string','form_type_'=>'prefix_s','Remote_Update'=>'array');
18 
19 dms_import($dms_ok);
20 
21  $HCU_Lang_Codes = array("en_US"=>"American English", "es_US"=>"American Spanish","pl_US"=>"Polish");
22 
23  $self = $_SERVER['PHP_SELF'];
24 
25  $act = (isset($act) && intval($act) > 0 ? intval($act) : 2);
26 
27 
28  $form_err = "";
29  $err_msg = "";
30  $sql = "";
31 
32 
33  // *** SAVE CODE
34  if ($act == 1 && isset($btnSave)) {
35 
36  /* VALIDATE THE INFORMATION - whether it's been saved before or NOT
37  After validation fails it will send the information back to the edit form
38  So it looks like it did before, the fail information will be at the bottom of the
39  screen.
40 
41  */
42 
43  // Now we need to load all the fields from the CU form, and post them back
44  // So the user does not have to press the back button.
45  // Load all the Fields from the previous form into hidden fields on this form
46 
47  if ($err_msg == "") {
48 
49  $save_qid = "";
50  $Remote_Results = "";
51 
52  // Everything was validated, we now need to save the information, either insert for new or update for edited.
53 
54  $save_lang = $form_lang;
55 
56  if (isset($form_qid) && intval($form_qid) > 0) {
57  $save_qid = intval($form_qid);
58  } else {
59  $sql = "SELECT nextval('cuquestmaster_quest_id_seq');";
60  $qid_rs = db_query($sql, $link);
61  list($save_qid) = db_fetch_array($qid_rs);
62  db_free_result($qid_rs);
63  }
64 
65  $sql = "";
66  foreach ($form_lang as $lang_key => $lang_value) {
67  $save_text = prep_save(trim(${"form_txt_" . $lang_value}));
68  $save_ex_text = prep_save(trim(${"form_ex_txt_" . $lang_value}));
69  $save_type = prep_save(trim(${"form_type_" . $lang_value}));
70 
71 
72  // *** FIRST LOOK FOR THE RECORD
73  $lkup_sql = "SELECT count(quest_id)
74  FROM cuquestmaster
75  WHERE quest_id = '$save_qid'
76  AND quest_lang = '$lang_value'; ";
77  $lookup_rs = db_query($lkup_sql, $link);
78  list($qid_count) = db_fetch_array($lookup_rs);
79  db_free_result($lookup_rs);
80 
81 
82  if ($qid_count > 0) {
83  // *** UPDATE THE TABLE
84  $sql .= "UPDATE cuquestmaster SET
85  quest_text = '$save_text',
86  example_text = '$save_ex_text',
87  quest_type = '$save_type'
88  WHERE quest_id = '$save_qid'
89  AND quest_lang = '$lang_value'; ";
90 
91  } else {
92  // *** INSERT INTO TABLE
93  $sql .= "INSERT INTO cuquestmaster
94  (quest_id, quest_lang, quest_text, example_text, quest_type)
95  VALUES
96  ('$save_qid',
97  '$lang_value',
98  '$save_text',
99  '$save_ex_text',
100  '$save_type' ); ";
101  }
102 
103 
104  // ** SAVE ANY REMOTE OPTIONS
105  if (Remote_Update_Selected()) {
106  // ** NOW CHECK FOR SAVING TO A REMOTE SERVER
107  // ** Add Each field that will be updated
108 
109  Remote_Field_Add("cuquest", "qst_id", $save_qid);
110  Remote_Field_Add("cuquest", "qst_lang", $lang_value);
111  Remote_Field_Add("cuquest", "qst_txt", trim(${"form_txt_" . $lang_value}));
112  Remote_Field_Add("cuquest", "qst_etxt", trim(${"form_ex_txt_" . $lang_value}));
113  Remote_Field_Add("cuquest", "qst_type", $save_type);
114 
115  // ** Need to create the url I am going to send to the remote server
116  $Remote_Results .= Remote_Update_Send("U", "cuquest") . "<br>";
117 
118  $Remote_Fields = array();
119  }
120 
121 
122  }
123 
124  $save_msg = "";
125 
126  if (!$save_rs = db_query($sql, $link)) {
127  // ** SAVE FAILED
128  $form_err = "Unable to Save Challenge Question!";
129  } else {
130  // ** ALL WAS SUCCESSFULL
131 
132  if ($save_qid == 0) {
133  // ** FOR NEW RECORDS -- fetch the ID
134  // *** POSTGRES METHOD
135  $psql_lastoid = pg_last_oid($save_rs);
136  $sql = "SELECT quest_id
137  FROM cuquestmaster
138  WHERE oid = '" . intval($psql_lastoid) . "'; ";
139  $oid_rs = db_query($sql, $link);
140  list($save_qid) = db_fetch_array($oid_rs);
141  db_free_result($oid_rs);
142  }
143 
144  // ** FORWARD the browser back to the 'list' screen along with a confirmation message
145  header ("Location: $self?msg=" . urlencode("The Challenge Question was saved.<br>") . urlencode($Remote_Results));
146 
147  exit;
148  }
149  } else {
150  // ** Failed Validation -- set the error up here -- reshow the form
151  $act = 1;
152  }
153  }
154 
155 
156 
157  switch ($act):
158  case "1": // Edit Challenge Questions
159  $qst_id = "";
160  $qst_txt = array();
161  // $qst_lang = array();
162  $qst_ex_txt = array();
163  $qst_type = array();
164 
165  $use_form = "";
166 
167  // ** Determine if we are Adding or Editing
168  if (isset($form_qid)) {
169 
170  // ** We are reloading the information --- set it up here
171  $qst_id = $form_qid;
172  foreach ($form_lang as $key=>$value) {
173  $qst_txt[$value] = ${"form_txt_" . $value};
174  $qst_ex_txt[$value] = ${"form_ex_txt_" . $value};
175  $qst_type[$value] = ${"form_type_" . $value};
176  }
177  $use_form = $form_use;
178  } elseif (isset($qid)) {
179 
180  // *Question ID set -- assume we are editing
181 
182  // Connect to the database - get the team information from the database
183  $query = "select *
184  from cuquestmaster
185  where quest_id = '" . intval($qid) . "'";
186 
187  $result = db_query($query, $link);
188  // Now fetch the row
189  $cnt = 0;
190  while ($cuquest_row = db_fetch_object($result, $cnt)) {
191  $qst_id = $cuquest_row->quest_id;
192  $qst_txt[$cuquest_row->quest_lang] = trim($cuquest_row->quest_text);
193  $qst_ex_txt[$cuquest_row->quest_lang] = trim($cuquest_row->example_text);
194  $qst_type[$cuquest_row->quest_lang] = $cuquest_row->quest_type;
195 
196  $cnt++;
197  }
198  db_free_result($result);
199 
200  $use_form = "Edit";
201  } else {
202  // Adding new Challenge Question, setup empty data.
203  foreach($HCU_Lang_Codes as $lang_key=>$lang_value) {
204  $qst_txt[$lang_key] = "";
205  $qst_type[$lang_key] = "";
206  $qst_ex_txt[$lang_key] = "";
207  }
208  }
209 
210  $use_form = ($use_form == "" ? "Add" : $use_form);
211  cu_header("Edit Challenge Questions");
212 ?>
213  <form action="<?php echo $self; ?>?act=<?php echo $act; ?>" method="post">
214  <p>&nbsp;<br>
215  <?php
216  if (strlen($form_err) > 0)
217  printf("\n<font color=\"red\">Errors found before saving the information. Please review errors at bottom of screen.\n<p></font>");
218  ?>
219  <center>
220  <table cellpadding="3" cellspacing="0" border="0" class="dmsbg" width="500"><tr><td>
221  <table cellpadding="2" cellspacing="0" border="0" bgcolor=white width="100%">
222  <tr>
223  <td colspan="2" class="bar" align="center">
224  <?php echo strtoupper($use_form); ?> CHALLENGE QUESTION
225  </td>
226  </tr>
227  <tr>
228  <td nowrap align="right" class="hdr">
229  Question ID:
230  </td>
231  <td nowrap class='dtl'>
232  <?php if ($use_form == "Add"): ?>
233  New Question
234  <?php else:
235  echo $qst_id; ?>
236  <?php endif; ?>
237  <input type="hidden" name="form_qid" value="<?php echo $qst_id; ?>">
238  </td>
239  </tr>
240  <?php foreach($HCU_Lang_Codes as $lang_key=>$lang_value): ?>
241  <tr>
242  <td nowrap align="right" class="hdr">
243  <input type="hidden" name="form_lang[]" value="<?php echo $lang_key; ?>">
244  &nbsp;
245  </td>
246  <td nowrap align="left" class="hdr">
247  Question for <?php echo $lang_value; ?>
248  </td>
249  </tr>
250  <tr>
251  <td nowrap align="right" class="hdr">
252  Question:
253  </td>
254  <td nowrap class='dtl'>
255  <input type="text" name="form_txt_<?php echo $lang_key; ?>" size="50" maxlength="80" value="<?php echo $qst_txt[$lang_key]; ?>">
256  </td>
257  </tr>
258  <tr>
259  <td nowrap align="right" class="hdr">
260  Question Answer Format:
261  </td>
262  <td nowrap class='dtl'>
263  <select name="form_type_<?php echo $lang_key; ?>">
264  <option value="T" <?php echo ($qst_type[$lang_key] == "T" ? "SELECTED" : ""); ?>>Text</option>
265  <option value="D" <?php echo ($qst_type[$lang_key] == "D" ? "SELECTED" : ""); ?>>Date Format</option>
266  </select>
267  </td>
268  </tr>
269  <tr>
270  <td nowrap align="right" class="hdr">
271  Example Text:
272  </td>
273  <td nowrap class='dtl'>
274  <input type="text" name="form_ex_txt_<?php echo $lang_key; ?>" size="20" maxlength="30" value="<?php echo $qst_ex_txt[$lang_key]; ?>">
275  </td>
276  </tr>
277  <?php endforeach; ?>
278  <tr>
279  <td class='dtl'>&nbsp;</td>
280  <td nowrap class='dtl'>
281  <?php remote_update_list(); ?>
282  </td>
283  </tr>
284  <tr>
285  <td class='dtl'>&nbsp;</td>
286  <td nowrap class='dtl'>
287  <input type="submit" name="btnSave" Value="Save">
288  <input type="button" name="Cancel" Value="Cancel" onClick="document.location='cuquest.prg'">
289  </td>
290  </tr>
291  <?php // If the error value is here then print it at the end
292  if (isset($form_err)) {
293  echo "<tr><td colspan=2 class='err'>$form_err</td></tr>";
294  }
295  echo "
296  </table>
297  </td></tr></table> ";
298  break;
299  case "2":
300  cu_header("CU Challenge Questions");
301  ?>
302  <form>
303  <?php
304  // Connect to the data and retrieve the current list of Home CU Products
305  $query = "SELECT * FROM cuquestmaster ORDER BY quest_id, quest_lang, quest_text ";
306  $qst_result = db_query($query, $link);
307 
308  ?>
309 
310  <!-- Print out the top of the table -->
311  <table border="0" cellpadding="3" cellspacing="0" align="center" width="500" class="dmsbg"><tr><td>
312  <table border="0" cellpadding="2" cellspacing="0" align="center" width="100%" bgcolor=white>
313  <tr>
314  <td colspan="4" class="bar" align="center">
315  CHALLENGE QUESTION LIST
316  </td>
317  </tr>
318  <?php if (isset($msg)): ?>
319  <tr>
320  <td colspan="4" class="msg" align="center">
321  <?php echo $msg; ?>
322  </td>
323  </tr>
324  <?php endif; ?>
325  <tr>
326  <td class="hdr" align="center">
327  Question ID
328  </td>
329  <td class="hdr" align="center">
330  Language
331  </td>
332  <td class="hdr" align="center">
333  Question Text
334  </td>
335  <td class="hdr" align="center">
336  Select
337  </td>
338  </tr>
339  <?php
340  $RGB = "odd";
341  $row = 0;
342  $last_qid = "";
343  while ($qst_row = db_fetch_object($qst_result, $row)):
344  $row++;
345  ?>
346  <tr class="<?php echo $RGB; ?>">
347  <?php if ($last_qid != $qst_row->quest_id): ?>
348  <td nowrap >
349  <?php echo trim($qst_row->quest_id) ?>
350  </td>
351  <td nowrap >
352  <?php echo trim($HCU_Lang_Codes["$qst_row->quest_lang"]) ?>
353  </td>
354  <td nowrap >
355  <?php echo trim($qst_row->quest_text) ?>
356  </td>
357  <td nowrap >
358  <a href="<?php echo $self; ?>?act=1&qid=<?php echo trim($qst_row->quest_id) ?>&qlang=<?php echo trim($qst_row->quest_lang); ?>">Edit</a>
359  <?php /*&nbsp;|&nbsp;
360  <a href="">Delete</a>
361  */ ?>
362  </td>
363  <?php
364  $last_qid = $qst_row->quest_id;
365  else: ?>
366  <td nowrap >
367  &nbsp;
368  </td>
369  <td nowrap >
370  <?php echo trim($HCU_Lang_Codes["$qst_row->quest_lang"]) ?>
371  </td>
372  <td nowrap >
373  <?php echo trim($qst_row->quest_text) ?>
374  </td>
375  <td nowrap >
376  &nbsp;
377  </td>
378  <?php endif; ?>
379  </tr>
380  <?php
381  $RGB = ($RGB == "odd" ? "even" : "odd");
382  endwhile;
383  db_free_result($qst_row);
384  ?>
385 
386  <tr>
387  <td colspan="4">
388  <a href="<?php echo $self; ?>?act=1">Add Challenge Questions</a>
389  &nbsp;|&nbsp;
390  <a href="<?php echo $infourl ?>/hcuadm/cuilist.prg" target="parent">Credit Union List</a>
391  </td>
392  </tr>
393  </table>
394  </td></tr></table>
395 <?php
396  break;
397  default:
398  cu_header("Error Displaying Form");
399  print ("<form>");
400  printError("Requested form not found!<br>Cancelling action.");
401  endswitch;
402  ?>
403  </form>
404  </body>
405 </html>