PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/actions/QuestionAddAction.php

http://libstats.googlecode.com/
PHP | 84 lines | 63 code | 9 blank | 12 comment | 14 complexity | 4a1ac38e6c374fdc6abd48f9b0504622 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require_once 'Action.php';
  3. /**
  4. * An Action to add a question to the Library Stats. Does all the
  5. * required checks and then adds the beast.
  6. */
  7. class QuestionAddAction extends Action {
  8. function perform() {
  9. // Get the resources we need to do this update
  10. $db = $_REQUEST['db'];
  11. $userFinder = new UserFinder($db);
  12. $user = $userFinder->findById($_SESSION['userId']);
  13. $questionFinder = new QuestionFinder($db);
  14. // Grok all the relevant data from the form
  15. $qHash = array();
  16. $qHash['library_id'] = $user['library_id'];
  17. $qHash['location_id'] = gpwd('location', null);
  18. $qHash['question_type_id'] = gpwd('questionType', null);
  19. $qHash['question_type_other'] = gpwd('questionTypeOther');
  20. $qHash['time_spent_id'] = gpwd('timeSpent', null);
  21. $qHash['patron_type_id'] = gpwd('patronType', null);
  22. $qHash['question_format_id'] = gpwd('questionFormat', null);
  23. $qHash['initials'] = gpwd('initials');
  24. $qHash['client_ip'] = getRemoteIp();
  25. $qHash['user_id'] = $_SESSION['userId'];
  26. $qHash['question'] = gpwd('question');
  27. $qHash['answer'] = gpwd('answer');
  28. $qHash['question'] = trim($qHash['question']);
  29. $qHash['answer'] = trim($qHash['answer']);
  30. $qHash['hide'] = 0;
  31. if ($qHash['question'] == '' && $qHash['answer'] == '') { $qHash['hide'] = 1; }
  32. // Do the date
  33. $qHash['question_date'] = trim(gpwd('mydate', 'now'));
  34. if ($qHash['question_date'] == '') {
  35. $qHash['question_date'] = 'now';
  36. }
  37. $stamp = strtotime($qHash['question_date']);
  38. if ($stamp != -1) {
  39. $qHash['question_date'] = date('Y-m-d H:i:s', $stamp);
  40. }
  41. else {
  42. $qHash['question_date'] = null;
  43. }
  44. $qHash['date_added'] = date('Y-m-d H:i:s');
  45. // Clean up qHash; make numbers really numeric. The dirty little
  46. // trick: add 0 to non-null values names .*_id
  47. foreach ($qHash as $key=>$val) {
  48. if (strpos($key, '_id')) {
  49. if ($val != null) {
  50. $qHash[$key] = $val + 0;
  51. }
  52. }
  53. }
  54. $target = "questionAddForm.do";
  55. $res = $questionFinder->addQuestion($qHash);
  56. if (!DB::isError($res)) {
  57. // Use a Location: header to fly back; we don't want to
  58. // be able to double-enter by mistake.... I think.
  59. $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
  60. $url = substr($url, 0, -strrchr($url, '/')). $target;
  61. header("Location: $url");
  62. exit;
  63. }
  64. else {
  65. // A page error occurred!
  66. $_REQUEST['dbResult'] = $res;
  67. $act = new PageErrorAction();
  68. return $act->perform();
  69. }
  70. }
  71. function isAuthenticationRequired() {
  72. return true;
  73. }
  74. }
  75. ?>