function template_selects($template)
{
    $selects = array();
    $fld_start = '<input ';
    $fld_start_len = strlen($fld_start);
    $fld_end = '>';
    $fld_end_len = strlen($fld_end);
    $offset = 0;
    do {
        $pos = strpos($template, $fld_start, $offset);
        if ($pos === false) {
            break;
        }
        $offset = $pos + $fld_start_len;
        $pos = strpos($template, $fld_end, $offset);
        if ($pos === false) {
            break;
        }
        $element = split_input_field(substr($template, $offset, $pos - $offset));
        $offset = $pos + $fld_end_len;
        if ($element['type'] == 'select') {
            $selects[$element['id']] = $element['name'];
        }
    } while (true);
    return $selects;
}
 function supplement_display($form, $fields)
 {
     $fld_start = '<input ';
     $fld_start_len = strlen($fld_start);
     $fld_end = '>';
     $fld_end_len = strlen($fld_end);
     $offset = 0;
     $date_format = 'd-m-y';
     do {
         $pos = strpos($form->template, $fld_start, $offset);
         if ($pos === false) {
             break;
         }
         if ($pos > $offset) {
             $this->_form->addElement('html', substr($form->template, $offset, $pos - $offset));
             // output any HTML
         }
         $offset = $pos + $fld_start_len;
         $pos = strpos($form->template, $fld_end, $offset);
         if ($pos === false) {
             break;
         }
         $element = split_input_field(substr($form->template, $offset, $pos - $offset));
         $offset = $pos + $fld_end_len;
         $text = $fields[$element['id']];
         if ($element['type'] == 'checkbox') {
             // map a checkbox value to a nice character
             if ($text == '1') {
                 $text = '&#10004;';
             } else {
                 $text = '';
             }
         }
         if ($element['type'] == 'date' && $text) {
             // map a UNIX date to a nice text string
             $date = date_create();
             date_timestamp_set($date, $text);
             $text = date_format($date, $date_format);
         }
         if ($element['type'] == 'file' && $text) {
             // map a file pathname hash to a URL for the file
             $text = get_file_link($text);
         }
         $this->_form->addElement('static', $element['id'], $element['value'], $text);
     } while (true);
     $this->_form->addElement('html', substr($form->template, $offset));
     // output any remaining HTML
     return;
 }
 function definition()
 {
     global $USER;
     $mform =& $this->_form;
     $data = new stdClass();
     $data->supplement = $this->_customdata['supplement'];
     $data->fields = $this->_customdata['fields'];
     $this->set_data($data->fields);
     $mform->addElement('hidden', 'supplement', $data->supplement->ref);
     $mform->setType('supplement', PARAM_RAW);
     $mform->addElement('hidden', 'version', $data->supplement->version);
     $mform->setType('version', PARAM_RAW);
     // Process the form
     $fld_start = '<input ';
     $fld_start_len = strlen($fld_start);
     $fld_end = '>';
     $fld_end_len = strlen($fld_end);
     $offset = 0;
     $date_format = 'd-m-y';
     $fs = get_file_storage();
     $context = context_user::instance($USER->id);
     // This 'dummy' element has two purposes:
     // - To force open the Moodle Forms invisible fieldset outside of any table on the form (corrupts display otherwise)
     // - To let us inform the user that there are validation errors without them having to scroll down further
     $mform->addElement('static', 'form_error');
     do {
         $pos = strpos($data->supplement->template, $fld_start, $offset);
         if ($pos === false) {
             break;
         }
         if ($pos > $offset) {
             $mform->addElement('html', substr($data->supplement->template, $offset, $pos - $offset));
             // output any HTML
         }
         $offset = $pos + $fld_start_len;
         $pos = strpos($data->supplement->template, $fld_end, $offset);
         if ($pos === false) {
             break;
         }
         $element = split_input_field(substr($data->supplement->template, $offset, $pos - $offset));
         $offset = $pos + $fld_end_len;
         switch ($element['type']) {
             case 'area':
                 $mform->addElement('textarea', $element['id'], $element['value'], $element['options']);
                 break;
             case 'checkbox':
                 if ($element['rule'] == 'required') {
                     // mustn't return a zero value
                     $mform->addElement('checkbox', $element['id'], $element['value']);
                 } else {
                     $mform->addElement('advcheckbox', $element['id'], $element['value'], $element['name'], null, array(0, 1));
                 }
                 break;
             case 'date':
                 $mform->addElement('date_selector', $element['id'], $element['value'], $element['options']);
                 break;
             case 'file':
                 if (isset($data->fields[$element['id']])) {
                     $pathnamehash = $data->fields[$element['id']];
                 } else {
                     $pathnamehash = '';
                 }
                 if ($pathnamehash == '') {
                     $draftitemid = 0;
                     $itemid = 0;
                 } else {
                     $file = $fs->get_file_by_hash($pathnamehash);
                     $itemid = $file->get_itemid();
                     $draftitemid = 0;
                 }
                 file_prepare_draft_area($draftitemid, $context->id, 'local_obu_application', 'file', $itemid, array('subdirs' => false, 'maxbytes' => 2097152, 'maxfiles' => 1));
                 $this->set_data(array($element['id'] => $draftitemid));
                 $mform->addElement('filepicker', $element['id'], $element['value'], null, array('maxbytes' => 2097152, 'accepted_types' => array('.pdf')));
                 break;
             case 'select':
                 switch ($element['name']) {
                     default:
                 }
                 $select = $mform->addElement('select', $element['id'], $element['value'], $options, null);
                 switch ($element['selected']) {
                     case 'start_selected':
                         $select->setSelected($data->start_selected);
                         break;
                     default:
                 }
                 break;
             case 'static':
                 switch ($element['name']) {
                     default:
                         $text = '';
                 }
                 $mform->addElement('static', '', $element['value'], $text);
                 // Display the field...
                 $mform->addElement('hidden', $element['id'], $text);
                 // ...and also return it
                 break;
             case 'text':
                 $mform->addElement('text', $element['id'], $element['value'], $element['options']);
                 $mform->setType($element['id'], PARAM_TEXT);
                 break;
             case 'alphabetic':
                 $mform->addElement('text', $element['id'], $element['value'], $element['options']);
                 $mform->setType($element['id'], PARAM_RAW);
                 $mform->addRule($element['id'], null, 'lettersonly', null, 'server');
                 // Let Moodle handle the rule
                 break;
             case 'numeric':
                 $mform->addElement('text', $element['id'], $element['value'], $element['options']);
                 $mform->setType($element['id'], PARAM_RAW);
                 $mform->addRule($element['id'], null, 'numeric', null, 'server');
                 // Let Moodle handle the rule
                 break;
             default:
         }
         if ($element['rule']) {
             // An extra validation rule applies to this field
             if ($element['rule'] == 'group') {
                 // At least one of this group of fields is required
                 $this->required_group[] = $element['id'];
                 // For our own validation
             } else {
                 $mform->addRule($element['id'], null, $element['rule'], null, 'server');
                 // Let Moodle handle the rule
                 if ($element['rule'] == 'required') {
                     $this->required_field[] = $element['id'];
                     // For our own extra validation
                 }
             }
         }
     } while (true);
     $mform->addElement('html', substr($data->supplement->template, $offset));
     // output any remaining HTML
     $this->add_action_buttons(true, get_string('save_continue', 'local_obu_application'));
 }
function get_file_elements($supplement)
{
    $files = array();
    $fld_start = '<input ';
    $fld_start_len = strlen($fld_start);
    $fld_end = '>';
    $fld_end_len = strlen($fld_end);
    $offset = 0;
    do {
        $pos = strpos($supplement, $fld_start, $offset);
        if ($pos === false) {
            break;
        }
        $offset = $pos + $fld_start_len;
        $pos = strpos($supplement, $fld_end, $offset);
        if ($pos === false) {
            break;
        }
        $element = split_input_field(substr($supplement, $offset, $pos - $offset));
        $offset = $pos + $fld_end_len;
        if ($element['type'] == 'file') {
            $files[] = $element['id'];
        }
    } while (true);
    return $files;
}
 function definition()
 {
     global $USER;
     $mform =& $this->_form;
     $data = new stdClass();
     $data->data_id = $this->_customdata['data_id'];
     $data->template = $this->_customdata['template'];
     $data->username = $this->_customdata['username'];
     $data->surname = $this->_customdata['surname'];
     $data->forenames = $this->_customdata['forenames'];
     $data->current_course = $this->_customdata['current_course'];
     $data->start_dates = $this->_customdata['start_dates'];
     $data->start_selected = $this->_customdata['start_selected'];
     $data->adviser = $this->_customdata['adviser'];
     $data->supervisor = $this->_customdata['supervisor'];
     $data->course = $this->_customdata['course'];
     $data->not_enroled = $this->_customdata['not_enroled'];
     $data->enroled = $this->_customdata['enroled'];
     $data->study_mode = $this->_customdata['study_mode'];
     $data->reason = $this->_customdata['reason'];
     $data->addition_reason = $this->_customdata['addition_reason'];
     $data->deletion_reason = $this->_customdata['deletion_reason'];
     $data->fields = $this->_customdata['fields'];
     $data->auth_state = $this->_customdata['auth_state'];
     $data->auth_level = $this->_customdata['auth_level'];
     $data->status_text = $this->_customdata['status_text'];
     $data->button_text = $this->_customdata['button_text'];
     // Start with the required hidden fields
     if ($data->data_id > 0) {
         // Using form to amend or view
         $mform->addElement('hidden', 'id', $data->data_id);
         $mform->setType('id', PARAM_RAW);
     } else {
         // Using form for initial input
         $mform->addElement('hidden', 'template', $data->template->id);
         $mform->setType('template', PARAM_RAW);
     }
     $mform->addElement('hidden', 'auth_state', $data->auth_state);
     $mform->setType('auth_state', PARAM_RAW);
     $mform->addElement('hidden', 'auth_level', $data->auth_level);
     $mform->setType('auth_level', PARAM_RAW);
     // Process the template
     $fld_start = '<input ';
     $fld_start_len = strlen($fld_start);
     $fld_end = '>';
     $fld_end_len = strlen($fld_end);
     $offset = 0;
     $date_format = 'd-m-y';
     // This 'dummy' element has two purposes:
     // - To force open the Moodle Forms invisible fieldset outside of any table on the form (corrupts display otherwise)
     // - To let us inform the user that there are validation errors without them having to scroll down further
     $mform->addElement('static', 'form_error');
     do {
         $pos = strpos($data->template->data, $fld_start, $offset);
         if ($pos === false) {
             break;
         }
         if ($pos > $offset) {
             $mform->addElement('html', substr($data->template->data, $offset, $pos - $offset));
             // output any HTML
         }
         $offset = $pos + $fld_start_len;
         $pos = strpos($data->template->data, $fld_end, $offset);
         if ($pos === false) {
             break;
         }
         $element = split_input_field(substr($data->template->data, $offset, $pos - $offset));
         $offset = $pos + $fld_end_len;
         if (!empty($data->fields)) {
             // simply display the field
             $text = $data->fields[$element['id']];
             if ($element['type'] == 'checkbox') {
                 // map a checkbox value to a nice character
                 if ($text == '1') {
                     $text = '&#10004;';
                 } else {
                     $text = '';
                 }
             }
             if ($element['type'] == 'date' && $text) {
                 // map a UNIX date to a nice text string
                 $date = date_create();
                 date_timestamp_set($date, $text);
                 $text = date_format($date, $date_format);
             }
             $mform->addElement('static', $element['id'], $element['value'], $text);
         } else {
             switch ($element['type']) {
                 case 'area':
                     $mform->addElement('textarea', $element['id'], $element['value'], $element['options']);
                     break;
                 case 'checkbox':
                     if (array_key_exists('rule', $element) && $element['rule'] == 'required') {
                         // mustn't return a zero value
                         $mform->addElement('checkbox', $element['id'], $element['value']);
                     } else {
                         if (array_key_exists('name', $element)) {
                             $mform->addElement('advcheckbox', $element['id'], $element['value'], $element['name'], null, array(0, 1));
                         } else {
                             $mform->addElement('advcheckbox', $element['id'], $element['value'], null, null, array(0, 1));
                         }
                     }
                     break;
                 case 'date':
                     if (array_key_exists('options', $element)) {
                         $mform->addElement('date_selector', $element['id'], $element['value'], $element['options']);
                     } else {
                         $mform->addElement('date_selector', $element['id'], $element['value']);
                     }
                     break;
                 case 'select':
                     switch ($element['name']) {
                         case 'start_dates':
                             $options = $data->start_dates;
                             break;
                         case 'adviser':
                             $options = $data->adviser;
                             break;
                         case 'supervisor':
                             $options = $data->supervisor;
                             break;
                         case 'course':
                             $options = $data->course;
                             break;
                         case 'not_enroled':
                             $options = $data->not_enroled;
                             break;
                         case 'enroled':
                             $options = $data->enroled;
                             break;
                         case 'study_mode':
                             $options = $data->study_mode;
                             break;
                         case 'reason':
                             $options = $data->reason;
                             break;
                         case 'addition_reason':
                             $options = $data->addition_reason;
                             break;
                         case 'deletion_reason':
                             $options = $data->deletion_reason;
                             break;
                         default:
                     }
                     $select = $mform->addElement('select', $element['id'], $element['value'], $options, null);
                     if (array_key_exists('selected', $element)) {
                         switch ($element['selected']) {
                             case 'start_selected':
                                 $select->setSelected($data->start_selected);
                                 break;
                             default:
                         }
                     }
                     break;
                 case 'static':
                     switch ($element['name']) {
                         case 'username':
                             $text = $data->username;
                             break;
                         case 'surname':
                             $text = $data->surname;
                             break;
                         case 'forenames':
                             $text = $data->forenames;
                             break;
                         case 'current_course':
                             $text = $data->current_course;
                             break;
                         default:
                             $text = '';
                     }
                     $mform->addElement('static', '', $element['value'], $text);
                     // Display the field...
                     $mform->addElement('hidden', $element['id'], $text);
                     // ...and also return it
                     $mform->setType($element['id'], PARAM_RAW);
                     break;
                 case 'text':
                     $mform->addElement('text', $element['id'], $element['value'], $element['options']);
                     $mform->setType($element['id'], PARAM_RAW);
                     break;
                 case 'alphabetic':
                     $mform->addElement('text', $element['id'], $element['value'], $element['options']);
                     $mform->setType($element['id'], PARAM_RAW);
                     $mform->addRule($element['id'], null, 'lettersonly', null, 'server');
                     // Let Moodle handle the rule
                     break;
                 case 'numeric':
                     $mform->addElement('text', $element['id'], $element['value'], $element['options']);
                     $mform->setType($element['id'], PARAM_RAW);
                     $mform->addRule($element['id'], null, 'numeric', null, 'server');
                     // Let Moodle handle the rule
                     break;
                 default:
             }
             if (array_key_exists('rule', $element)) {
                 // An extra validation rule applies to this field
                 if ($element['rule'] == 'group') {
                     // At least one of this group of fields is required
                     $this->required_group[] = $element['id'];
                     // For our own validation
                 } else {
                     if ($element['rule'] == 'check') {
                         // A (single) check box that controls whether groups of fields are mandatory
                         $this->check_id = $element['id'];
                         // For our own validation
                     } else {
                         if ($element['rule'] == 'check_set') {
                             // A field that is mandatory if the controlling check box is set
                             $this->set_group[] = $element['id'];
                             // For our own validation
                         } else {
                             if ($element['rule'] == 'check_unset') {
                                 // A field that is mandatory if the controlling check box is unset
                                 $this->unset_group[] = $element['id'];
                                 // For our own validation
                             } else {
                                 $mform->addRule($element['id'], null, $element['rule'], null, 'server');
                                 // Let Moodle handle the rule
                                 if ($element['rule'] == 'required') {
                                     $this->required_field[] = $element['id'];
                                     // For our own extra validation
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } while (true);
     $mform->addElement('html', substr($data->template->data, $offset));
     // output any remaining HTML
     if (!empty($data->status_text)) {
         $mform->addElement('html', '<p /><strong>' . $data->status_text . '</strong>');
         // output any status text
     }
     $buttonarray = array();
     if ($data->button_text != 'cancel') {
         $buttonarray[] =& $mform->createElement('submit', 'submitbutton', get_string($data->button_text, 'local_obu_forms'));
     }
     if ($data->button_text != 'continue') {
         if ($data->button_text == 'authorise') {
             $mform->addElement('text', 'comment', get_string('comment', 'local_obu_forms'));
             $mform->setType('comment', PARAM_RAW);
             $buttonarray[] =& $mform->createElement('submit', 'rejectbutton', get_string('reject', 'local_obu_forms'));
             if (is_manager() && $data->auth_state == 0) {
                 // This user can redirect the form
                 $buttonarray[] =& $mform->createElement('submit', 'redirectbutton', get_string('redirect', 'local_obu_forms'));
             }
         }
         $buttonarray[] =& $mform->createElement('cancel');
     } else {
         if (is_manager() && $data->auth_state == 0) {
             // This user can redirect the form
             $buttonarray[] =& $mform->createElement('submit', 'redirectbutton', get_string('redirect', 'local_obu_forms'));
         }
     }
     $mform->addGroup($buttonarray, 'buttonarray', '', array(' '), false);
     $mform->closeHeaderBefore('buttonarray');
 }