Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_begins_with.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_begins_with_true'),
59  0 => translate('core_form_rule_begins_with_false'),
60  );
61 
62  }//end getOperators()
63 
64 
77  function generateJSCode(&$q_asset, $rule_data)
78  {
79  // have we printed out the validation function yet?
80  // (since this is static across all class instances, this will work)
81  static $printed_fn = false;
82 
83  $operator = array_get_index($rule_data, 'operator', 1);
84  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
85  $value = array_get_index($rule_data, 'value', '');
86  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
87 
88  ob_start(); // buffer this please
89 
90  if (!$printed_fn) {
91  $printed_fn = true;
92 
93  ?>
94  function sq_form_validate_begins_with(answer, comparison, case_sensitive)
95  {
96  if (case_sensitive) {
97  return answer.indexOf(comparison) == 0;
98  } else {
99  return answer.toLowerCase().indexOf(comparison.toLowerCase()) == 0;
100  }
101  }//end sq_form_validate_begins_with()
102  <?php
103 
104  }
105 
106  if ($cq_id != 0) {
107  $comparison = 'form.elements["q'.$cq_id.'"].value';
108  } else {
109  $comparison = '"'.addslashes($value).'"';
110  }
111 
112  ?>
113  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_begins_with(form.elements["q<?php echo $q_asset->id; ?>"].value, <?php echo $comparison ?>, <?php echo (int)$case_sensitive; ?>)) {
114  submission_errors[submission_errors.length] = "<?php
115  if (empty($rule_data['custom_text'])) {
116  echo addslashes($this->defaultError($q_asset, $rule_data));
117  } else {
118  echo addslashes($rule_data['custom_text']);
119  }
120  ?>";
121  }
122  <?php
123 
124  $contents = ob_get_contents();
125  ob_end_clean();
126 
127  return $contents;
128 
129  }//end generateJSCode()
130 
131 
147  function evaluate($answer, $rule_data)
148  {
149  $operator = array_get_index($rule_data, 'operator', 1);
150  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
151  $value = array_get_index($rule_data, 'value', '');
152  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
153 
154  if ($cq_id != 0) {
155  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
156  if ($case_sensitive) {
157  $valid = (strpos($answer, $comparison->getValue()) === 0) ? 1 : 0;
158  } else {
159  $valid = (stripos(strtolower($answer), strtolower($comparison->getValue())) === 0) ? 1 : 0;
160  }
161  } else { // sent just a value
162  if ($case_sensitive) {
163  $valid = (strpos($answer, $value) === 0) ? 1 : 0;
164  } else {
165  $valid = (strpos(strtolower($answer), strtolower($value)) === 0) ? 1 : 0;
166  }
167  }
168 
169  return $valid == $operator;
170 
171  }//end evaluate()
172 
173 
183  function ruleDescription(&$q_asset, $rule_data)
184  {
185  $operator = array_get_index($rule_data, 'operator', 1);
186  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
187  $value = array_get_index($rule_data, 'value', '');
188  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
189 
190  $string_code = 'core_form_rule_begins_with_desc_'.($operator ? 'true' : 'false');
191  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
192 
193  if ($cq_id != 0) {
194  $string_code .= '_q';
195  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
196  $value = $comparison->name;
197  } else { // sent just a value
198  $value = array_get_index($rule_data, 'value', '');
199  }
200 
201  $error_str = translate($string_code, $q_asset->name, $value).' '.translate('core_form_'.($case_sensitive ? '' : 'not_').'case_sensitive');
202 
203  return $error_str;
204 
205  }//end ruleDescription()
206 
207 
208 }//end class
209 ?>