Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_numeric.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 
32 {
33 
34 
43  function __construct($assetid=0)
44  {
45  parent::__construct($assetid);
46 
47  }//end constructor
48 
49 
59  public static function getOperators()
60  {
61  return Array(
62  1 => translate('core_form_rule_numeric_true'),
63  0 => translate('core_form_rule_numeric_false'),
64  );
65 
66  }//end getOperators()
67 
68 
78  function generateJSCode(&$q_asset, $rule_data)
79  {
80  // have we printed out the validation function yet?
81  // (since this is static across all class instances, this will work)
82  static $printed_fn_required = FALSE;
83  static $printed_fn_not_required = FALSE;
84 
85  $operator = array_get_index($rule_data, 'operator', 1);
86 
87  // regex code - allow empty or leading plus or minute, digits, decimal point
88  $regex_code_not_required = '/(^$)|(^[-+]?\d*\.?\d+$)/';
89  // regex code - allow leading plus or minute, digits, decimal point, but not empty
90  $regex_code_required = '/^[-+]?\d*\.?\d+$/';
91 
92  ob_start(); // buffer this please
93 
94  // output js function for *required* validation
95  if (!$printed_fn_required && $q_asset->attr('is_required')) {
96  $printed_fn_required = TRUE;
97 
98  ?>
99  function sq_form_validate_numeric_required(answer)
100  {
101  answer.replace(/,/, "");
102  return answer.match(<?php echo $regex_code_required ?>);
103 
104  }//end sq_form_validate_numeric_required()
105  <?php
106  // output js function for validation where field is not required to be filled
107  } else if (!$printed_fn_not_required) {
108  $printed_fn_not_required = TRUE;
109 
110  ?>
111  function sq_form_validate_numeric_not_required(answer)
112  {
113  answer.replace(/,/, "");
114  return answer.match(<?php echo $regex_code_not_required ?>);
115 
116  }//end sq_form_validate_numeric_not_required()
117 
118  <?php
119 
120  }
121 
122  // generate calls to the above 2 functions
123  // if this field is required
124  if ($q_asset->attr('is_required')) {
125  ?>
126  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_numeric_required(form.elements["q<?php echo $q_asset->id; ?>"].value)) {
127  submission_errors[submission_errors.length] = "<?php
128  if (empty($rule_data['custom_text'])) {
129  echo addslashes($this->defaultError($q_asset, $rule_data));
130  } else {
131  echo addslashes($rule_data['custom_text']);
132  }
133  ?>";
134  } <?php
135  // if this field is not required
136  } else if (!$q_asset->attr('is_required')) {
137  ?>
138  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_numeric_not_required(form.elements["q<?php echo $q_asset->id; ?>"].value)) {
139  submission_errors[submission_errors.length] = "<?php
140  if (empty($rule_data['custom_text'])) {
141  echo addslashes($this->defaultError($q_asset, $rule_data));
142  } else {
143  echo addslashes($rule_data['custom_text']);
144  }
145  ?>";
146  } <?php
147  }
148 
149  $contents = ob_get_contents();
150  ob_end_clean();
151 
152  return $contents;
153 
154  }//end generateJSCode()
155 
156 
170  function evaluate($answer, $rule_data)
171  {
172  $operator = array_get_index($rule_data, 'operator', 1);
173 
174  $valid = (is_numeric($answer)) ? 1 : 0;
175  return ($valid == $operator);
176 
177  }//end evaluate()
178 
179 
189  function ruleDescription(&$q_asset, $rule_data)
190  {
191  $operator = array_get_index($rule_data, 'operator', 1);
192  $error_str = translate('core_form_rule_numeric_desc_'.($operator ? 'true' : 'false'), $q_asset->attr('name'));
193 
194  return $error_str;
195 
196  }//end ruleDescription()
197 
198 
199 }//end class
200 ?>