uid = $uid; $this->answersets = $answersets; $this->playerresults = $playerresults; $this->groupresults = $groupresults; $this->periodicresults = $periodicresults; } /** * funtion evaluate() * evaluates the references of all results */ public function evaluate() { if(!empty($this->answersets) && is_string($this->answersets[0])) { $newAnswerSets = array(); foreach($this->answersets as $answerSet) { $result = AnswerSet::get(array("uid" => $answerSet)); if(!isset($result[0])) return false; $newAnswerSets[] = $result[0]; } $this->answersets = $newAnswerSets; } //TODO: Evaluate the other sets when they are implemented. return true; } /** * 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(ResultSet::$filename); $resourceResultSet = new Resource(RESULTSET . '/' . $this->uid); //Remove the old value stored with the given id $model->subtract($model->find($resourceResultSet, null, null)); //Set new values $resourceResultSetType = new Resource(RESULTSET); $predicateRType = new Resource(RTYPE); $model->add(new Statement($resourceResultSet, $predicateRType, $resourceResultSetType)); $resultSetId = new Literal($this->uid); $predicateId = new Resource(UID); $model->add(new Statement($resourceResultSet, $predicateId, $resultSetId)); if(isset($this->answersets)) { foreach($this->answersets as $answerset) { $answerSetId = new Literal($answerset->uid); $predicateAnswerSet = new Resource(HAS_ANSWERSET); $model->add(new Statement($resourceResultSet, $predicateAnswerSet, $answerSetId)); } } //TODO: store other sets when they are implemented. $model->saveAs(ResultSet::$filename, 'rdf'); return true; } /** * function get() * @param type $arguments : An array having one or more of the following elements: * 'uid', 'answersets', 'playerresults', 'groupresults', 'periodicresults' */ public static function get($arguments) { $model = ResearchToolObject::load(ResultSet::$filename); //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid WHERE { _resultset predicates:resource_type resources:resultset ; predicates:uid ?uid ; ' . ResearchToolObject::createArguments($arguments) . ' }'; //Query the model $results = $model->sparqlQuery($querystring); $resultSets = array(); if(!empty($results)) { foreach($results as $result) { $answerSets = ResultSet::getAnswerSets($model, $result['?uid']->label); $resultSets[] = new ResultSet($result['?uid']->label, $answerSets, array(), array(), array()); } } return $resultSets; } /** * function getAnswerSets() * @param type $uid : The ResultSet uid for which the answerSets should be retrieved. */ private static function getAnswerSets($model, $uid) { $result = $model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null); $iterator = $result->getStatementIterator(); $answersets = array(); while($iterator->hasNext()) { $element = $iterator->next(); $answersets[] = $element->getLabelObject(); } return $answersets; } }