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