Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_integer.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 
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  return Array(
58  1 => translate('core_form_rule_integer_true'),
59  0 => translate('core_form_rule_integer_false'),
60  );
61 
62  }//end getOperators()
63 
64 
74  function generateJSCode(&$q_asset, $rule_data)
75  {
76  // have we printed out the validation function yet?
77  // (since this is static across all class instances, this will work)
78  static $printed_fn = false;
79 
80  $operator = array_get_index($rule_data, 'operator', 1);
81 
82  // regex code - allow leading plus or minus, digits, no dec pt though
83  $regex_code = '/^[-+]?\d+$/';
84 
85  ob_start(); // buffer this please
86 
87  if (!$printed_fn) {
88  $printed_fn = true;
89 
90  ?>
91  function sq_form_validate_integer(answer)
92  {
93  answer.replace(/,/, "");
94  return answer.match(<?php echo $regex_code ?>);
95 
96  }//end sq_form_validate_integer()
97  <?php
98 
99  }
100 
101  ?>
102  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_integer(form.elements["q<?php echo $q_asset->id; ?>"].value)) {
103  submission_errors[submission_errors.length] = "<?php
104  if (empty($rule_data['custom_text'])) {
105  echo addslashes($this->defaultError($q_asset, $rule_data));
106  } else {
107  echo addslashes($rule_data['custom_text']);
108  }
109  ?>";
110  }
111  <?php
112 
113  $contents = ob_get_contents();
114  ob_end_clean();
115 
116  return $contents;
117 
118  }//end generateJSCode()
119 
120 
133  function evaluate($answer, $rule_data)
134  {
135  $operator = array_get_index($rule_data, 'operator', 1);
136 
137  $valid = (is_numeric($answer) && ($answer == (int)$answer)) ? 1 : 0;
138  return ($valid == $operator);
139 
140  }//end evaluate()
141 
142 
152  function ruleDescription(&$q_asset, $rule_data)
153  {
154  $operator = array_get_index($rule_data, 'operator', 1);
155  $error_str = translate('core_form_rule_integer_desc_'.($operator ? 'true' : 'false'), $q_asset->attr('name'));
156 
157  return $error_str;
158 
159  }//end ruleDescription()
160 
161 
162 }//end class
163 ?>