uid = $uid; $this->question = $question; $this->values = $values; } /** * function evaluate() * Evaluates the references and fills in the objects for them instead. */ public function evaluate() { if(is_string($this->question)) { $result = Question::get(array("code" => $this->question)); if(!isset($result[0])) return false; $this->question = $result[0]; } return true; } /** * function get() * @param type $arguments: An array having one or more of the following elements: * 'uid', 'question', 'values'. */ public static function get($arguments) { $model = ResearchToolObject::load(Answer::$filename); //Create the querystring $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?question_code WHERE { _answer predicates:resource_type resources:answer; predicates:uid ?uid ; predicates:question_code ?question_code ; ' . ResearchToolObject::createArguments($arguments) . ' }'; //Query the model $results = $model->sparqlQuery($querystring); //An answer can have multiple values, get all these values for the answer instances found. $answers = array(); if(!empty($results)) { foreach($results as $result) { $answers[] = new Answer($result['?uid']->label, $result['?question_code']->label, Answer::getValues($model, $result['?uid']->label)); } } return $answers; } /** * function getValues() * @param type $uid : The uid of the Answer for which the values should be gotten. */ private static function getValues($model, $uid) { $result = $model->findRegex("[(".$uid.")]", "[(answered)]", null); $iterator = $result->getStatementIterator(); $values = array(); while($iterator->hasNext()) { $element = $iterator->next(); $values[] = $element->getLabelObject(); } return $values; } /** * function save() * Saves the current object into the database. */ public function save() { //If evaluation fails, some references are incorrect. //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed. //TODO: Decide how to fix invalid references graciously. if(!$this->evaluate()) return false; //Ensure the required folder exists. if(!is_dir('data/results')) mkdir('data/results'); $model = ResearchToolObject::load(Answer::$filename); $resourceAnswer = new Resource(ANSWER.'/'.$this->uid); //Remove the old value stored with the given id $model->subtract($model->find($resourceAnswer, null, null)); //Save all the new values $resourceAnswerType = new Resource(ANSWER); $predicateRType = new Resource(RTYPE); $model->add(new Statement($resourceAnswer,$predicateRType,$resourceAnswerType)); $answerId = new Literal($this->uid); $predicateId = new Resource(UID); $model->add(new Statement($resourceAnswer, $predicateId, $answerId)); $questionId = new Literal($this->question->uid); $predicateQId = new Resource(QCODE); $model->add(new Statement($resourceAnswer, $predicateQId, $questionId)); if(isset($this->values)) { foreach($this->values as $value) { $answerValue = new Literal($value); $predicateAnswer = new Resource(ANSWERED); $model->add(new Statement($resourceAnswer, $predicateAnswer, $answerValue)); } } $model->saveAs(Answer::$filename, 'rdf'); return true; } } ?>