uid = $uid; $this->title = $title; $this->creator = $creator; $this->creationdate = $datetime; $this->pipeline = $pipeline; } /* * function evaluate(() * Evaluates all the references of this object. */ public function evaluate() { if(is_string($this->creator)) { $result = User::get(array("uid" => $this->creator)); if(!isset($result[0])) return false; $this->creator = $result[0]; } if(!empty($this->pipeline) && is_string($this->pipeline[0])) { $newPipeline = array(); foreach($this->pipeline as $element) { //Check if the element's UID can be found in surveys, if not: //Check applications. If it isn't in either: invalid reference. $result = Survey::get(array("uid" => $element)); if(!isset($result[0])) $result = Application::get(array("uid" => $element)); if(!isset($result[0])) return false; $newPipeline[] = $result[0]; } $this->pipeline = $newPipeline; } return true; } /** * function save() * Saves the current object into the database. */ public function save() { //If evaluation fails, some references are incorrect. //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed. //TODO: Decide how to fix invalid references graciously. if(!$this->evaluate()) return false; //Ensure the required folder exists. if(!is_dir('data/sessions/')) mkdir('data/sessions/'); $model = ResearchToolObject::load(Session::$filename); $resourceSession = new Resource(SESSION . '/' . $this->uid); //Remove the old value stored with the given id $model->subtract($model->find($resourceSession, null, null)); //Set the new values $resourceSessionType = new Resource(SESSION); $predicateType = new Resource(RTYPE); $model->add(new Statement($resourceSession, $predicateType, $resourceSessionType)); $sessionId = new Literal($this->uid); $predicateId = new Resource(UID); $model->add(new Statement($resourceSession, $predicateId, $sessionId)); $sessionTitle = new Literal($this->title); $predicateTitle = new Resource(TITLE); $model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle)); $sessionCreator = new Literal($this->creator->uid); $predicateCreator = new Resource(CREATOR); $model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator)); $sessionTimestamp = new Literal($this->creationdate->getTimestamp()); // $sessionTimestamp = new Literal($this->datetime); // Edit of above function, allows for creation of session, but still results in errors... $predicateTimestamp = new Resource(CREATIONDATE); $model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp)); //Create a sequence to store the different applications and surveys. if(isset($this->pipeline)) { foreach($this->pipeline as $element) { $sessionObject = new Literal($element->uid); $predicateObject = null; if(get_class($element) == "Application") $predicateObject = new Resource(HAS_APPLICATION); else if(get_class($element) == "Survey") $predicateObject = new Resource(HAS_SURVEY); if(isset($predicateObject)) $model->add(new Statement($resourceSession, $predicateObject, $sessionObject)); } } if(isset($this->answersets)) { foreach($this->answersets as $answerset) { $sessionAnswerSet = new Literal($answerset->uid); $predicateAnswerSet = new Resource(HAS_ANSWERSET); $model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet)); } } $model->saveAs(Session::$filename, 'rdf'); return true; } /** * function get() * @param type $arguments : An array containing one or more of the following elements: * 'uid', 'title', 'creationdate', 'applications', 'surveys' */ public static function get($arguments) { $model = ResearchToolObject::load(Session::$filename); //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?creator, ?creationdate WHERE { _session predicates:resource_type resources:session ; predicates:uid ?uid ; predicates:title ?title ; predicates:creator ?creator ; predicates:creationdate ?creationdate ; ' . ResearchToolObject::createArguments($arguments) . ' }'; //Query the model $results = $model->sparqlQuery($querystring); $sessions = array(); if(!empty($results)) { foreach($results as $result) { //Create a session object out of every result, get all required fields as well. $pipeline = Session::getPipeline($model, $result['?uid']->label); $creator = $result['?creator']->label; $creationdate = new DateTime(); $creationdate->setTimestamp(intval($result['?creationdate']->label)); $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $creationdate, $pipeline); } } return $sessions; } /** * function getPipeline() * param type $uid : The Session uid for which the pipeline should be retrieved. */ private static function getPipeline($model, $uid) { $result = $model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null); $iterator = $result->getStatementIterator(); $pipeline = array(); while($iterator->hasNext()) { $element = $iterator->next(); $pipeline[] = $element->getLabelObject(); } return $pipeline; } /** * function toSPSS * @param type $location * Writes an SPSS parseable file at the given location, named '$datetime_$title.txt' * One big note about toSPSS: If this session contains multiple instances of THE SAME Survey, * the world burns, people die, etcetera. (or, in a more fortunate case, the latest values * are written to the file whilst the others get lost. */ //DO NOT USE - BROKEN AS OF 23/12/2011 public function toSPSS($location) { $this->evaluate(); if(!is_dir($location)) { mkdir($location); } //Create the filename, open the file at the given location $fileName = $this->datetime->format("d-m-Y") . "_" . $this->title . ".txt"; //Make a list of all 'column headers', e.g. "respondent" and all the question codes. $headers = array(); $headers[] = "respondent"; foreach ($this->pipeline as $element) { if(get_class($element) == "Survey") { $element->evaluate(); foreach($element->questions as $question) { //TODO: This might need fixing after lazy initialization refactoring. $headers[] = $element->title . '_' . $question->uid; } } } //Return if there are no surveys. if(count($headers) < 2) return; //Get the answers for all respondents $answers = $this->getAnswers($headers); //Create the strings and write to file. $headerString = ""; foreach($headers as $headerElement) { $headerString = $headerString . $headerElement . "\t"; } $fileHandler = fopen($location . '/' . $fileName, 'w') or die("File in use"); fwrite($fileHandler, $headerString . "\n"); foreach($answers as $entry) { $entryString = ""; //PHP doesn't automatically sort the indices so I have to do it myself //Entry elements can now be printed in order to match the headers for ($i = 0; $i < count($headers); $i++) { if(in_array($i, array_keys($entry))) $entryString = $entryString . $entry[$i] . "\t"; else $entryString = $entryString . "\t"; } fwrite($fileHandler, $entryString . "\n"); } } /* * function getAnswers * @param type $headers * Based on the questions specified in the headers array, matches the answers of every * respondent to the correct questions and returns an array containing a list of answers per * respondent */ private function getAnswers($headers) { //Add the answers per survey per respondent. Requires some extra looping to assure proper ordering. //Keeps a list of respondents which have already been used. If an answerset exists for a respondent which //already exists within the answers-array, use the index of the respondent. Else: make a new row. $respondentsUsed = array(); $answers = array(); foreach($this->answersets as $answerset) { $entryIndex; if(in_array($answerset->respondent->name, $respondentsUsed)) { $entryIndex = array_search($answerset->respondent->name, $respondentsUsed); $entry = $answers[$entryIndex]; } else { $entry = array(); $entry[0] = $answerset->respondent->name; $entryIndex = count($respondentsUsed); $respondentsUsed[] = $answerset->respondent->name; } foreach ($answerset->answers as $answer) { $index = array_search($answerset->survey->title . '_' . $answer->question->uid, $headers); //Index has to be a good number, else the answer doesn't belong in this session //Causes might be a missing question or having a wrong survey if($index != 0) { $value = ""; foreach ($answer->values as $val) { $value = $value . $val . ","; } //We all hate trailing commas if (strlen($value) > 0) { $value = substr($value, 0, strlen($value)-1); } $entry[$index] = $value; } } $answers[$entryIndex] = $entry; } return $answers; } } ?>