[149] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Description of AnswerSetConnector
|
---|
| 5 | *
|
---|
| 6 | * @author jkraaijeveld
|
---|
| 7 | */
|
---|
| 8 | class AnswerSetConnector implements IConnector{
|
---|
| 9 | protected $model;
|
---|
| 10 | protected $fileName = 'data/answers/answersets.rdf';
|
---|
| 11 | protected $db;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Constructor for the AnswerSetConnector
|
---|
| 15 | */
|
---|
| 16 | public function __construct()
|
---|
| 17 | {
|
---|
| 18 | //Ensure the required folder for this connector exists
|
---|
| 19 | if(!is_dir('data/answers'))
|
---|
| 20 | mkdir('data/answers');
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * function load()
|
---|
| 25 | * Loads the file into the standard MemModel
|
---|
| 26 | */
|
---|
| 27 | public function load()
|
---|
| 28 | {
|
---|
| 29 | //Get the Memory Model from the ModelFactory
|
---|
| 30 | $this->model = ModelFactory::getDefaultModel();
|
---|
| 31 |
|
---|
| 32 | //Ensure the required file exists before loading
|
---|
| 33 | if(file_exists($this->fileName))
|
---|
| 34 | $this->model->load($this->fileName);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * function save()
|
---|
| 39 | * Saves the MemModel into the given file.
|
---|
| 40 | */
|
---|
| 41 | public function save() {
|
---|
| 42 | $this->model->saveAs($this->fileName,'rdf');
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
[158] | 46 | * function get()
|
---|
| 47 | * @param type $arguments : An array having one ore more of the following elements:
|
---|
| 48 | * 'uid', 'survey', 'respondent', 'answers'.
|
---|
[149] | 49 | */
|
---|
| 50 | public function get($arguments)
|
---|
| 51 | {
|
---|
| 52 | $this->load();
|
---|
| 53 |
|
---|
| 54 | $keys = array_keys($arguments);
|
---|
| 55 | $uid = ""; $survey = ""; $respondent = ""; $answers = "";
|
---|
| 56 | if(in_array("uid", $keys))
|
---|
| 57 | $uid = 'predicates:uid \''.$arguments["uid"].'\' ';
|
---|
| 58 | if(in_array("survey", $keys))
|
---|
| 59 | $survey = 'predicates:for_survey \''.$arguments["survey"].'\' ';
|
---|
| 60 | if(in_array("respondent", $keys))
|
---|
| 61 | $respondent = 'predicates:by_respondent \''.$arguments["respondent"].'\' ';
|
---|
| 62 | if(in_array("answers", $keys))
|
---|
| 63 | {
|
---|
| 64 | foreach ($arguments["answers"] as $answer)
|
---|
| 65 | {
|
---|
| 66 | $answers = $answers . 'predicates:given_answer \'' . $answer . '\' ';
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | //Build the query string
|
---|
| 71 | $querystring = '
|
---|
| 72 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
| 73 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
| 74 | SELECT DISTINCT ?uid, ?for_survey, ?by_respondent
|
---|
| 75 | WHERE
|
---|
| 76 | {
|
---|
| 77 | _answerset predicates:resource_type resources:answerset ;
|
---|
| 78 | predicates:uid ?uid ;
|
---|
| 79 | predicates:for_survey ?for_survey ;
|
---|
| 80 | predicates:by_respondent ?by_respondent ;
|
---|
| 81 | ' . $uid . $survey . $respondent . $answers . '
|
---|
| 82 | }';
|
---|
| 83 |
|
---|
| 84 | //Query the model
|
---|
| 85 | $results = $this->model->sparqlQuery($querystring);
|
---|
| 86 | $answerSets = array();
|
---|
| 87 | if(!empty($results))
|
---|
| 88 | {
|
---|
| 89 | $this->db = new DatabaseInterface();
|
---|
| 90 | foreach($results as $result)
|
---|
| 91 | {
|
---|
| 92 | $survey = $this->db->get("survey", array("uid" => $result['?for_survey']->label));
|
---|
| 93 | $respondent = $this->db->get("respondent", array("uid" => $result['?by_respondent']->label));
|
---|
| 94 | $answers = $this->getAnswers($result['?uid']->label);
|
---|
| 95 | $answerSets[] = new AnswerSet($result['?uid']->label, $survey[0], $respondent[0], $answers);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | return $answerSets;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * function getAnswers()
|
---|
[158] | 103 | * @param type $uid : the uid for which the answers should be retrieved.
|
---|
[149] | 104 | */
|
---|
| 105 | private function getAnswers($uid)
|
---|
| 106 | {
|
---|
| 107 | $querystring = '
|
---|
| 108 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE .'>
|
---|
| 109 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE .'>
|
---|
| 110 | SELECT DISTINCT ?given_answer
|
---|
| 111 | WHERE
|
---|
| 112 | {
|
---|
| 113 | _answerset predicates:resource_type resources:answerset ;
|
---|
| 114 | predicates:given_answer ?given_answer ;
|
---|
| 115 | predicates:uid \'' . $uid . '\'
|
---|
| 116 | }';
|
---|
| 117 | $answers = $this->model->sparqlQuery($querystring);
|
---|
| 118 | $returnArray = array();
|
---|
| 119 | if(!empty($answers))
|
---|
| 120 | {
|
---|
| 121 | foreach($answers as $answer)
|
---|
| 122 | {
|
---|
| 123 | $answerR = $this->db->get("answer", array("uid" => $answer["?given_answer"]->label));
|
---|
| 124 | $returnArray[] = $answerR[0];
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | return $returnArray;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
[158] | 131 | * function set()
|
---|
| 132 | * @param type $rToolObject : The ResearchToolObject to be saved.
|
---|
[149] | 133 | */
|
---|
| 134 | public function set($rToolObject)
|
---|
| 135 | {
|
---|
| 136 | $this->load();
|
---|
| 137 | $resourceAnswerSet = new Resource(ANSWERSET . '/' . $rToolObject->uid);
|
---|
| 138 | //Remove the old value stored with the given id
|
---|
| 139 | $this->model->subtract($this->model->find($resourceAnswerSet, null, null));
|
---|
| 140 |
|
---|
| 141 | //Set the new values
|
---|
| 142 | $resourceAnswerSetType = new Resource(ANSWERSET);
|
---|
| 143 | $predicateRType = new Resource(RTYPE);
|
---|
| 144 | $this->model->add(new Statement($resourceAnswerSet, $predicateRType, $resourceAnswerSetType));
|
---|
| 145 |
|
---|
| 146 | $answerSetId = new Literal($rToolObject->uid);
|
---|
| 147 | $predicateId = new Resource(UID);
|
---|
| 148 | $this->model->add(new Statement($resourceAnswerSet, $predicateId, $answerSetId));
|
---|
| 149 |
|
---|
| 150 | $surveyId = new Literal($rToolObject->survey->uid);
|
---|
| 151 | $predicateSurvey = new Resource(FOR_SURVEY);
|
---|
| 152 | $this->model->add(new Statement($resourceAnswerSet, $predicateSurvey, $surveyId));
|
---|
| 153 |
|
---|
| 154 | $respondentId = new Literal($rToolObject->respondent->uid);
|
---|
| 155 | $predicateRespondent = new Resource(BY_RESPONDENT);
|
---|
| 156 | $this->model->add(new Statement($resourceAnswerSet, $predicateRespondent, $respondentId));
|
---|
| 157 |
|
---|
| 158 | if(isset($rToolObject->answers))
|
---|
| 159 | {
|
---|
| 160 | foreach($rToolObject->answers as $answer)
|
---|
| 161 | {
|
---|
| 162 | $answerId = new Literal($answer->uid);
|
---|
| 163 | $predicateAnswer = new Resource(GIVEN_ANSWER);
|
---|
| 164 | $this->model->add(new Statement($resourceAnswerSet, $predicateAnswer, $answerId));
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | $this->save();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|