1178 lines
40 KiB
PHP
1178 lines
40 KiB
PHP
<?php
|
|
/** \file include\db_questionaire.php
|
|
* \brief DI webinterface database functions - (evaluation) questionaire
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version $Revision: 1.13 $
|
|
* \date $Date: 2014/06/03 16:03:23 $
|
|
*
|
|
* This file contains database functions for the (evaluation) questionaire.
|
|
*/
|
|
|
|
|
|
/*
|
|
* Script template definitions
|
|
*/
|
|
define("ENQ_FEEDBACK_TEMPL_A", 1);
|
|
define("ENQ_FEEDBACK_TEMPL_B", 2);
|
|
define("ENQ_FRONT_COVER_LWB", 3);
|
|
define("ENQ_COMBI_SCRIPT", 4);
|
|
define("ENQ_SUBSCRIPTION_LIST", 5);
|
|
define("ENQ_KEY_LIST", 6);
|
|
define("ENQ_TRANSFER_TECH_MEAS", 7);
|
|
define("ENQ_FRONT_COVER_LLV", 8);
|
|
define("ENQ_WECO", 9);
|
|
define("ENQ_WECO_TRDL", 10);
|
|
define("ENQ_WECO_WORKPLACE", 11);
|
|
define("ENQ_WECO_WORKTIME", 12);
|
|
define("ENQ_WECO_NOVOLTAGE", 13);
|
|
define("ENQ_WECO_SAFETY", 14);
|
|
define("ENQ_VTI", 15);
|
|
define("ENQ_INCIDENT_REPORTING", 16);
|
|
define("ENQ_WORKING_VISIT_REPORT", 17);
|
|
define("ENQ_VHI", 18);
|
|
define("ENQ_BAM", 19);
|
|
define("ENQ_ACCEPT", 20);
|
|
define("ENQ_STRUKTON_RAIL_LLV", 21);
|
|
define("ENQ_LMRA", 22);
|
|
define("ENQ_SWITCH_LIST", 23);
|
|
|
|
/**
|
|
* Read all questionaires for a customer
|
|
*
|
|
* Inputs:
|
|
* - i18n Language for the questionaire
|
|
* - questionare_ids Database id for the questionaire(s)
|
|
* - customer_ids Customer id or array with customer ids; may be 'null'
|
|
* - type Select type of enquete (feedback/draaiboek/NULL)
|
|
*
|
|
* Returns: Array with questionaires ('actie','id', 'datum', 'titel' and array of
|
|
* questions)
|
|
*/
|
|
function db_fetch_questionaires($i18n, $questionaire_ids = NULL, $customer_ids = NULL, $type = NULL)
|
|
{
|
|
if (!$questonaire_ids) {
|
|
if (!$customer_ids) {
|
|
// Search also for all underlying customers
|
|
$row_customers = db_search_customers();
|
|
|
|
// Add own id to array => when not in array
|
|
$found_cust = 0;
|
|
if (is_array($row_customers)) {
|
|
foreach ($row_customers as $row_customer) {
|
|
if ($row_customer['id'] == $_PAGE_INFO['login']['customer']['id']) {
|
|
$found_cust = 1;
|
|
}
|
|
}
|
|
}
|
|
if (!$found_cust) {
|
|
$current_customer = db_fetch_customer($_PAGE_INFO['login']['customer']['id'], 1);
|
|
array_push($row_customers, $current_customer);
|
|
}
|
|
|
|
// build array with customer ids
|
|
$customer_ids = array();
|
|
foreach ($row_customers as $row_customer)
|
|
$customer_ids[] = $row_customer['id'];
|
|
|
|
// All underlying templates we have, now add global templates
|
|
array_push($customer_ids, "NULL");
|
|
} else if (!is_array($customer_ids)) {
|
|
// convert single customer id into an array
|
|
$customer_ids = array($customer_ids);
|
|
}
|
|
} else {
|
|
// don't use customer ids when questionaire ids are provided
|
|
$customer_ids = null;
|
|
}
|
|
|
|
|
|
// get questionaire(s)
|
|
$query = "SELECT enquete2.id,enquete2.level,enquete2.datum,enquete_i18n2.data as titel ";
|
|
$query .= "FROM enquete2,enquete_i18n2 ";
|
|
$query .= "WHERE ";
|
|
$query .= "enquete2.id = enquete_i18n2.enquete AND ";
|
|
|
|
// Use type
|
|
if (!is_null($type)) {
|
|
$query .= "enquete2.type ='" . $type . "' AND ";
|
|
}
|
|
|
|
$query .= "enquete_i18n2.i18n='" . $i18n . "' AND ";
|
|
$query .= "enquete_i18n2.vraag IS NULL AND ";
|
|
if ($customer_ids)
|
|
// Check for global templates
|
|
if (in_array("NULL", $customer_ids)) {
|
|
$query .= "(";
|
|
}
|
|
|
|
$query .= "enquete2.klant IN (" . implode(",", $customer_ids) . ") ";
|
|
|
|
// Check for global templates
|
|
if (in_array("NULL", $customer_ids)) {
|
|
$query .= "OR enquete2.klant is NULL) ";
|
|
} elseif (is_array($questionaire_ids))
|
|
$query .= "enquete2.id IN (" . implode(",", $questionaire_ids) . ")";
|
|
else
|
|
$query .= "enquete2.id=" . $questionaire_ids;
|
|
$questionaires = db_fetch_data($query);
|
|
|
|
if (!$questionaires) return FALSE;
|
|
|
|
// build the result array
|
|
$result = array();
|
|
foreach ($questionaires as $questionaire) {
|
|
$questionaire['vragen'] = db_fetch_questionaire($questionaire['id'], $i18n);
|
|
array_push($result, $questionaire);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Read a questionaire
|
|
* Private function, always use 'db_fetch_questionaires()'
|
|
*
|
|
* Inputs:
|
|
* - dbid Database id for the questionaire
|
|
* - i18n Language
|
|
*
|
|
* Returns: Array with questions
|
|
*/
|
|
function db_fetch_questionaire($dbid, $i18n)
|
|
{
|
|
// fetch the questions
|
|
$query = "SELECT enquete_vraag2.actie,enquete_vraag2.actie,enquete_vraag2.id,enquete_vraag2.volgorde,enquete_vraag2.type,enquete_vraag2.antwoorden,enquete_i18n2.data as vraag ";
|
|
$query .= "FROM enquete_vraag2,enquete_i18n2 ";
|
|
$query .= "WHERE ";
|
|
$query .= "enquete_i18n2.vraag=enquete_vraag2.id AND ";
|
|
$query .= "enquete_vraag2.enquete=" . $dbid . " AND ";
|
|
$query .= "enquete_i18n2.i18n='" . $i18n . "' AND ";
|
|
$query .= "enquete_i18n2.key IS NULL ";
|
|
$query .= "ORDER BY enquete_vraag2.volgorde";
|
|
$questionaire = db_fetch_data($query);
|
|
if (!$questionaire) return FALSE;
|
|
|
|
// re-arrange the questions and get additional info
|
|
$result = array();
|
|
foreach ($questionaire as $question) {
|
|
$query = "SELECT `key`,`data` ";
|
|
$query .= "FROM enquete_i18n2 ";
|
|
$query .= "WHERE ";
|
|
$query .= "i18n='" . $i18n . "' AND ";
|
|
$query .= "enquete=" . $dbid . " AND ";
|
|
$query .= "vraag=" . $question['id'];
|
|
$question_info = db_fetch_data($query);
|
|
// re-arrange question_inofo
|
|
foreach ($question_info as $question_data)
|
|
$question_info[$question_data['key']] = $question_data['data'];
|
|
// prepare the meta-info arrays 'match' or 'antwoorden'
|
|
switch ($question['type']) {
|
|
case 'text':
|
|
case 'simpletext':
|
|
$question['match'] = $question['antwoorden'];
|
|
unset($question['antwoorden']);
|
|
break;
|
|
case 'set':
|
|
case 'enum':
|
|
$answers = array();
|
|
foreach (explode(",", $question['antwoorden']) as $answer) {
|
|
// preserve order
|
|
$answers[] = array('key' => $answer, 'antwoord' => $question_info[$answer]);
|
|
}
|
|
$question['antwoorden'] = $answers;
|
|
break;
|
|
}
|
|
|
|
$result[$question['id']] = $question;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Read a single questionaire
|
|
*
|
|
* Inputs:
|
|
* - id Question id
|
|
*
|
|
* Returns: Array with question info
|
|
*/
|
|
function db_fetch_single_questionaire($id, $i18n, $dbid)
|
|
{
|
|
|
|
// Initial values
|
|
// $query = "SELECT * FROM enquete_vraag2 where id='" . $id . "'";
|
|
|
|
|
|
$query = "SELECT enquete_vraag2.actie,enquete_vraag2.actie,enquete_vraag2.id,enquete_vraag2.volgorde,enquete_vraag2.type,enquete_vraag2.antwoorden,enquete_i18n2.data as vraag ";
|
|
$query .= "FROM enquete_vraag2,enquete_i18n2 ";
|
|
$query .= "WHERE ";
|
|
$query .= "enquete_i18n2.vraag=enquete_vraag2.id AND ";
|
|
$query .= "enquete_vraag2.id=" . $id . " AND ";
|
|
$query .= "enquete_i18n2.i18n='" . $i18n . "' AND ";
|
|
$query .= "enquete_i18n2.key IS NULL ";
|
|
$query .= "ORDER BY enquete_vraag2.volgorde";
|
|
$questionaire = db_fetch_data($query);
|
|
|
|
if (!$questionaire) return FALSE;
|
|
|
|
if (is_array($questionaire)) {
|
|
|
|
// re-arrange the questions and get additional info
|
|
$result = array();
|
|
foreach ($questionaire as $question) {
|
|
|
|
|
|
$query = "SELECT `key`,`data` ";
|
|
$query .= "FROM enquete_i18n2 ";
|
|
$query .= "WHERE ";
|
|
$query .= "i18n='" . $i18n . "' AND ";
|
|
$query .= "enquete=" . $dbid . " AND ";
|
|
$query .= "vraag=" . $question['id'];
|
|
$question_info = db_fetch_data($query);
|
|
|
|
if (is_array($question_info)) {
|
|
// re-arrange question_inofo
|
|
foreach ($question_info as $question_data) {
|
|
$question_info[$question_data['key']] = $question_data['data'];
|
|
}
|
|
// prepare the meta-info arrays 'match' or 'antwoorden'
|
|
switch ($question['type']) {
|
|
case 'text':
|
|
case 'simpletext':
|
|
$question['match'] = $question['antwoorden'];
|
|
unset($question['antwoorden']);
|
|
break;
|
|
case 'set':
|
|
case 'enum':
|
|
$answers = array();
|
|
foreach (explode(",", $question['antwoorden']) as $answer) {
|
|
// preserve order
|
|
$answers[] = array('key' => $answer, 'antwoord' => $question_info[$answer]);
|
|
}
|
|
$question['antwoorden'] = $answers;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$result[$question['id']] = $question;
|
|
}
|
|
} else {
|
|
$result = $questionaire;
|
|
}
|
|
return $result;
|
|
|
|
}
|
|
|
|
/**
|
|
* Get enqeute_vraag information by enquete
|
|
*
|
|
* Inputs:
|
|
* - enquete: enquete number
|
|
*
|
|
* Return: result
|
|
*/
|
|
function db_fetch_questionaires_enquete($enquete)
|
|
{
|
|
$result = null;
|
|
|
|
$result = db_fetch_data("SELECT * FROM enquete_vraag2 WHERE enquete='" . $enquete . "'");
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Store a questionaire
|
|
*
|
|
* TO DO
|
|
*/
|
|
function db_store_questionaire($user_id, $project_id, $dbid, $answers, $latitude, $longitude, $referencenr, $userTable)
|
|
{
|
|
|
|
global $_PAGE_INFO;
|
|
// Check coordinates
|
|
if (is_null($latitude)) {
|
|
|
|
if (isset($_SESSION[$_PAGE_INFO['id']]['app']['gps']['lat']) && !empty($_SESSION[$_PAGE_INFO['id']]['app']['gps']['lat'])) {
|
|
$latitude = $_SESSION[$_PAGE_INFO['id']]['app']['gps']['lat'];
|
|
}
|
|
}
|
|
|
|
if (is_null($longitude)) {
|
|
if (isset($_SESSION[$_PAGE_INFO['id']]['app']['gps']['lon']) && !empty($_SESSION[$_PAGE_INFO['id']]['app']['gps']['lon'])) {
|
|
$longitude = $_SESSION[$_PAGE_INFO['id']]['app']['gps']['lon'];
|
|
}
|
|
}
|
|
|
|
$check_entry = db_get_enquete_entry_id($dbid, $user_id, $project_id, 0, $userTable);
|
|
|
|
// Check if there is not already an entry for this user in combination with the enquete en project
|
|
if (empty($check_entry)) {
|
|
// Create enquete entry
|
|
|
|
// store user info etc
|
|
$query = "INSERT INTO enquete_entry (enquete,gebruiker,project,datum,latitude,longitude,gebruiker_tabel) VALUES (";
|
|
$query .= $dbid . ",";
|
|
$query .= $user_id . ",";
|
|
$query .= ($project_id ? $project_id : "NULL") . ",";
|
|
$query .= "NOW() ,";
|
|
$query .= (!is_numeric($latitude)) ? "NULL," : "'" . ($latitude) . "',";
|
|
$query .= (!is_numeric($longitude)) ? "NULL," : "'" . ($longitude) . "',";
|
|
$query .= "'$userTable'";
|
|
$query .= ")";
|
|
|
|
if (!db_store_data($query)) return FALSE;
|
|
$id = db_fetch_last_id();
|
|
DBG('create entry');
|
|
} else {
|
|
$id = $check_entry[0]['id'];
|
|
}
|
|
|
|
// Anser is array (always size of one)
|
|
foreach ($answers as $question => $answer) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry=" . $id . " AND vraag=" . $question . " AND volgnummer=" . $referencenr . "";
|
|
}
|
|
|
|
$questionaire = db_fetch_data($query);
|
|
|
|
$update = false;
|
|
|
|
// Check if there is already an question
|
|
if (!empty($questionaire)) {
|
|
$update = true;
|
|
}
|
|
|
|
// Check if question already is stored in db
|
|
if ($update) {
|
|
|
|
foreach ($answers as $question => $answer) {
|
|
// update question
|
|
$query = "UPDATE enquete_data SET antwoord=";
|
|
if (is_array($answer))
|
|
$query .= "'" . specialchars(implode(",", $answer)) . "'";
|
|
else
|
|
$query .= "'" . specialchars($answer) . "'";
|
|
|
|
$query .= " WHERE enquete_entry='" . $id . "' AND vraag='" . $question . "' AND volgnummer=" . $referencenr . "";
|
|
}
|
|
db_store_data($query);
|
|
|
|
} else {
|
|
// Store question
|
|
$query = "INSERT INTO enquete_data (enquete_entry,vraag,antwoord,volgnummer) VALUES ";
|
|
$sep = "";
|
|
foreach ($answers as $question => $answer) {
|
|
$query .= $sep . "(";
|
|
$query .= $id . ",";
|
|
$query .= $question . ",";
|
|
if (is_array($answer))
|
|
$query .= "'" . specialchars(implode(",", $answer)) . "',";
|
|
else
|
|
$query .= "'" . specialchars($answer) . "',";
|
|
|
|
$query .= "'" . $referencenr . "'";
|
|
$query .= ")";
|
|
//$sep = ",";
|
|
|
|
}
|
|
db_store_data($query);
|
|
|
|
}
|
|
|
|
//return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Get enquete_entry number
|
|
*
|
|
* Inputs:
|
|
* - enquete: enquete number
|
|
* - user: user id
|
|
* - project: project id
|
|
* - choice: choice number
|
|
*
|
|
* Return: result
|
|
*/
|
|
function db_get_enquete_entry_id($enquete, $user, $project, $choice = 0, $userTable = 'gebruiker')
|
|
{
|
|
$result = null;
|
|
|
|
if ($choice == 0) {
|
|
$query = "SELECT * FROM enquete_entry WHERE enquete='$enquete' AND gebruiker='$user' AND project='$project' AND gebruiker_tabel='$userTable'";
|
|
} elseif ($choice == 1) {
|
|
$query = "SELECT * FROM enquete_entry WHERE enquete='" . $enquete . "' AND project='" . $project . "'";
|
|
} elseif ($choice == 2) {
|
|
$query = "SELECT * FROM enquete_entry WHERE gebruiker='" . $user . "' AND project='" . $project . "' AND gebruiker_tabel = '$userTable'";
|
|
} elseif ($choice == 3) {
|
|
$query = "SELECT * FROM enquete_entry WHERE id='" . $enquete . "'";
|
|
}
|
|
$result = db_fetch_data($query);
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Store enquete_entry number
|
|
*
|
|
* Inputs:
|
|
* - enquete: enquete number
|
|
* - user: user id
|
|
* - project: project id
|
|
* - choice: choice number
|
|
*
|
|
* Return: result
|
|
*/
|
|
function db_store_enquete_entry_id($dbid, $user, $userTable, $project, $lat, $lon)
|
|
{
|
|
// Create enquete entry
|
|
|
|
// store user info etc
|
|
$query = "INSERT INTO enquete_entry (enquete,gebruiker,project,datum,latitude,longitude,gebruiker_tabel) VALUES (";
|
|
$query .= $dbid . ",";
|
|
$query .= $user . ",";
|
|
$query .= $project . ",";
|
|
$query .= "NOW() ,";
|
|
$query .= "'" . $lat . "',";
|
|
$query .= "'" . $lon . "',";
|
|
$query .= "'$userTable'";
|
|
$query .= ")";
|
|
|
|
if (!db_store_data($query)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* return $result;
|
|
* }
|
|
* Get question information
|
|
*
|
|
* Inputs:
|
|
* - entry: enquete_entry number
|
|
* - choice: choice number
|
|
* - question: question number
|
|
* - answer: answer
|
|
*
|
|
* Return: result
|
|
*/
|
|
function db_get_question($entry, $choice = 0, $question = "", $answer = "")
|
|
{
|
|
$result = null;
|
|
|
|
if ($choice == 0) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' ORDER BY vraag DESC LIMIT 1";
|
|
} elseif ($choice == 1) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' ORDER BY volgnummer,vraag";
|
|
} elseif ($choice == 2) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' AND vraag='" . $question . "' AND antwoord='" . $answer . "'";
|
|
} elseif ($choice == 3) {
|
|
$query = "SELECT * FROM enquete_data WHERE (enquete_entry='" . $entry . "' AND vraag='" . $question[0] . "' AND antwoord='" . $answer[0] . "') OR (enquete_entry='" . $entry . "' AND vraag='" . $question[1] . "' AND antwoord='" . $answer[1] . "')";
|
|
} elseif ($choice == 4) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' AND vraag='" . $question . "'";
|
|
} elseif ($choice == 5) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' AND volgnummer='" . $question . "'";
|
|
} elseif ($choice == 6) {
|
|
$query = "SELECT * FROM enquete_data WHERE vraag='" . $entry . "'";
|
|
} elseif ($choice == 7) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' ORDER BY volgnummer DESC LIMIT 1";
|
|
} elseif ($choice == 8) {
|
|
$query = "SELECT * FROM enquete_data WHERE enquete_entry='" . $entry . "' AND vraag='" . $question . "' AND antwoord='" . $answer . "'";
|
|
}
|
|
|
|
$result = db_fetch_data($query);
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Delete question information from enquete_data
|
|
*
|
|
* Inputs:
|
|
* - entry: enquete_entry number
|
|
* - refnr: ref number
|
|
*
|
|
* Return: 1/0(ERROR)
|
|
*/
|
|
function db_delete_question($entry, $refnr, $choice = 0, $question = "")
|
|
{
|
|
$result = 0;
|
|
|
|
if (!$choice) {
|
|
$query = "DELETE FROM enquete_data WHERE enquete_entry='" . $entry . "' AND volgnummer='" . $refnr . "'";
|
|
}
|
|
|
|
if ($choice == 1) {
|
|
$query = "UPDATE enquete_data SET antwoord='' WHERE enquete_entry='" . $entry . "' AND volgnummer='" . $refnr . "' AND vraag='" . $question . "'";
|
|
|
|
}
|
|
|
|
// if(db_store_data("DELETE from enquete_data where enquete_entry='" . $entry . "' and volgnummer='" . $refnr . "';")){
|
|
// $result = 1;
|
|
// }
|
|
|
|
if (db_store_data($query)) {
|
|
$result = 1;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Delete enquete entry from enquete_entry
|
|
*
|
|
* Inputs:
|
|
* - entry: enquete_entry number
|
|
* - project: project id
|
|
* - user: user id
|
|
*
|
|
* Return: 1/0(ERROR)
|
|
*/
|
|
function db_delete_enquete_entry($entry, $project, $user, $userTable)
|
|
{
|
|
$result = 0;
|
|
|
|
if (db_store_data("DELETE FROM enquete_entry WHERE id='$entry' AND project='$project' AND gebruiker='$user' AND gebruiker_tabel='$userTable';")) {
|
|
$result = 1;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Store Save flag in database
|
|
*
|
|
* Inputs:
|
|
* - enquete_entry: enquete_entry number
|
|
*
|
|
* Return: 1/0(Error)
|
|
*/
|
|
function db_store_flag($enquete_entry)
|
|
{
|
|
$result = 1;
|
|
$query = "UPDATE enquete_data SET opgeslagen='yes' WHERE enquete_entry='" . $enquete_entry . "'";
|
|
|
|
if (!db_store_data($query)) {
|
|
$result = 0;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Update a questionaire
|
|
*
|
|
* TO DO
|
|
*/
|
|
function db_update_questionaire()
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Read the answers for a questionaire by id
|
|
*
|
|
* Inputs:
|
|
* - dbid Database ID, for
|
|
* - what The questionaire (what == 0), project ID (what == 1)
|
|
* or a single entry (what = 2); see QUESTIONAIRE_xxx
|
|
* defines below
|
|
*
|
|
* Returns: Array with questionaire answers ('datum', 'gebruiker', 'project'
|
|
* and array with "vraag"/"antwoord" pairs)
|
|
*/
|
|
define('QUESTIONAIRE_ALL', 0);
|
|
define('QUESTIONAIRE_PROJECT', 1);
|
|
define('QUESTIONAIRE_SINGLE', 2);
|
|
function db_fetch_questionaire_answers($dbid, $what, $type = NULL)
|
|
{
|
|
$query = "SELECT enquete_entry.id,enquete_entry.gebruiker,enquete_entry.enquete,enquete_entry.project,enquete_entry.datum,enquete_entry.longitude,enquete_entry.latitude,enquete_entry.gebruiker_tabel ";
|
|
$query .= "FROM enquete_entry ";
|
|
|
|
// Use type
|
|
if (!is_null($type)) {
|
|
$query .= ",enquete2 ";
|
|
}
|
|
|
|
if ($what == QUESTIONAIRE_ALL)
|
|
$query .= "WHERE enquete=" . $dbid;
|
|
else if ($what == QUESTIONAIRE_PROJECT) {
|
|
$query .= "WHERE project=" . $dbid . " ";
|
|
|
|
// Use type
|
|
if (!is_null($type)) {
|
|
$query .= "AND enquete2.id=enquete_entry.enquete AND enquete2.type='" . $type . "' ";
|
|
}
|
|
|
|
$query .= "ORDER BY enquete_entry.id DESC";
|
|
} else if ($what == QUESTIONAIRE_SINGLE)
|
|
$query .= "WHERE id=" . $dbid;
|
|
// else: everything by default; this is not the intended interface :-)
|
|
if (!($result = db_fetch_data($query))) return FALSE;
|
|
for ($i = 0; $i < count($result); $i++) {
|
|
$query = "SELECT enquete_entry, vraag,antwoord,opgeslagen,volgnummer ";
|
|
$query .= "FROM enquete_data ";
|
|
$query .= "WHERE enquete_entry=" . $result[$i]['id'] . " ";
|
|
$query .= "ORDER by vraag ";
|
|
$result[$i]['antwoorden'] = db_fetch_data($query);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get answer from question by vraag and enquete_entry and volgnummer
|
|
*
|
|
* Inputs:
|
|
* - enquete_entry: enquete_entry number
|
|
* - question: question number
|
|
* - refnr: reference number
|
|
*
|
|
* Return: result
|
|
*/
|
|
function db_fetch_questionaire_answer($enquete_entry, $question, $refnr)
|
|
{
|
|
$result = null;
|
|
$query = "SELECT * FROM enquete_data WHERE vraag='" . $question . "' AND enquete_entry='" . $enquete_entry . "' AND volgnummer='" . $refnr . "';";
|
|
|
|
$result = db_fetch_data($query);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
/**
|
|
* Store the answers for a questionaire
|
|
*
|
|
* Inputs:
|
|
* - user_id User id
|
|
* - project_id Project id
|
|
* - dbid Database id for the questionaire
|
|
* - answers Array with an array of answers for each question
|
|
*
|
|
* Returns: Success (database id) or failure (boolean FALSE)
|
|
*/
|
|
function db_store_questionaire_answers($user_id, $project_id, $userTable, $dbid, $answers, $latitude, $longitude)
|
|
{
|
|
global $_PAGE_INFO;
|
|
|
|
// store user info etc
|
|
$query = "INSERT INTO enquete_entry (enquete,gebruiker,project,datum,latitude,longitude,gebruiker_tabel) VALUES (";
|
|
$query .= $dbid . ",";
|
|
$query .= $user_id . ",";
|
|
$query .= ($project_id ? $project_id : "NULL") . ",";
|
|
$query .= "NOW() ,";
|
|
$query .= (!is_numeric($latitude)) ? "NULL," : "'" . ($latitude) . "',";
|
|
$query .= (!is_numeric($longitude)) ? "NULL," : "'" . ($longitude) . "',";
|
|
$query .= "'$userTable'";
|
|
$query .= ")";
|
|
|
|
if (!db_store_data($query)) return FALSE;
|
|
$id = db_fetch_last_id();
|
|
|
|
$query = "INSERT INTO enquete_data (enquete_entry,vraag,antwoord) VALUES ";
|
|
$sep = "";
|
|
|
|
// Are there answers ?
|
|
if (is_array($answers) & !empty($answers)) {
|
|
foreach ($answers as $question => $answer) {
|
|
$query .= $sep . "(";
|
|
$query .= $id . ",";
|
|
$query .= $question . ",";
|
|
if (is_array($answer))
|
|
$query .= "'" . specialchars(implode(",", $answer)) . "'";
|
|
else
|
|
$query .= "'" . specialchars($answer) . "'";
|
|
$query .= ")";
|
|
$sep = ",";
|
|
}
|
|
}
|
|
|
|
db_store_data($query);
|
|
|
|
if ($project_id) {
|
|
require_once("include/report_evaluation.php");
|
|
|
|
$file['filename'] = _("MTinfo") . " " . strtolower(_("Project")) . " " . strtolower(_("evaluation")) . "_" . str_replace("-", "", $date) . ".pdf";
|
|
$file['mimetype'] = "application/pdf";
|
|
$file['document'] = report_evaluation($id, "S", $user_id, $userTable);
|
|
|
|
// Need to change the skin based on the rights holding user's company
|
|
$rightsHoldingUserId = $user_id;
|
|
|
|
// Get rights holding user id so we can get their company
|
|
if ($userTable === 'gebruiker_profiel') {
|
|
$checkInOutInfo = user_check_inout_info($project_id, $user_id, 8, $userTable);
|
|
|
|
if (is_array($checkInOutInfo)) {
|
|
$rightsHoldingUserId = getRightsHoldingUserId($project_id, $checkInOutInfo['lwb'], $checkInOutInfo['lwb_tabel']);
|
|
}
|
|
}
|
|
|
|
// Store the original skin
|
|
$originalSkinName = $_SESSION[$_PAGE_INFO['id']]['skin_name'];
|
|
$originalSkin = $_SESSION[$_PAGE_INFO['id']]['skin'];
|
|
|
|
// Get user
|
|
$user = db_fetch_user($rightsHoldingUserId, null, 1);
|
|
if (is_array($user)) {
|
|
// Get company
|
|
$customer = db_fetch_customer($user['klant'], 1);
|
|
|
|
if (is_array($customer) && empty($customer['skin']) === false) {
|
|
// Define skin name
|
|
$_SESSION[$_PAGE_INFO['id']]['skin_name'] = _(strtolower($customer['skin']));
|
|
$_SESSION[$_PAGE_INFO['id']]['skin'] = ($_PAGE_INFO['tablet']) ? strtolower($customer['skin']) . "_tablet" : strtolower($customer['skin']);
|
|
}
|
|
}
|
|
|
|
db_project_send_mail_for_file_upload($project_id, 'evaluatie', $file);
|
|
|
|
// Restore the original skin
|
|
$_SESSION[$_PAGE_INFO['id']]['skin_name'] = $originalSkinName;
|
|
$_SESSION[$_PAGE_INFO['id']]['skin'] = $originalSkin;
|
|
}
|
|
|
|
return $id;
|
|
}
|
|
|
|
|
|
/**
|
|
* Fetch project questionaire (feedback templates)
|
|
*
|
|
* Inputs:
|
|
* - project: Project id
|
|
*
|
|
* Return: array containing selected questionaires
|
|
*/
|
|
function db_fetch_project_questionaire($project, $level = "")
|
|
{
|
|
|
|
if (!isset($level)) {
|
|
// Fetch questionaires
|
|
$questionaires = db_fetch_data("SELECT * FROM enquete_project WHERE project='" . $project . "'");
|
|
} else if (isset($level)) {
|
|
// Fetch questionaires
|
|
$questionaires = db_fetch_data("SELECT * FROM enquete_project WHERE project='" . $project . "' AND level >= '" . $level . "'");
|
|
}
|
|
|
|
return $questionaires;
|
|
}
|
|
|
|
|
|
/**
|
|
* Fetch enquete (enquete info)
|
|
*
|
|
* Inputs:
|
|
* - id: enquete id
|
|
*
|
|
* Return: array containing selected enquete info
|
|
*/
|
|
function db_fetch_enqeute($id, $type = NULL)
|
|
{
|
|
// Initial values
|
|
$query = "SELECT * FROM enquete2 WHERE id='" . $id . "'";
|
|
|
|
// Use type
|
|
if (!is_null($type)) {
|
|
$query .= " AND `type`='" . $type . "'";
|
|
}
|
|
|
|
// Fetch questionaires
|
|
$enquete = db_fetch_data($query);
|
|
|
|
return $enquete;
|
|
}
|
|
|
|
|
|
/**
|
|
* Fetch dependencies from questionaire
|
|
*
|
|
* Inputs:
|
|
* - question: Question id
|
|
* - $choice: Choice (0/1)
|
|
*
|
|
* Return: array containing selected dependencies
|
|
*/
|
|
function db_fetch_dependency_questionaire($question, $choice = 0)
|
|
{
|
|
|
|
// Fetch dependencies
|
|
if ($choice == 0) {
|
|
// Get question with dependeny
|
|
$dependencies = db_fetch_data("SELECT * FROM enquete_dependency2 WHERE vraag='" . $question . "'");
|
|
} else if ($choice == 1) {
|
|
// Get question where other question depends on
|
|
$dependencies = db_fetch_data("SELECT * FROM enquete_dependency2 WHERE dependency='" . $question . "'");
|
|
}
|
|
|
|
return $dependencies;
|
|
}
|
|
|
|
/**
|
|
* db_fetch_enquete_translations
|
|
*
|
|
* Inputs:
|
|
* - question: Question id
|
|
* - lang: language
|
|
*
|
|
* Return: array containing selected translation
|
|
*/
|
|
function db_fetch_enquete_translations($question, $lang = 'nl', $enquete, $choice = 0)
|
|
{
|
|
|
|
$result = 0;
|
|
|
|
if ($question == NULL) {
|
|
$result = db_fetch_data("SELECT * FROM enquete_i18n2 WHERE vraag IS NULL AND i18n='" . $lang . "' AND enquete='" . $enquete . "' ");
|
|
} else {
|
|
|
|
$query = "SELECT * FROM enquete_i18n2 WHERE vraag = '" . $question . "' AND i18n='" . $lang . "'";
|
|
if ($choice == 1) {
|
|
$query .= " AND `key` is NULL";
|
|
}
|
|
|
|
$result = db_fetch_data($query);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Store project questionaire (feedback templates)
|
|
*
|
|
* Inputs:
|
|
* - project: Project id
|
|
* - questionaire: Array containing selected questionaires
|
|
*
|
|
* Return: 1 (OK)/ 0(Error)
|
|
*/
|
|
function db_store_project_questionaire($project, $questionaires)
|
|
{
|
|
// Initial return value
|
|
$result = 0;
|
|
|
|
// Start transaction
|
|
db_start_transaction();
|
|
|
|
// Delete older feedback templates
|
|
db_store_data("DELETE FROM enquete_project WHERE project='" . $project . "'");
|
|
|
|
if (is_array($questionaires)) {
|
|
foreach ($questionaires as $questionaire) {
|
|
db_store_data("INSERT INTO enquete_project(project, enquete,level) VALUES('" . $project . "','" . $questionaire['id'] . "','" . $questionaire['level'] . "')");
|
|
}
|
|
}
|
|
|
|
// Commit transaction
|
|
if (db_commit_transaction()) {
|
|
$result = 1;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Get question type
|
|
*
|
|
* Inputs:
|
|
* - enquete: enquete number
|
|
* - type: type question
|
|
*
|
|
* Return: result
|
|
*/
|
|
function db_get_questions_by_type($enquete, $type)
|
|
{
|
|
$result = null;
|
|
$result = db_fetch_data("SELECT * FROM enquete_vraag2 WHERE enquete='" . $enquete . "' AND type='" . $type . "'");
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
/**
|
|
* Get question with button type
|
|
*
|
|
* Inputs:
|
|
* - enquete: enquete number
|
|
*
|
|
* Return: result
|
|
*/
|
|
function check_buttons($enquete)
|
|
{
|
|
$result = null;
|
|
|
|
$result = db_fetch_data("SELECT * FROM enquete_vraag2 WHERE enquete='" . $enquete . "' AND type LIKE 'button%'");
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Store check in/out or request to check in/out
|
|
*
|
|
* Inputs:
|
|
* - info: array with information to check /in/out the user
|
|
*
|
|
* Return: 1 (OK)/ -1(Error)
|
|
*/
|
|
function db_store_check_in_out_2($info)
|
|
{
|
|
// Store check in data
|
|
if (is_array($info)) {
|
|
|
|
$query = "INSERT INTO project_incheck_uitcheck (project, gebruiker_tabel, gebruiker,gebruiker_actie, naam, mobielnr, dvp , t, latitude, longitude, actie,lwb, lwb_tabel) VALUES (";
|
|
|
|
$query .= ($info['project_id'] ? $info['project_id'] : "NULL") . ",";
|
|
$query .= ($info['user_table'] ? "'" . $info['user_table'] . "'" : "NULL") . ",";
|
|
$query .= ($info['user_id'] ? $info['user_id'] : "NULL") . ",";
|
|
$query .= ($info['user_action'] ? $info['user_action'] : "NULL") . ",";
|
|
$query .= ($info['name'] ? "'" . $info['name'] . "'" : "NULL") . ",";
|
|
$query .= ($info['mobile_nr'] ? "'" . $info['mobile_nr'] . "'" : "NULL") . ",";
|
|
$query .= ($info['dvp'] ? "'" . $info['dvp'] . "'" : "NULL") . ",";
|
|
$query .= ($info['t'] ? $info['t'] : "NULL") . ",";
|
|
$query .= ($info['lat'] ? $info['lat'] : "NULL") . ",";
|
|
$query .= ($info['long'] ? $info['long'] : "NULL") . ",";
|
|
$query .= ($info['actie'] ? "'" . $info['actie'] . "'" : "NULL") . ",";
|
|
$query .= ($info['lwb'] ? "'" . $info['lwb'] . "'" : "NULL") . ",";
|
|
$query .= ($info['lwb_table'] ? "'" . $info['lwb_table'] . "'" : "NULL");
|
|
$query .= ")";
|
|
|
|
if (!db_store_data($query)) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Get check in/out information
|
|
*
|
|
* Inputs:
|
|
* - project_id: project id
|
|
* - name: name
|
|
* - choice: choice number
|
|
* - mobile: mboile number
|
|
*
|
|
* Return: result
|
|
*/
|
|
function user_check_inout_info($project_id, $name, $choice = 0, $mobile = "", $lwbTable = null, $accessLevel = null)
|
|
{
|
|
|
|
$result = null;
|
|
if (!$choice) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND naam='" . $name . "' ORDER BY t DESC;");
|
|
} else if ($choice == 1) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND gebruiker='" . $name . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} else if ($choice == 2) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND naam='" . $name . "' AND mobielnr='" . $mobile . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} elseif ($choice == 3) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND naam='" . $name . "' ORDER BY t DESC;");
|
|
return ($result);
|
|
} elseif ($choice == 4) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND mobielnr='" . $mobile . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} elseif ($choice == 5) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE mobielnr='" . $mobile . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} elseif ($choice == 6) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE mobielnr LIKE '%" . $mobile . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} else if ($choice == 7) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND naam='" . $name . "' AND mobielnr LIKE '%" . $mobile . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} else if ($choice == 8) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND gebruiker_tabel='" . $mobile . "' AND gebruiker='" . $name . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} else if ($choice == 9) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE gebruiker_tabel='" . $project_id . "' AND gebruiker='" . $name . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} else if ($choice == 10) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND dvp='" . $name . "' ORDER BY t DESC LIMIT 1;");
|
|
return ($result[0]);
|
|
} else if ($choice == 11) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' ORDER BY dvp,t DESC;");
|
|
return ($result);
|
|
} else if ($choice == 12) {
|
|
$subQuery = "(SELECT b.id FROM project_incheck_uitcheck AS b WHERE a.gebruiker = b.gebruiker AND b.project='" . $project_id . "'";
|
|
|
|
if ($lwbTable) {
|
|
$subQuery .= " AND lwb_tabel='$lwbTable'";
|
|
}
|
|
|
|
$subQuery .= " ORDER BY b.t DESC LIMIT 1)";
|
|
$query = "SELECT * FROM project_incheck_uitcheck AS a WHERE a.id = $subQuery";
|
|
$result = db_fetch_data($query);
|
|
return $result;
|
|
} else if ($choice == 13) {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='$project_id' AND level=$accessLevel ORDER BY dvp,t DESC;");
|
|
return ($result);
|
|
} else {
|
|
$result = db_fetch_data("SELECT * FROM project_incheck_uitcheck WHERE project='" . $project_id . "' AND naam='" . $name . "' ORDER BY t DESC LIMIT 2;");
|
|
return ($result);
|
|
}
|
|
|
|
return ($result[0]);
|
|
|
|
}
|
|
|
|
/**
|
|
* Returns the current access level for the provided profile and project id
|
|
*
|
|
* @param int $profileId The id of the profile to get the access level for
|
|
* @param int $projectId The id of the project to get the access level for
|
|
* @return int|bool The access level if there is one or false if the query failed
|
|
*/
|
|
function getCheckInOutAccessLevel($profileId, $projectId, $action = null)
|
|
{
|
|
$query = "SELECT level FROM project_incheck_uitcheck WHERE gebruiker = " . $profileId .
|
|
" AND project='" . $projectId . "'";
|
|
|
|
if ($action) {
|
|
$query .= " AND actie='$action'";
|
|
}
|
|
|
|
$query .= " ORDER BY t DESC LIMIT 1";
|
|
|
|
$result = db_fetch_data($query);
|
|
|
|
// Check if the result actually contains the access level
|
|
if (is_array($result) && count($result) > 0 && array_key_exists('level', $result[0])) {
|
|
return $result[0]['level'];
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the access level and work location for the provided profile and project id
|
|
*
|
|
* @param int $accessLevel The new access level
|
|
* @param int $workLocation The new work location
|
|
* @param int $profileId The id of the profile to update the access level for
|
|
* @param int $projectId The id of the project to update the access level for
|
|
* @return int 1 if the update succeeded or 0 if it did not
|
|
*/
|
|
function updateCheckInOutAccessLevelAndWorkLocation($accessLevel, $workLocation, $profileId, $projectId)
|
|
{
|
|
// Start transaction
|
|
db_start_transaction();
|
|
|
|
// Update the access level of the signed in profile
|
|
$query = "UPDATE project_incheck_uitcheck SET level = $accessLevel, work_location='$workLocation'
|
|
WHERE gebruiker = $profileId AND project = $projectId
|
|
ORDER BY t DESC LIMIT 1";
|
|
|
|
$success = db_store_data($query);
|
|
|
|
if ($success) {
|
|
// Only need to generate a sign-in code for profiles with LEVEL 2 access
|
|
if ($accessLevel == 9) {
|
|
// Generate sign-in code
|
|
$success = generateAndStoreSignInCode($profileId, $projectId, 'gebruiker_profiel_inlogcode', 'gebruiker_profiel');
|
|
}
|
|
|
|
// Finish transaction
|
|
$transactionFinished = db_commit_transaction();
|
|
|
|
if ($success) {
|
|
// Only set the commit transaction result as the return value when the queries have succeeded
|
|
$success = $transactionFinished;
|
|
}
|
|
}
|
|
|
|
return $success;
|
|
}
|
|
|
|
/**
|
|
* Get check in/out information
|
|
*
|
|
* Inputs:
|
|
* - user_level: user level
|
|
* - user_project: current user project
|
|
* - current_user_level: current user level
|
|
*
|
|
* Return: result
|
|
*/
|
|
function get_higher_project_users($user_level, $user_project, $current_user_level)
|
|
{
|
|
|
|
if ($current_user_level == 8) {
|
|
$result = db_fetch_data("SELECT * FROM project_gebruiker WHERE project='" . $user_project . "' AND level < '" . $user_level . "' AND rol='normaal';");
|
|
}
|
|
|
|
if ($current_user_level == 9) {
|
|
$result = db_fetch_data("SELECT * FROM project_gebruiker WHERE project='" . $user_project . "' AND level <= '" . $user_level . "' AND rol='normaal';");
|
|
}
|
|
|
|
return ($result);
|
|
}
|
|
|
|
/**
|
|
* Delete entry from project_incheck_uitcheck
|
|
*
|
|
* Inputs:
|
|
* - project: project id
|
|
* - user: user id
|
|
* - t: time
|
|
*
|
|
* Return: result
|
|
*/
|
|
function delete_user_check_inout($project, $user, $t)
|
|
{
|
|
$result = db_store_data("DELETE FROM project_incheck_uitcheck WHERE project='" . $project . "' AND gebruiker='" . $user . "' AND t='" . $t . "';");
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Store signin/out signature on file system
|
|
*
|
|
* Inputs:
|
|
* - signature: signature
|
|
*
|
|
* Return: (FALSE/TRUE)
|
|
*/
|
|
function db_store_signin_signature($info)
|
|
{
|
|
global $_RELEASE;
|
|
|
|
// save off the identifier for the new document
|
|
$id = $info['project_id'] . "_" . $info['user_id'] . "_" . $info['actie'] . "_" . $info['t'];
|
|
|
|
$table = "project_incheck_uitcheck/handtekening";
|
|
|
|
$upload_dir = realpath($_RELEASE[((is_ReleaseCandidate()) ? 'rc' : 'release')]['upload'] . strtolower($table));
|
|
|
|
// upload directory should exist, so the error is never triggered in a release environment
|
|
if (!$upload_dir) {
|
|
trigger_error("Cannot save document: path \"" . $upload_dir . "\" is invalid, expanded from \"" . $_RELEASE[((is_ReleaseCandidate()) ? 'rc' : 'release')]['upload'] . strtolower($table) . "\"", E_USER_ERROR);
|
|
return FALSE;
|
|
}
|
|
|
|
// Open/create handle
|
|
$handle = fopen($upload_dir . "/" . $id, "w");
|
|
|
|
// Write document data to file
|
|
fwrite($handle, specialchars($info['signature']));
|
|
|
|
// Close handle
|
|
fclose($handle);
|
|
return TRUE;
|
|
}
|
|
|
|
/**
|
|
* Removes the sign-in codes linked to the provided project id and profile id combination
|
|
* @param int $projectId The project id for which to remove the sign-in code
|
|
* @param int $profileId The profile id for which to remove the sign-in code
|
|
* @return int 1 (OK)/ 0(Error)
|
|
*/
|
|
function db_delete_user_profile_login_code($projectId, $profileId)
|
|
{
|
|
return db_store_data("DELETE FROM gebruiker_profiel_inlogcode WHERE project='$projectId' AND gebruiker_profiel='$profileId'");
|
|
}
|