00001 <?php
00005 class CSERDquestionnaire
00006 {
00007 private $reviewId;
00008 private $questions;
00009 private $responses;
00010 private $refOrder;
00011
00012
00013
00014 private $responseUpdateFlags;
00015
00016 function __construct($reviewId = "") {
00017
00018 $this->populate($reviewId);
00019 }
00020
00021
00022
00023 public function populate($reviewId = "") {
00024 $CSERD_DBI = CSERDModelBase::getDBI();
00025
00026
00027 if ($reviewId != ""){
00028
00029
00030 if (!is_numeric($reviewId)){
00031 print("Attempt to call populate() with reviewId '$reviewId' failed: "
00032 . "reviewId is non-numeric");
00033 return false;
00034 }
00035
00036
00037
00038
00039
00040 $query = "SELECT review.reviewId FROM review "
00041 . "WHERE reviewId = '" . $reviewId . "' LIMIT 1;";
00042
00043
00044
00045 $result = $CSERD_DBI->query($query);
00046 if ($result === false) return false;
00047
00048
00049 if (count($result) != 1) {
00050 print("Attempt to populate Questionnaire for reviewId '$reviewId' failed:"
00051 . " no such review in the database.");
00052 return false;
00053 }
00054 }
00055
00056
00057
00058
00059 $this->questions = array();
00060 $this->responses = array();
00061 $this->refOrder = array();
00062
00063
00064 $query = "SELECT question.questionId, question.ref FROM question "
00065 . "WHERE active = '1' ORDER BY question.qOrder;";
00066
00067 $result = $CSERD_DBI->query($query);
00068 if ($result === false) return false;
00069
00070
00071 foreach ($result as $row)
00072 {
00073 $ref = $row["ref"];
00074 $qid = $row["questionId"];
00075
00076
00077 $question = new CSERDquestion(array('questionId' => (int)$qid));
00078
00079 if ($question === false)
00080 {
00081 print("Created new question for questionId $qid");
00082 print("Error Creating Question Object for questionId $qid");
00083 return false;
00084 }
00085 $this->addQuestion($question);
00086 }
00087
00088
00089
00090
00091 if ($reviewId != "" && is_numeric($reviewId)) {
00092
00093 $this->setReviewId($reviewId);
00094
00095
00096 $query = "SELECT response.responseId, question.ref FROM response "
00097 . "LEFT JOIN question ON response.questionId = question.questionId "
00098 . "WHERE reviewId='" . $reviewId . "' AND question.active='1';";
00099
00100
00101 $result = $CSERD_DBI->query($query);
00102 if ($result === false) return false;
00103
00104
00105
00106
00107 foreach ($result as $row) {
00108 $ref = $row["ref"];
00109 $rid = $row["responseId"];
00110
00111 $r = new CSERDresponse(array('responseId' => $rid));
00112 if(!$this->addResponseForRef($ref, $r)){
00113
00114 return false;
00115 }
00116 }
00117 }
00118 return true;
00119 }
00120
00121 public function addQuestion($question) {
00122
00123
00124
00125
00126
00127
00128
00129
00130 if (!$question->isValidQuestion())
00131 {
00132 $this->setError("Attempt to addQuestion() failed: question is not valid");
00133 return false;
00134 }
00135 $ref = $question->ref;
00136
00137 if (isset($this->questions[$ref]))
00138 {
00139
00140 return false;
00141 }
00142 $this->questions[$ref] = $question;
00143 $this->responses[$ref] = array();
00144
00145 array_push($this->refOrder, $ref);
00146 }
00147
00148 public function getQuestionByRef($ref) {
00149 if (!isset($this->questions[$ref]))
00150 {
00151 $this->setError("Attempt to getQuestionByRef() failed: the ref '$ref' does not exist.");
00152 return false;
00153 }
00154 return $this->questions[$ref];
00155 }
00156
00157 public function addResponseForRef($ref, $response) {
00158
00159 if (!isset($this->responses[$ref])) {
00160 print("Attempt to addResponseForRef() failed: the array of responses for ref '$ref' does not exist.");
00161 return false;
00162 }
00163 array_push($this->responses[$ref], $response);
00164 if(isset($this->responseUpdateFlags[$ref])){
00165 array_push($this->responseUpdateFlags[$ref], 1);
00166 }
00167 return true;
00168 }
00169
00170 public function getResponsesForRef($ref) {
00171
00172 if (!isset($this->responses[$ref])) {
00173 print("Attempt to getResponsesForRef() failed: the array of responses for ref '$ref' does not exist.");
00174 return false;
00175 }
00176 return $this->responses[$ref];
00177 }
00178
00179 public function getResponseByRefAndId($ref, $responseId) {
00180 if (!isset($this->responses[$ref])) {
00181 print("Attempt to getResponseByRefAndId() failed: the array of responses for ref '$ref' does not exist.");
00182 return false;
00183 }
00184 foreach($this->responses[$ref] as $response) {
00185 if ($response->getResponseId() == $responseId) {
00186 return $response;
00187 }
00188 }
00189 print("Attempt to getResponseByRefAndId() failed: a response with that Id was not found in responses for ref '$ref'.");
00190 return false;
00191 }
00192
00193 public function getRating() {
00194 $responses = $this->getResponsesForRef('usability');
00195 if ((!$responses) || (!isset($responses[0]))) return '0';
00196 return $responses[0]->response;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 public function commit() {
00206
00207
00208
00209
00210
00211
00212 if ($this->reviewId == "") {
00213
00214
00215 $questions = $this->getQuestions();
00216 foreach($questions as $ref => $question)
00217 {
00218 $question->commit();
00219 }
00220 } else {
00221
00222
00223 $responses = $this->getResponses();
00224 foreach($responses as $respRef) {
00225 foreach($respRef as $response) {
00226 $response->commit();
00227 }
00228 }
00229 }
00230
00231 return true;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 public function getXFormsModel()
00244 {
00245
00246
00247
00248
00249
00250
00251
00252 $ret = "<model>\n<instance>\n";
00253 $order = $this->getRefOrder();
00254 $questions = $this->getQuestions();
00255
00256
00257 foreach($order as $ref)
00258 {
00259
00260 $question = $questions[$ref];
00261
00262
00263
00264
00265
00266 $responses = $this->getResponsesForRef($ref);
00267
00268
00269 if ($responses === false) {
00270 print("Attempt to getXFormsModel() failed: "
00271 . "call to getResponsesByRef() failed for ref $ref");
00272 return false;
00273 }
00274
00275
00276
00277 if (count($responses) > 0) {
00278 foreach($responses as $response) {
00279
00280 $ret .= $question->getXFormsOpenTag($response->responseId)
00281 . SXMLHelper::contentToXML($response->response)
00282 . $question->getXFormsCloseTag() . "\n";
00283 }
00284 } else {
00285
00286
00287 $ret .= $question->getXFormsOpenCloseTag() . "\n";
00288 }
00289 }
00290 $ret .= "</instance>\n</model>\n";
00291 return $ret;
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 public function setXFormsModel($xml) {
00306
00307
00308
00309
00310 if ($this->getReviewId() == "" || !is_numeric($this->getReviewId())) {
00311 $this->setError("Attempt to call setXFormsModel() failed: "
00312 . "questionnaire object must have a reviewId");
00313 return false;
00314 }
00315
00316
00317 $x = SXMLHelper::parse($xml);
00318 if ($x === false) {
00319 $this->setError("Attempt to setXFormsModel() failed: could not parse input XML.");
00320 return false;
00321 }
00322
00323
00324 if (!isset($x->instance)){
00325 $this->setError("Attempt to setXFormsModel() failed: input XML lacks instance node.");
00326 return false;
00327 }
00328 $instance = $x->instance;
00329
00330
00331 $this->responseUpdateFlags = array();
00332
00333
00334
00335
00336 foreach($instance->children() as $ref => $value) {
00337
00338
00339 $responsesForRef = $this->getResponsesForRef($ref);
00340
00341 if ($responsesForRef === false) {
00342 print("Attempt to setXFormsModel() failed: unable to"
00343 . " get responses array for ref $ref in Questionnaire.");
00344 return false;
00345 }
00346
00347
00348
00349
00350
00351 if (!isset($this->responseUpdateFlags[$ref])) {
00352
00353 $this->responseUpdateFlags[$ref] = array();
00354 foreach($responsesForRef as $aResponse){
00355
00356 $aResponse->response = "";
00357 }
00358 }
00359
00360
00361 $numFilledForRef = count($this->responseUpdateFlags[$ref]);
00362
00363
00364
00365
00366 $numRespObjsForRef = count($responsesForRef);
00367
00368
00369
00370
00371
00372 if ($numFilledForRef < $numRespObjsForRef) {
00373
00374 $aResponse = $responsesForRef[$numFilledForRef];
00375
00376
00377
00378 $aResponse->response = $value;
00379 array_push($this->responseUpdateFlags[$ref], true);
00380
00381 } else {
00382
00383
00384
00385
00386 $q = $this->getQuestionByRef($ref);
00387 if ($q === false) {
00388 print("Attempt to setXFormsModel() failed: unable"
00389 . " to getQuestionByRef $ref in Questionnaire.");
00390 return false;
00391 }
00392
00393
00394 $r = new CSERDresponse();
00395 $r->reviewId = $this->reviewId;
00396 $r->questionId = $q->questionId;
00397 $r->response = $value;
00398
00399
00400 $this->addResponseForRef($ref, $r);
00401 }
00402
00403 }
00404
00405
00406 return true;
00407 }
00408
00409
00410
00411
00412
00413 protected function getQuestions() {
00414 return $this->questions;
00415 }
00416
00417 protected function getRefOrder() {
00418 return $this->refOrder;
00419 }
00420
00421 protected function getResponses() {
00422 return $this->responses;
00423 }
00424
00425 public function getReviewId() {
00426 return $this->reviewId;
00427 }
00428
00429 protected function setReviewId($reviewId) {
00430 $this->reviewId = $reviewId;
00431 }
00432
00433
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 ?>