fileName = 'data/surveys/surveys.rdf'; //Ensure the required folder for this connector exists if (!is_dir('data/surveys/')) mkdir('data/surveys/'); } /** * 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(); //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 ; ' . $this->createArguments($arguments) . ' }'; //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); $creator = $this->db->get("user", array("uid" => $result['?creator']->label)); $surveys[] = new Survey($result['?uid']->label, $result['?title']->label, $result['?description']->label, $creator[0], $questions); } } return $surveys; } /** * Gets the questions corresponding to the survey * @param type $uid : The uid for which the questions should be gotten. */ private function getQuestions($uid) { $result = $this->model->findRegex("[(".$uid.")]", "[(has_question)]", null); $iterator = $result->getStatementIterator(); $questions = array(); while($iterator->hasNext()) { $element = $iterator->next(); $result = $this->db->get("question", array("code" => $element->getLabelObject())); $questions[] = $result[0]; } return $questions; } /** * Saves the given Survey object. * @param type $rToolObject : The ResearchToolObject to be saved. */ 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)); $predicateCreator = new Resource(CREATOR); $surveyCreator = new Literal($rToolObject->creator->uid); $this->model->add(new Statement($resourceSurvey, $predicateCreator, $surveyCreator)); 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(); } } ?>