model = ModelFactory::getDefaultModel(); if(file_exists($this->fileName)) $this->model->load($this->fileName); } /** * fucntion save() * Saves the MemModel into the given file. */ public function save() { $this->model->saveAs($this->fileName,'rdf'); } /** * Get the Sessions corresponding to the arguments * @ param type $arguments * Arguments can be one or more of the following: * uid, title, datetime, applications, surveys, answersets * Where applications, surveys and answersets are arrays containing ids */ public function get($arguments) { $this->load(); $keys = array_keys($arguments); $uid = ""; $title = ""; $datetime = ""; $applications = ""; $surveys = ""; $answersets = ""; if(in_array("uid", $keys)) $uid = 'predicates:uid \''.$arguements["uid"].'\' '; if(in_array("title", $keys)) $title = 'predicates:title \''.$arguments["title"].'\' '; if(in_array("datetime", $keys)) $datetime = 'predicates:datetime \''.$arguments["datetime"].'\' '; if(in_array("applications", $keys)) { foreach($arguments["applications"] as $application) { $applications = $applications . 'predicates:has_application \'' . $application . '\' '; } } if(in_array("surveys", $keys)) { foreach($arguments["surveys"] as $survey) { $surveys = $surveys . 'predicates:has_survey \'' . $survey . '\' '; } } if(in_array("answersets", $keys)) { foreach($arguments["answersets"] as $answerset) { $answersets = $answersets . 'predicates:has_answerset \'' . $answerset . '\' '; } } //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?datetime WHERE { _session predicates:resource_type resources:session ; predicates:uid ?uid ; predicates:title ?title ; ' . $uid . $title . $datetime . $applications . $surveys . $answersets . ' }'; //Query the model $results = $this->model->sparqlQuery($querystring); $sessions = array(); if(!empty($results)) { $this->db = new DatabaseInterface(); foreach($results as $result) { //Create a session object out of every result, get all required fields as well. $pipeline = $this->getPipeline($result['?uid']->label); $answersets = $this->getAnswerSets($result['?uid']->label); $datetime = new DateTime(); $datetime->setTimestamp(intval($result['?datetime'])); $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $datetime, $pipeline, $answersets); } } return $sessions; } /** * function getPipeline * Get the application and survey objects of the Session in order */ private function getPipeline($uid) { $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null); $iterator = $result->getStatementIterator(); $pipeline = array(); while($iterator->hasNext()) { $element = $iterator->next(); if(strpos($element->getLabelPredicate(), "has_application") != false) { $applications = $this->db->get("application", array("uid" => $element->getLabelObject())); $pipeline[] = $applications[0]; } else { $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject())); $pipeline[] = $surveys[0]; } } return $pipeline; } /** * function getAnswerSets * Get the answersets belonging to the given session */ private function getAnswerSets($uid) { $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null); $iterator = $result->getStatementIterator(); $answersets = array(); while($iterator->hasNext()) { $element = $iterator->next(); $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject())); $answersets[] = $resultsets[0]; } return $answersets; } /** * Save the given Session object in the file. * @param type $rToolObjects */ public function set($rToolObject) { $this->load(); $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid); //Remove the old value stored with the given id $this->model->subtract($this->model->find($resourceSession, null, null)); //Set the new values $resourceSessionType = new Resource(SESSION); $predicateType = new Resource(RTYPE); $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType)); $sessionId = new Literal($rToolObject->uid); $predicateId = new Resource(UID); $this->model->add(new Statement($resourceSession, $predicateId, $sessionId)); $sessionTitle = new Literal($rToolObject->title); $predicateTitle = new Resource(TITLE); $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle)); $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp()); $predicateTimestamp = new Resource(DATETIME); $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp)); //Create a sequence to store the different applications and surveys. if(isset($rToolObject->pipeline)) { foreach($rToolObject->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)) $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject)); } } if(isset($rToolObject->answersets)) { foreach($rToolObject->answersets as $answerset) { $sessionAnswerSet = new Literal($answerset->uid); $predicateAnswerSet = new Resource(HAS_ANSWERSET); $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet)); } } $this->save(); } } ?>