/[dtapublic]/to_be_filed/webprojs/php_libraries/php_library/fboprime/resvx.inc
ViewVC logotype

Contents of /to_be_filed/webprojs/php_libraries/php_library/fboprime/resvx.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations) (download)
Sat Oct 8 23:35:33 2016 UTC (6 years, 3 months ago) by dashley
File size: 24229 byte(s)
Initial commit.
1 <?php
2 //$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/phplib/resvx.inc,v 1.6 2006/08/13 00:50:00 dashley Exp $
3 //--------------------------------------------------------------------------------------------------------------
4 //resvx.inc--FboPrime Database resv Table Manipulation Functions (Not Scheduler Critical)
5 //Copyright (C) 2006 David T. Ashley
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //********************************************************************************
21 //Contains functions related to [operations on] the usrs table of the database that
22 //aren't necessary for the operation of the dayview scheduler.
23 //--------------------------------------------------------------------------------------------------------------
24 //
25 require_once("config.inc");
26 require_once("dbx.inc");
27 require_once("dt8.inc");
28 require_once("global.inc");
29 require_once("sguid.inc");
30 require_once("stimex.inc");
31 require_once("resv.inc");
32 //
33 //--------------------------------------------------------------------------------------------------------------
34 //Description
35 // Adds or modifies a RESV record. Certain parameters specify what is expected.
36 //
37 //Inputs
38 // action_in
39 // Enumerated type. Possibilities are:
40 // "A" : Add new.
41 // "M" : Modify existing.
42 //
43 // rec_in
44 // The data about the record to add or modify, as an associative array.
45 //
46 // Adding a record:
47 // a)The index must be absent (it is assigned automatically by the database).
48 // b)The userid must be included and must be globally unique, otherwise an error will be
49 // returned and the record won't be added.
50 // c)Fields out of range or defective will be corrected, and warnings will be returned.
51 // The record will, however, be added if possible.
52 // c)All fields not present in the associative array will be assigned default values.
53 // d)The creation/modification SGUID is handled automatically and may not be included in the
54 // associative array.
55 //
56 // Modifying a record:
57 // a)The record is specified by either index or userid. If both are specified, they must
58 // correspond to an existing record and must be consistent.
59 // b)Fields not included in the associative array are not touched.
60 // c)The creation/modification SGUID is handled automatically. If this field is included
61 // in the associative array, it means to check this against what is in the database
62 // and error out on editing collision if the value doesn't match what is in the database.
63 //
64 //Outputs
65 // result_code_out
66 // Adding a record:
67 // 0 if the operation failed, or the index if it succeeded.
68 // Modifying an existing record.
69 // 0 if the operation failed or 1 if it succeeds.
70 //
71 // errors_out
72 // If any errors resulted, an associative array of integers specifying the errors per the defined
73 // constants, or FALSE otherwise.
74 //
75 // warnings_out
76 // If any warnings resulted, an associative array of integers specifying the warnings per the
77 // defined constants, or FALSE otherwise.
78 //
79 //Database Locking
80 // Locking is performed per the recursive method described in the documentation. If, for example,
81 // the client wishes to call this function from within a larger critical section, as long as the
82 // client uses the recursive locking method, everything will operate correctly.
83 //
84 function RESV_record_add_modify($action_in, $rec_in, &$result_code_out, &$errors_out, &$warnings_out)
85 {
86 global $GLOBAL_dbhandle;
87 global $GLOBAL_dblocked;
88 global $CONFIG_FBO_USER_CATEGORIES;
89 global $GLOBAL_stime_string;
90
91 $in_termination_sequence = FALSE;
92 $errors_out = FALSE;
93 $warnings_out = FALSE;
94
95
96 if (!$in_termination_sequence)
97 {
98 //Lock the database using the recursive critical section method (discussed in the
99 //manual). This is necessary because the test for presence and the insert have to
100 //be combined atomically.
101 $db_was_locked = $GLOBAL_dblocked;
102 if (! $GLOBAL_dblocked)
103 {
104 DB_db_lock();
105 $GLOBAL_dblocked = TRUE;
106 }
107
108
109 if ($action_in == "A")
110 {
111 //Form the query strings. There are two different types, depending on whether
112 //we are adding or modifying a record.
113 //
114 //For adding, the string will be of the form:
115 // INSERT INTO usrs SET fn1=val1, fn2=val2, etc.
116 //
117 //For modifying, the string will be of the form:
118 // UPDATE usrs SET fn1=val1, fn2=val2, etc. WHERE idx=val.
119 //
120 if (!$in_termination_sequence)
121 {
122 //Obtain an SGUID to for the create/modification stamp.
123 //
124 $crmodsguid = SGUID_sguid();
125 }
126 }
127 else
128 {
129 //Modify
130 //
131 }
132
133 } //End if not in termination sequence.
134
135 //Unlock the database (if it was locked) using the recursive critical section
136 //method.
137 if (! $db_was_locked)
138 {
139 DB_db_unlock();
140 $GLOBAL_dblocked = FALSE;
141 }
142
143 //-----------------------------------------------------------
144 //Clean up the output parameters to go back to the caller.
145 //
146 if ($in_termination_sequence)
147 {
148 $result_code_out = 0;
149 }
150 else if ($result_code_out == -1)
151 {
152 //Something went wrong with the query. Return 0 as the
153 //error code.
154 $result_code_out = 0;
155 }
156 else
157 {
158 //The result code contains the index of what was added.
159 }
160
161 if (isset($errors_out_local))
162 {
163 $errors_out = $errors_out_local;
164 }
165 else
166 {
167 $errors_out = FALSE;
168 }
169
170 if (isset($warnings_out_local))
171 {
172 $warnings_out = $warnings_out_local;
173 }
174 else
175 {
176 $warnings_out = FALSE;
177 }
178 }
179 //
180 //--------------------------------------------------------------------------------------------------------------
181 //Retrieves the reservations for an individual user that:
182 // a)Are owned by the user OR
183 // b)Are instructional appointments with the user as the flight instructor.
184 // c)End at or after the current time (i.e. are currently in progress or in the future).
185 // d)Are not banner reservations.
186 //
187 //Results returned are information about the reservation except description, as an associative
188 //array in order from now to the future.
189 //
190 //If no such reservations exist, FALSE is returned.
191 //
192 function RESV_usrs_cur_future_resv_a($useridx_in, $curtime_in)
193 {
194 global $GLOBAL_dblocked;
195 global $GLOBAL_dbhandle;
196
197 //Lock the database using the recursive protocol. This is done to be sure that nobody
198 //deletes the user or the resource during this operation.
199 $db_was_locked = $GLOBAL_dblocked;
200 if (! $GLOBAL_dblocked)
201 {
202 DB_db_lock();
203 $GLOBAL_dblocked = TRUE;
204 }
205
206 //See if the user record exists. If not, can't go further, and return FALSE.
207 //
208 $userinfo = USRS_retrieve_by_idx($useridx_in);
209 if ($userinfo === FALSE)
210 {
211 if (! $db_was_locked)
212 {
213 DB_db_unlock();
214 $GLOBAL_dblocked = FALSE;
215 }
216
217 return(FALSE);
218 }
219
220 //Figure out if the user is a flight instructor or not. This is done by
221 //looking at the resources to see if there is a resource that points back to the
222 //user.
223 //
224 //Form the query string.
225 $query_string = "SELECT idx FROM rscs WHERE usercorres=\""
226 .
227 mysql_real_escape_string((string)$useridx_in, $GLOBAL_dbhandle)
228 .
229 "\"";
230
231 //Execute the query.
232 $result = mysql_query($query_string, $GLOBAL_dbhandle);
233
234 if ($result === FALSE)
235 {
236 //Unknown query failure. Return FALSE to the caller. No need to free,
237 //as this is not a result.
238 $rscs_corresponding = FALSE;
239 }
240 else
241 {
242 //Figure out how many rows in the result.
243 $nrows = mysql_num_rows($result);
244
245 if ($nrows == 0)
246 {
247 //No rows in the result. The query failed to give us a record, but still
248 //we need to free the result set.
249
250 //Free the result.
251 mysql_free_result($result);
252
253 //The caller gets FALSE. No record with that SID.
254 $rscs_corresponding = FALSE;
255 }
256 else
257 {
258 //We have at least one record. Assume just one, because the IDX is supposed
259 //to be unique.
260 $resource_record = mysql_fetch_assoc($result); //Get the associative record.
261 $rscs_corresponding = $resource_record["idx"];
262
263 //Free the result.
264 mysql_free_result($result);
265 }
266 }
267
268 //Form and execute the query to get the relevant reservations. The query is slightly
269 //more complex if the user is also a flight instructor.
270 if ($rscs_corresponding === FALSE)
271 {
272 //User who is not a flight instructor.
273 $query_string = "SELECT idx,type,useridx,alias,"
274 .
275 "finstid,finsttimestart,finsttimeend,"
276 .
277 "acftsimid,acftsimtimestart,acftsimtimeend,"
278 .
279 "resvtimestart,resvtimeend "
280 .
281 "FROM resv WHERE useridx=\""
282 .
283 $useridx_in
284 .
285 "\" AND type!=\"" . RESV_TYPE_BANNER . "\" AND resvtimeend>=\""
286 .
287 $curtime_in
288 .
289 "\" ORDER BY resvtimeend ASC, idx ASC";
290 //echo "<pre>\n";
291 //print_r($query_string);
292 //echo "</pre>\n";
293 }
294 else
295 {
296 //User who is a flight instructor.
297 $query_string = "SELECT idx,type,useridx,alias,"
298 .
299 "finstid,finsttimestart,finsttimeend,"
300 .
301 "acftsimid,acftsimtimestart,acftsimtimeend,"
302 .
303 "resvtimestart,resvtimeend "
304 .
305 "FROM resv WHERE (useridx=\""
306 .
307 $useridx_in
308 .
309 "\" OR finstid=\"" . $rscs_corresponding . "\") AND type!=\"" . RESV_TYPE_BANNER . "\" AND resvtimeend>=\""
310 .
311 $curtime_in
312 .
313 "\" ORDER BY resvtimeend ASC, idx ASC";
314
315 //echo "<pre>\n";
316 //print_r($query_string);
317 //echo "</pre>\n";
318 }
319
320 //Run the query, get the result.
321 $result = mysql_query($query_string, $GLOBAL_dbhandle);
322
323 //If the query did not go through successfully, return FALSE to the caller.
324 //
325 if ($result === FALSE)
326 {
327 $rv = FALSE;
328 }
329 //
330 //If the number of rows in the result set is 0, also give the caller FALSE.
331 else if (mysql_num_rows($result) <= 0)
332 {
333 //Technically, we got a result set, so free it.
334 mysql_free_result($result);
335
336 //The caller gets FALSE signaling no data.
337 $rv = FALSE;
338 }
339 else
340 {
341 //If we're here, we have a valid result set. Each row of the returned array is
342 //the expected fields from the resv table.
343
344 //Copy out the results to the array we'll return.
345 while ($row = mysql_fetch_assoc($result))
346 {
347 $rv[] = $row;
348 }
349
350 //Free memory.
351 mysql_free_result($result);
352 }
353
354 //Unlock the database (if it was locked) using the recursive critical section
355 //method.
356 if (! $db_was_locked)
357 {
358 DB_db_unlock();
359 $GLOBAL_dblocked = FALSE;
360 }
361
362 //Return the agreed-on result to the caller.
363 return($rv);
364 }
365 //
366 //--------------------------------------------------------------------------------------------------------------
367 //Retrieves the reservations for an individual user that:
368 // a)Are owned by the user OR
369 // b)Are instructional appointments with the user as the flight instructor.
370 // c)End before the current time (i.e. have fully finished).
371 // d)Are not banner reservations.
372 //
373 //Results returned are information about the reservation except description, as an associative
374 //array in order from now to the future.
375 //
376 //If no such reservations exist, FALSE is returned.
377 //
378 function RESV_usrs_past_resv_a($useridx_in, $curtime_in)
379 {
380 global $GLOBAL_dblocked;
381 global $GLOBAL_dbhandle;
382
383 //Lock the database using the recursive protocol. This is done to be sure that nobody
384 //deletes the user or the resource during this operation.
385 $db_was_locked = $GLOBAL_dblocked;
386 if (! $GLOBAL_dblocked)
387 {
388 DB_db_lock();
389 $GLOBAL_dblocked = TRUE;
390 }
391
392 //See if the user record exists. If not, can't go further, and return FALSE.
393 //
394 $userinfo = USRS_retrieve_by_idx($useridx_in);
395 if ($userinfo === FALSE)
396 {
397 if (! $db_was_locked)
398 {
399 DB_db_unlock();
400 $GLOBAL_dblocked = FALSE;
401 }
402
403 return(FALSE);
404 }
405
406 //Figure out if the user is a flight instructor or not. This is done by
407 //looking at the resources to see if there is a resource that points back to the
408 //user.
409 //
410 //Form the query string.
411 $query_string = "SELECT idx FROM rscs WHERE usercorres=\""
412 .
413 mysql_real_escape_string((string)$useridx_in, $GLOBAL_dbhandle)
414 .
415 "\"";
416
417 //Execute the query.
418 $result = mysql_query($query_string, $GLOBAL_dbhandle);
419
420 if ($result === FALSE)
421 {
422 //Unknown query failure. Return FALSE to the caller. No need to free,
423 //as this is not a result.
424 $rscs_corresponding = FALSE;
425 }
426 else
427 {
428 //Figure out how many rows in the result.
429 $nrows = mysql_num_rows($result);
430
431 if ($nrows == 0)
432 {
433 //No rows in the result. The query failed to give us a record, but still
434 //we need to free the result set.
435
436 //Free the result.
437 mysql_free_result($result);
438
439 //The caller gets FALSE. No record with that SID.
440 $rscs_corresponding = FALSE;
441 }
442 else
443 {
444 //We have at least one record. Assume just one, because the IDX is supposed
445 //to be unique.
446 $resource_record = mysql_fetch_assoc($result); //Get the associative record.
447 $rscs_corresponding = $resource_record["idx"];
448
449 //Free the result.
450 mysql_free_result($result);
451 }
452 }
453
454 //Form and execute the query to get the relevant reservations. The query is slightly
455 //more complex if the user is also a flight instructor.
456 if ($rscs_corresponding === FALSE)
457 {
458 //User who is not a flight instructor.
459 $query_string = "SELECT idx,type,useridx,alias,"
460 .
461 "finstid,finsttimestart,finsttimeend,"
462 .
463 "acftsimid,acftsimtimestart,acftsimtimeend,"
464 .
465 "resvtimestart,resvtimeend "
466 .
467 "FROM resv WHERE useridx=\""
468 .
469 $useridx_in
470 .
471 "\" AND type!=\"" . RESV_TYPE_BANNER . "\" AND resvtimeend<\""
472 .
473 $curtime_in
474 .
475 "\" ORDER BY resvtimeend DESC, idx ASC";
476 //echo "<pre>\n";
477 //print_r($query_string);
478 //echo "</pre>\n";
479 }
480 else
481 {
482 //User who is a flight instructor.
483 $query_string = "SELECT idx,type,useridx,alias,"
484 .
485 "finstid,finsttimestart,finsttimeend,"
486 .
487 "acftsimid,acftsimtimestart,acftsimtimeend,"
488 .
489 "resvtimestart,resvtimeend "
490 .
491 "FROM resv WHERE (useridx=\""
492 .
493 $useridx_in
494 .
495 "\" OR finstid=\"" . $rscs_corresponding . "\") AND type!=\"" . RESV_TYPE_BANNER . "\" AND resvtimeend<\""
496 .
497 $curtime_in
498 .
499 "\" ORDER BY resvtimeend DESC, idx ASC";
500
501 //echo "<pre>\n";
502 //print_r($query_string);
503 //echo "</pre>\n";
504 }
505
506 //Run the query, get the result.
507 $result = mysql_query($query_string, $GLOBAL_dbhandle);
508
509 //If the query did not go through successfully, return FALSE to the caller.
510 //
511 if ($result === FALSE)
512 {
513 $rv = FALSE;
514 }
515 //
516 //If the number of rows in the result set is 0, also give the caller FALSE.
517 else if (mysql_num_rows($result) <= 0)
518 {
519 //Technically, we got a result set, so free it.
520 mysql_free_result($result);
521
522 //The caller gets FALSE signaling no data.
523 $rv = FALSE;
524 }
525 else
526 {
527 //If we're here, we have a valid result set. Each row of the returned array is
528 //the expected fields from the resv table.
529
530 //Copy out the results to the array we'll return.
531 while ($row = mysql_fetch_assoc($result))
532 {
533 $rv[] = $row;
534 }
535
536 //Free memory.
537 mysql_free_result($result);
538 }
539
540 //Unlock the database (if it was locked) using the recursive critical section
541 //method.
542 if (! $db_was_locked)
543 {
544 DB_db_unlock();
545 $GLOBAL_dblocked = FALSE;
546 }
547
548 //Return the agreed-on result to the caller.
549 return($rv);
550 }
551 //
552 //--------------------------------------------------------------------------------------------------------------
553 //Returns the time for a reservation display time.
554 //
555 function RESV_time_string_a($st_in)
556 {
557 //Split apart the hour and minute as integers.
558 sscanf(SubStr($st_in, 10, 4), "%02d%02d", $hour, $minute);
559
560 if (CONFIG_TIME_FORMAT_24HR)
561 {
562 $rv = sprintf("%02d:%02d", $hour, $minute);
563 }
564 else
565 {
566 if ($hour == 0)
567 {
568 $rv = sprintf("12:%02d a.m.", $minute);
569 }
570 else if ($hour == 12)
571 {
572 $rv = sprintf("12:%02d p.m.", $minute);
573 }
574 else if ($hour > 12)
575 {
576 $rv = sprintf("%d:%02d p.m.", $hour-12, $minute);
577 }
578 else
579 {
580 $rv = sprintf("%d:%02d a.m.", $hour, $minute);
581 }
582 }
583
584 return($rv);
585 }
586 //
587 //--------------------------------------------------------------------------------------------------------------
588 //Returns the date for a reservation display date.
589 //
590 function RESV_date_string_a($st_in)
591 {
592 if (CONFIG_DATE_FORMAT_EUROPEAN)
593 {
594 $rv = SubStr($st_in, 2, 4) . "/" . SubStr($st_in, 6, 2) . "/" . SubStr($st_in, 8, 2);
595 }
596 else
597 {
598 $rv = SubStr($st_in, 6, 2) . "/" . SubStr($st_in, 8, 2) . "/" . SubStr($st_in, 2, 4);
599 }
600
601 return($rv);
602 }
603 //
604 //--------------------------------------------------------------------------------------------------------------
605 //Returns the text corresponding to the date range for printout of reservations. If the two
606 //STIMEs correspond to the same date, the date is compressed into one.
607 //
608 function RESV_date_range_string_a($st_start_in, $st_end_in)
609 {
610 if (SubStr($st_start_in, 0, 10) == SubStr($st_end_in, 0, 10))
611 {
612 //Dates are the same, so can just do the date once.
613 $rv = RESV_date_string_a($st_start_in)
614 .
615 " ("
616 .
617 STIME_dow_string_a($st_start_in)
618 .
619 ") "
620 .
621 RESV_time_string_a($st_start_in)
622 .
623 " - "
624 .
625 RESV_time_string_a($st_end_in);
626 }
627 else
628 {
629 //Dates are different so must do it twice.
630 $rv = RESV_date_string_a($st_start_in)
631 .
632 " ("
633 .
634 STIME_dow_string_a($st_start_in)
635 .
636 ") "
637 .
638 RESV_time_string_a($st_start_in)
639 .
640 " - "
641 .
642 RESV_date_string_a($st_end_in)
643 .
644 " ("
645 .
646 STIME_dow_string_a($st_end_in)
647 .
648 ") "
649 .
650 RESV_time_string_a($st_end_in);
651 }
652
653 //Tack on the duration.
654 $rv = $rv . " (" . STIME_diff_string_a($st_start_in, $st_end_in) . ")";
655
656 return($rv);
657 }
658 //
659 //--------------------------------------------------------------------------------------------------------------
660 //Returns the text corresponding to a given reservation, flight instructor first. If the date
661 //and time for the aircraft/simulator and flight instructor are the same, only one string is returned.
662 //
663 function RESV_resv_date_string_a($resv_in, $personstring, $acftsimstring)
664 {
665 if (($resv_in["finstid"] != 0) && ($resv_in["acftsimid"] != 0))
666 {
667 //Both a flight instructor and an aircraft.
668 if (($resv_in["finsttimestart"] == $resv_in["acftsimtimestart"]) && ($resv_in["finsttimeend"] == $resv_in["acftsimtimeend"]))
669 {
670 //Start and end time of the instructor and aircraft are the same. Can combine them.
671 //
672 $rv = RESV_date_range_string_a($resv_in["finsttimestart"], $resv_in["finsttimeend"]);
673 }
674 else
675 {
676 //Start and end time of instructor and aircraft are different. Cannot combine them.
677 $rv = $personstring
678 .
679 " "
680 .
681 RESV_date_range_string_a($resv_in["finsttimestart"], $resv_in["finsttimeend"])
682 .
683 ", "
684 .
685 $acftsimstring
686 .
687 " "
688 .
689 RESV_date_range_string_a($resv_in["acftsimtimestart"], $resv_in["acftsimtimeend"])
690 ;
691 }
692
693 }
694 else if (($resv_in["finstid"] == 0) && ($resv_in["acftsimid"] != 0))
695 {
696 //Aircraft but no flight instructor.
697 $rv = RESV_date_range_string_a($resv_in["acftsimtimestart"], $resv_in["acftsimtimeend"]);
698 }
699 else if (($resv_in["finstid"] != 0) && ($resv_in["acftsimid"] == 0))
700 {
701 //Flight instructor but no aircraft.
702 $rv = RESV_date_range_string_a($resv_in["finsttimestart"], $resv_in["finsttimeend"]);
703 }
704 else
705 {
706 //Should never be here. Return the empty string to be safe.
707 $rv = "";
708 }
709
710 return($rv);
711 }
712 //
713 //--------------------------------------------------------------------------------------------------------------
714 //End of $RCSfile: resvx.inc,v $.
715 //--------------------------------------------------------------------------------------------------------------
716 ?>

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25