\documentclass{article} \title{Database Connection for the CPS Facilitator Tool} \author{Jos Kraaijeveld} \date{\today} \usepackage{moreverb} \usepackage{fullpage} \usepackage{framed} \usepackage{float} \usepackage{multirow} \usepackage[final]{pdfpages} \usepackage{hyperref} \begin{document} \maketitle \pagebreak \tableofcontents \pagebreak \section{Changelog} \textit{13-01-2012}: Fixed the issue with Open-Closed, updated documentation accordingly. \section{Introduction} This document describes the database design and implementation for the CPS Facilitator Tool. The goal is to make sure everyone can communicate with the RDF-based database without having to write queries. The DBInterface is written in PHP and requires slight PHP knowledge before use. It is heavily based on initial work by Bas van Nuland. \\ If you use this framework, you can skip to the `How to use'-part of this document (\ref{howtouse}). If you are planning to improve this framework, I recommend reading the RDF Primer\footnote{http://www.w3.org/TR/rdf-primer/} and SPARQL Query Language for RDF\footnote{http://www.w3.org/TR/rdf-sparql-query/}. A full specification of the current implementation can be found in section \ref{spec}. The areas I suggest improving upon first are described in section \ref{futurework}. \section{How to use}\label{howtouse} \subsection{Initialization} Before the database can be used, you should remember to import master.php on every page: \begin{verbatimtab} require 'classes/master.php'; \end{verbatimtab} \subsection{Basic queries} \subsubsection{Retrieving data} Getting data is done by calling the get method of the class for which you want to retrieve data. The specification for this method is the same for every class and is as follows.\\ \\ \begin{tabular}{| l || r |} \hline \multicolumn{2}{|c|}{Function get()} \\ \hline Second argument: \$arguments & Array \\ \hline Return type: & Array/Variable \\ \hline \end{tabular} \\ \\ The return type is an Array of objects with the type corresponding to the class you caled it on. For instance, if you call User::get() you will only get User objects. Below are a few examples showing the usage of get(). A description of what arguments are possible per type is given in section \ref{arguments}. \begin{figure}[H] \caption{Retrieving all questions and echo their title.} \begin{framed} \begin{verbatimtab} $questions = User::get(array()); foreach ($questions as $question) { echo $question->title; } \end{verbatimtab} \end{framed} \end{figure} \begin{figure}[H] \caption{Retrieving all surveys containing questions with IDs q1 and q2, created by the user Jos, printing all questions in those surveys} \begin{framed} \begin{verbatimtab} //To get all surveys created by the user Jos, we need his UID. //We first query the database for the User object belonging to Jos. $userResults = User::get(array("name" => "Jos")); //Assuming there is a result, the UID is: $josUID = $userResults[0]->uid; //Now to get the requested surveys: $surveys = Survey::get(array("questions" => array("q1", "q2"), "creator" => $josUID)); //And to print all the questions in these surveys: foreach($surveys as $survey) { echo "All the questions in " . $survey->name; foreach($survey->questions as $question) { print_r($question); } } \end{verbatimtab} \end{framed} \end{figure} \subsubsection{Storing data} Storing data is easy, as long as you stick to using the given PHP Classes. Retrieve these objects by using the get() function, and call the save() function of the objects.: \\ \\ \begin{tabular}{| l || r |} \hline \multicolumn{2}{|c|}{Function save()} \\ \hline Returns: \$boolean & succeeded \\ \hline \end{tabular} \\ \\ If the save function returns false, there is an invalid references in that object which needs to be resolved before saving. This ensures the database is always valid. Another important thing to note is that currently it will overwrite a previous object with the same UID in the database. The following examples show how to create new objects and save them, as well as edit old objects and save them. \begin{figure}[H] \caption{Creating a new Answer object and storing this in the database.} \begin{framed} \begin{verbatimtab} //For this example, I choose a random question to answer $questions = Question::get(array()); $question = $questions[2]; //Note two things: //1 - If you pass 'null' as first argument when creating any object // A new UID will be generated, indicating a new object. //2 - Depending on the question, there can be multiple answers // This means the values-argument (third argument) is an array. $answer = new Answer(null, $question, array("12345", "four")); //Save the answer in the database if($answer->save()) echo "Success"; \end{verbatimtab} \end{framed} \end{figure} \begin{figure}[H] \caption{Getting a Survey object from the database and removing the first question. Also alter this question.} \begin{framed} \begin{verbatimtab} //Get the survey $surveyResults = Survey::get(array("uid" => "b91d39e8667372e220bb861b3f94b5bd")); $survey = surveyResults[0]; //Remove the question $question = $survey->questions[0]; unset($survey->questions[0]); //Change the question $question->title = "New Title"; //Save the survey and question $question->save(); $survey->save(); \end{verbatimtab} \end{framed} \end{figure} \subsection{Arguments}\label{arguments} The arguments you can supply when calling the get() function differ greatly per type. Unfortunately, there is no way around this, so here is a complete overview of arguments per type. \\ \begin{tabular}{| l | c | c | l |} \hline \textbf{Type} & \textbf{Argument name} & \textbf{Argument type} & \textbf{Extra notes} \\ \hline \hline \multirow{3}{*}{Answer} & uid & String & \\ & question & String & UID of the question \\ & values & Array of Strings & \\ \hline \hline \multirow{4}{*}{AnswerSet} & uid & String & \\ & survey & String & UID of the Survey \\ & respondent & String & UID of the Respondent \\ & answers & Array of Strings & UIDs of the Answers \\ \hline \hline \multirow{4}{*}{Application} & uid & String & \\ & title & String & \\ & description & String & \\ & style & String & \\ \hline \hline \multirow{5}{*}{Question} & code & String & \\ & title & String & \\ & type & String & \\ & description & String & \\ & category & String & \\ & definedanswers & Array of Strings & String values of possible answers \\ \hline \hline \multirow{3}{*}{Respondent/User} & uid & String & \\ & name & String & \\ & password & String & Use hashes to store passwords \\ \hline \hline \multirow{7}{*}{Session} & uid & String & \\ & title & String & \\ & creator & String & UID of the User \\ & creationdate & String & Unix Timestamp. No way to search on intervals (yet). \\ & applications & Array of Strings & UIDs of the Applications \\ & surveys & Array of Strings & UIDs of the Surveys \\ & answersets & Array of Strings & UIDs of the AnswerSets \\ \hline \hline \multirow{9}{*}{SessionInstance} & uid & String & \\ & title & String & \\ & location & String & \\ & facilitator & String & UID of the user that facilitated the session \\ & startime & String & Unix Timestamp. No way to search on intervals (yet). \\ & endtime & String & Unix Timestamp. No way to search on intervals (yet). \\ & notes & Array of Strings & \\ & session & String & UID of the Session this Instance refers to. \\ & resultset & String & UID of the ResultSet this Instance has. \\ \hline \hline \multirow{4}{*}{Survey} & uid & String & \\ & title & String & \\ & description & String & \\ & creator & String & UID of the user that created this survey \\ & questions & Array of Strings & UIDs of the Questions \\ \hline \hline \end{tabular} \\ Whenever an Array has to be supplied, it will match for results that satisfy \textit{all} the constraints given in the array. \section{Specification}\label{spec} \subsection{Models} The framework heavily relies on the Object Oriented Programming paradigm, and only allows you to create, edit and store data through instances of precreated classes. All these classes inherit from a global ResearchToolObject class. A UML class diagram of the model classes can be found in Appendix \ref{models}. The most important thing to note is that references to other objects are not evaluated immediately. Rather, these classes with references have to override the evaluate() function, which in turn tries to query the database and resolve those UIDs to model objects. This gives us two advantages: initial queries do not scale exponentially because of the 'lazy' evaluation, and the boolean return value notifies us when there exists an incorrect reference. By evaluating before saving an object, we ensure that there cannot exist invalid values in the database. \subsection{Static functions, non-static functions} It is important to note that the get() function of each object is static, whilst the save() function is not. This is to provide a better saving mechanism in the future: tracking when an object gets changed and saving it automatically. The get() function is static because there is no reason for it to be linked to a specific Object, only to the type it describes as a whole. \subsection{Adding to the system} Adding support for new datatypes is easy and can be done by extending the ResearchToolObject class and ensuring the get() and save() methods are implemented correctly. Simply look at the existing classes and stick to the same idea unless you want to radically change the infrastructure. \subsection{PHPDoc} PHPDoc for the database classes can be found at \href{http://svn.tbm.tudelft.nl/TBM-CPS/RESEARCHTOOL/Doc/Backend/PHPDoc/index.html}{SVN}. \section{Future Work}\label{futurework} \subsection{Deal with invalid or missing values} This goes in two parts. Firstly, the database now assumes that given an entry, all the fields for its type are there and filled in. This is unwanted since it can cause issues with backwards compatability of certain .rdf files. Secondly, it assumes that fields containing a UID to a different entry actually point to a valid entry. For instance, a Survey evaluation will try to get a User object as its creator value from the database as well, assuming it exists. It will return false if this user object does not exist. \appendix \pagebreak \section{Class diagram of the models}\label{models} \begin{figure} \includepdf[scale=1.22]{DBModels.pdf} \end{figure} \end{document}