[312] | 1 | <?php |
---|
| 2 | require_once 'rdfConstants.php'; |
---|
| 3 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); |
---|
| 4 | /** |
---|
| 5 | * Description of SurveyInstance |
---|
| 6 | * |
---|
| 7 | * @author jkraaijeveld |
---|
| 8 | */ |
---|
| 9 | class SurveyInstance extends ResearchToolObject{ |
---|
| 10 | public static $filename = 'data/surveys/surveyinstances.rdf'; |
---|
| 11 | |
---|
| 12 | public $survey; |
---|
| 13 | public $starttime; |
---|
| 14 | public $endtime; |
---|
| 15 | public $open; |
---|
| 16 | public $presetanswers; |
---|
| 17 | public $answersets; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * Constructor for a SurveyInstance object |
---|
| 21 | * @param type $uid: The UID of the survey instance object. |
---|
| 22 | * @param type $starttime: The starttime of the survey round. |
---|
| 23 | * @param type $endtime: The endtime of the survey round. |
---|
| 24 | * @param type $open: A boolean to specify whether the survey is open. |
---|
| 25 | * @param type $presetanswers: A list of answers which are preset for everyone doing the survey. |
---|
| 26 | * @param type $answersets: A list of answersets. |
---|
| 27 | */ |
---|
| 28 | public function __construct($uid = null, $survey = null, $starttime = null, $endtime = null, $open = null, $presetanswers = null, $answersets = null) |
---|
| 29 | { |
---|
| 30 | if(!isset($uid)) |
---|
| 31 | { |
---|
| 32 | $uid = md5(uniqid(rand(), true)); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | $this->uid = $uid; |
---|
| 36 | $this->survey = $survey; |
---|
| 37 | $this->starttime = $starttime; |
---|
| 38 | $this->endtime = $endtime; |
---|
| 39 | $this->open = $open; |
---|
| 40 | $this->presetanswers = $presetanswers; |
---|
| 41 | $this->answersets = $answersets; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * function evaluate() |
---|
| 47 | * Evaluates the references in this object |
---|
| 48 | */ |
---|
| 49 | public function evaluate() |
---|
| 50 | { |
---|
| 51 | if(is_string($this->survey)) |
---|
| 52 | { |
---|
| 53 | $result = Survey::get(array("uid" => $this->survey)); |
---|
| 54 | if(!isset($result[0])) |
---|
| 55 | { |
---|
| 56 | return false; |
---|
| 57 | } |
---|
| 58 | $this->survey = $result[0]; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /* Check if all preset answers exist */ |
---|
| 62 | if(!empty($this->presetanswers) && is_string($this->presetanswers[0])) |
---|
| 63 | { |
---|
| 64 | $newPresets = array(); |
---|
| 65 | foreach($this->presetanswers as $answer) |
---|
| 66 | { |
---|
| 67 | $result = Answer::get(array("uid" => $answer)); |
---|
| 68 | if(!isset($result[0])) |
---|
| 69 | return false; |
---|
| 70 | $newPresets[] = $result[0]; |
---|
| 71 | } |
---|
| 72 | $this->presetanswers = $newPresets; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /* Check if all answersets exist */ |
---|
| 76 | if(!empty($this->answersets) && is_string($this->answersets[0])) |
---|
| 77 | { |
---|
| 78 | $newAnswerSets = array(); |
---|
| 79 | foreach($this->answersets as $answerSet) |
---|
| 80 | { |
---|
| 81 | $result = AnswerSet::get(array("uid" => $answerSet)); |
---|
| 82 | if(!isset($result[0])) |
---|
| 83 | { |
---|
| 84 | return false; |
---|
| 85 | } |
---|
| 86 | $newAnswerSets[] = $result[0]; |
---|
| 87 | } |
---|
| 88 | $this->answersets = $newAnswerSets; |
---|
| 89 | } |
---|
| 90 | return true; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * function save() |
---|
| 95 | * Saves the current object into the database. |
---|
| 96 | */ |
---|
| 97 | public function save() |
---|
| 98 | { |
---|
| 99 | //If evaluation fails, some references are incorrect. We shouldn't save in this case. |
---|
| 100 | //TODO: Decide how to fix invalid references graciously. |
---|
| 101 | if(!$this->evaluate()) |
---|
| 102 | throw new Exception('Evaluation failed.'); |
---|
| 103 | |
---|
| 104 | //Ensure the required folder exists. |
---|
| 105 | if(!is_dir('data/surveys/')) |
---|
| 106 | mkdir('data/surveys/'); |
---|
| 107 | |
---|
| 108 | $model = ResearchToolObject::load(SurveyInstance::$filename); |
---|
| 109 | |
---|
| 110 | $resourceSI = new Resource(SURVEYINSTANCE . '/' . $this->uid); |
---|
| 111 | //Remove the old value stored with the given id |
---|
| 112 | $model->subtract($model->find($resourceSI, null, null)); |
---|
| 113 | |
---|
| 114 | //Set the new values |
---|
| 115 | $resourceSIType = new Resource(SURVEYINSTANCE); |
---|
| 116 | $predicateRType = new Resource(RTYPE); |
---|
| 117 | $model->add(new Statement($resourceSI, $predicateRType, $resourceSIType)); |
---|
| 118 | |
---|
| 119 | $SIID = new Literal($this->uid); |
---|
| 120 | $predicateId = new Resource(UID); |
---|
| 121 | $model->add(new Statement($resourceSI, $predicateId, $SIID)); |
---|
| 122 | |
---|
| 123 | $SISurvey = new Resource(SURVEY . '/' . $this->survey->uid); |
---|
| 124 | $predicateSurvey = new Resource(OF_SURVEY); |
---|
| 125 | $model->add(new Statement($resourceSI, $predicateSurvey, $SISurvey)); |
---|
| 126 | |
---|
| 127 | $SIStartTime = new Literal($this->starttime->getTimestamp()); |
---|
| 128 | $predicateStartTime = new Resource(STARTTIME); |
---|
| 129 | $model->add(new Statement($resourceSI, $predicateStartTime, $SIStartTime)); |
---|
| 130 | |
---|
| 131 | $SIEndTime = new Literal($this->endtime->getTimestamp()); |
---|
| 132 | $predicateEndTime = new Resource(ENDTIME); |
---|
| 133 | $model->add(new Statement($resourceSI, $predicateEndTime, $SIEndTime)); |
---|
| 134 | |
---|
| 135 | $SIOpen = new Literal((bool) $this->open); |
---|
| 136 | $predicateOpen = new Resource(OPEN); |
---|
| 137 | $model->add(new Statement($resourceSI, $predicateOpen, $SIOpen)); |
---|
| 138 | |
---|
| 139 | if(isset($this->presetanswers)) |
---|
| 140 | { |
---|
| 141 | foreach($this->presetanswers as $answer) |
---|
| 142 | { |
---|
| 143 | $answerId = new Resource(ANSWER . '/' . $answer->uid); |
---|
| 144 | $predicateAnswer = new Resource(PRESET_ANSWER); |
---|
| 145 | $model->add(new Statement($resourceSI, $predicateAnswer, $answerId)); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | if(isset($this->answersets)) |
---|
| 150 | { |
---|
| 151 | foreach($this->answersets as $answer) |
---|
| 152 | { |
---|
| 153 | $answerSetId = new Resource(ANSWERSET . '/' . $answer->uid); |
---|
| 154 | $predicateAnswerSet = new Resource(HAS_ANSWERSET); |
---|
| 155 | $model->add(new Statement($resourceSI, $predicateAnswerSet, $answerSetId)); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | $model->saveAs(SurveyInstance::$filename, 'rdf'); |
---|
| 159 | return true; |
---|
| 160 | } |
---|
| 161 | /** |
---|
| 162 | * function get($arguments) |
---|
| 163 | * Gets the array of SurveyInstance objects belonging tothe arguments supplied. |
---|
| 164 | * @param type $arguments: An array containing zero or more of the following keys: |
---|
| 165 | * 'uid', 'of_survey', 'starttime', 'endtime', 'open', 'preset_answers', 'has_answerset' |
---|
| 166 | */ |
---|
| 167 | public static function get($arguments) |
---|
| 168 | { |
---|
| 169 | $model = ResearchToolObject::load(SurveyInstance::$filename); |
---|
| 170 | //Build the query string |
---|
| 171 | $querystring = ' |
---|
| 172 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 173 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 174 | SELECT DISTINCT ?uid, ?of_survey, ?starttime, ?endtime, ?open |
---|
| 175 | WHERE |
---|
| 176 | { |
---|
| 177 | _surveyinstance predicates:resource_type resources:surveyinstance; |
---|
| 178 | predicates:uid ?uid ; |
---|
| 179 | predicates:of_survey ?of_survey ; |
---|
| 180 | predicates:starttime ?starttime ; |
---|
| 181 | predicates:endtime ?endtime ; |
---|
| 182 | predicates:open ?open ; |
---|
| 183 | ' . ResearchToolObject::createArguments($arguments) . ' |
---|
| 184 | }'; |
---|
| 185 | //Query the model |
---|
| 186 | $results = $model->sparqlQuery($querystring); |
---|
| 187 | //Create the corresponding SurveyInstance objects |
---|
| 188 | $sInstances = array(); |
---|
| 189 | if(!empty($results)) |
---|
| 190 | { |
---|
| 191 | foreach($results as $result) |
---|
| 192 | { |
---|
| 193 | $startTime = new DateTime(); |
---|
| 194 | $startTime->setTimestamp(intval($result['?starttime']->label)); |
---|
| 195 | $endTime = new DateTime(); |
---|
| 196 | $endTime->setTimestamp(intval($result['?endtime']->label)); |
---|
| 197 | $open = (bool) $result['?open']; |
---|
| 198 | $presetAnswers = SurveyInstance::getPresetAnswers($model, $result['?uid']->label); |
---|
| 199 | $answerSets = SurveyInstance::getAnswerSets($model, $result['?uid']->label); |
---|
| 200 | $sInstances[] = new SurveyInstance($result['?uid']->label, $result['?of_survey']->uri, $startTime, $endTime, $open, $presetAnswers, $answerSets); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | return $sInstances; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * function getFields() |
---|
| 208 | * @param type $model : The RDF model to query against. |
---|
| 209 | * @param type $uid : The UID of the SurveyInstance for which the preset answers and answersets should be retrieved. |
---|
| 210 | * returns : an array with [0] => all preset answers and [1] => all answersets. |
---|
| 211 | */ |
---|
| 212 | private static function getPresetAnswers($model, $uid) |
---|
| 213 | { |
---|
| 214 | $result = $model->findRegex("[(".$uid.")]", "[(preset_answer)|(has_answerset)]", null); |
---|
| 215 | $iterator = $result->getStatementIterator(); |
---|
| 216 | $answers = array(); |
---|
| 217 | $sets = array(); |
---|
| 218 | while($iterator->hasNext()) |
---|
| 219 | { |
---|
| 220 | $element = $iterator->next(); |
---|
| 221 | if($element->getLabelPredicate() == PRESET_ANSWER) |
---|
| 222 | $answers[] = $element->getLabelObject(); |
---|
| 223 | else if($element->getLabelPredicate() == HAS_ANSWERSET) |
---|
| 224 | $sets[] = $element->getLabelObject(); |
---|
| 225 | } |
---|
| 226 | return array($answers, $sets); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * function getAnswerSets() |
---|
| 231 | * @param type $model : The RDF model to query against. |
---|
| 232 | * @param type $uid : The UID of the SurveyInstance for which the preset answers should be retrieved. |
---|
| 233 | */ |
---|
| 234 | private static function getAnswerSets($model, $uid) |
---|
| 235 | { |
---|
| 236 | $result = $model->findRegex("[(".$uid.")]", "[(has_answerset)]", null); |
---|
| 237 | $iterator = $result->getStatementIterator(); |
---|
| 238 | $answersets = array(); |
---|
| 239 | while($iterator->hasNext()) |
---|
| 240 | { |
---|
| 241 | $element = $iterator->next(); |
---|
| 242 | $answersets[] = $element->getLabelObject(); |
---|
| 243 | } |
---|
| 244 | return $answersets; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * Creates a new SurveyInstance object out of the given object. |
---|
| 249 | */ |
---|
| 250 | public static function create($obj) |
---|
| 251 | { |
---|
| 252 | return new Survey($obj->uid, $obj->survey, $obj->starttime, $obj->endtime, $obj->open, $obj->presetanswers, $obj->answersets); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | ?> |
---|