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'); } /** * Get the questions corresponding to the arguments. * @param type $arguments * @return type Array */ public function get($arguments) { $this->load(); $keys = array_keys($arguments); //Set default values for arguments $uid = "?uid"; $title = "?title"; $type = "?type"; $description = "?description"; $category = "?category"; $has_answer = ""; //Set the arguments if they are supplied if(in_array("uid", $keys)) $uid = '\''.$arguments["uid"].'\''; if(in_array("title", $keys)) $title = '\''.$arguments["title"].'\''; if(in_array("type", $keys)) $type = '\''.$arguments["type"].'\''; if(in_array("description", $keys)) $description = "\"".$arguments["description"]."\""; if(in_array("category", $keys)) $style = "\"".$arguments["category"]."\""; if(in_array("answers", $keys)) { foreach($arguments["answers"] as $answer) { $has_answer = $has_answer . 'predicates:has_answer \'' . $answer . '\' '; } } $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 ; predicates:question_code ' . $uid . ' predicates:title ' . $title . ' predicates:question_type ' . $type . ' predicates:description ' . $description . ' predicates:question_category ' . $category . ' ' . $has_answer . ' }'; //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 * @return type Array */ private function getAnswers($uid) { $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?has_answer WHERE { _question predicates:resource_type resources:question ; predicates:has_answer ?has_answer ; predicates:question_code \'' . $uid . '\' }'; $answers = $this->model->sparqlQuery($querystring); $returnArray = array(); if(!empty($answers)) { foreach($answers as $answer) { $returnArray[] = $answer['?has_answer']->label; } } return $returnArray; } /** * Save the given ResearchTool object * @param type $rToolObject */ 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(); } } ?>