Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_comparison.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 
31 class Form_Question_Rule_Type_Comparison extends Form_Question_Rule
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
55  public static function getOperators()
56  {
57  $operators = Array(
58  '==' => translate('core_form_rule_comparison_=='),
59  '!=' => translate('core_form_rule_comparison_!='),
60  '>' => translate('core_form_rule_comparison_>'),
61  '<' => translate('core_form_rule_comparison_<'),
62  '>=' => translate('core_form_rule_comparison_>='),
63  '<=' => translate('core_form_rule_comparison_<='),
64  );
65 
66  return $operators;
67 
68  }//end getOperators()
69 
70 
83  function generateJSCode(&$q_asset, $rule_data)
84  {
85  $operator = array_get_index($rule_data, 'operator', 1);
86  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
87  $value = array_get_index($rule_data, 'value', '');
88 
89  ob_start(); // buffer this please
90 
91  if ($cq_id != 0) {
92  $comparison = 'form.elements["q'.$cq_id.'"].value';
93  } else {
94  $comparison = '"'.addslashes($value).'"';
95  }
96 
97  // if it's a numeric field, make sure that JavaScript
98  // interprets it as a number
99  if ($q_asset instanceof Form_Question_Type_Numeric) {
100  ?>if (!(parseFloat(form.elements["q<?php echo $q_asset->id; ?>"].value) <?php echo $operator ?> parseFloat(<?php echo $comparison ?>))) {<?php
101  } else {
102  ?>if (!(form.elements["q<?php echo $q_asset->id; ?>"].value <?php echo $operator ?> <?php echo $comparison ?>)) {<?php
103  }
104  ?>submission_errors[submission_errors.length] = "<?php
105  if (empty($rule_data['custom_text'])) {
106  echo addslashes($this->defaultError($q_asset, $rule_data));
107  } else {
108  echo addslashes($rule_data['custom_text']);
109  }
110  ?>";
111  }
112  <?php
113 
114  $contents = ob_get_contents();
115  ob_end_clean();
116 
117  return $contents;
118 
119  }//end generateJSCode()
120 
121 
137  function evaluate($answer, $rule_data)
138  {
139  $operator = array_get_index($rule_data, 'operator', '==');
140  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
141  $value = array_get_index($rule_data, 'value', '');
142 
143  if ($cq_id != 0) {
144  $comparison_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
145  $value = $comparison_asset->getValue();
146  }
147 
148  switch ($operator) {
149  case '==':
150  $valid = ($answer == $value);
151  break;
152 
153  case '!=':
154  $valid = ($answer != $value);
155  break;
156 
157  case '>':
158  $valid = ($answer > $value);
159  break;
160 
161  case '>=':
162  $valid = ($answer >= $value);
163  break;
164 
165  case '<':
166  $valid = ($answer < $value);
167  break;
168 
169  case '<=':
170  $valid = ($answer <= $value);
171  break;
172  }
173 
174  return $valid;
175 
176  }//end evaluate()
177 
178 
188  function ruleDescription(&$q_asset, $rule_data)
189  {
190  $operator = array_get_index($rule_data, 'operator', '==');
191  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
192  $value = array_get_index($rule_data, 'value', '');
193 
194  if (($value == '') && ($operator == '!=')) {
195  if (strlen($q_asset->attr('cust_required_error'))) {
196  $error_str = $q_asset->attr('cust_required_error');
197  } else {
198  $error_str = translate('core_form_rule_comparison_required', $q_asset->attr('name'));
199  }
200  } else {
201  $operator_list = $this->getOperators();
202  $string_code = 'core_form_rule_comparison_desc_'.$operator;
203 
204  if ($cq_id != 0) {
205  $string_code .= '_q';
206  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
207  $value = $comparison->name;
208  } else { // sent just a value
209  $value = array_get_index($rule_data, 'value', '');
210  }
211 
212  $error_str = translate($string_code, $q_asset->name, $value);
213  }
214 
215  return $error_str;
216 
217  }//end ruleDescription()
218 
219 
220 }//end class
221 ?>