3
0
Fork 0
mirror of https://github.com/fsr/eseeva synced 2025-04-28 09:28:31 +02:00

upgrades to sqlite, first step towards #2

This commit is contained in:
Lucas Woltmann 2017-01-25 18:24:31 +01:00
parent 134775b2d7
commit fe8a426334
9 changed files with 515 additions and 495 deletions

302
libs/dbLib.php Normal file
View file

@ -0,0 +1,302 @@
<?php
//============================================================================
// Name : dbLib.php
// Author : Lucas Woltmann
// Version : 1.0
// Date : 01-2017
// Description : Provides functions for managing the answers of the evaluation
// for students and tutors.
//============================================================================
define ("LOGDB", "db/eseeva.db");
/**
* Reads the database with the specified name and writes all log data to the corresponding arrays.
* Returns true if the log file has been successfully read, otherwise false.
* Is guaranteed to initialize the specified log arrays, even when the database file could not be
* read in which case they will simply be empty.
*
* @param string $fileName The database from which to read.
* @param int $student Whether the user is a student(1) or a tutor(0).
* @param array $questionData Will be created by this function and is passed by reference.
* Is an array of arrays that consists of the unique id, the question
* and the fields for the possible response options which contain
* the number of times that option has been selected.
* @param array $tutorData Will be created by this function and is passed by reference.
* Is an array of arrays that consists of the tutor name and the
* fields for the possible response options which contain the number
* of times that option has been selected.
* @param array $commentData Will be created by this function and is passed by reference.
* Is an array of string that resemble the different comments that
* have been made.
* @return boolean
*/
function ReadLogDatabase($fileName, $student, &$questionData, &$tutorData, &$commentData)
{
// init arrays regardless of the file not being found or eventual errors
$questionData = array();
$tutorData = array();
$commentData = array();
$questions = array();
$tutors = array();
$comments = array();
// return if the file does not exist
if (file_exists($fileName) == false)
return false;
// open the db
$handle = new SQLite3($fileName, SQLITE3_OPEN_READONLY);
if (!$handle)
return false;
$stmt = $handle->prepare("SELECT Answer FROM answers WHERE student=:student;");
if ($stmt)
{
$stmt->bindValue(':student', $student, SQLITE3_INTEGER);
$result = $stmt->execute();
//read to ararys, commentData does not need any further attention
PrepareAnswer($result, $questions, $tutors, $commentData);
}
else
{
$handle->close();
return false;
}
$handle->close();
//convert data into legacy data structure
ReadLogQuestionData($questions, $questionData);
ReadLogTutorData($tutors, $tutorData);
return true;
}
/**
* Writes the specified log arrays to a database with the specified name.
* Expects data in the same format as in the legacy version, but combines them to a single array
* which is going to be saved in the database.
* Returns true if the file was written successfully, otherwise false.
*
* @param string $fileName The name of the database to which the log data should be written.
* @param int $student Whether the user is a student(1) or a tutor(0).
* @param array $questionData The question data that should be written to the log file. Passed by reference.
* @param array $tutorData The tutor data that should be written to the log file. Passed by reference.
* @param array $commentData The list of comments that should be written to the log file. Passed by reference.
* @param string $key The key the user used.
* @return boolean
*/
function WriteLogDatabase($fileName, $student, &$questionData, &$tutorData, &$commentData, $key)
{
$answer = array($questionData, $tutorData, $commentData);
$handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE);
if (!$handle)
return false;
$stmt = $handle->prepare("UPDATE answers SET Status='activated', Student=:student, Answer=:answer WHERE KeyId=:keyid;");
if ($stmt)
{
$stmt->bindValue(':student', $student, SQLITE3_INTEGER);
$stmt->bindValue(':answer', serialize($answer), SQLITE3_BLOB);
$stmt->bindValue(':keyid', $key, SQLITE3_TEXT);
$result = $stmt->execute();
}
else
{
$handle->close();
return false;
}
$handle->close();
return true;
}
/**
* Legacy data management for the question data.
* Generates an array of arrays with all the questions.
* The first index of each subarray is the full title and the remaining indices are the votes (answers).
*
* @param array $formData $_POST reference. Passed by reference.
* @param array $questionData The array data that should be written to. Passed by reference.
* @param array $questionnaire The general structure (questions) of the questionnaire. Passed by reference.
*/
function AddQuestionData(&$formData, &$questionData, &$questionnaire)
{
foreach($formData as $id => $value)
{
if (!isset($questionnaire[$id]))
continue;
// get the type of the element with the same id as the form element from the questionnaire
$type = $questionnaire[$id][0];
// check if the element is a question on continue with the next one if that is not the case
if ($type != "Question")
continue;
// if there is not field for the current element in the question dsta array, create a new
// blank field containing the question and zeros for the number of times each answer was picked
if (array_key_exists($id, $questionData) == false)
$questionData[$id] = array($questionnaire[$id][1], 0, 0, 0, 0, 0, 0);
// increment the answer that was selected in the formular by one
$questionData[$id][(int)$value]++;
}
}
/**
* Legacy data management for the tutor data.
* Generates an array of arrays with the tutor.
* The first index of each subarray is the full name and the remaining indices are the votes (answers).
*
* @param array $formData $_POST reference. Passed by reference.
* @param array $tutorData The array data that should be written to. Passed by reference.
*/
function AddTutorData(&$formData, &$tutorData)
{
// get the name of the tutor from the form
$tutorName = $formData["tutorName"];
// get the selected answer of the tutorRating from the form
$tutorValue = $formData["tutorRating"];
// if there is no field for the current tutor in the tutor array, create a new
// nlank one with zeros for the number of times each answer was picked
if (array_key_exists($tutorName, $tutorData) == false)
$tutorData[$tutorName] = array(0, 0, 0, 0, 0, 0);
// increment the answer that was selected in the form by one
$tutorData[$tutorName][$tutorValue - 1]++;
}
/**
* Legacy data management for the comment data.
* Checks if a comment was left and writes it into the passed reference.
*
* @param array $formData $_POST reference. Passed by reference.
* @param string $commentData The array data that should be written to. Passed by reference.
*/
function AddCommentData(&$formData, &$commentData)
{
// if the comment field was filled, the comment
// array is appended by the new comment
if (trim($formData["comment"]) != "")
{
$commentData = $formData["comment"];
}
}
/**
* Reads all question data out of a provided log file array from the database.
* This function should NOT BE CALLED directly, instead the ReadLogDatabase function should be
* used to read a log file as a whole.
* All data is written to the specified question data array and
* True is returned if a valid question data block was found, otherwise false.
*
* @param array $questions An array from the database. Passed by reference.
* @param array $questionData An array to which all question data is written to. Passed by reference.
* @return boolean.
*/
function ReadLogQuestionData(&$questions, &$questionData)
{
foreach ((array)$questions as $user) {
foreach ($user as $id => $values) {
if (isset($questionData[$id]))
{
$questionData[$id] = SumArrays($values, $questionData[$id]);
}
else
{
$questionData[$id] = $values;
}
}
}
return true;
}
/**
* Reads all tutor data out of a provided log file array from the database.
* This function should NOT BE CALLED directly, instead the ReadLogDatabase function should be
* used to read a log file as a whole.
* All data is written to the specified question data array and
* True is returned if a valid tutor data block was found, otherwise false.
*
* @param array $tutors An array from the database. Passed by reference.
* @param array $tutorData An array to which all question data is written to. Passed by reference.
* @return boolean.
*/
function ReadLogTutorData(&$tutors, &$tutorData)
{
foreach ((array)$tutors as $user) {
foreach ($user as $id => $values) {
if (isset($tutorData[$id]))
{
$tutorData[$id] = SumArrays($values, $tutorData[$id]);
}
else
{
$tutorData[$id] = $values;
}
}
}
return true;
}
/**
* Reads all comment data out of a provided log file array from the database.
* This function should NOT BE CALLED directly, instead the ReadLogDatabase function should be
* used to read a log file as a whole.
* All data is written to the specified question data array and
* True is returned if a valid comment data block was found, otherwise false.
*
* @param array $comments An array from the database. Passed by reference.
* @param array $commentData An array to which all question data is written to. Passed by reference.
* @return boolean.
*/
function ReadLogCommentData(&$comments, &$commentData)
{
foreach ((array)$comments as $user) {
foreach ($user as $id => $values) {
if (isset($commentData[$id]))
{
$commentData[$id] = SumArrays($values, $commentData[$id]);
}
else
{
$commentData[$id] = $values;
}
}
}
return true;
}
/**
* Splits the three arrays from the 'Answer' column in the database into the legacy ararys.
*
* @param array $resultSet An array from the database. Passed by reference.
* @param array $questionsa An array to which all question data is written to. Passed by reference.
* @param array $tutors An array to which all tutor data is written to. Passed by reference.
* @param array $comments An array to which all comments data is written to. Passed by reference.
*/
function PrepareAnswer(&$resultSet, &$questions, &$tutors, &$comments)
{
$i = 0;
while($res = $resultSet->fetchArray(SQLITE3_ASSOC))
{
$res = unserialize($res["Answer"]);
$questions[$i] = $res[0];
$tutors[$i] = $res[1];
$comments[$i] = $res[2];
$i++;
}
}
/**
* Little helper function for adding up single user answers to a global statistic.
* Could be compared with a group by.
*
* @param array $array1 First array to merge/sum.
* @param array $array2 Second array to merge/sum.
* @return array Summed array by index.
*/
function SumArrays(&$array1, &$array2)
{
$result = array();
//the name
$result[0] = $array2[0];
$count = count($array2);
//start one index into counting, because index 0 is the name
for ($i=1; $i < $count; $i++) {
$result[$i] = $array1[$i] + $array2[$i];
}
return $result;
}
?>

View file

@ -1,15 +1,15 @@
<?php
//============================================================================
// Name : keyLib.php
// Author : Patrick Reipschläger
// Version : 1.0
// Date : 08-2013
// Author : Patrick Reipschläger, Lucas Woltmann
// Version : 2.0
// Date : 01-2017
// Description : Provides several functions for creating and handling keys
// for the ESE evaluation for students and tutors.
//============================================================================
// Constant for the file which contains the keys, should be used by all other scripts so it can easily be changed
define ("KEYFILE", "keys/Keys.csv");
define ("KEYFILE", "db/eseeva.db");
// Constants for the different key states that are possible, should always be used when altering or checking the state of a key
define ("KEYSTATE_NONEXISTENT", "nonexistent");
define ("KEYSTATE_UNISSUED", "unissued");
@ -48,133 +48,176 @@
return array_unique($keys);
}
/**
* Generates a new key file with the specified name that contains the specified
* amount of newly generated keys.
* Generates a new keys in the database.
* Returns true if the key file was created successfully, otherwise false.
*
* @param integer $keyAmount The amount of keys that should be generated.
* @param string $fileName The name of the key file that should be created.
* @param string $fileName The name of the key database that should be created.
*/
function CreateKeyFile($keyAmount, $fileName)
function CreateKeys($keyAmount, $fileName)
{
$handle = fopen($fileName, 'w');
$handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE);
if (!$handle)
return false;
$keys = GenerateKeys($keyAmount);
$data = "Nr;Key;Status\n";
$count = count($keys) - 1;
$count = count($keys);
for ($i = 0; $i < $count; $i++)
$data = $data . $i . ";" . $keys[$i] . ";" . KEYSTATE_UNISSUED. "\n";
$data = $data . $count . ";" . $keys[$count] . ";". KEYSTATE_UNISSUED;
fwrite($handle, $data);
fclose($handle);
{
$stmt = $handle->prepare("INSERT INTO answers (KeyId, Status) VALUES (:key,:status);");
if ($stmt)
{
$stmt->bindValue(':key', $keys[$i], SQLITE3_TEXT);
$stmt->bindValue(':status', KEYSTATE_UNISSUED, SQLITE3_TEXT);
$result = $stmt->execute();
}
else
{
$handle->close();
return false;
}
}
$handle->close();
return true;
}
/**
* Opens the file with the specified name and reads all key data that the file contains.
* Opens the database with the specified name and reads all key data that the database contains.
* The resulting data type will be an array of arrays consisting of the key and its state.
* Returns null if the key file could not be found or read.
*
* @param string $fileName The file which should be read
* @param string $fileName The database which should be read.
* @return array
*/
function ReadKeyFile($fileName)
function ReadKeys($fileName)
{
if (!file_exists($fileName))
return null;
$handle = fopen($fileName, 'r');
$handle = new SQLite3($fileName, SQLITE3_OPEN_READONLY);
if (!$handle)
return null;
$data = fread($handle, filesize($fileName));
$lines = explode("\n", $data);
fclose($handle);
$keyData = array();
for ($i = 1; $i < count($lines); $i++)
{
$tmp = explode(";", $lines[$i]);
array_push($keyData, array($tmp[1], $tmp[2]));
}
return $keyData;
}
/**
* Writes the specified key data to the file with the specified name.
* The data type of the $keyData should be an array of arrays consisting of the key and its state.
* Returns true if the key file was successfully written, otherwise false.
*
* @param string $fileName The name of the file to which the key data should be written.
* @param array $keyData The key data which should be written to the file. Passed by reference.
* @return boolean
*/
function WriteKeyFile($fileName, &$keyData)
{
if (!isset($fileName) || !isset($keyData))
return false;
$handle = fopen($fileName, 'c');
if ($handle == null)
return false;
// debug code
//echo "<script type='text/javascript'>alert('file opened');</script>";
$data = "Nr;Key;Status\n";
$count = count($keyData) - 1;
for ($i = 0; $i < $count; $i++)
$data = $data . $i . ";" . $keyData[$i][0] . ";" . $keyData[$i][1] . "\n";
$data = $data . $count . ";" . $keyData[$count][0] . ";" . $keyData[$count][1];
$data = $handle->query("SELECT KeyId, Status FROM answers;");
$data = PrepareResult($data);
$handle->close();
//use exclusive lock for writing
flock($handle, LOCK_EX);
ftruncate($handle, 0);
$res = fwrite($handle, $data);
fflush($handle);
flock($handle, LOCK_UN);
// debug Code
//if ($res == false)
// echo "<script type='text/javascript'>alert('file not written');</script>";
//else
// echo "<script type='text/javascript'>alert('file written');</script>";
fclose($handle);
if ($res)
if ($data)
{
return true;
}
else
{
return false;
return $data;
}
return array();
}
/**
* Get the current state of the specified key which will be one of the defines
* Get the current state of the specified key from the database, which will be one of the defines
* KEYSTATE constants.
* The data type of the key data should be an array of arrays consisting of the key and its state.
*
* @param array $keyData The key data array in which the key should be found. Passed by reference.
* @param array $fileName The database with the keys.
* @param string $key The key which state should be got. Passed by reference.
* @return integer
*/
function GetKeyState(&$keyData, &$key)
function GetKeyState($fileName, &$key)
{
for ($i = 0; $i < count($keyData); $i++)
if ($key == $keyData[$i][0])
return $keyData[$i][1];
if (!file_exists($fileName))
return null;
$handle = new SQLite3($fileName, SQLITE3_OPEN_READONLY);
if (!$handle)
return null;
$stmt = $handle->prepare("SELECT Status FROM answers WHERE KeyId=:keyid;");
if ($stmt)
{
$stmt->bindValue(':keyid', $key, SQLITE3_TEXT);
$result = $stmt->execute();
$result = $result->fetchArray(SQLITE3_ASSOC);
$handle->close();
return $result["Status"];
}
$handle->close();
return KEYSTATE_NONEXISTENT;
}
/**
* Set the state of the specified key to the specified state.
* The data type of the $keyData should be an array of arrays consisting of the key and its state.
* Returns true if the key was found within the key data and the state has been changed, otherwise false.
*
* @param array $keyData The key data array in which the key should be found. Passed by reference.
* @param array $fileName The database in which the key should be found.
* @param string $key The key which state should be changed.
* @param integer $newState The new state of the specified key. Must be on of the KEYSTATE constants.
* @return boolean
*/
function SetKeyState(&$keyData, &$key, $newState)
function SetKeyState($fileName, &$key, $newState)
{
if (!file_exists($fileName))
return false;
$handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE);
if (!$handle)
return false;
//secure override by checking if key has been uesed already
$stmt = $handle->prepare("UPDATE answers SET Status=:status WHERE KeyId=:keyid AND Status!=:status;");
if ($stmt)
{
$stmt->bindValue(':keyid', $key, SQLITE3_TEXT);
$stmt->bindValue(':status', $newState, SQLITE3_TEXT);
$result = $stmt->execute();
$handle->close();
return true;
}
$handle->close();
return false;
}
/**
* Deletes a single key in the database if it has not been used yet.
* Returns true if the key was deleted, otherwise false.
*
* @param array $fileName The database in which the key should be found.
* @param string $key The key which should be deleted.
* @return boolean
*/
function DeleteKey($fileName, &$key)
{
for ($i = 0; $i < count($keyData); $i++)
if ($key == $keyData[$i][0])
{
$keyData[$i][1] = $newState;
return true;
if (!file_exists($fileName))
return false;
$handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE);
if (!$handle)
return false;
//secure deleting by checking if key has been uesed already
$stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid AND Answer!=NULL;");
if ($stmt)
{
$stmt->bindValue(':keyid', $key, SQLITE3_TEXT);
$result = $stmt->execute();
$handle->close();
return true;
}
$handle->close();
return false;
}
/**
* Prepares the results from the database to an array of keys with their state.
*
* @param array $resultSet Cursor from the sqlite database. Passed by reference.
* @return array
*/
function PrepareResult(&$resultSet)
{
$result = array();
$i = 0;
while($res = $resultSet->fetchArray(SQLITE3_ASSOC))
{
foreach ($res as $key => $value) {
$result[$i][$key] = $value;
}
return false;
$i++;
}
return $result;
}
?>

View file

@ -1,310 +0,0 @@
<?php
//============================================================================
// Name : loggingLib.php
// Author : Patrick Reipschläger
// Version : 1.0
// Date : 08-2013
// Description : Provides functions for creating, reading and appending
// log files and data for the ESE evaluation for students
// and tutors.
//============================================================================
define ("STUDENTLOGFILE", "logs/ESE_studentLog.txt");
define ("TUTORLOGFILE", "logs/ESE_tutorLog.txt");
/**
* Reads all question data out of a provided log file array that has been split by new lines.
* This function should NOT BE CALLED directly, instead the ReadLogFile function should be
* used to read a log file as a whole.
* All data is written to the specified question data array and
* True is returned if a valid question data block was found, otherwise false.
*
* @param array $lines An array of string resembling the different lines of the log file. Passed by reference.
* @param integer $index The index within the lines array by which the processing should start.
* The index is passed by reference and will be incremented by this function.
* @param array $questionData An array to which all question data is written to.
* @return boolean.
*/
function ReadLogQuestionData(&$lines, &$index, &$questionData)
{
// check if the current line is a question block
// and return false if that is not the case
if (trim($lines[$index]) != "###QuestionData###")
return false;
// the index must be incremented so the next loop starts at the right line
$index++;
while($index < count($lines))
{
// if the character '#' indicates that the question data block has been finished
// so the loop is completed
if ($lines[$index][0] == "#")
break;
// split the current line by the character ';' to get the individual data elements
$tmp = explode(";", $lines[$index]);
// set identifier is the first data element and is used to init the field of the
// current question in the questionData array
$questionData[$tmp[0]] = array();
// push all other data elements in the previously created array
for ($i = 1; $i < count($tmp); $i++)
array_push($questionData[$tmp[0]], $tmp[$i]);
// increment the index to continue with the next line of the file
$index++;
}
return true;
}
/**
* Reads all tutor data out of a provided log file array that has been split by new lines.
* This function should NOT BE CALLED directly, instead the ReadLogFile function should be
* used to read a log file as a whole.
* All data is written to the specified tutor data array.
* True is returned if a valid tutor data block was found, otherwise false.
*
* @param array $lines An array of string resembling the different lines of the log file. Passed by reference.
* @param integer $index The index within the lines array by which the processing should start.
* The index is passed by reference and will be incremented by this function.
* @param array $tutorData An array to which all tutor data is written to.
* @return boolean.
*/
function ReadLogTutorData(&$lines, &$index, &$tutorData)
{
// check if the current line is a tutor data block
// and return false if that is not the case
if (trim($lines[$index]) != "###TutorData###")
return false;
// the index must be incremented so the next loop starts at the right line
$index++;
// parse the tutor data - its basically the same as the question data
while($index < count($lines))
{
// if the character '#' indicates that the tutor data block has been finished
// so the loop is completed
if ($lines[$index][0] == "#")
break;
// split the current line by the character ';' to get the individual data elements
$tmp = explode(";", $lines[$index]);
// set identifier is the first data element and is used to init the field of the
// current tutor in the tutorData array
$tutorData[$tmp[0]] = array();
// push all other data elements in the previously created array
for ($i = 1; $i < count($tmp); $i++)
array_push($tutorData[$tmp[0]], $tmp[$i]);
// increment the index to continue with the next line of the file
$index++;
}
return true;
}
/**
* Reads all comment data out of a provided log file array that has been split by new lines.
* This function should NOT BE CALLED directly, instead the ReadLogFile function should be
* used to read a log file as a whole.
* All data is written to the specified comment data array.
* True is returned if a valid comment data block was found, otherwise false.
*
* @param array $lines An array of string resembling the different lines of the log file. Passed by reference.
* @param integer $index The index within the lines array by which the processing should start.
* The index is passed by reference and will be incremented by this function.
* @param array $commentData An array to which all comment data is written to.
* @return boolean.
*/
function ReadLogCommentData(&$lines, &$index, &$commentData)
{
// check if the current line is a comment data block
// and return false if that is not the case
if (trim($lines[$index]) != "###CommentData###")
return false;
// the index must be incremented so the next loop starts at the right line
$index++;
// the current comment that gets pushed to the commentData array
// when a comment end sequence has been found
$comment = "";
// parse the comment data
while($index < count($lines))
{
// if the line start with the character '~' the line is
// interpreted as the comment end sequence and the current
// comment is pushed at the end of the comment array
// and the loop continues with a new comment
if (trim($lines[$index]) != "" && $lines[$index][0] == "~")
{
// the last character of the comment is erased because it's
// just an unnecessary new line
array_push($commentData, substr($comment, 0, -1));
$comment = "";
}
// otherwise the current comment is simply appended by the
// current line
else
$comment = $comment . $lines[$index] . "\n";
// increment the index to continue with the next line of the file
$index++;
}
return true;
}
/**
* Reads the log file with the specified name and writes all log data to the corresponding arrays.
* Returns true if the log file has been successfully written, otherwise false.
* Is guaranteed to initialize the specified log arrays, even when the log file could not be
* read in which case they will simply be empty.
*
* @param array $questionData Will be created by this function and is passed by reference.
* Is an array of arrays that consists of the unique id, the question
* and the fields for the possible response options which contain
* the number of times that option has been selected.
* @param array $tutorData Will be created by this function and is passed by reference.
* Is an array of arrays that consists of the tutor name and the
* fields for the possible response options which contain the number
* of times that option has been selected.
* @param array $commentData Will be created by this function and is passed by reference.
* Is an array of string that resemble the different comments that
* have been made.
*/
function ReadLogFile($fileName, &$questionData, &$tutorData, &$commentData)
{
// init arrays regardless of the file not being found or eventual errors
$questionData = array();
$tutorData = array();
$commentData = array();
// return if the file does not exist
if (file_exists($fileName) == false)
return false;
// open the file and get its length
$handle = fopen($fileName, 'r');
if (!$handle)
return false;
$length = filesize($fileName);
// if the length is zero, nothing can be read, so return
if ($length == 0)
return false;
// otherwise read the file and split the string at each new line
$fileData = fread($handle, $length);
$lines = explode("\n", $fileData);
fclose($handle);
// begin parsing of the file, the beginning is the second line, because the
// first should contains the disclaimer
$index = 1;
ReadLogQuestionData($lines, $index, $questionData);
ReadLogTutorData($lines, $index, $tutorData);
ReadLogCommentData($lines, $index, $commentData);
return true;
}
/**
* Writes the specified log arrays to a log file with the specified name.
* Expects data in the same format as provided by the ReadLogFile function.
* Returns true if the file was written successfully, otherwise false.
*
* @param string $fileName The name of the file to which the log data should be written.
* @param array $questionData The question data that should be written to the log file.
* @param array $tutorData The tutor data that should be written to the log file.
* @param array $commentData The list of comments that should be written to the log file. Passed by reference.
* @return boolean
*/
function WriteLogFile($fileName, &$questionData, &$tutorData, &$commentData)
{
$handle = fopen($fileName, 'c');
if (!$handle)
return false;
// write disclaimer and question data block identifier
$fileData = "# Automatically generated file - Do not Change ! #\n###QuestionData###\n";
// write all question data to the file
foreach($questionData as $id => $entry)
{
// the order is id, question and the number of times each of the six answers was picked
$fileData = $fileData . $id . ";";
for ($i = 0; $i < 6; $i++)
$fileData = $fileData . $entry[$i] . ";";
$fileData = $fileData . $entry[6] . "\n";
}
// write tutor data block identifier
$fileData = $fileData . "###TutorData###\n";
// write all tutor data to the file
foreach($tutorData as $id => $entry)
{
// the order is tutor name and the number of times each of the six answers was picked
$fileData = $fileData . $id . ";";
for ($i = 0; $i < 5; $i++)
$fileData = $fileData . $entry[$i] . ";";
$fileData = $fileData . $entry[5] . "\n";
}
// write comment data block identifier
$fileData = $fileData . "###CommentData###";
// write comment data
foreach($commentData as $comment)
// each comment is escapd by a new line containing three tilde characters
$fileData = $fileData . "\n" . $comment . "\n~~~";
// write the generated data to the file and close it
// use exclusive lock
flock($handle, LOCK_EX);
ftruncate($handle, 0);
fwrite($handle, $fileData);
fflush($handle);
flock($handle, LOCK_UN);
fclose($handle);
return true;
}
/**
* Add the question data from the specified questionnaire form data to an existing form data array.
*
* @param array $formData The list of all form elements which for most cases will simply be the
* $_POST array that has been submitted by the questionnaire form.
* Passed by reference.
* @param array $commentData The list of existing question data that will be appended by the question
* Data of the form. Passed by reference.
*/
function AddQuestionData(&$formData, &$questionData, &$questionnaire)
{
foreach($formData as $id => $value)
{
if (!isset($questionnaire[$id]))
continue;
// get the type of the element with the same id as the form element from the questionnaire
$type = $questionnaire[$id][0];
// check if the element is a question on continue with the next one if that is not the case
if ($type != "Question")
continue;
// if there is not field for the current element in the question dsta array, create a new
// blank field containing the question and zeros for the number of times each answer was picked
if (array_key_exists($id, $questionData) == false)
$questionData[$id] = array($questionnaire[$id][1], 0, 0, 0, 0, 0, 0);
// increment the answer that was selected in the formular by one
$questionData[$id][(int)$value]++;
}
}
/**
* Add the tutor data from the specified questionnaire form data to an existing tutor data array.
*
* @param array $formData The list of all form elements which for most cases will simply be the
* $_POST array that has been submitted by the questionnaire form.
* Passed by reference.
* @param array $commentData The list of existing tutor data that will be appended by the tutor
* Data of the form. Passed by reference.
*/
function AddTutorData(&$formData, &$tutorData)
{
// get the name of the tutor from the form
$tutorName = $formData["tutorName"];
// get the selected answer of the tutorRating from the form
$tutorValue = $formData["tutorRating"];
// if there is no field for the current tutor in the tutor array, create a new
// nlank one with zeros for the number of times each answer was picked
if (array_key_exists($tutorName, $tutorData) == false)
$tutorData[$tutorName] = array(0, 0, 0, 0, 0, 0);
// increment the answer that was selected in the form by one
$tutorData[$tutorName][$tutorValue - 1]++;
}
/**
* Add the comment data from the specified questionnaire form data to an existing comment data array.
*
* @param array $formData The list of all form elements which for most cases will simply be the
* $_POST array that has been submitted by the questionnaire form.
* Passed by reference.
* @param array $commentData The list of existing comments that will be appended by the comment
* Data of the form. Passed by reference.
*/
function AddCommentData(&$formData, &$commentData)
{
// if the comment field was filled, the comment
// array is appended by the new comment
if (array_key_exists("comment", $formData) && trim($formData["comment"]) != "")
array_push($commentData, $formData["comment"]);
}
?>