Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
online_quiz_question.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
20 
21 
22 define('SQ_ONLINE_QUIZ_QUESTION_SESSION_VAR', 'SQ_ONLINE_QUIZ');
23 
24 
42 {
43 
44 
52  var $_value = NULL;
53 
54 
60  var $_position = NULL;
61 
62 
71  function Online_Quiz_Question($assetid=0)
72  {
73  $this->Asset($assetid);
74 
75  // on question load, load up the question values for the current user
76  $this->loadQuestion();
77 
78  }//end constructor
79 
80 
90  function _getName($short_name=FALSE)
91  {
92  return $this->attr('name');
93 
94  }//end _getName()
95 
96 
104  function _getAllowedLinks()
105  {
106  $page_links = parent::_getAllowedLinks();
107  $page_links[SQ_LINK_TYPE_2]['bodycopy'] = Array('card' => 1, 'exclusive' => FALSE);
108  $page_links[SQ_LINK_TYPE_3]['bodycopy'] = Array('card' => 1, 'exclusive' => FALSE);
109  return $page_links;
110 
111  }//end _getAllowedLinks()
112 
113 
120  function loadQuestion()
121  {
122  $question = Array(
123  'type_code' => $this->type(),
124  'value' => NULL,
125  'position' => NULL,
126  );
127 
128  // load everything from the session
129  if (!empty($_SESSION[SQ_ONLINE_QUIZ_QUESTION_SESSION_VAR]['question'][$this->id])) {
130  $question = $_SESSION[SQ_ONLINE_QUIZ_QUESTION_SESSION_VAR]['question'][$this->id];
131  }
132 
133  // use the question data to set instance vars
134  $this->_value = array_get_index($question, 'value', NULL);
135  $this->_position = array_get_index($question, 'position', NULL);
136 
137  return TRUE;
138 
139  }//end loadQuestion()
140 
141 
148  function saveQuestion()
149  {
150  $question = $this->getQuestionData();
151 
152  // save everything from the session
153  $_SESSION[SQ_ONLINE_QUIZ_QUESTION_SESSION_VAR]['question'][$this->id] = $question;
154 
155  return TRUE;
156 
157  }//end saveQuestion()
158 
159 
166  function getValue()
167  {
168  return $this->_value;
169 
170  }//end getValue()
171 
172 
181  function setValue($value)
182  {
183  $this->_value = $value;
184 
185  }//end setValue()
186 
187 
194  function getSummary()
195  {
196  return $this->getValue();
197 
198  }//end getSummary()
199 
200 
207  function getCorrectValue()
208  {
209  return $this->getValue();
210 
211  }//end getCorrectValue()
212 
213 
220  function getCorrectSummary()
221  {
222  return $this->getSummary();
223 
224  }//end getCorrectSummary()
225 
226 
233  function getPosition()
234  {
235  return $this->_position;
236 
237  }//end getPosition()
238 
239 
248  function setPosition($position)
249  {
250  $this->_position = $position;
251 
252  }//end setPosition()
253 
254 
261  function getQuestionText()
262  {
263  ob_start();
264  $printed = $this->printQuestionTextBodycopy();
265  $question_text = ob_get_clean();
266 
267  if ($printed) {
268  return $question_text;
269  } else {
270  return $this->attr('question_text');
271  }
272 
273  }//end getQuestionText()
274 
275 
282  function getQuestionNote()
283  {
284  $question_note = $this->attr('question_note');
285 
286  // replace internal links to pages and images with their full URLs
287  $matches = Array();
288  preg_match_all('|\./\?a=([0-9]+)|', $question_note, $matches);
289  $urls = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($matches[1]));
290  foreach ($urls as $assetid => $url) {
291  $question_note = preg_replace('|\./\?a='.$assetid.'([^0-9])|', $url.'\\1', $question_note);
292  }
293 
294  return $question_note;
295 
296  }//end getQuestionNote()
297 
298 
305  function getQuestionData()
306  {
307  return Array(
308  'type_code' => $this->type(),
309  'value' => $this->getValue(),
310  'position' => $this->getPosition(),
311  );
312 
313  }//end getQuestionData()
314 
315 
331  {
332  // children should look at $_REQUEST, validate the result, and set $this->_value
333  $value = array_get_index($_REQUEST, $this->getPrefix().'_test', NULL);
334 
335  $valid = $this->validateValue($value);
336  $errors = array_get_index($valid, 'errors', Array());
337  if (empty($errors)) {
338  $this->_value = $value;
339  $this->saveQuestion();
340  }
341 
342  // return the error stack
343  return $valid;
344 
345  }//end processResponseForm()
346 
347 
367  function validateValue($value=NULL)
368  {
369  if (is_null($value)) $value = $this->_value;
370 
371  $errors = Array(
372  'errors' => Array(),
373  'warnings' => Array(),
374  );
375 
376  if (empty($value)) {
377  $errors['warnings'][] = translate('online_quiz_question_warning_empty_question', $this->getPosition());
378  }
379 
380  return $errors;
381 
382  }//end validateValue()
383 
384 
391  function getValidationJS()
392  {
393  return '';
394 
395  }//end getValidationJS()
396 
397 
406  function getPoints($value=NULL)
407  {
408  if (is_null($value)) $value = $this->getValue();
409 
410  return 0;
411 
412  }//end getPoints()
413 
414 
422  {
423  return 0;
424 
425  }//end getAvailablePoints()
426 
427 
445  function getResults()
446  {
447  $value = $question->getValue();
448  $summary = $question->getSummary();
449  $correct_value = $question->getCorrectValue();
450  $correct_summary = $question->getCorrectSummary();
451  $position = $question->getPosition();
452  $points = $question->getPoints();
453  $available = $question->getAvailablePoints();
454 
455  $results = Array(
456  'assetid' => $id,
457  'value' => $value,
458  'summary' => $summary,
459  'correct_value' => $correct_value,
460  'correct_summary' => $correct_summary,
461  'points' => $points,
462  'available_points' => $available,
463  );
464 
465  return $results;
466 
467  }//end getResults()
468 
469 
470 //-- Bodycopy Manipulation --//
471 
472 
483  function createQuestionTextBodycopy($enable_on_create=TRUE)
484  {
485  $bodycopy_link = $this->getQuestionTextBodycopyLink(SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3);
486 
487  // we already have a bodycopy link: bail out
488  if ($bodycopy_link) {
489  return FALSE;
490  } else {
491  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy');
492 
493  $link_type = ($enable_on_create ? SQ_LINK_TYPE_2 : SQ_LINK_TYPE_3);
494 
495  $asset = new Bodycopy();
496  $copy_link = Array(
497  'asset' => &$this,
498  'value' => 'question_text',
499  'link_type' => $link_type,
500  'is_dependant' => 1,
501  'is_exclusive' => 1,
502  );
503 
504  $asset->setAttrValue('name', translate('online_quiz_question_bodycopy_question_text'));
505  $args = Array(
506  'content' => $this->attr('question_text'),
507  );
508  if (!$asset->create($copy_link, $args)) return FALSE;
509 
510  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
511  unset($asset);
512  }
513 
514  return TRUE;
515 
516  }//end createQuestionTextBodycopy()
517 
518 
529  function &getQuestionTextBodycopy($only_if_enabled=TRUE)
530  {
531  $asset = NULL;
532 
533  if ($only_if_enabled) {
534  $link_types = SQ_LINK_TYPE_2;
535  } else {
536  $link_types = SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3;
537  }
538 
539  $bodycopy_link = $this->getQuestionTextBodycopyLink($link_types);
540  if ($bodycopy_link) {
541  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($bodycopy_link['minorid'], 'bodycopy');
542  }
543 
544  return $asset;
545 
546  }//end getQuestionTextBodycopy()
547 
548 
557  function getQuestionTextBodycopyLink($link_type)
558  {
559  $tmp_bodycopy = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, $link_type, 'bodycopy', FALSE, 'major', 'question_text');
560  return reset($tmp_bodycopy);
561 
562  }//end getQuestionTextBodycopyLink()
563 
564 
573  function setQuestionTextBodycopy($enabled)
574  {
575  $bodycopy_link = $this->getQuestionTextBodycopyLink(SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3);
576 
577  if (!$bodycopy_link) {
578  // no bodycopy yet? If we're trying to set to disabled, then we don't
579  // need to do anything - if not then we need to create it
580  if ($enabled) {
581  if (!$this->createQuestionTextBodycopy()) return FALSE;
582  }
583  } else {
584  // set link type to either TYPE_2 if enabled or TYPE_3 if disabled
585  $new_link_type = ($enabled) ? SQ_LINK_TYPE_2 : SQ_LINK_TYPE_3;
586  if ($bodycopy_link['link_type'] != $new_link_type) {
587  $GLOBALS['SQ_SYSTEM']->am->updateLink($bodycopy_link['linkid'], $new_link_type);
588  }
589  }
590 
591  return TRUE;
592 
593  }//end setQuestionTextBodycopy()
594 
595 
603  {
604  $bodycopy_printed = FALSE;
605  $bodycopy = $this->getQuestionTextBodycopy();
606 
607  if (!is_null($bodycopy)) {
608  $bodycopy->printBody();
609  $bodycopy_printed = TRUE;
610  }
611 
612  return $bodycopy_printed;
613 
614  }//end printQuestionTextBodycopy()
615 
616 
617 //-- Keyword Replacements --//
618 
619 
634  {
635  $keywords = parent::getAvailableKeywords();
636 
637  $keywords['question_text'] = translate('online_quiz_question_keyword_question_text');
638  $keywords['response_form'] = translate('online_quiz_question_keyword_response_form');
639  $keywords['available_points'] = translate('online_quiz_question_keyword_available_points');
640  $keywords['question_number'] = translate('online_quiz_question_keyword_question_number');
641  $keywords['question_note'] = translate('online_quiz_question_keyword_question_note');
642 
643  return $keywords;
644 
645  }//end getAvailableKeywords()
646 
647 
655  {
656  return $this->getQuestionText();
657 
658  }//end getQuestionTextKeywordReplacement()
659 
660 
670  {
671  // print form element(s), eg. a text box, or list of checkboxes
672  // $this->getPrefix should be used for any form elements
673 
674  $prefix = $this->getPrefix();
675 
676  // example element
677  // <input id="PREFIX_test" name="PREFIX_test" type="text" value="online_quiz_question::getValue()">
678 
679  return '';
680 
681  }//end getResponseFormKeywordReplacement()
682 
683 
691  {
692  return $this->getAvailablePoints();
693 
694  }//end getAvailablePointsKeywordReplacement()
695 
696 
704  {
705  return $this->getPosition();
706 
707  }//end getQuestionNumberKeywordReplacement()
708 
709 
717  {
718  return $this->getQuestionNote();
719 
720  }//end getQuestionNoteKeywordReplacement()
721 
722 
723 }//end class
724 ?>