fileName = 'data/answers/answersets.rdf'; //Ensure the required folder for this connector exists if(!is_dir('data/answers')) mkdir('data/answers'); } /** * 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(); //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 ; ' . $this->createArguments($arguments) . ' }'; //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) { $result = $this->model->findRegex("[(".$uid.")]", "[(given_answer)]", null); $iterator = $result->getStatementIterator(); $answers = array(); while($iterator->hasNext()) { $element = $iterator->next(); $answersR = $this->db->get("answer", array("uid" => $element->getLabelObject())); $answers[] = $answersR[0]; } return $answers; } /** * 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(); } }