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