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() * @param type $arguments : An array having one ore more of the following elements: * 'uid', 'survey', 'respondent', 'answers'. */ public function get($arguments) { $this->load(); $keys = array_keys($arguments); $uid = ""; $survey = ""; $respondent = ""; $answers = ""; if(in_array("uid", $keys)) $uid = 'predicates:uid \''.$arguments["uid"].'\' '; if(in_array("survey", $keys)) $survey = 'predicates:for_survey \''.$arguments["survey"].'\' '; if(in_array("respondent", $keys)) $respondent = 'predicates:by_respondent \''.$arguments["respondent"].'\' '; if(in_array("answers", $keys)) { foreach ($arguments["answers"] as $answer) { $answers = $answers . 'predicates:given_answer \'' . $answer . '\' '; } } //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?for_survey, ?by_respondent WHERE { _answerset predicates:resource_type resources:answerset ; predicates:uid ?uid ; predicates:for_survey ?for_survey ; predicates:by_respondent ?by_respondent ; ' . $uid . $survey . $respondent . $answers . ' }'; //Query the model $results = $this->model->sparqlQuery($querystring); $answerSets = array(); if(!empty($results)) { $this->db = new DatabaseInterface(); foreach($results as $result) { $survey = $this->db->get("survey", array("uid" => $result['?for_survey']->label)); $respondent = $this->db->get("respondent", array("uid" => $result['?by_respondent']->label)); $answers = $this->getAnswers($result['?uid']->label); $answerSets[] = new AnswerSet($result['?uid']->label, $survey[0], $respondent[0], $answers); } } return $answerSets; } /** * function getAnswers() * @param type $uid : the uid for which the answers should be retrieved. */ private function getAnswers($uid) { $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE .'> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE .'> SELECT DISTINCT ?given_answer WHERE { _answerset predicates:resource_type resources:answerset ; predicates:given_answer ?given_answer ; predicates:uid \'' . $uid . '\' }'; $answers = $this->model->sparqlQuery($querystring); $returnArray = array(); if(!empty($answers)) { foreach($answers as $answer) { $answerR = $this->db->get("answer", array("uid" => $answer["?given_answer"]->label)); $returnArray[] = $answerR[0]; } } return $returnArray; } /** * function set() * @param type $rToolObject : The ResearchToolObject to be saved. */ public function set($rToolObject) { $this->load(); $resourceAnswerSet = new Resource(ANSWERSET . '/' . $rToolObject->uid); //Remove the old value stored with the given id $this->model->subtract($this->model->find($resourceAnswerSet, null, null)); //Set the new values $resourceAnswerSetType = new Resource(ANSWERSET); $predicateRType = new Resource(RTYPE); $this->model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType)); $answerSetId = new Literal($rToolObject->uid); $predicateId = new Resource(UID); $this->model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId)); $surveyId = new Literal($rToolObject->survey->uid); $predicateSurvey = new Resource(FOR_SURVEY); $this->model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId)); $respondentId = new Literal($rToolObject->respondent->uid); $predicateRespondent = new Resource(BY_RESPONDENT); $this->model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId)); if(isset($rToolObject->answers)) { foreach($rToolObject->answers as $answer) { $answerId = new Literal($answer->uid); $predicateAnswer = new Resource(GIVEN_ANSWER); $this->model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId)); } } $this->save(); } }