PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/dialog/index.php

https://github.com/ovnis/Mepsol
PHP | 138 lines | 128 code | 10 blank | 0 comment | 28 complexity | d34fb5096ccd4e7bd5541e3655253903 MD5 | raw file
  1. <?php
  2. class dialog extends module {
  3. public $language = "";
  4. function execute(){
  5. $action = $_GET["action"];
  6. switch ($action){
  7. case "start":
  8. $serviceid = $_SESSION["serviceid"];
  9. $qr = mysql_query("SELECT id FROM states WHERE checked = 'checked' AND serviceid=$serviceid LIMIT 1");
  10. $_SESSION["states_open"] = array();
  11. $_SESSION["history_stack"] = array();
  12. $_SESSION["open_stack"] = array();
  13. $r = mysql_fetch_assoc($qr);
  14. $id = $r["id"];
  15. $this->openState($id);
  16. $this->setCurrentState($id);
  17. $this->getCurrentState();
  18. break;
  19. case "advance":
  20. $this->answer();
  21. $this->advance();
  22. $this->getCurrentState();
  23. break;
  24. case "step_back":
  25. $this->stepBack();
  26. break;
  27. case "get_current":
  28. $this->getCurrentState();
  29. break;
  30. }
  31. }
  32. function answer(){
  33. for($i=0; $i<sizeof($_SESSION["states_open"]); $i++){
  34. if($_SESSION["states_open"][$i]["id"] == $_SESSION["current_state"]){
  35. $_SESSION["states_open"][$i]["answer"] = $_GET["answer"];
  36. $_SESSION["states_open"][$i]["advanced"] = false;
  37. break;
  38. }
  39. }
  40. }
  41. function stepBack(){
  42. if(sizeof($_SESSION["history_stack"])>1){
  43. unset($_SESSION["history_stack"][sizeof($_SESSION["history_stack"])-1]);
  44. $last = $_SESSION["history_stack"][sizeof($_SESSION["history_stack"])-1];
  45. unset($_SESSION["open_stack"][sizeof($_SESSION["open_stack"])-1]);
  46. $_SESSION["states_open"] = $_SESSION["open_stack"][sizeof($_SESSION["open_stack"])-1];
  47. $this->setCurrentState($last, false);
  48. }
  49. $this->getCurrentState();
  50. }
  51. function advance(){
  52. $was_open = false;
  53. for($i=0; $i<sizeof($_SESSION["states_open"]); $i++){
  54. if($_SESSION["states_open"][$i]["id"]==$_SESSION["current_state"]){
  55. $_SESSION["states_open"][$i]["visited"] = true;
  56. }
  57. if(!$_SESSION["states_open"][$i]["visited"]){
  58. $this->setCurrentState($_SESSION["states_open"][$i]["id"]);
  59. $was_open = true;
  60. break;
  61. }
  62. }
  63. if(!$was_open){
  64. $opened = false;
  65. for($i=0; $i<sizeof($_SESSION["states_open"]); $i++){
  66. if($_SESSION["states_open"][$i]["visited"] && !$_SESSION["states_open"][$i]["advanced"]){
  67. $_SESSION["states_open"][$i]["advanced"] = true;
  68. $qr = mysql_query("SELECT connections.expr, id2 FROM connections INNER JOIN states ON states.id=connections.id2 WHERE id1 = ".$_SESSION["states_open"][$i]["id"]);
  69. while($r = mysql_fetch_array($qr)){
  70. if($_SESSION["states_open"][$i]["type"]=="d"){
  71. if($_SESSION["states_open"][$i]["decision_type"]=="Input"){
  72. if($_SESSION["states_open"][$i]["answer"] != $r["expr"]) continue;
  73. }
  74. }
  75. if($this->openState($r["id2"]))
  76. $opened = true;
  77. }
  78. }
  79. }
  80. if($opened) {
  81. $this->advance();
  82. }
  83. }
  84. }
  85. function getCurrentState(){
  86. echo "{";
  87. $id = $_SESSION["current_state"];
  88. if(!$id){
  89. echo '"state":[]}';
  90. return;
  91. }
  92. print_jason_records(false, "SELECT id, name, description_$this->language AS description, type, decision_type, input_type, question_$this->language AS question FROM states WHERE id = $id", "state");
  93. print_jason_records(true, "SELECT connections.expr FROM connections INNER JOIN states ON states.id=connections.id2 WHERE id1 = $id", "answers");
  94. $r = mysql_fetch_assoc(mysql_query("SELECT ".build_field_list(array("description", "info", "document", "video_link"), $this->language)." FROM states WHERE id = $id"));
  95. echo ", \"video_links\":".json_encode(explode("|", $r["video_link"]));
  96. echo ", \"documents\":".json_encode(explode("|", $r["document"]));
  97. echo ", \"info\":".json_encode(explode("|", $r["info"]));
  98. echo "}";
  99. }
  100. function setCurrentState($id, $store_in_history = true){
  101. $_SESSION["current_state"] = $id;
  102. if(!isset($_SESSION["history_stack"])) $_SESSION["history_stack"] = array();
  103. if(!isset($_SESSION["open_stack"])) $_SESSION["open_stack"] = array();
  104. if($store_in_history) $_SESSION["history_stack"][] = $id;
  105. if($store_in_history) $_SESSION["open_stack"][] = $_SESSION["states_open"];
  106. }
  107. function openState($id){
  108. $qr = mysql_query("SELECT id, name, type, decision_type, decision_variable FROM states WHERE id = $id");
  109. $r = mysql_fetch_assoc($qr);
  110. $found = false;
  111. for($i=0; $i<sizeof($_SESSION["states_open"]); $i++){
  112. if($_SESSION["states_open"][$i]["id"] == $id){
  113. $found = true;
  114. break;
  115. }
  116. }
  117. if(!$found){
  118. $_SESSION["states_open"][] = array("id"=>$id, "name"=>$r["name"], "type"=>$r["type"], "decision_type"=>$r["decision_type"], "decision_variable"=>$r["decision_variable"], "visited"=>false, "advanced"=>false, "answer" =>null);
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. }
  124. function init(){
  125. $this->language = isset($_SESSION["language"]) ? $_SESSION["language"] : "en";
  126. }
  127. }
  128. ?>