uid = $uid; $this->title = $title; $this->description = $description; $this->creator = $creator; $this->questions = $questions; } /** * function evaluate() * Evaluates the references */ public function evaluate() { if(is_string($this->creator)) { $result = User::get(array("uid" => $this->creator)); if(!isset($result[0])) return false; $this->creator = $result[0]; } if(!empty($this->questions) && is_string($this->questions[0])) { $newQuestions = array(); foreach($this->questions as $question) { $result = Question::get(array("code" => $question)); if(!isset($result[0])) return false; $newQuestions[] = $result[0]; } $this->questions = $newQuestions; } 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; if(!is_dir('data/surveys/')) mkdir('data/surveys/'); $model = ResearchToolObject::load(Survey::$filename); $resourceSurvey = new Resource(SURVEY.'/'.$this->uid); //Remove the old value stored with the given id $model->subtract($model->find($resourceSurvey, null, null)); $resourceSurveyType = new Resource(SURVEY); $predicateRType = new Resource(RTYPE); $model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType)); $predicateUniqueID = new Resource(UID); $literalSurveyID = new Literal($this->uid); $model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID)); $predicateTitle = new Resource(TITLE); $surveyTitle = new Literal($this->title); $model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle)); $predicateDescription = new Resource(DESCRIPTION); $surveyDescription = new Literal($this->description); $model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription)); $predicateCreator = new Resource(CREATOR); $surveyCreator = new Literal($this->creator->uid); $model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator)); if(isset($this->questions)) { foreach($this->questions as $question) { $questionUid = new Literal($question->uid); $predicateQuid = new Resource(HAS_QUESTION); $model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid)); } } $model->saveAs(Survey::$filename, 'rdf'); return true; } /** * function get($arguments) * Gets the array of Survey objects belonging to arguments supplied. * @param type $arguments : An array containing zero or more of the following keys: * 'uid', 'title', 'description', 'questions' * Note: questions has to be an array of question IDs (codes) for this filter to work. */ public static function get($arguments) { $model = ResearchToolObject::load(Survey::$filename); //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?description, ?creator WHERE { _survey predicates:resource_type resources:survey ; predicates:uid ?uid ; predicates:title ?title ; predicates:description ?description ; predicates:creator ?creator ; ' . ResearchToolObject::createArguments($arguments) . ' }'; //Query the model $results = $model->sparqlQuery($querystring); //Create the corresponding Survey objects $surveys = array(); if(!empty($results)) { foreach($results as $result) { $questions = Survey::getQuestions($model, $result['?uid']->label); $creator = $result['?creator']->label; $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator, $questions); } } return $surveys; } /** * Gets the questions corresponding to the survey * @param type $uid : The uid for which the questions should be gotten. */ private static function getQuestions($model, $uid) { $result = $model->findRegex("[(".$uid.")]", "[(has_question)]", null); $iterator = $result->getStatementIterator(); $questions = array(); while($iterator->hasNext()) { $element = $iterator->next(); $questions[] = $element->getLabelObject(); } return $questions; } } ?>