Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
online_quiz_question_multichoice_edit_fns.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_online_quiz/online_quiz_question/online_quiz_question_edit_fns.inc';
19 require_once SQ_ATTRIBUTES_PATH.'/wysiwyg/wysiwyg.inc';
20 
21 
35 {
36 
37 
43  {
45 
46  }//end constructor
47 
48 
59  function paintResponseForm(&$asset, &$o, $prefix)
60  {
61  $write_access = $asset->writeAccess('attributes');
62 
63  $use_html_options = $asset->attr('use_html_options');
64 
65  $o->openField(translate('online_quiz_question_multichoice_use_html_options_question'));
66  if ($write_access) {
67  $options = Array(
68  '0' => translate('no'),
69  '1' => translate('yes'),
70  );
71 
72  combo_box($prefix.'_question_use_html_options', $options, FALSE, Array($use_html_options));
73  } else {
74  if ($use_html_options) {
75  echo translate('yes');
76  } else {
77  echo translate('no');
78  }
79  }
80  $o->note(translate('online_quiz_question_multichoice_use_html_options_note'));
81  $o->closeField();
82 
83  $o->openField(translate('online_quiz_question_multichoice_options'));
84 
85  $options = $asset->attr('response_form');
86 
87  if (empty($options)) {
88  echo translate('online_quiz_question_multichoice_empty_options');
89  } else {
90 
91  ?>
92  <table class="sq-backend-table">
93  <tr>
94  <th><?php echo translate('online_quiz_question_multichoice_option'); ?></th>
95  <th><?php echo translate('online_quiz_question_multichoice_option_text'); ?></th>
96  <th><?php echo translate('online_quiz_question_multichoice_points'); ?></th>
97  <?php
98  if ($write_access) {
99  ?>
100  <th><?php echo translate('delete_question'); ?></th>
101  <?php
102  }
103  ?>
104  </tr>
105  <?php
106  foreach ($options as $option_key => $option) {
107  ?>
108  <tr>
109  <td><?php
110  echo $option_key;
111  ?></td>
112 
113  <td><?php
114  if ($use_html_options) {
115  ?>
116  <table border="0" width="100%">
117  <tr>
118  <td>
119  <?php
120  $wysiwyg_prefix = $prefix.'_options_text_wysiwyg_'.$option_key;
121  $wysiwyg = new Asset_Attribute_WYSIWYG();
122  if (isset($option['text'])) {
123  $wysiwyg->value = $option['text'];
124  }
125  $wysiwyg->paint($wysiwyg_prefix, !$write_access);
126  ?>
127  </td>
128  </tr>
129  </table>
130  <?php
131  } else {
132  if ($write_access) {
133  text_box($prefix.'_options['.$option_key.'][text]', $option['text'], 60);
134  } else {
135  echo $option['text'];
136  }
137  }
138  ?></td>
139 
140  <td><?php
141  if ($write_access) {
142  int_text_box($prefix.'_options['.$option_key.'][points]', $option['points'], TRUE, 10);
143  } else {
144  echo $option['points'];
145  }
146  ?></td>
147 
148  <?php
149  if ($write_access) {
150  ?><td><?php check_box($prefix.'_options['.$option_key.'][delete]'); ?></td><?php
151  }
152  ?></td>
153  </tr>
154 
155  <?php
156  }//end foreach ($options)
157 
158  ?>
159  </table>
160  <?php
161 
162  }//end else empty($options)
163  $o->closeField();
164 
165  if ($write_access) {
166  $o->openField(translate('online_quiz_question_multichoice_new_option'));
167  if ($use_html_options) {
168  ?>
169  <table border="0" width="100%">
170  <tr>
171  <td>
172  <?php
173  $wysiwyg_prefix = $prefix.'_new_option';
174  $wysiwyg = new Asset_Attribute_WYSIWYG();
175  $wysiwyg->paint($wysiwyg_prefix, !$write_access);
176  ?>
177  </td>
178  </tr>
179  </table>
180  <?php
181  } else {
182  text_box($prefix.'_new_option', '', 60);
183  }
184  $o->closeField();
185  }
186 
187  return $write_access;
188 
189  }//end paintResponseForm()
190 
191 
202  function processResponseForm(&$asset, &$o, $prefix)
203  {
204  $write_access = $asset->writeAccess('attributes');
205 
206  if ($write_access) {
207 
208  // Retrieve both the current setting of HTML options and that set by the form
209  $asset_use_html_options = $asset->attr('use_html_options');
210  $new_use_html_options = array_get_index($_POST, $prefix.'_question_use_html_options', NULL);
211 
212  // delete & edit
213  $options = Array();
214  $to_edit = array_get_index($_REQUEST, $prefix.'_options', Array());
215 
216  // "number" the options from a-z
217  $option_key_counter = ord('a');
218  $option_key_rollover = 1;
219 
220  foreach ($to_edit as $option_key => $option) {
221  // if we're deleting, just omit this option from the entire process
222  if (!array_get_index($option, 'delete')) {
223  $text = trim(array_get_index($option, 'text'));
224  $points = array_get_index($option, 'points', NULL);
225 
226  $supplementary_html = '';
227  if (isset($_REQUEST[$prefix.'_supplement_'.$option_key])) {
228  $supplementary_html = $_REQUEST[$prefix.'_supplement_'.$option_key];
229  }
230 
231  // Ensure that the content is retained when setting HTML options (text to WYSIWYG) or disabling this option (WYSIWYG to text)
232  if (($new_use_html_options) || ($asset_use_html_options && !$new_use_html_options)) {
233  if (isset($_REQUEST[$prefix.'_options_text_wysiwyg_'.$option_key])) {
234  $text = trim($_REQUEST[$prefix.'_options_text_wysiwyg_'.$option_key]);
235  }
236  }
237 
238  if (!empty($text) && !is_null($points)) {
239  if ($option_key_counter > ord('z')) {
240  $option_key_counter = ord('a');
241  $option_key_rollover++;
242  }
243  // a, b, c... y, z, a2, b2,
244  $option_key = chr($option_key_counter).($option_key_rollover <= 1 ? '' : $option_key_rollover);
245 
246  $options[$option_key]['text'] = $text;
247  $options[$option_key]['points'] = (int)$points;
248  $options[$option_key]['response_supplement'] = $supplementary_html;
249  $option_key_counter++;
250  }
251  }
252  }//end foreach
253 
254  // add
255  $new_option = trim(array_get_index($_REQUEST, $prefix.'_new_option'));
256  if (!empty($new_option)) {
257  if ($option_key_counter > ord('z')) {
258  $option_key_counter = ord('a');
259  $option_key_rollover++;
260  }
261  // a, b, c... y, z, a2, b2,
262  $option_key = chr($option_key_counter).($option_key_rollover <= 1 ? '' : $option_key_rollover);
263 
264  // Change text to use entities if we are creating a new option
265  if ($new_use_html_options && !$asset_use_html_options) {
266  $new_option = htmlentities($new_option);
267  }
268 
269  $options[$option_key] = Array(
270  'text' => $new_option,
271  'points' => 0,
272  'response_supplement' => '',
273  );
274 
275  $option_key_counter++;
276  }
277 
278  $asset->setAttrValue('response_form', $options);
279  $asset->setAttrValue('use_html_options', $new_use_html_options);
280 
281  return TRUE;
282 
283  }//end if ($write_access)
284 
285  return FALSE;
286 
287  }//end processResponseForm()
288 
289 
300  function paintResponseSupplements(&$asset, &$o, $prefix)
301  {
302  $write_access = $asset->writeAccess('attributes');
303 
304  $use_html_options = $asset->attr('use_html_options');
305 
306  $o->openField(translate('online_quiz_question_multichoice_options'));
307 
308  $options = $asset->attr('response_form');
309 
310  if (empty($options)) {
311  echo translate('online_quiz_question_multichoice_empty_options');
312  } else {
313 
314  ?>
315  <table class="sq-backend-table">
316  <tr>
317  <th><?php echo translate('online_quiz_question_multichoice_option'); ?></th>
318  <?php
319  if (!$use_html_options) {
320  ?><th><?php echo translate('online_quiz_question_multichoice_option_text'); ?></th>
321  <?php
322  }
323  ?>
324  <th><?php echo translate('online_quiz_question_multichoice_response_supplement'); ?></th>
325  </tr>
326  <?php
327 
328  foreach ($options as $option_key => $option) {
329  ?>
330  <tr>
331  <td><?php
332  echo $option_key;
333  ?></td>
334 
335  <?php
336  if (!$use_html_options) {
337  ?><td><?php
338  echo htmlentities($option['text']);
339  ?></td>
340  <?php
341  }
342  ?>
343  <td>
344  <table border="0" width="100%">
345  <tr>
346  <td>
347  <?php
348  $wysiwyg_prefix = $prefix.'_supplement_'.$option_key;
349  $wysiwyg = new Asset_Attribute_WYSIWYG();
350  if (isset($option['response_supplement'])) {
351  $wysiwyg->value = $option['response_supplement'];
352  }
353  $wysiwyg->paint($wysiwyg_prefix, !$write_access);
354  ?>
355  </td>
356  </tr>
357  </table>
358  </td>
359  </tr>
360 
361  <?php
362  }//end foreach ($options)
363 
364  ?>
365  </table>
366  <?php
367 
368  }//end else empty($options)
369  $o->closeField();
370 
371  return $write_access;
372 
373  }//end paintResponseSupplements()
374 
375 
376 }//end class
377 ?>