[143] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Description of SessionConnector
|
---|
| 4 | *
|
---|
| 5 | * @author jkraaijeveld
|
---|
| 6 | */
|
---|
| 7 | class SessionConnector implements IConnector
|
---|
| 8 | {
|
---|
| 9 | protected $model;
|
---|
| 10 | protected $fileName = 'data/sessions/sessions.rdf';
|
---|
| 11 | protected $db;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Constructor for the SessionConnector
|
---|
| 15 | */
|
---|
| 16 | public function __construct()
|
---|
| 17 | {
|
---|
| 18 | //Ensure the required folder for this connector exists
|
---|
| 19 | if(!is_dir('data/sessions'))
|
---|
| 20 | mkdir('data/sessions');
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * function load()
|
---|
| 25 | * Loads the file into the standard MemModel
|
---|
| 26 | */
|
---|
| 27 | public function load()
|
---|
| 28 | {
|
---|
| 29 | $this->model = ModelFactory::getDefaultModel();
|
---|
| 30 | if(file_exists($this->fileName))
|
---|
| 31 | $this->model->load($this->fileName);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * fucntion save()
|
---|
| 36 | * Saves the MemModel into the given file.
|
---|
| 37 | */
|
---|
| 38 | public function save()
|
---|
| 39 | {
|
---|
| 40 | $this->model->saveAs($this->fileName,'rdf');
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Get the Sessions corresponding to the arguments
|
---|
| 45 | * @ param type $arguments
|
---|
| 46 | * Arguments can be one or more of the following:
|
---|
| 47 | * uid, title, datetime, applications, surveys, answersets
|
---|
| 48 | * Where applications, surveys and answersets are arrays containing ids
|
---|
| 49 | */
|
---|
| 50 | public function get($arguments)
|
---|
| 51 | {
|
---|
| 52 | $this->load();
|
---|
| 53 |
|
---|
| 54 | $keys = array_keys($arguments);
|
---|
| 55 | $uid = ""; $title = ""; $datetime = ""; $applications = ""; $surveys = ""; $answersets = "";
|
---|
| 56 | if(in_array("uid", $keys))
|
---|
| 57 | $uid = 'predicates:uid \''.$arguements["uid"].'\' ';
|
---|
| 58 | if(in_array("title", $keys))
|
---|
| 59 | $title = 'predicates:title \''.$arguments["title"].'\' ';
|
---|
| 60 | if(in_array("datetime", $keys))
|
---|
| 61 | $datetime = 'predicates:datetime \''.$arguments["datetime"].'\' ';
|
---|
| 62 | if(in_array("applications", $keys))
|
---|
| 63 | {
|
---|
| 64 | foreach($arguments["applications"] as $application)
|
---|
| 65 | {
|
---|
| 66 | $applications = $applications . 'predicates:has_application \'' . $application . '\' ';
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | if(in_array("surveys", $keys))
|
---|
| 70 | {
|
---|
| 71 | foreach($arguments["surveys"] as $survey)
|
---|
| 72 | {
|
---|
| 73 | $surveys = $surveys . 'predicates:has_survey \'' . $survey . '\' ';
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | if(in_array("answersets", $keys))
|
---|
| 77 | {
|
---|
| 78 | foreach($arguments["answersets"] as $answerset)
|
---|
| 79 | {
|
---|
| 80 | $answersets = $answersets . 'predicates:has_answerset \'' . $answerset . '\' ';
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | //Build the query string
|
---|
| 85 | $querystring = '
|
---|
| 86 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
| 87 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
| 88 | SELECT DISTINCT ?uid, ?title, ?datetime
|
---|
| 89 | WHERE
|
---|
| 90 | {
|
---|
| 91 | _session predicates:resource_type resources:session ;
|
---|
| 92 | predicates:uid ?uid ;
|
---|
| 93 | predicates:title ?title ;
|
---|
| 94 | ' . $uid . $title . $datetime . $applications . $surveys . $answersets . '
|
---|
| 95 | }';
|
---|
| 96 | //Query the model
|
---|
| 97 | $results = $this->model->sparqlQuery($querystring);
|
---|
| 98 |
|
---|
| 99 | $sessions = array();
|
---|
| 100 | if(!empty($results))
|
---|
| 101 | {
|
---|
| 102 | $this->db = new DatabaseInterface();
|
---|
| 103 | foreach($results as $result)
|
---|
| 104 | {
|
---|
| 105 | $pipeline = $this->getPipeline($result['?uid']->label);
|
---|
| 106 | $answersets = $this->getAnswerSets($result['?uid']->label);
|
---|
| 107 | $datetime = new DateTime();
|
---|
| 108 | $datetime->setTimestamp(intval($result['?datetime']));
|
---|
| 109 | $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $datetime, $pipeline, $answersets);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | return $sessions;
|
---|
| 113 | }
|
---|
| 114 | /**
|
---|
| 115 | * function getPipeline
|
---|
| 116 | * Get the application and survey objects of the Session in order
|
---|
| 117 | */
|
---|
| 118 | private function getPipeline($uid)
|
---|
| 119 | {
|
---|
| 120 | $querystring = '
|
---|
| 121 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE .'>
|
---|
| 122 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE .'>
|
---|
| 123 | SELECT DISTINCT ?object
|
---|
| 124 | WHERE
|
---|
| 125 | {
|
---|
| 126 | _session predicates:resource_type resources:session .
|
---|
| 127 | ?x predicates:uid \'' . $uid . '\' .
|
---|
| 128 | { ?x predicates:has_application ?object .}
|
---|
| 129 | UNION
|
---|
| 130 | { ?x predicates:has_survey ?object .}
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | }';
|
---|
| 134 | $result = $this->model->sparqlQuery($querystring);
|
---|
| 135 | print_r($result);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * function getAnswerSets
|
---|
| 140 | * Get the answersets belonging to the given session
|
---|
| 141 | */
|
---|
| 142 | private function getAnswerSets($uid)
|
---|
| 143 | {
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Save the given Session object in the file.
|
---|
| 148 | * @param type $rToolObjects
|
---|
| 149 | */
|
---|
| 150 | public function set($rToolObject)
|
---|
| 151 | {
|
---|
| 152 | $this->load();
|
---|
| 153 | $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
|
---|
| 154 | //Remove the old value stored with the given id
|
---|
| 155 | $this->model->subtract($this->model->find($resourceSession, null, null));
|
---|
| 156 |
|
---|
| 157 | //Set the new values
|
---|
| 158 | $resourceSessionType = new Resource(SESSION);
|
---|
| 159 | $predicateType = new Resource(RTYPE);
|
---|
| 160 | $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
|
---|
| 161 |
|
---|
| 162 | $sessionId = new Literal($rToolObject->uid);
|
---|
| 163 | $predicateId = new Resource(UID);
|
---|
| 164 | $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
|
---|
| 165 |
|
---|
| 166 | $sessionTitle = new Literal($rToolObject->title);
|
---|
| 167 | $predicateTitle = new Resource(TITLE);
|
---|
| 168 | $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
|
---|
| 169 |
|
---|
| 170 | $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp());
|
---|
| 171 | $predicateTimestamp = new Resource(DATETIME);
|
---|
| 172 | $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
|
---|
| 173 |
|
---|
| 174 | if(isset($rToolObject->pipeline))
|
---|
| 175 | {
|
---|
| 176 | foreach($rToolObject->pipeline as $element)
|
---|
| 177 | {
|
---|
| 178 | $sessionObject = new Literal($element->uid);
|
---|
| 179 | $predicateObject = null;
|
---|
| 180 | if(get_class($element) == "Application")
|
---|
| 181 | $predicateObject = new Resource(HAS_APPLICATION);
|
---|
| 182 | else if(get_class($element) == "Survey")
|
---|
| 183 | $predicateObject = new Resource(HAS_SURVEY);
|
---|
| 184 | if(isset($predicateObject))
|
---|
| 185 | $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | if(isset($rToolObjects->answerSets))
|
---|
| 190 | {
|
---|
| 191 | foreach($rToolObject->answersets as $answerset)
|
---|
| 192 | {
|
---|
| 193 | $sessionAnswerSet = new Literal($answerset->uid);
|
---|
| 194 | $predicateAnswerSet = new Resource(HAS_ANSWERSET);
|
---|
| 195 | $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | $this->save();
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | }
|
---|
| 205 | ?>
|
---|
| 206 |
|
---|