model = ModelFactory::getDefaultModel(); //Ensure the required file exists before loading if(file_exists($this->fileName)) $this->model->load($this->fileName); } /** * function save() * Saves the MemModel into the given file. */ public function save() { $this->model->saveAs($this->fileName,'rdf'); } /** * Function get() * Get the answers corresponding to the arguments * 'Question' argument is supposed to be the question ID * 'Values' has to be an array to make sure multiple answers can be selected. */ public function get($arguments) { $this->load(); $keys = array_keys($arguments); $uid = ""; $question = ""; $value = ""; if(in_array("uid", $keys)) $uid = 'predicates:uid \''.$arguments["uid"].'\''; if(in_array("question", $keys)) $question = 'predicates:question_code \''.$arguments["question"].'\''; if(in_array("values", $keys)) { foreach ($arguments["values"] as $val) { $value = $value . 'predicates:answered \'' . $val . '\' '; } } //Create the querystring $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?question_code WHERE { _answer predicates:resource_type resources:answer; predicates:uid ?uid ; predicates:question_code ?question_code ; ' . $uid . $question . $value . ' }'; //Query the model $results = $this->model->sparqlQuery($querystring); //An answer can have multiple values, get all these values for the answer instances found. $answers = array(); if(!empty($results)) { $this->db = new DatabaseInterface(); foreach($results as $result) { $values = $this->getValues($result['?uid']->label); $questionResult = $this->db->get("question", array("uid" => $result['?question_code']->label)); $answers[] = new Answer($result['?uid']->label, $questionResult[0], $values); } } return $answers; } /** * Gets the values that belong to the answer * specified by the UID */ private function getValues($uid) { $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?answered WHERE { _answer predicates:resource_type resources:answer ; predicates:answered ?answered ; predicates:uid \''. $uid . '\' }'; $values = $this->model->sparqlQuery($querystring); $returnArray = array(); if(!empty($values)) { foreach($values as $value) { $returnArray[] = $value['?answered']->label; } } return $returnArray; } /** * Saves the answer Object to the RDF file. * @param type $rToolObject */ public function set($rToolObject) { $this->load(); $resourceAnswer = new Resource(ANSWER.'/'.$rToolObject->uid); //Remove the old value stored with the given id $this->model->subtract($this->model->find($resourceAnswer, null, null)); //Save all the new values $resourceAnswerType = new Resource(ANSWER); $predicateRType = new Resource(RTYPE); $this->model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); $answerId = new Literal($rToolObject->uid); $predicateId = new Resource(UID); $this->model->add(new Statement($resourceAnswer, $predicateId, $answerId)); $questionId = new Literal($rToolObject->question->uid); $predicateQId = new Resource(QCODE); $this->model->add(new Statement($resourceAnswer, $predicateQId, $questionId)); if(isset($rToolObject->values)) { foreach($rToolObject->values as $value) { $answerValue = new Literal($value); $predicateAnswer = new Resource(ANSWERED); $this->model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue)); } } $this->save(); } } ?>