[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 | */
|
---|
[171] | 12 | class SessionConnector extends Connector
|
---|
[149] | 13 | {
|
---|
| 14 | protected $db;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Constructor for the SessionConnector
|
---|
| 18 | */
|
---|
| 19 | public function __construct()
|
---|
| 20 | {
|
---|
[171] | 21 | $this->fileName = 'data/sessions/sessions.rdf';
|
---|
[149] | 22 | //Ensure the required folder for this connector exists
|
---|
| 23 | if(!is_dir('data/sessions'))
|
---|
| 24 | mkdir('data/sessions');
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
[158] | 28 | * function get()
|
---|
| 29 | * @param type $arguments : An array containing one or more of the following elements:
|
---|
[195] | 30 | * 'uid', 'title', 'creationdate', 'applications', 'surveys'
|
---|
[149] | 31 | */
|
---|
| 32 | public function get($arguments)
|
---|
| 33 | {
|
---|
| 34 | $this->load();
|
---|
| 35 |
|
---|
| 36 | //Build the query string
|
---|
| 37 | $querystring = '
|
---|
| 38 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
|
---|
| 39 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
|
---|
[195] | 40 | SELECT DISTINCT ?uid, ?title, ?creator, ?creationdate
|
---|
[149] | 41 | WHERE
|
---|
| 42 | {
|
---|
| 43 | _session predicates:resource_type resources:session ;
|
---|
| 44 | predicates:uid ?uid ;
|
---|
| 45 | predicates:title ?title ;
|
---|
[159] | 46 | predicates:creator ?creator ;
|
---|
[195] | 47 | predicates:creationdate ?creationdate ;
|
---|
[171] | 48 | ' . $this->createArguments($arguments) . '
|
---|
[149] | 49 | }';
|
---|
| 50 | //Query the model
|
---|
| 51 | $results = $this->model->sparqlQuery($querystring);
|
---|
| 52 | $sessions = array();
|
---|
| 53 | if(!empty($results))
|
---|
| 54 | {
|
---|
| 55 | $this->db = new DatabaseInterface();
|
---|
| 56 | foreach($results as $result)
|
---|
| 57 | {
|
---|
| 58 | //Create a session object out of every result, get all required fields as well.
|
---|
| 59 | $pipeline = $this->getPipeline($result['?uid']->label);
|
---|
[186] | 60 | $creator = $result['?creator']->label;
|
---|
[205] | 61 | $creationdate = new DateTime();
|
---|
| 62 | $creationdate->setTimestamp(intval($result['?creationdate']->label));
|
---|
| 63 | $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $creationdate, $pipeline);
|
---|
[149] | 64 | }
|
---|
| 65 | }
|
---|
| 66 | return $sessions;
|
---|
| 67 | }
|
---|
| 68 | /**
|
---|
[158] | 69 | * function getPipeline()
|
---|
| 70 | * param type $uid : The Session uid for which the pipeline should be retrieved.
|
---|
[149] | 71 | */
|
---|
| 72 | private function getPipeline($uid)
|
---|
| 73 | {
|
---|
| 74 | $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null);
|
---|
| 75 | $iterator = $result->getStatementIterator();
|
---|
| 76 | $pipeline = array();
|
---|
| 77 | while($iterator->hasNext())
|
---|
| 78 | {
|
---|
| 79 | $element = $iterator->next();
|
---|
[186] | 80 | $pipeline[] = $element->getLabelObject();
|
---|
[149] | 81 | }
|
---|
| 82 | return $pipeline;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
[158] | 86 | * function getAnswerSets()
|
---|
| 87 | * @param type $uid : The Session uid for which the answerSets should be retrieved.
|
---|
[149] | 88 | */
|
---|
| 89 | private function getAnswerSets($uid)
|
---|
| 90 | {
|
---|
| 91 | $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null);
|
---|
| 92 | $iterator = $result->getStatementIterator();
|
---|
| 93 | $answersets = array();
|
---|
| 94 | while($iterator->hasNext())
|
---|
| 95 | {
|
---|
| 96 | $element = $iterator->next();
|
---|
[186] | 97 | $answersets[] = $element->getLabelObject();
|
---|
[149] | 98 | }
|
---|
| 99 | return $answersets;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
[158] | 103 | * function set()
|
---|
| 104 | * @param type $rToolObjects : The ResearchToolObject to be saved.
|
---|
[149] | 105 | */
|
---|
| 106 | public function set($rToolObject)
|
---|
| 107 | {
|
---|
| 108 | $this->load();
|
---|
[186] | 109 | //If evaluation fails, some references are incorrect.
|
---|
| 110 | //We shouldn't save in this case. Instead - let the user know. This function returns false if the evaluation has failed.
|
---|
| 111 | //TODO: Decide how to fix invalid references graciously.
|
---|
| 112 | if(!$rToolObject->evaluate())
|
---|
| 113 | return false;
|
---|
| 114 |
|
---|
[149] | 115 | $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid);
|
---|
| 116 | //Remove the old value stored with the given id
|
---|
| 117 | $this->model->subtract($this->model->find($resourceSession, null, null));
|
---|
| 118 |
|
---|
| 119 | //Set the new values
|
---|
| 120 | $resourceSessionType = new Resource(SESSION);
|
---|
| 121 | $predicateType = new Resource(RTYPE);
|
---|
| 122 | $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType));
|
---|
| 123 |
|
---|
| 124 | $sessionId = new Literal($rToolObject->uid);
|
---|
| 125 | $predicateId = new Resource(UID);
|
---|
| 126 | $this->model->add(new Statement($resourceSession, $predicateId, $sessionId));
|
---|
| 127 |
|
---|
| 128 | $sessionTitle = new Literal($rToolObject->title);
|
---|
| 129 | $predicateTitle = new Resource(TITLE);
|
---|
| 130 | $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle));
|
---|
| 131 |
|
---|
[159] | 132 | $sessionCreator = new Literal($rToolObject->creator->uid);
|
---|
| 133 | $predicateCreator = new Resource(CREATOR);
|
---|
| 134 | $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator));
|
---|
| 135 |
|
---|
[205] | 136 | $sessionTimestamp = new Literal($rToolObject->creationdate->getTimestamp());
|
---|
[157] | 137 | // $sessionTimestamp = new Literal($rToolObject->datetime); // Edit of above function, allows for creation of session, but still results in errors...
|
---|
[205] | 138 | $predicateTimestamp = new Resource(CREATIONDATE);
|
---|
[149] | 139 | $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp));
|
---|
| 140 |
|
---|
| 141 | //Create a sequence to store the different applications and surveys.
|
---|
| 142 | if(isset($rToolObject->pipeline))
|
---|
| 143 | {
|
---|
| 144 | foreach($rToolObject->pipeline as $element)
|
---|
| 145 | {
|
---|
| 146 | $sessionObject = new Literal($element->uid);
|
---|
| 147 | $predicateObject = null;
|
---|
| 148 | if(get_class($element) == "Application")
|
---|
| 149 | $predicateObject = new Resource(HAS_APPLICATION);
|
---|
| 150 | else if(get_class($element) == "Survey")
|
---|
| 151 | $predicateObject = new Resource(HAS_SURVEY);
|
---|
| 152 | if(isset($predicateObject))
|
---|
| 153 | $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject));
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | if(isset($rToolObject->answersets))
|
---|
| 158 | {
|
---|
| 159 | foreach($rToolObject->answersets as $answerset)
|
---|
| 160 | {
|
---|
| 161 | $sessionAnswerSet = new Literal($answerset->uid);
|
---|
| 162 | $predicateAnswerSet = new Resource(HAS_ANSWERSET);
|
---|
| 163 | $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet));
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | $this->save();
|
---|
[186] | 168 | return true;
|
---|
[149] | 169 | }
|
---|
| 170 | }
|
---|
| 171 | ?>
|
---|
| 172 |
|
---|