Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
online_quiz_submission.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
29 {
30 
31 
38  function Online_Quiz_Submission($assetid=0)
39  {
40  $this->_ser_attrs = TRUE;
41  return $this->Asset($assetid);
42 
43  }//end constructor
44 
45 
56  function saveAttributes($dont_run_updated=FALSE)
57  {
58  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
59  $val = parent::saveAttributes($dont_run_updated);
60  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
61 
62  return $val;
63 
64  }//end saveAttributes()
65 
66 
73  function getIP()
74  {
75  $a = $this->attr('attributes');
76  if (isset($a['misc']['ip'])) return $a['misc']['ip'];
77  return FALSE;
78 
79  }//end getIP()
80 
81 
90  function setIP($ip)
91  {
92  $a = $this->attr('attributes');
93  if (empty($a['misc'])) $a['misc'] = Array();
94 
95  $a['misc']['ip'] = $ip;
96 
97  if (!$this->setAttrValue('attributes', $a)) {
98  return FALSE;
99  }
100 
101  return TRUE;
102 
103  }//end setIP()
104 
105 
112  function getScore()
113  {
114  $a = $this->attr('attributes');
115  if (isset($a['misc']['score'])) {
116  return $a['misc']['score'];
117  }
118 
119  return FALSE;
120 
121  }//end getScore()
122 
123 
132  function setScore($score)
133  {
134  $a = $this->attr('attributes');
135  if (empty($a['misc'])) $a['misc'] = Array();
136 
137  $a['misc']['score'] = $score;
138 
139  if (!$this->setAttrValue('attributes', $a)) {
140  return FALSE;
141  }
142 
143  return TRUE;
144 
145  }//end setScore()
146 
147 
156  function getUserDetailsAnswer($asset_id)
157  {
158  $user_details_answers = $this->getUserDetailsAnswers();
159  $user_details_answer = NULL;
160 
161  if (isset($user_details_answers[$asset_id]['answer'])) {
162  $user_details_answer = $user_details_answers[$asset_id]['answer'];
163  }
164 
165  return $user_details_answer;
166 
167  }//end getUserDetailsAnswer()
168 
169 
179  function setUserDetailsAnswer($asset_id, $answer)
180  {
181  $user_details_answers = $this->getUserDetailsAnswers();
182  if (empty($user_details_answers)) {
183  $user_details_answers = Array();
184  }
185 
186  $user_details_answers[$asset_id]['answer'] = $answer;
187  $result = $this->setAttrValue('user_details_answers', $user_details_answers);
188 
189  return $result;
190 
191  }//end setUserDetailsAnswer()
192 
193 
202  function getQuizAnswer($asset_id)
203  {
204  $quiz_answers = $this->getQuizAnswers();
205  $quiz_answer = NULL;
206 
207  if (isset($quiz_answers[$asset_id]['answer'])) {
208  $quiz_answer = $quiz_answers[$asset_id]['answer'];
209  }
210 
211  return $quiz_answer;
212 
213  }//end getQuizAnswer()
214 
215 
225  function setQuizAnswer($asset_id, $answer)
226  {
227  $quiz_answers = $this->getQuizAnswers();
228  if (empty($quiz_answers)) $quiz_answers = Array();
229 
230  $quiz_answers[$asset_id] = $answer;
231  $result = $this->setAttrValue('quiz_answers', $quiz_answers);
232 
233  return $result;
234 
235  }//end setQuizAnswer()
236 
237 
245  {
246  $user_details_answers = $this->attr('user_details_answers');
247 
248  return $user_details_answers;
249 
250  }//end getUserDetailsAnswers()
251 
252 
259  function getQuizAnswers()
260  {
261  $quiz_answers = $this->attr('quiz_answers');
262 
263  return $quiz_answers;
264 
265  }//end getQuizAnswers()
266 
267 
277  function getSummary($incl_user_details=TRUE, $incl_quiz_responses=TRUE)
278  {
279  $summary = Array();
280 
281  if ($incl_user_details){
282  $user_details_questions = $this->getUserDetailsQuestions();
283  $user_details_answers = $this->getUserDetailsAnswers();
284  foreach ($user_details_answers as $question_id => $answer_details) {
285  $summary[$question_id]['name'] = $user_details_questions[$question_id];
286  $summary[$question_id]['answer'] = $answer_details['answer'];
287  }
288  }
289 
290  if ($incl_quiz_responses){
291  $quiz_questions = $this->getQuizQuestions();
292  $quiz_answers = $this->getQuizAnswers();
293  foreach ($quiz_answers as $question_id => $answer_details) {
294  $summary[$question_id]['name'] = $quiz_questions[$question_id];
295  $summary[$question_id]['answer'] = $answer_details['summary'];
296  }
297  }
298 
299  return $summary;
300 
301  }//end getSummary()
302 
303 
313  function setUserDetailsQuestion($asset_id, $question_name)
314  {
315  $user_details_questions = $this->getUserDetailsQuestions();
316  if (empty($user_details_questions)) {
317  $user_details_questions = Array();
318  }
319 
320  $user_details_questions[$asset_id] = $question_name;
321  $result = $this->setAttrValue('user_details_questions', $user_details_questions);
322 
323  return $result;
324 
325  }//end setUserDetailsQuestion()
326 
327 
337  function setQuizQuestion($asset_id, $question_name)
338  {
339  $quiz_questions = $this->getQuizQuestions();
340  if (empty($quiz_questions)) $quiz_questions = Array();
341 
342  $quiz_questions[$asset_id] = $question_name;
343  $result = $this->setAttrValue('quiz_questions', $quiz_questions);
344 
345  return $result;
346 
347  }//end setQuizQuestion()
348 
349 
357  {
358  return $this->attr('user_details_questions');
359 
360  }//end getUserDetailsQuestions()
361 
362 
369  function getQuizQuestions()
370  {
371  return $this->attr('quiz_questions');
372 
373  }//end getQuizQuestions()
374 
375 
391  {
392  $res = parent::getAvailableKeywords();
393  $res['quiz_qX_name'] = 'The name of the question with ID X';
394  $res['quiz_qX_answer_value'] = 'The answer to the question with ID X';
395  $res['quiz_qX_answer_summary'] = 'The summary of the question with ID X';
396  $res['quiz_qX_answer_awarded'] = 'The points awarded of the question with ID X';
397  $res['quiz_qX_answer_available'] = 'The points available of the question with ID X';
398  $res['user_details_X:qY_question'] = 'The user deatil form question with asset ID X:qY';
399  $res['user_details_X:qY_answer'] = 'The user deatil form answer with question asset ID X:qY';
400  $res['quiz_qX_answer_available'] = 'The points available of the question with ID X';
401  $res['user_ip_address'] = 'IP address that created this submission';
402  $res['user_details'] = 'User details summary, blank if User Details form not used';
403  $res['user_asset_id'] = 'The User who created the Submission,';
404  $res['quiz_responses'] = 'Summary of responses nad points awarded and maximum points per answer';
405  $res['quiz_total'] = 'Total points awarded and available';
406  $res['quiz_score'] = 'Quiz Score';
407  return $res;
408 
409  }//end getAvailableKeywords()
410 
411 
426  function getKeywordReplacement($keyword)
427  {
428 
429  $matches = Array();
430  if (preg_match('/quiz\_q([0-9]+)\_name/', $keyword, $matches)) {
431  $attrs = $this->attr('quiz_questions');
432  foreach ($attrs as $id => $value) {
433  if ($id == $matches[1]) {
434  return $value;
435  }
436  }
437  return '';
438  }
439 
440  if (preg_match('/quiz\_q([0-9]+)\_answer_value/', $keyword, $matches)) {
441  $attrs = $this->attr('quiz_answers');
442  foreach ($attrs as $id => $value) {
443  if ($id == $matches[1]) {
444  return $value['value'];
445  }
446  }
447  return '';
448  }
449 
450  if (preg_match('/quiz\_q([0-9]+)\_answer_summary/', $keyword, $matches)) {
451  $attrs = $this->attr('quiz_answers');
452  foreach ($attrs as $id => $value) {
453  if ($id == $matches[1]) {
454  return $value['summary'];
455  }
456  }
457  return '';
458  }
459 
460  if (preg_match('/quiz\_q([0-9]+)\_points_awarded/', $keyword, $matches)) {
461  $attrs = $this->attr('quiz_answers');
462  foreach ($attrs as $id => $value) {
463  if ($id == $matches[1]) {
464  return $value['points'];
465  }
466  }
467  return '';
468  }
469 
470  if (preg_match('/quiz\_q([0-9]+)\_points_available/', $keyword, $matches)) {
471  $attrs = $this->attr('quiz_answers');
472  foreach ($attrs as $id => $value) {
473  if ($id == $matches[1]) {
474  return $value['available_points'];
475  }
476  }
477  return '';
478  }
479 
480  if (preg_match('/user_details\_([0-9]+:q[0-9]+)\_question/', $keyword, $matches)) {
481 
482  $attrs = $this->attr('user_details_questions');
483  foreach ($attrs as $id => $value) {
484  if ($id == $matches[1]) {
485  return $value;
486  }
487  }
488  return '';
489  }
490 
491 
492  if (preg_match('/user_details\_([0-9]+:q[0-9]+)\_answer/', $keyword, $matches)) {
493  $attrs = $this->attr('user_details_answers');
494  foreach ($attrs as $id => $value) {
495  if ($id == $matches[1]) {
496  return $value['answer'];
497  }
498  }
499  return '';
500  }
501 
502  if (preg_match('/user_ip_address/', $keyword, $matches)) {
503  return $this->getIP();
504  }
505 
506 
507  if (preg_match('/user_asset_id/', $keyword, $matches)) {
508  return $this->updated_userid;
509  }
510 
511  if (preg_match('/user_details/', $keyword, $matches)) {
512  $string = '<table border="0" cellspacing="2" cellpadding="2">'."\n";
513  $string .= $this->getSummaryUserDetails();
514  $string .="</table>\n";
515  return $string;
516  }
517 
518  if (preg_match('/quiz_responses/', $keyword, $matches)) {
519  $string = '<table border="0" cellspacing="2" cellpadding="2">'."\n";
520  $string .= $this->getSummaryQuizResponse();
521  $string .="</table>\n";
522  return $string;
523  }
524 
525  if (preg_match('/quiz_total/', $keyword, $matches)) {
526  $string = '<table border="0" cellspacing="2" cellpadding="2">'."\n";
527  $string .= $this->getSummaryPoints();
528  $string .="</table>\n";
529  return $string;
530  }
531 
532  if (preg_match('/quiz_score/', $keyword, $matches)) {
533  return $this->getScore();
534  }
535 
536  return parent::getKeywordReplacement($keyword);
537 
538  }//end getKeywordReplacement()
539 
540 
541 
548  function printBody() {
549  echo '<table border="0" cellspacing="2" cellpadding="2">'."\n";
550  echo ($this->getSummaryUserDetails());
551  echo '<tr><td><strong>User IP Address</strong></td><td>'.$this->getIP().'</td></tr>';
552  echo ($this->getSummaryQuizResponse());
553  echo ($this->getSummaryPoints());
554  echo "</table>\n";
555 
556  }//end printBody()
557 
558 
566  {
567  $user_details_questions = $this->getUserDetailsQuestions();
568  $user_details_answers = $this->getUserDetailsAnswers();
569 
570  $string = '';
571  foreach ($user_details_questions as $id => $value) {
572  $string .= "<tr>\n<td><strong>$value</strong></td>\n";
573  if(isset($user_details_answers[$id]['answer'])) {
574  $string .= "<td>".$user_details_answers[$id]['answer']."</td>";
575  }
576  $string .= "\n</tr>\n";
577  }
578 
579  if (empty($string)) {
580  return "<tr>\n<td><strong>User Asset ID</strong></td>\n<td>$this->updated_userid</td>\n</tr>\n";
581  }
582  else {
583  return $string;
584  }
585 
586  }//end getSummaryUserDetails()
587 
588 
596  {
597  $quiz_questions = $this->getQuizQuestions();
598  $quiz_answers = $this->getQuizAnswers();
599 
600  $string = '';
601  foreach ($quiz_questions as $id => $value) {
602  $string .= "<tr>\n<td><strong>$value</strong></td>\n<td>";
603  if(isset($quiz_answers[$id]['summary'])) {
604  $string .= $quiz_answers[$id]['summary'].", ";
605  }
606  if(isset($quiz_answers[$id]['points']) && isset($quiz_answers[$id]['available_points'])) {
607  $string .= " ".$quiz_answers[$id]['points']." of ".$quiz_answers[$id]['available_points']." points";
608  }
609 
610  $string .= "</td>\n</tr>\n";
611  }
612 
613  return $string;
614 
615  }//end getSummaryQuizResponse()
616 
617 
624  function getSummaryPoints()
625  {
626 
627  $quiz_answers = $this->getQuizAnswers();
628  $total_points_available = 0;
629  $total_points_awarded = 0;
630 
631  foreach ($quiz_answers as $id => $value) {
632  if(isset($value['available_points']))
633  $total_points_available += $value['available_points'];
634  if(isset($value['points']))
635  $total_points_awarded += $value['points'];
636  }
637 
638  $string = "<tr>\n<td><strong>Total points awarded</strong></td>\n<td>$total_points_awarded</td>\n</tr>\n";
639  $string .="<tr>\n<td><strong>Total points available</strong></td>\n<td>$total_points_available</td>\n</tr>\n";
640  return $string;
641 
642  }//end getSummaryPoints()
643 
644 }//end class
645 ?>