Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_select_limit.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 
32 {
33 
34 
43  function __construct($assetid=0)
44  {
45  parent::__construct($assetid);
46 
47  }//end constructor
48 
49 
57  public static function getOperators()
58  {
59  $operators = Array(
60  '==' => 'Must select exactly this many options...',
61  '!=' => 'Must not select this many options...',
62  '>' => 'Must select more than this many options...',
63  '<' => 'Must select less than this many options...',
64  '>=' => 'Must select at least this many options...',
65  '<=' => 'Must select no more than this many options...',
66  );
67 
68  return $operators;
69 
70  }//end getOperators()
71 
72 
87  function generateJSCode(&$q_asset, $rule_data)
88  {
89  $operator = array_get_index($rule_data, 'operator', '==');
90  $value = array_get_index($rule_data, 'value', '');
91 
92  $q_name = 'q'.$q_asset->id;
93 
94  if ($q_asset instanceof Form_Question_Type_Select) {
95  // selection list or derivative - check multiple attribute
96  $q_id = $q_name.($q_asset->attr('multiple') ? '[]' : '');
97  } else if ($q_asset instanceof Form_Question_Type_Option_List) {
98  // option list - always single (*sniff* :`-( )
99  $q_id = $q_name;
100  } else {
101  // tickbox list - always multiple
102  $q_id = $q_name.'[]';
103  }
104 
105  $q_safe_name = str_replace(':', '_', $q_name);
106 
107  // since this is going to be a generic rule type applicable to both select lists and tickbox
108  // lists, we need different types of magic for each possible question type.
109 
110  // For a selection list (and descendants) we need something like this.......
111  ob_start();
112 
113  ?>
114  var selectCount = 0;
115 
116  <?php
117  if ($q_asset instanceof Form_Question_Type_Select) {
118  ?>var select = form.elements["<?php echo $q_id ?>"];
119  for(i=0; i<select.options.length; i++) {
120  if (select.options[i].selected) selectCount++;
121  }<?php
122  } else { // tickbox (or option) list needs something else
123  foreach ($q_asset->getOptions() as $key => $option) {
124  ?>if (document.getElementById("<?php echo $q_safe_name.'_'.$key ?>").checked) selectCount++;
125  <?php
126  }
127  }
128 
129  ?>
130  if (!(selectCount <?php echo $operator ?> <?php echo $value ?>)) {
131  submission_errors[submission_errors.length] = "<?php
132  if (!strlen($q_asset->attr('cust_required_error'))) {
133  ?>You have selected "+selectCount+" option"+(selectCount == 1 ? "" : "s")+" at question \"<?php echo addslashes($q_asset->attr('name')) ?>\" - <?php
134  switch ($operator) {
135  case '==':
136  echo ' you must select '.$value;
137  break;
138 
139  case '!=':
140  echo ' you must not select this many';
141  break;
142 
143  case '>':
144  echo ' you must select more than '.$value;
145  break;
146 
147  case '>=':
148  echo ' you must select at least '.$value;
149  break;
150 
151  case '<':
152  echo ' you must select less than '.$value;
153  break;
154 
155  case '<=':
156  echo ' you must select no more than '.$value;
157  break;
158 
159  }//end switch
160  } else {
161  echo addslashes($q_asset->attr('cust_required_error'));
162  }
163  ?>";
164  }
165  <?php
166  $contents = ob_get_contents();
167  ob_end_clean();
168 
169  return $contents;
170 
171  }//end generateJSCode()
172 
173 
187  function evaluate($answer, $rule_data)
188  {
189  $operator = array_get_index($rule_data, 'operator', '==');
190  $value = array_get_index($rule_data, 'value', '');
191 
192  $valid = eval('return \''.count($answer).'\''.$operator.'\''.$value.'\';');
193  return $valid;
194 
195  }//end evaluate()
196 
197 
207  function defaultError(&$q_asset, $rule_data)
208  {
209  $operator = array_get_index($rule_data, 'operator', '==');
210  $value = array_get_index($rule_data, 'value', '');
211  if ($operator == '>=' && $value == 1 && strlen($q_asset->attr('cust_required_error'))) {
212  $error_str = $q_asset->attr('cust_required_error');
213  } else {
214  $selected_options = count($q_asset->getValue());
215  $error_str = translate('core_form_rule_select_limit_error_'.$operator, $selected_options, $q_asset->attr('name'), $value);
216  }
217  return $error_str;
218 
219  }//end defaultError()
220 
221 
231  function ruleDescription(&$q_asset, $rule_data)
232  {
233  $operator = array_get_index($rule_data, 'operator', '==');
234  $value = array_get_index($rule_data, 'value', '');
235 
236  $error_str = translate('core_form_rule_select_limit_desc_'.$operator, $value, $q_asset->attr('name'));
237 
238  return $error_str;
239 
240  }//end ruleDescription()
241 
242 
243 }//end class
244 ?>