Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_ends_with.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
56  public static function getOperators()
57  {
58  return Array(
59  1 => translate('core_form_rule_ends_with_true'),
60  0 => translate('core_form_rule_ends_with_false'),
61  );
62 
63  }//end getOperators()
64 
65 
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 = false;
83 
84  $operator = array_get_index($rule_data, 'operator', 1);
85  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
86  $value = array_get_index($rule_data, 'value', '');
87  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
88 
89  ob_start(); // buffer this please
90 
91  if (!$printed_fn) {
92  $printed_fn = true;
93 
94  ?>
95  function sq_form_validate_ends_with(answer, comparison, case_sensitive)
96  {
97  if (case_sensitive) {
98  pos = answer.indexOf(comparison);
99  } else {
100  pos = answer.toLowerCase().indexOf(comparison.toLowerCase());
101  }
102 
103  // if position is less than zero than it can't be found,
104  // therefore it can't end with it
105  return (pos >= 0) && (pos == answer.length - comparison.length);
106  }//end sq_form_validate_ends_with()
107  <?php
108 
109  }
110 
111  if ($cq_id != 0) {
112  $comparison = 'form.elements["q'.$cq_id.'"].value';
113  } else {
114  $comparison = '"'.addslashes($value).'"';
115  }
116 
117  ?>
118  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_ends_with(form.elements["q<?php echo $q_asset->id; ?>"].value, <?php echo $comparison ?>, <?php echo (int)$case_sensitive; ?>)) {
119  submission_errors[submission_errors.length] = "<?php
120  if (empty($rule_data['custom_text'])) {
121  echo addslashes($this->defaultError($q_asset, $rule_data));
122  } else {
123  echo addslashes($rule_data['custom_text']);
124  }
125  ?>";
126  }
127  <?php
128 
129  $contents = ob_get_contents();
130  ob_end_clean();
131 
132  return $contents;
133 
134  }//end generateJSCode()
135 
136 
152  function evaluate($answer, $rule_data)
153  {
154  $operator = array_get_index($rule_data, 'operator', 1);
155  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
156  $value = array_get_index($rule_data, 'value', '');
157  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
158 
159  if ($cq_id != 0) {
160  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
161  $value = $comparison->getValue();
162  }
163 
164  if (!$case_sensitive) {
165  $value = strtolower($value);
166  $answer = strtolower($answer);
167  }
168 
169  // we could use strrpos() if we were in PHP5, but since it's limited to
170  // one character needles in PHP4, we need to do an iterative search
171 
172  $last_pos = -1;
173  while (($pos = strpos($answer, $value, $last_pos + 1)) !== false) {
174  $last_pos = $pos;
175  }
176 
177  $valid = ($last_pos === strlen($answer)-strlen($value)) ? 1 : 0;
178  return $valid == $operator;
179 
180  }//end evaluate()
181 
182 
192  function ruleDescription(&$q_asset, $rule_data)
193  {
194  $operator = array_get_index($rule_data, 'operator', 1);
195  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
196  $value = array_get_index($rule_data, 'value', '');
197  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
198 
199  $string_code = 'core_form_rule_ends_with_desc_'.($operator ? 'true' : 'false');
200  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
201 
202  if ($cq_id != 0) {
203  $string_code .= '_q';
204  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
205  $value = $comparison->name;
206  } else { // sent just a value
207  $value = array_get_index($rule_data, 'value', '');
208  }
209 
210  $error_str = translate($string_code, $q_asset->name, $value).' '.translate('core_form_'.($case_sensitive ? '' : 'not_').'case_sensitive');
211 
212  return $error_str;
213 
214  }//end ruleDescription()
215 
216 
217 }//end class
218 ?>