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