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'); } /** * 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 function get($arguments) { $this->load(); $keys = array_keys($arguments); //Set default values for arguments $uid = "?uid"; $title = "?title"; $description = "?description"; $questions = ""; if(in_array("uid", $keys)) $uid = '\''.$arguments["uid"].'\''; if(in_array("title", $keys)) $title = '\''.$arguments["title"].'\''; if(in_array("description", $keys)) $description = "\"".$arguments["description"]."\""; if(in_array("questions", $keys)) { //Append the questions string to filter for questions properly foreach ($arguments["questions"] as $questionUid) { $questions = $questions . 'predicates:has_question \'' . $questionUid . '\' '; } } //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?description WHERE { _survey predicates:resource_type resources:survey ; predicates:uid ?uid ; predicates:title ?title ; predicates:description ?description ; predicates:uid ' . $uid . ' predicates:title ' . $title . ' predicates:description ' . $description . ' ' . $questions . ' }'; //Query the model $results = $this->model->sparqlQuery($querystring); //Create the corresponding Survey objects $surveys = array(); if(!empty($results)) { $this->db = new DatabaseInterface(); foreach($results as $result) { $questions = $this->getQuestions($result['?uid']->label); $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $questions); } } return $surveys; } /** * Gets the questions corresponding to the survey * @param type $uid */ private function getQuestions($uid) { //Create the querystring. Note that the UID is the only argument in this case. $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?has_question WHERE { _survey predicates:resource_type resources:survey ; predicates:has_question ?has_question ; predicates:uid \'' . $uid . '\' }'; $results = $this->model->sparqlQuery($querystring); $questions = array(); //For every question id, get the corresponding question if(!empty($results)) { foreach($results as $questionId) { $resultQ = $this->db->get("question", array("uid" => $questionId['?has_question']->label)); $questions[] = $resultQ[0]; } } return $questions; } /** * Saves the given Survey object. * @param type $rToolObject */ public function set($rToolObject) { $this->load(); $resourceSurvey = new Resource(SURVEY.'/'.$rToolObject->uid); //Remove the old value stored with the given id $this->model->subtract($this->model->find($resourceSurvey, null, null)); $resourceSurveyType = new Resource(SURVEY); $predicateRType = new Resource(RTYPE); $this->model->add(new Statement($resourceSurvey,$predicateRType,$resourceSurveyType)); $predicateUniqueID = new Resource(UID); $literalSurveyID = new Literal($rToolObject->uid); $this->model->add(new Statement($resourceSurvey,$predicateUniqueID,$literalSurveyID)); $predicateTitle = new Resource(TITLE); $surveyTitle = new Literal($rToolObject->title); $this->model->add(new Statement($resourceSurvey,$predicateTitle,$surveyTitle)); $predicateDescription = new Resource(DESCRIPTION); $surveyDescription = new Literal($rToolObject->description); $this->model->add(new Statement($resourceSurvey,$predicateDescription,$surveyDescription)); if(isset($rToolObject->questions)) { foreach($rToolObject->questions as $question) { $questionUid = new Literal($question->uid); $predicateQuid = new Resource(HAS_QUESTION); $this->model->add(new Statement($resourceSurvey,$predicateQuid,$questionUid)); } } $this->save(); } } ?>