18 require_once SQ_PACKAGES_PATH.
'/cms/page_templates/page_online_quiz/online_quiz_question/online_quiz_question.inc';
19 require_once SQ_LIB_PATH.
'/html_form/html_form.inc';
52 $this->_ser_attrs = TRUE;
70 case 'max_option_selections':
71 if ($value < 1) $value = 1;
74 return parent::setAttrValue($name, $value);
97 switch ($this->
attr(
'option_style')) {
99 if ($this->
attr(
'max_option_selections') <= 1) {
100 $value = array_get_index($_REQUEST, $prefix.
'_response', NULL);
105 $value = array_get_index($_REQUEST, $prefix.
'_response', Array());
110 if ($this->
attr(
'max_option_selections') <= 1) {
111 $value = array_get_index($_REQUEST, $prefix.
'_response', NULL);
116 $value = array_keys(array_get_index($_REQUEST, $prefix.
'_response', Array()));
122 $errors = array_get_index($valid,
'errors', Array());
123 if (empty($errors)) {
124 $this->_value = $value;
155 if (is_null($value)) $value = $this->_value;
159 'warnings' => Array(),
163 $errors[
'warnings'][] = translate(
'online_quiz_question_warning_empty_question', $this->
getPosition());
179 static $printed_fn = FALSE;
189 function <?php echo $this->
type(); ?>_validate(error_stack, question, answer)
191 if (!answer.length) {
192 error_stack[
"warnings"][error_stack[
"warnings"].length] =
"<?php echo translate('online_quiz_question_warning_empty_question', '%s'); ?>".replace(
'%s', question);
199 $options = $this->
attr(
'response_form');
203 var selected =
new Array();
206 if ($this->
attr(
'option_style') ==
'radio') {
208 foreach ($options as $option_key => $option) {
210 if (document.getElementById(
"<?php echo $prefix.'_response_'.$option_key; ?>").checked) selected[selected.length] =
"<?php echo $option_key; ?>";
215 select_box = document.getElementById(
"<?php echo $prefix.'_response'; ?>");
216 selected[selected.length] = select_box.selectedIndex;
220 <?php echo $this->
type(); ?>_validate(error_stack,
"<?php echo $this->getPosition(); ?>", selected);
223 return ob_get_clean();
240 $options = $this->
attr(
'response_form');
242 if ($this->
attr(
'max_option_selections') <= 1) {
243 $selected_option = array_get_index($options, $value, Array());
244 return array_get_index($selected_option,
'text',
'');
247 if (!empty($value)) {
248 foreach ($value as $response) {
249 $selected_option = array_get_index($options, $response, Array());
250 $summary[] = array_get_index($selected_option,
'text',
'');
269 $options = $this->
attr(
'response_form');
270 $correct_keys = Array();
271 foreach ($options as $option_key => $option) {
272 if ($option[
'points'] > 0) {
273 $correct_keys[] = $option_key;
277 return $correct_keys;
303 $options = $this->
attr(
'response_form');
307 foreach ($correct_keys as $key) {
308 if (!empty($options[$key][
'text'])) {
309 $summary[] = $options[$key][
'text'];
328 if (is_null($value)) $value = $this->
getValue();
332 $options = $this->
attr(
'response_form');
333 $max_selections = $this->
attr(
'max_option_selections');
334 $forfeit_penalty = $this->
attr(
'forfeit_penalty');
336 if (empty($options) || empty($max_selections)) {
342 if (empty($value))
return ($forfeit_penalty * -1);
344 if ($max_selections == 1) {
347 if (!isset($options[$value][
'points'])) {
348 return ($forfeit_penalty * -1);
351 $total_points += $options[$value][
'points'];
358 if (count($value) > $max_selections) {
359 return ($forfeit_penalty * -1);
362 foreach ($options as $option_key => $option) {
363 if (in_array($option_key, $value)) {
364 $total_points += $option[
'points'];
370 return $total_points;
385 $options = $this->
attr(
'response_form');
386 $max_selections = $this->
attr(
'max_option_selections');
388 if (empty($options) || empty($max_selections)) {
396 $option_points = Array();
397 foreach ($options as $option_key => $option) {
398 $option_points[] = $option[
'points'];
400 rsort($option_points, SORT_NUMERIC);
403 $slice_max = min(count($option_points), $max_selections);
405 $points_to_count = array_slice($option_points, $slice_min, $slice_max);
407 foreach ($points_to_count as $points_value) {
408 if ($points_value > 0) $total_points += $points_value;
411 return $total_points;
446 'assetid' => $this->
id,
448 'summary' => $summary,
449 'correct_value' => $correct_value,
450 'correct_summary' => $correct_summary,
452 'available_points' => $available,
456 foreach (Array(
'value',
'correct_value') as $field) {
457 $value = $results[$field];
458 if ($flatten && is_array($value)) {
459 $results[$field] = implode(
', ', $value);
463 foreach (Array(
'summary',
'correct_summary') as $field) {
464 $summary = $results[$field];
465 if ($flatten && is_array($summary)) {
466 $results[$field] = implode(
"\n", $summary);
493 $options = $this->
attr(
'response_form');
494 $use_html_options = $this->
attr(
'use_html_options');
500 $class =
'sq-'.str_replace(
'_',
'-', $this->
type());
501 $class = $feedback_mode ? $class.
' '.$class.
'-feedback' : $class;
502 ?><div
class=
"<?php echo $class; ?>"><?php
504 if (empty($options)) {
505 echo translate(
'online_quiz_question_multichoice_empty_options');
507 switch ($this->
attr(
'option_style')) {
509 $selection_contents = Array();
510 foreach ($options as $option_key => $option) {
511 $selection_contents[$option_key] = htmlentities($option[
'text']);
513 $extras = ($feedback_mode) ?
'disabled="disabled"' :
'';
514 combo_box($prefix.
'_response', $selection_contents, ($this->attr(
'max_option_selections') > 1), $value, 0, $extras);
515 if ($feedback_mode) {
516 foreach ($options as $option_key => $option) {
518 if (!empty($response_supplement)){
519 if (in_array($option_key, $correct_value)){
520 ?><div
class=
"online_quiz_response_supplement_correct"><?php echo $response_supplement ?></div><?php
522 ?><div
class=
"online_quiz_response_supplement_incorrect"><?php echo $response_supplement ?></div><?php
533 foreach ($options as $option_key => $option) {
536 $is_selected = ((is_array($value) && in_array($option_key, $value)) || $option_key == $value);
537 if (in_array($option_key, $correct_value)){
539 ?><li
class=
"online_quiz_correct_answer online_quiz_user_selection"><?php
541 ?><li
class=
"online_quiz_correct_answer"><?php
545 ?><li
class=
"online_quiz_incorrect_answer online_quiz_user_selection"><?php
547 ?><li
class=
"online_quiz_incorrect_answer"><?php
554 $extras =
'id="'.$prefix.
'_response_'.$option_key.
'"';
555 if ($feedback_mode) $extras .=
' disabled="disabled"';
558 if ($this->
attr(
'max_option_selections') <= 1) {
559 radio_button($prefix.
'_response', $option_key, ($value == $option_key),
'', $extras);
561 if (!is_array($value)) $value = Array();
562 check_box($prefix.
'_response['.$option_key.
']',
'1', (in_array($option_key, $value)),
'', $extras);
564 $option_input_field = ob_get_clean();
567 if ($use_html_options) {
569 preg_match_all(
'|\./\?a=([0-9]+)|', $option[
'text'], $matches);
570 $urls = $GLOBALS[
'SQ_SYSTEM']->am->getAssetURL(array_unique($matches[1]));
571 foreach ($urls as $assetid => $url) {
572 $option[
'text'] = preg_replace(
'|\./\?a='.$assetid.
'([^0-9])|', $url.
'\\1', $option[
'text']);
575 label($option[
'text'], $prefix.
'_response_'.$option_key);
577 label(htmlentities($option[
'text']), $prefix.
'_response_'.$option_key);
579 $option_label = ob_get_clean();
582 if ($feedback_mode) {
584 if (!empty($response_supplement)){
585 if (in_array($option_key, $correct_value)){
586 ?><div
class=
"online_quiz_response_supplement_correct"><?php echo $response_supplement ?></div><?php
588 ?><div
class=
"online_quiz_response_supplement_incorrect"><?php echo $response_supplement ?></div><?php
592 $option_response_supplement = ob_get_clean();
594 if (!empty($option_format)){
595 $option_format_output = $option_format;
596 $option_format_output = str_replace(
'%option_number%', $option_number, $option_format_output);
597 $option_format_output = str_replace(
'%option_input_field%', $option_input_field, $option_format_output);
598 $option_format_output = str_replace(
'%option_label%', $option_label, $option_format_output);
599 $option_format_output = str_replace(
'%option_text%', $option[
'text'], $option_format_output);
600 $option_format_output = str_replace(
'%option_response_supplement%', $option_response_supplement, $option_format_output);
601 echo $option_format_output;
603 echo $option_input_field.$option_label.$option_response_supplement;
615 return ob_get_clean();
630 $response_supplement =
'';
633 $response_options = $this->
attr(
'response_form');
634 if (empty($selected_option)) {
635 $selected_option = $this->
getValue();
638 if (!is_array($selected_option) && isset($response_options[$selected_option])) {
639 $option = $response_options[$selected_option];
642 if (isset($option[
'response_supplement'])) {
643 $response_supplement = $option[
'response_supplement'];
647 preg_match_all(
'|\./\?a=([0-9]+)|', $response_supplement, $matches);
648 $urls = $GLOBALS[
'SQ_SYSTEM']->am->getAssetURL(array_unique($matches[1]));
649 foreach ($urls as $assetid => $url) {
650 $response_supplement = preg_replace(
'|\./\?a='.$assetid.
'([^0-9])|', $url.
'\\1', $response_supplement);
654 return $response_supplement;