function coursemetadata_edit_field($id, $datatype, $redirect)
{
    global $CFG, $DB, $OUTPUT, $PAGE;
    if (!($field = $DB->get_record('coursemetadata_info_field', array('id' => $id)))) {
        $field = new stdClass();
        $field->datatype = $datatype;
        $field->description = '';
        $field->descriptionformat = FORMAT_HTML;
        $field->defaultdata = '';
        $field->defaultdataformat = FORMAT_HTML;
    }
    // Clean and prepare description for the editor
    $field->description = clean_text($field->description, $field->descriptionformat);
    $field->description = array('text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0);
    require_once 'coursemetadata_field_form.php';
    $fieldform = new coursemetadata_field_form(null, $field->datatype);
    // Convert the data format for
    if (is_array($fieldform->editors())) {
        foreach ($fieldform->editors() as $editor) {
            if (isset($field->{$editor})) {
                $field->{$editor} = clean_text($field->{$editor}, $field->{$editor . 'format'});
                $field->{$editor} = array('text' => $field->{$editor}, 'format' => $field->{$editor . 'format'}, 'itemid' => 0);
            }
        }
    }
    $fieldform->set_data($field);
    if ($fieldform->is_cancelled()) {
        redirect($redirect);
    } else {
        if ($data = $fieldform->get_data()) {
            require_once $CFG->dirroot . '/course/coursemetadata/field/' . $datatype . '/define.class.php';
            $newfield = 'coursemetadata_define_' . $datatype;
            $formfield = new $newfield();
            // Collect the description and format back into the proper data structure from the editor
            // Note: This field will ALWAYS be an editor
            $data->descriptionformat = $data->description['format'];
            $data->description = $data->description['text'];
            // Check whether the default data is an editor, this is (currently) only the
            // textarea field type
            if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) {
                // Collect the default data and format back into the proper data structure from the editor
                $data->defaultdataformat = $data->defaultdata['format'];
                $data->defaultdata = $data->defaultdata['text'];
            }
            // Convert the data format for
            if (is_array($fieldform->editors())) {
                foreach ($fieldform->editors() as $editor) {
                    if (isset($field->{$editor})) {
                        $field->{$editor . 'format'} = $field->{$editor}['format'];
                        $field->{$editor} = $field->{$editor}['text'];
                    }
                }
            }
            $formfield->define_save($data);
            coursemetadata_reorder_fields();
            coursemetadata_reorder_categories();
            redirect($redirect);
        }
        $datatypes = coursemetadata_list_datatypes();
        if (empty($id)) {
            $strheading = get_string('coursemetadatacreatenewfield', 'admin', $datatypes[$datatype]);
        } else {
            $strheading = get_string('coursemetadataeditfield', 'admin', $field->name);
        }
        /// Print the page
        $PAGE->navbar->add($strheading);
        echo $OUTPUT->header();
        echo $OUTPUT->heading($strheading);
        $fieldform->display();
        echo $OUTPUT->footer();
        die;
    }
}
        foreach ($fields as $field) {
            $table->data[] = array(format_string($field->name), coursemetadata_field_icons($field));
        }
    }
    echo $OUTPUT->heading(format_string($category->name) . ' ' . coursemetadata_category_icons($category));
    if (count($table->data)) {
        echo html_writer::table($table);
    } else {
        echo $OUTPUT->notification($strnofields);
    }
}
/// End of $categories foreach
echo '<hr />';
echo '<div class="metadataeditor">';
/// Create a new field link
$options = coursemetadata_list_datatypes();
$popupurl = new moodle_url('/course/coursemetadata/index.php?id=0&action=editfield');
echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array('' => $strcreatefield), 'newfieldform');
//add a div with a class so themers can hide, style or reposition the text
html_writer::start_tag('div', array('class' => 'adminuseractionhint'));
echo get_string('or', 'lesson');
html_writer::end_tag('div');
/// Create a new category link
$options = array('action' => 'editcategory');
echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('coursemetadatacreatecategory', 'admin'));
echo '</div>';
echo $OUTPUT->footer();
die;
/***** Some functions relevant to this script *****/
/**
 * Create a string containing the editing icons for the course metadata categories