Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_found_in.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_found_in_true'),
59  0 => translate('core_form_rule_found_in_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  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
82  $value = array_get_index($rule_data, 'value', '');
83  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
84 
85  ob_start(); // buffer this please
86 
87  if (!$printed_fn) {
88  $printed_fn = true;
89 
90  ?>
91  function sq_form_validate_found_in(answer, comparison, case_sensitive)
92  {
93  if (case_sensitive) {
94  return comparison.indexOf(answer) != -1;
95  } else {
96  return comparison.toLowerCase().indexOf(answer.toLowerCase()) != -1;
97  }
98  }//end sq_form_validate_found_in()
99  <?php
100 
101  }
102 
103  if ($cq_id != 0) {
104  $comparison = 'form.elements["q'.$cq_id.'"].value';
105  } else {
106  $comparison = '"'.addslashes($value).'"';
107  }
108 
109  ?>
110  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_found_in(form.elements["q<?php echo $q_asset->id; ?>"].value, <?php echo $comparison ?>, <?php echo (int)$case_sensitive; ?>)) {
111  submission_errors[submission_errors.length] = "<?php
112  if (empty($rule_data['custom_text'])) {
113  echo addslashes($this->defaultError($q_asset, $rule_data));
114  } else {
115  echo addslashes($rule_data['custom_text']);
116  }
117  ?>";
118  }
119  <?php
120 
121  $contents = ob_get_contents();
122  ob_end_clean();
123 
124  return $contents;
125 
126  }//end generateJSCode()
127 
128 
144  function evaluate($answer, $rule_data)
145  {
146  $operator = array_get_index($rule_data, 'operator', 1);
147  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
148  $value = array_get_index($rule_data, 'value', '');
149  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
150 
151  if ($cq_id != 0) {
152  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
153  if (!empty($answer)) {
154  if ($case_sensitive) {
155  $valid = (strpos($comparison->getValue(), $answer) !== false) ? 1 : 0;
156  } else {
157  $valid = (strpos(strtolower($comparison->getValue()), strtolower($answer)) !== false) ? 1 : 0;
158  }
159  }
160  } else { // sent just a value
161  if (!empty($answer)) {
162  if ($case_sensitive) {
163  $valid = (strpos($value, $answer) !== false) ? 1 : 0;
164  } else {
165  $valid = (strpos(strtolower($value), strtolower($answer)) !== false) ? 1 : 0;
166  }
167  }
168  }
169  if (isset($valid)) {
170  return $valid == $operator;
171  } else
172  return FALSE;
173 
174  }//end evaluate()
175 
176 
186  function ruleDescription(&$q_asset, $rule_data)
187  {
188  $operator = array_get_index($rule_data, 'operator', 1);
189  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
190  $value = array_get_index($rule_data, 'value', '');
191  $case_sensitive = array_get_index($rule_data, 'case_sensitive', 1);
192 
193  $string_code = 'core_form_rule_found_in_desc_'.($operator ? 'true' : 'false');
194  $cq_id = array_get_index($rule_data, 'comparison_question_id', 0);
195 
196  if ($cq_id != 0) {
197  $string_code .= '_q';
198  $comparison = $GLOBALS['SQ_SYSTEM']->am->getAsset($cq_id);
199  $value = $comparison->name;
200  } else { // sent just a value
201  $value = array_get_index($rule_data, 'value', '');
202  }
203 
204  $error_str = translate($string_code, $q_asset->name, $value).' '.translate('core_form_'.($case_sensitive ? '' : 'not_').'case_sensitive');
205 
206  return $error_str;
207 
208  }//end ruleDescription()
209 
210 
211 }//end class
212 ?>