PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/server/clientFunc/evaluationData.php

https://gitlab.com/flummispeed/Automated-importance-assessment-for-new-technologies-and-standards
PHP | 410 lines | 247 code | 28 blank | 135 comment | 45 complexity | 3efbc7ac22ccb072d0a01c79d6721fc9 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2016, Georg-August-Universität Göttingen
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  6. * this file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under the
  11. * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. * either express or implied. See the License for the specific language governing permissions
  13. * and limitations under the License.
  14. *
  15. * Information
  16. * Master thesis title: Automated importance assessment for new technologies and standards
  17. *
  18. * University: Georg August Universität Göttingen - Institute of Computer Science
  19. * Software Engineering for Distributed Systems
  20. *
  21. * Author: Florian Unger
  22. * Submission date: 2016-11-11
  23. *
  24. */
  25. //error_reporting(E_ALL);
  26. //ini_set('display_errors', '1');
  27. require '../class/classes.php';
  28. require '../config/config.php';
  29. require '../func/func.php';
  30. header('Content-type: application/json');
  31. header('Cache-Control: no-cache, must-revalidate');
  32. /**
  33. * Store the evaluated data to database for comparison and future work.
  34. */
  35. /*
  36. * Database connection.
  37. */
  38. $mysqli = new ROSmysqli();
  39. /*
  40. * Catch Data.
  41. */
  42. if (file_get_contents("php://input") != '') {
  43. $data = json_decode(file_get_contents("php://input"));
  44. addSearch($data, $mysqli);
  45. } elseif (isset($_GET['searchid'])) {
  46. $term = getSearchTerm($_GET['searchid'], $mysqli);
  47. $resultItems = getSearchResults($_GET['searchid'], $mysqli);
  48. echo '{"resultItems":[' . $resultItems . '],' . $term . ', "idSearch":' . $_GET['searchid'] . ' , "searchTime":"' . getSearchDateTime($_GET['searchid'], $mysqli) . '"}';
  49. }
  50. /**
  51. * Get SearchTerm in JSON Format of a search.
  52. * @param $search_done_id
  53. * Identifier of the actual search
  54. * @param $mysqli
  55. * Database connection
  56. * @return string
  57. * SearchTerm in JSON format.
  58. */
  59. function getSearchTerm($search_done_id, $mysqli)
  60. {
  61. $term = '';
  62. $query = "SELECT search_term.id, search_term.term, search_term.searchTopic_id "
  63. . "FROM `search_done` , `search_term` "
  64. . "WHERE search_done.searchTerm_id = search_term.id "
  65. . "AND search_done.id =" . $search_done_id;
  66. if ($result = $mysqli->query($query)) {
  67. while ($row = $result->fetch_assoc()) {
  68. //"term":{"id":12,"term":"UML","searchTopicId":2}
  69. $term = '"term":{"id":' . $row['id'];
  70. $term .= ',"term":"' . $row['term'];
  71. $term .= '","searchTopicId":' . $row['searchTopic_id'];
  72. $term .= '}';
  73. }
  74. }
  75. return $term;
  76. }
  77. /**
  78. * Query the evaluation results of a search.
  79. * @param $search_done_id
  80. * @param $mysqli
  81. * @return string
  82. */
  83. function getSearchResults($search_done_id, $mysqli)
  84. {
  85. $searchResults = '';
  86. $searchResults = queryResult($mysqli, 'int', $searchResults, $search_done_id);
  87. $searchResults = queryResult($mysqli, 'double', $searchResults, $search_done_id);
  88. $searchResults = queryResult($mysqli, 'category', $searchResults, $search_done_id);
  89. $searchResults = queryResult($mysqli, 'text', $searchResults, $search_done_id);
  90. $searchResults = queryResult($mysqli, 'json', $searchResults, $search_done_id);
  91. return $searchResults;
  92. }
  93. /**
  94. * Query the evaluation results of a table.
  95. * @param $mysqli
  96. * @param $tableType
  97. * @param $searchResults
  98. * @param $search_done_id
  99. * @return string
  100. */
  101. function queryResult($mysqli, $tableType, $searchResults, $search_done_id)
  102. {
  103. $query = "SELECT value, id_evaluation, valueCanNotBeFound FROM value_" . $tableType . " WHERE search_done_id =" . $search_done_id;
  104. if($tableType == 'json')
  105. {
  106. $query = "SELECT value, id_evaluation, valueCanNotBeFound, object FROM value_" . $tableType . " WHERE search_done_id =" . $search_done_id;
  107. }
  108. if ($result = $mysqli->query($query)) {
  109. while ($row = $result->fetch_assoc()) {
  110. if ($searchResults != '') {
  111. $searchResults .= ', ';
  112. }
  113. if($tableType != 'json')
  114. {
  115. $searchResults .= '{"value":"' . $row['value'] . '",' .
  116. '"type":"' . $tableType . '",';
  117. }
  118. else
  119. {
  120. $searchResults .= '{"value":' . $row['value'] . ','.
  121. '"type":"' . $row['object'] . '",';
  122. }
  123. if($row['valueCanNotBeFound'] == 2)
  124. {
  125. $searchResults .= '"valueCanNotBeFound":"noValA",';
  126. }
  127. else
  128. {
  129. $searchResults .= '"valueCanNotBeFound":"val",';
  130. }
  131. $searchResults .= '"idEvaluation":' . $row['id_evaluation'] . '}';
  132. }
  133. }
  134. return $searchResults;
  135. }
  136. /**
  137. * Insert new search. Get id of this search to map the evaluated data.
  138. * @param $data
  139. * Decoded JSON data to be added to database.
  140. * @param $mysqli
  141. * Database connection.
  142. */
  143. function addSearch($data, $mysqli)
  144. {
  145. $searchId = $data->idSearch * 1;
  146. if($searchId > 0)
  147. {
  148. $query = "UPDATE search_done SET `timestamp` = now() WHERE `search_done`.`id` =" . $searchId;
  149. $mysqli->query($query);
  150. $search_done_id = $searchId;
  151. }
  152. else{
  153. $termId = $data->term->id * 1;
  154. $query = "INSERT INTO search_done (searchTerm_id) VALUES (" . $termId . ")";
  155. $mysqli->query($query);
  156. $search_done_id = $mysqli->insert_id;
  157. }
  158. addEvaluationValues($search_done_id, $data, $mysqli);
  159. }
  160. /**
  161. * Add evaluation values to database
  162. * @param $search_done_id
  163. * Identifier of the actual search
  164. * @param $data
  165. * Evaluated data to be stored
  166. * @param $mysqli
  167. * Database connection
  168. */
  169. function addEvaluationValues($search_done_id, $data, $mysqli)
  170. {
  171. /*
  172. * Values for insert statements for the appropriate tables.
  173. * Depending on the type of the resultItems the values will be stored in appropriate tables.
  174. * E.g.
  175. * $valuesInt = ('12', '2', '1'), (...)
  176. * INSERT INTO value_int (`value`, `search_done_id`, `id_evaluation`) VALUES ($valuesInt)
  177. *
  178. */
  179. $valuesDouble = '';
  180. $valuesInt = '';
  181. $valuesText = '';
  182. $valuesCategory = '';
  183. foreach ($data->resultItems as $item) {
  184. $dataType = $item->type . '';
  185. switch ($dataType) {
  186. case 'int':
  187. if(!updateValue($search_done_id, $item, $mysqli, 'value_int'))
  188. {
  189. $valuesInt = getInputStream($search_done_id, $item, $valuesInt);
  190. }
  191. break;
  192. case 'double':
  193. if(!updateValue($search_done_id, $item, $mysqli, 'value_double'))
  194. {
  195. $valuesDouble = getInputStream($search_done_id, $item, $valuesDouble);
  196. }
  197. break;
  198. case 'category':
  199. if(!updateValue($search_done_id, $item, $mysqli, 'value_category'))
  200. {
  201. $valuesCategory = getInputStream($search_done_id, $item, $valuesCategory);
  202. }
  203. break;
  204. case 'hyperlinkList':
  205. if(!updateValueJson($search_done_id, $item, $mysqli))
  206. {
  207. insertValueJson($search_done_id, $item, $mysqli, 'hyperlinkList');
  208. }
  209. //var_dump();
  210. break;
  211. default: //either this is a text value or it is not a defined type and will be stored as text
  212. if(!updateValue($search_done_id, $item, $mysqli, 'value_text'))
  213. {
  214. $valuesText = getInputStream($search_done_id, $item, $valuesText);
  215. }
  216. }
  217. }
  218. if ($valuesInt != '') {
  219. $query = "INSERT INTO value_int (`value`, `search_done_id`, `id_evaluation`, `valueCanNotBeFound`) VALUES " . $valuesInt;
  220. $mysqli->query($query);
  221. }
  222. if ($valuesDouble != '') {
  223. $query = "INSERT INTO value_double (`value`, `search_done_id`, `id_evaluation`, `valueCanNotBeFound`) VALUES " . $valuesDouble;
  224. $mysqli->query($query);
  225. }
  226. if ($valuesCategory != '') {
  227. $query = "INSERT INTO value_category (`value`, `search_done_id`, `id_evaluation`, `valueCanNotBeFound`) VALUES " . $valuesCategory;
  228. $mysqli->query($query);
  229. }
  230. if ($valuesText != '') {
  231. $query = "INSERT INTO value_text (`value`, `search_done_id`, `id_evaluation`, `valueCanNotBeFound`) VALUES " . $valuesText;
  232. $mysqli->query($query);
  233. }
  234. //Only "idSearch": must be added
  235. $jsonOutput = file_get_contents("php://input");
  236. $pos = strrpos($jsonOutput, '}');
  237. if ($pos === false) {
  238. $pos = strlen($jsonOutput) - 1;
  239. }
  240. $jsonOutput[$pos] = ' '; //remove }
  241. $jsonOutput .= ', "idSearch":' . $search_done_id . ' , "searchTime":"' . getSearchDateTime($search_done_id, $mysqli) . '"}';
  242. echo $jsonOutput;
  243. }
  244. /**
  245. * Get date and time of search
  246. * @param $search_done_id
  247. * @param $mysqli
  248. * @return string
  249. */
  250. function getSearchDateTime($search_done_id, $mysqli)
  251. {
  252. $searchTime = '';
  253. $query = "SELECT DATE_FORMAT( `timestamp` , '%d.%m.%Y - %H:%i' ) AS searchTime FROM `search_done` WHERE id=" . $search_done_id;
  254. if ($result = $mysqli->query($query)) {
  255. while ($row = $result->fetch_assoc()) {
  256. $searchTime = $row['searchTime'];
  257. }
  258. }
  259. return $searchTime;
  260. }
  261. /**
  262. * Extend input stream with further tuple.
  263. * @param $search_done_id
  264. * Identifier of the actual search
  265. * @param $item
  266. * One evaluation that should be stored
  267. * @param $valueStream
  268. * Data stream of tuple to be inserted in database
  269. * @return string
  270. * Data stream of tuple to be inserted in database extend by new item
  271. */
  272. function getInputStream($search_done_id, $item, $valueStream)
  273. {
  274. if ($valueStream != '') {
  275. $valueStream .= ', ';
  276. }
  277. $value = handleInputValue($item->value, $item->valueCanNotBeFound);
  278. return $valueStream . "(" . $value . ", '" . $search_done_id . "', '" . $item->idEvaluation . "', "
  279. . handleValueCanNotBeFound($item->valueCanNotBeFound) .")";
  280. }
  281. /**
  282. * Tries to update the values. Return true in case of update and false else.
  283. * @param $search_done_id
  284. * @param $item
  285. * @param $mysqli
  286. * @param $table
  287. * @return bool
  288. *
  289. */
  290. function updateValue($search_done_id, $item, $mysqli, $table)
  291. {
  292. $query = "SELECT id FROM ".$table." WHERE `search_done_id` = ".$search_done_id." AND `id_evaluation` = " . $item->idEvaluation;
  293. $mysqli->query($query);
  294. $id = 0; //id of the existing value
  295. if ($result = $mysqli->query($query)) {
  296. while ($row = $result->fetch_assoc()) {
  297. $id = $row['id'];
  298. }
  299. }
  300. if($id > 0)
  301. {
  302. $value = handleInputValue($item->value, $item->valueCanNotBeFound);
  303. $query = "UPDATE ".$table." SET `value` = " . $value . ", `valueCanNotBeFound` = '"
  304. .handleValueCanNotBeFound($item->valueCanNotBeFound)."' WHERE id =" . $id;
  305. $mysqli->query($query);
  306. return true;
  307. }
  308. return false;
  309. }
  310. /**
  311. * Updates values that are stored as json objects.
  312. * @param $search_done_id
  313. * @param $item
  314. * @param $mysqli
  315. * @return bool
  316. */
  317. function updateValueJson($search_done_id, $item, $mysqli)
  318. {
  319. $query = "SELECT id FROM value_json WHERE `search_done_id` = ".$search_done_id." AND `id_evaluation` = " . $item->idEvaluation;
  320. $mysqli->query($query);
  321. $id = 0; //id of the existing value
  322. if ($result = $mysqli->query($query)) {
  323. while ($row = $result->fetch_assoc()) {
  324. $id = $row['id'];
  325. }
  326. }
  327. if($id > 0)
  328. {
  329. $json = json_encode($item->value);
  330. $query = "UPDATE value_json SET `value` = '" . $json . "', `valueCanNotBeFound` = '"
  331. .handleValueCanNotBeFound($item->valueCanNotBeFound)."' WHERE id =" . $id;
  332. $mysqli->query($query);
  333. return true;
  334. }
  335. return false;
  336. }
  337. /**
  338. * Insert values that are stored as json objects.
  339. * @param $search_done_id
  340. * @param $item
  341. * @param $mysqli
  342. * @param $object
  343. */
  344. function insertValueJson($search_done_id, $item, $mysqli, $object)
  345. {
  346. $json = json_encode($item->value);
  347. $query = "INSERT INTO value_json (`value`, `object`, `search_done_id`, `id_evaluation`, `valueCanNotBeFound` ) VALUES ('" .
  348. $json . "', '" .
  349. $object . "', " .
  350. $search_done_id . ", " .
  351. $item->idEvaluation . ", '" .
  352. handleValueCanNotBeFound($item->valueCanNotBeFound) . "')";
  353. $mysqli->query($query);
  354. }
  355. /**
  356. * Proof whether the value is null or not
  357. * @param $value
  358. * @param $noVal
  359. * @return string
  360. */
  361. function handleInputValue($value, $noVal)
  362. {
  363. $value = $value . '';
  364. if($value != '' && $noVal != 'noValA')
  365. {
  366. return "'".$value . "'";
  367. }
  368. return 'NULL';
  369. }
  370. /**
  371. * Handles string state to database state of the attribute.
  372. * @param $valueCanNotBeFound
  373. * @return int
  374. */
  375. function handleValueCanNotBeFound($valueCanNotBeFound)
  376. {
  377. if($valueCanNotBeFound == 'noValA')
  378. {
  379. return 2;
  380. }
  381. return 1;
  382. }