fileName = 'data/questions/questions.rdf'; //Ensure the required folder for this connector exists if (!is_dir('data/questions/')) mkdir('data/questions/'); } /** * Get the questions corresponding to the arguments. * @param type $arguments : An array having one ore more of the following elements: * 'code', 'title', 'type', 'description', 'category', 'definedanswers'. */ public function get($arguments) { $this->load(); $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?type, ?description, ?category WHERE { _question predicates:resource_type resources:question ; predicates:question_code ?uid ; predicates:title ?title ; predicates:question_type ?type ; predicates:description ?description ; predicates:question_category ?category ; '. $this->createArguments($arguments) . ' }'; //Create the querystring $results = $this->model->sparqlQuery($querystring); $questions = array(); if(!empty($results)) { foreach($results as $result) { $answers = $this->getAnswers($result['?uid']->label); $questions[] = new Question($result['?uid']->label, $result['?title']->label, $result['?type']->label, $result['?description']->label, $result['?category']->label, $answers); } } return $questions; } /** * Gets the answers belonging to the given uid. * @param type $uid : The uid of the Question in question (haha, pun). */ private function getAnswers($uid) { $result = $this->model->findRegex("[(".$uid.")]", "[(has_answer)]", null); $iterator = $result->getStatementIterator(); $answers = array(); while($iterator->hasNext()) { $element = $iterator->next(); $answers[] = $element->getLabelObject(); } return $answers; } /** * Save the given ResearchTool object * @param type $rToolObject : The ResearchToolObject to be saved. */ public function set($rToolObject) { $this->load(); $resourceQuestion = new Resource(QUESTION.'/'.$rToolObject->uid); //Remove the old value stored with the given id $this->model->subtract($this->model->find($resourceQuestion, null, null)); //Save all the new values $resourceQuestionType = new Resource(QUESTION); $predicateRType = new Resource(RTYPE); $this->model->add(new Statement($resourceQuestion,$predicateRType,$resourceQuestionType)); $questionQCode = new Literal($rToolObject->uid); $predicateQCode = new Resource(QCODE); $this->model->add(new Statement($resourceQuestion,$predicateQCode,$questionQCode)); $questionTitle = new Literal($rToolObject->title); $predicateTitle = new Resource(TITLE); $this->model->add(new Statement($resourceQuestion,$predicateTitle,$questionTitle)); $questionDescription = new Literal($rToolObject->description); $predicateDescription = new Resource(DESCRIPTION); $this->model->add(new Statement($resourceQuestion,$predicateDescription,$questionDescription)); $resourceQuestionType = new Literal($rToolObject->type); $predicateQType = new Resource(QTYPE); $this->model->add(new Statement($resourceQuestion,$predicateQType,$resourceQuestionType)); $questionQCategory = new Literal($rToolObject->category); $predicateQCategory = new Resource(QCATEGORY); $this->model->add(new Statement($resourceQuestion,$predicateQCategory,$questionQCategory)); if(isset($rToolObject->answers)) { foreach($rToolObject->answers as $answer) { $answerValue = new Literal($answer); $predicateAnswer = new Resource(HAS_ANSWER); $this->model->add(new Statement($resourceQuestion,$predicateAnswer,$answerValue)); } } $this->save(); } } ?>