root/form/lib.php

Revision 302, 22.8 kB (checked in by kevin, 7 months ago)

I had accidentally turned form notification off in the last commit. This fixes that and fixes a problem with some profile choice fields not being displayed properly.

Line 
1 <?php
2 function form_pagesetup() {
3     global $CFG;
4     global $PAGE;
5     
6     if (defined("context") && context == "admin") {
7         if (isloggedin() && run("users:flags:get", array("admin", $_SESSION['userid']))) {
8         
9         // set up admin page link
10         
11             $PAGE->menu_sub[] = array ( 'name' => 'form:admin',
12                                         'html' => "<a href=\"{$CFG->wwwroot}mod/form/index.php\">"
13                                         . gettext("Manage forms") . '</a>');
14         }
15     }
16 }
17
18 function form_init() {
19     global $METATABLES,$CFG;
20     
21     if (!in_array($CFG->prefix . "forms", $METATABLES) || !in_array($CFG->prefix . "form_fields", $METATABLES) || !in_array($CFG->prefix . "form_fields_choice", $METATABLES)) {
22         if (file_exists($CFG->dirroot . "mod/form/$CFG->dbtype.sql")) {
23             modify_database($CFG->dirroot . "mod/form/$CFG->dbtype.sql");
24         } else {
25             error("Error: Your database ($CFG->dbtype) is not yet supported by the Form module.");
26         }
27         print_continue("index.php");
28         exit;
29     }
30 }
31
32 function form_get_field_form($form_id,$field=null,$field_name = '') {
33     
34     global $CFG,$metatags;
35     
36     // this include is inside the function to avoid putting a huge amount of source code
37     // inside lib.php
38     
39     include('field_form_definition.php');
40     
41     return $body;
42 }
43
44 function form_get_form_form($form = null) {
45     global $CFG;
46     
47     $view_all_link_text = __gettext("View all forms");
48     
49     $edit_msg = __gettext('Edit');
50     $remove_msg = __gettext('Remove');
51     $remove_confirm_msg = __gettext("Are you sure that you want to remove this field from this form?");
52     $moveup_msg = __gettext('Move Up');
53     $movedown_msg = __gettext('Move Down');
54     
55     $img_template = '<img border="0" width="16" height="16" alt="%s" title="%s" src="'.$CFG->wwwroot.'mod/widget/images/%s" />';
56     $edit_img = sprintf($img_template,$edit_msg,$edit_msg,"16-em-pencil.png");
57     $remove_img = sprintf($img_template,$remove_msg,$remove_msg,"16-em-cross.png");
58     $moveup_img = sprintf($img_template,$moveup_msg,$moveup_msg,"16-em-open.png");
59     $movedown_img = sprintf($img_template,$movedown_msg,$movedown_msg,"16-em-down.png");
60     
61     $field_template = <<<END
62 <a href="{$CFG->wwwroot}mod/form/manage_field.php?action=edit&id=%s">$edit_img</a> |
63 <a onclick="return confirm('$remove_confirm_msg')" href="{$CFG->wwwroot}mod/form/manage_field.php?action=remove&id=%s">$remove_img</a> |
64 <a href="{$CFG->wwwroot}mod/form/manage_field.php?action=move&id=%s&direction=up">$moveup_img</a> |
65 <a href="{$CFG->wwwroot}mod/form/manage_field.php?action=move&id=%s&direction=down">$movedown_img</a>
66 %s
67 <br />
68 END;
69     
70     if ($form) {
71         $action = 'change';
72         $manage_form_title = __gettext("Manage form data");
73         $explanation = _gettext("You can manage all aspects of this form using the fields below.");
74         $form_manage_button = __gettext("Change form");
75         $form_id = $form->ident;
76         $form_name = $form->name;
77         $form_title = $form->title;
78         $description = $form->description;
79         $response_text = $form->response_text;
80         $access = $form->access;
81         $notification = $form->notification;
82         $field_list = '<h1>'.__gettext("Current fields").'</h1><br />';
83         if (isset($form->fields) && count($form->fields) > 0 ) {
84             foreach ($form->fields AS $field) {
85                 $ident = $field->map_id;
86                 $field_list .= sprintf( $field_template,$ident,$ident,$ident,$ident,
87                     $field->title . ' ('.$field->internal_name.': '.$field->field_type.')');
88             }
89             $field_list .= '<br />';
90         } else {
91             $field_list .= '<p>'.__gettext("None").'.</p>';
92         }
93       
94         
95         /* buttons and links:
96          *
97          * preview form (link)
98          * add existing field (input box for field name)
99          * copy existing field (copies field definition) (input boxes for field name)
100          * add new field (input box for field name)
101          */     
102         
103          $preview_link_text = __gettext("Preview this form");
104          $public_link_text = __gettext("Public link for this form");
105
106          $add_existing_button = __gettext("Add existing field");
107          $add_existing_field_description = __gettext("Enter the name of an existing field to add it to this form.");
108          $copy_existing_button = __gettext("Copy existing field");
109          $copy_existing_field_description = __gettext("Enter the name of an existing field and the name of the new "
110             ."field to create a copy that will be added to this form.");
111          $add_new_button = __gettext("Add new field");
112          $add_new_description = __gettext("Enter the name of a new field to add it to this form.");
113          $existing_label = __gettext("Existing field name");
114          $new_label = __gettext("New field name");
115         
116          $add_fields_title = __gettext("Add fields");
117          $add_existing_title = __gettext("Add existing field");
118          $copy_existing_title = __gettext("Copy existing field"); 
119          $add_new_title __gettext("Add new field");
120         
121          $links = <<<END
122          <p><a href="{$CFG->wwwroot}mod/form/form.php?id=$form_id&preview=true">$preview_link_text</a>
123          | <a href="{$CFG->wwwroot}mod/form/form.php?id=$form_id">$public_link_text</a>
124          | <a href="{$CFG->wwwroot}mod/form/index.php">$view_all_link_text</a>
125          </p>
126 END;
127         
128          $buttons = <<<END
129          <h1>$add_fields_title</h1>
130          <br />
131          <form action="{$CFG->wwwroot}mod/form/manage_field.php" method="post">
132          <h2>$add_new_title</h2>
133          <input type="hidden" name="form_id" value="$form_id">
134          <input type="hidden" name="action" value="add_new">
135          <label class="labelclass" for="new_field_name">$new_label</label>
136          <input class="standard" type="text" name="new_field_name" value="">
137          <p class="description">$add_new_description</p>
138          <input type="submit" name="submit" value="$add_new_button">
139          </form>
140          <br />
141          <form action="{$CFG->wwwroot}mod/form/manage_field.php" method="post">
142          <h2>$add_existing_title</h2>
143          <input type="hidden" name="form_id" value="$form_id">
144          <input type="hidden" name="action" value="add_existing">
145          <label class="labelclass" for="existing_field_name">$existing_label</label>
146          <input class="standard" type="text" name="existing_field_name" value="">
147          <p class="description">$add_existing_field_description</p>
148          <input type="submit" name="submit" value="$add_existing_button">
149          </form>
150          <br />
151          <form action="{$CFG->wwwroot}mod/form/manage_field.php" method="post">
152          <h2>$copy_existing_title</h2>
153          <input type="hidden" name="form_id" value="$form_id">
154          <input type="hidden" name="action" value="copy_existing">
155          <label class="labelclass" for="existing_field_name">$existing_label</label>
156          <input class="standard" type="text" name="existing_field_name" value="">
157          <label class="labelclass" for="new_field_name">$new_label</label>
158          <input class="standard" type="text" name="new_field_name" value="">
159          <p class="description">$copy_existing_field_description</p>
160          <input type="submit" name="submit" value="$copy_existing_button">
161          </form>
162          <br /><br />
163 END;
164     } else {
165         $action = 'add';
166         $manage_form_title = '';
167         $explanation = _gettext("Enter the information below and press the Create form button to create this form."
168             ." Once the form has been created, you will be able to add fields to it.");
169         $form_manage_button = __gettext("Create form");
170         $form_id = 0;
171         $form_name = '';
172         $form_title = '';
173         $description = '';
174         $response_text = '';
175         $access = $CFG->default_access;
176         $notification = 0;
177         $field_list = '';
178         $buttons = '';
179         $links = <<<END
180          <p><a href="{$CFG->wwwroot}mod/form/index.php">$view_all_link_text</a></p>
181 END;
182     }
183     
184     $access_select = run("display:access_level_select",array("access",$access));
185     
186     $form_name_label = __gettext("Form name");
187     $form_name_description = __gettext("Enter an internal name for this form (no blanks, only letters, names and/or underscores)");
188     $form_title_label = __gettext("Form title");
189     $form_title_description = __gettext("Enter a brief title that will appear as a heading at the top of the form.");
190     $description_label = __gettext("Description");
191     $form_description_description = __gettext("Enter a sentence or two of text that will be displayed at the top of your form to explain its purpose.");
192     $response_text_label = __gettext("Response text");
193     $response_description = __gettext("Enter the content that will be displayed after the form has been submitted.");
194     $access_label = __gettext("Access");
195     $access_description = __gettext("The access determines who is allowed to see this form.");
196     $notification_label = __gettext("Notification");
197     $notification_description = __gettext("Tick this box to receive a notification each time someone submits this form.");
198     if ($notification) {
199         $notification_checked = 'checked';
200     } else {
201         $notification_checked = '';
202     }
203                   
204     
205     $body = <<<END
206     <style>
207 div.invisible {
208     display: none;
209 }
210 div.visibleright {
211     float:right;
212     width: 100px;
213 }
214
215 label.labelclass {
216     display:block;
217     font-weight:bold;
218 }
219
220 p.description {
221     font-size: 0.9em;
222     margin-top: 0px;
223 }
224
225 span.option_value_header {
226     font-weight: bold;
227     width: 150px;
228     display: block;
229     float: left;
230 }
231
232 span.option_label_header {
233     font-weight: bold;
234     width: 300px;
235     margin-left: 10px;
236 }
237
238 input.option_value_input {
239     width:150px;
240 }
241
242 input.option_label_input {
243     margin-left:10px;
244     width:300px;
245 }
246
247 textarea.smalltextarea {
248     width:350px;
249     height: 100px;
250 }  
251 </style>
252 $links
253 <p>$explanation</p>
254 $buttons
255 $field_list
256 <form action="{$CFG->wwwroot}/mod/form/manage_form.php" method="post">
257 <h1>$manage_form_title</h1>
258 <input type="hidden" name="form_id" value="$form_id">
259 <input type="hidden" name="action" value="$action">
260 <label class="labelclass" for="form_name">$form_name_label</label>
261 <input type="text" class="standard" name="form_name" value="$form_name">
262 <p class="description">$form_name_description</p>
263 <label class="labelclass" for="form_title">$form_title_label</label>
264 <input type="text" class="standard" name="form_title" value="$form_title">
265 <p class="description">$form_title_description</p>
266 <label class="labelclass" for="form_title">$description_label</label>
267 <textarea class="smalltextarea" name="description">$description</textarea>
268 <p class="description">$form_description_description</p>
269 <label class="labelclass" for="response_text">$response_text_label</label>
270 <textarea class="smalltextarea" name="response_text">$response_text</textarea>
271 <p class="description">$response_description</p>
272 <label class="labelclass" for="access">$access_label</label>
273 $access_select
274 <p class="description">$access_description</p>
275 <label class="labelclass" for="notification">$notification_label</label>
276 <input type="checkbox" name="notification" value="1" $notification_checked>
277 <p class="description">$notification_description</p>
278 <br />
279 <input type="submit" name="submit" value="$form_manage_button">
280 </form>
281 END;
282
283 return $body;
284         
285 }
286
287 function form_get_field_definition($field_id) {
288     
289     global $CFG;
290     
291     // get the main field data
292     
293     $field = get_record('form_fields','ident',$field_id);
294     
295     // get the choice data, if any
296     
297     if (isset($field) && $field->field_type == 'choices') {
298         $field->options1 = get_records_sql("SELECT * FROM {$CFG->prefix}form_fields_choice WHERE field_id = $field_id AND group_id = 1 ORDER BY ident ASC");
299         $field->options2 = get_records_sql("SELECT * FROM {$CFG->prefix}form_fields_choice WHERE field_id = $field_id AND group_id = 2 ORDER BY ident ASC");
300     }
301     
302     return $field;
303 }
304
305 function form_set_field_definition() {
306     $action = optional_param('action','');
307     $field = new stdClass;
308     $field->form_id = optional_param('form_id',0,PARAM_INT);
309     if ($action == 'change') {
310         $field->ident = optional_param('field_id',0,PARAM_INT);
311     }
312     $field->internal_name = optional_param('internal_name','');
313     $field->title = optional_param('title','');
314     $field->description = optional_param('description','');
315     $field->required = optional_param('required',0,PARAM_INT);
316     $field->profile = optional_param('profile',0,PARAM_INT);
317     $field->category = optional_param('category','');
318     $field->field_type = optional_param('field_type','');
319     $field->invisible = optional_param('invisible',0,PARAM_INT);
320     $field->col = optional_param('column','');
321     $field->default_access = optional_param('default_access','');
322     $field->choice_type = optional_param('choice_type','');
323     $field->orientation = optional_param('orientation','');
324     $field->default_value = optional_param('default_value','');
325     $field->is_keyword_tag = optional_param('is_keyword_tag',0,PARAM_INT);
326     
327     if ($field->field_type == 'contact') {
328         $field->field_type = optional_param('contact_type','');
329     }
330     
331     if ($action == 'change') {
332         update_record('form_fields',$field);
333         delete_records('form_fields_choice','field_id',$field->ident);
334     } else {
335         $field->ident = insert_record('form_fields',$field);
336         $map = new stdClass;
337         $map->field_id = $field->ident;
338         $map->form_id = $field->form_id;
339         $map->display_order = 100000;
340         insert_record('form_fields_map',$map);
341         form_reorder($field->form_id);
342     }
343     
344     $number_of_options1 = optional_param('number_of_options1',0,PARAM_INT);
345     
346     for ($i = 0; $i < $number_of_options1; $i++) {
347         $option = new stdClass;
348         $option->field_id = $field->ident;
349         $option->group_id = 1;
350         $option->value = optional_param('option_1_'.$i.'_value','');
351         $option->label = optional_param('option_1_'.$i.'_label','');
352         if ($option->value) {
353             insert_record('form_fields_choice',$option);
354         }
355     }
356     
357     $number_of_options2 = optional_param('number_of_options2',0,PARAM_INT);
358     
359     for ($i = 0; $i < $number_of_options2; $i++) {
360         $option = new stdClass;
361         $option->field_id = $field->ident;
362         $option->group_id = 2;
363         $option->value = optional_param('option_2_'.$i.'_value','');
364         $option->label = optional_param('option_2_'.$i.'_label','');
365         if ($option->value) {
366             insert_record('form_fields_choice',$option);
367         }
368     }       
369 }
370
371 function form_reorder($form_id) {
372     $maps = form_get_maps($form_id);
373     $order = array();
374     $i = 1;
375     foreach($maps as $map) {
376         $order[$map->ident] = $i * 10;
377         $i++;
378     }
379     foreach($order as $ident => $display_order) {
380         $map = new StdClass;
381         $map->display_order = $display_order;
382         $map->ident = $ident;
383         update_record('form_fields_map',$map);
384     }
385 }
386
387 function form_get_maps($form_id) {
388     return get_records('form_fields_map','form_id',$form_id,'display_order ASC');
389 }
390
391 // Move a form/field map up
392 function form_field_moveup($map) {
393     $map->display_order = $map->display_order - 11;
394     update_record('form_fields_map',$map);
395     form_reorder($map->form_id);
396 }
397
398 // Move a form/field map down
399 function form_field_movedown($map) {
400     $map->display_order = $map->display_order + 11;
401     update_record('form_fields_map',$map);
402     form_reorder($map->form_id);
403 }
404
405 /*
406  * - includes an array of field_id keys $field_id => array($internal_name => $title)
407  *
408  */
409
410 function form_get_form_definition($form_id) {
411     global $CFG;
412     
413     // get the main form data
414     
415     $form = get_record('forms','ident',$form_id);
416     
417     if ($form) {
418         $fields_from_database = get_records_sql("SELECT f.*, m.ident AS map_id FROM {$CFG->prefix}form_fields f INNER JOIN {$CFG->prefix}form_fields_map m ".
419             "ON (f.ident = m.field_id) WHERE m.form_id = $form_id ORDER by m.display_order ASC");
420     }
421     
422     $fields = array();
423     
424     if (!empty($fields_from_database)) {   
425         foreach ($fields_from_database AS $field) {
426             $fields[] =  $field;
427         }
428     }
429     $form->fields = $fields;
430     
431     return $form;
432 }
433
434 function form_set_form_definition() {
435     $action = optional_param('action','');
436     $form = new stdClass;
437
438     $form->name = optional_param('form_name','');
439     $form->title = optional_param('form_title','');
440     $form->description = optional_param('description','');
441     $form->response_text = optional_param('response_text','');
442     $form->access = optional_param('access','');
443     $form->notification = optional_param('notification',0,PARAM_INT);
444     if ($action == 'change') {
445         $form->ident = optional_param('form_id',0,PARAM_INT);
446         update_record('forms',$form);
447     } else if ($action == 'add') {
448         $form->created = time();
449         $form->owner = $_SESSION['userid'];
450         $form->ident = insert_record('forms',$form);
451     }
452     
453     return $form;
454 }
455
456 function form_handle_profile() {
457     global $data;
458     if (!isset($data['profile:details'])) {
459         
460         // get the registration field ids, if any
461         $registration_field_ids = array();
462         $registration_form_id = get_field('forms','ident','name','registration');
463         if (isset($registration_form_id)) {
464             $registration_form = form_get_form_definition($registration_form_id);
465             if (isset($registration_form) && isset($registration_form->fields)) {
466                 foreach($registration_form->fields AS $field) {
467                      $registration_field_ids[] = $field->ident;
468                  }
469              }
470          }
471         
472          // get the profile form and fields
473         
474         $profile_form_id = get_field('forms','ident','name','profile');
475         if (isset($profile_form_id)) {
476             $profile_form = form_get_form_definition($profile_form_id);
477         }
478         if (isset($profile_form) && isset($profile_form->fields)) {
479             foreach ($profile_form->fields AS $field) {
480                 if ($field->profile) {
481                     form_export_profile_data($field,'person',$registration_field_ids);
482                 }                   
483             }
484         }
485         
486          // get the community profile form and fields
487         
488         $profile_form_id = get_field('forms','ident','name'