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