dataid = $dataid;
$newfield->type = $type;
$newfield->name = $name;
$newfield->description = $desc;
$newfield->param1 = $autolink;
$newfield->param2 = $width;
$newfield->param3 = $height;
if (!insert_record('data_fields', $newfield)) {
notify('Insertion of new field failed!');
}
}
/***********************************************
* Prints the form element in the add template *
***********************************************/
function display_add_field($id, $rid=0) {
global $CFG;
if (!$field = get_record('data_fields', 'id', $id)){
notify('That is not a valid field id!');
exit;
}
if ($rid) {
$dataContent = get_record('data_content', 'fieldid', $id, 'recordid', $rid);
$content = $dataContent->content;
}
else {
$content = '';
}
$str = '';
if ($field->description) {
$str .= ' ';
}
if (can_use_richtext_editor()) {
// Show a rich text html editor.
$str .= helpbutton("richtext", get_string("helprichtext"), "moodle", true, true, '', true);
$str .= data_field_textarea::gen_textarea(true, 'field_' . $field->id, $field->param2, $field->param3, $content);
$str .= '';
}
else {
// Show a normal textarea. Also let the user specify the format to be used.
$str .= data_field_textarea::gen_textarea(false, 'field_' . $field->id, $field->param2, $field->param3, $content);
// Get the available text formats for this field.
$formatsForField = format_text_menu();
$str .= ' ';
if (empty($dataContent->content1)) {
$str .= choose_from_menu($formatsForField, 'field_' . $field->id . '_content1', '', 'choose', '', '', true);
}
else {
$str .= choose_from_menu($formatsForField, 'field_' . $field->id . '_content1', $dataContent->content1, 'choose', '', '', true);
}
$str .= helpbutton("textformat", get_string("helpformatting"), 'moodle', true, false, '', true);
}
return $str;
}
function gen_textarea($usehtmleditor, $name, $cols=65, $rows=10, $value='') {
global $CFG, $course;
static $scriptcount; // For loading the htmlarea script only once.
if (empty($courseid)) {
if (!empty($course->id)) { // Search for it in global context.
$courseid = $course->id;
}
}
if (empty($scriptcount)) {
$scriptcount = 0;
}
$output = '';
if ($usehtmleditor) {
if (!empty($courseid) and isteacher($courseid)) {
$output .= ($scriptcount < 1) ? ''."\n" : '';
}
else {
$output .= ($scriptcount < 1) ? ''."\n" : '';
}
$output .= ($scriptcount < 1) ? ''."\n" : '';
$scriptcount++;
}
$output .= ''."\n";
return $output;
}
function print_after_form() {
if (can_use_richtext_editor()) {
$this->use_html_editor('field_' . $this->id);
}
}
/**
* Sets up the HTML editor on textareas in the current page.
* If a field name is provided, then it will only be
* applied to that field - otherwise it will be used
* on every textarea in the page.
*
* This is basically the same as use_html_editor() in
* /lib/weblib.php, except that this function returns a
* string instead of echoing out the javascript. The
* reasons why /lib/weblib.php has not been modified are:
*
* 1) So that the database module is compatible with
* Moodle 1.5.x
* 2) The weblib will be reworked in the future use
* smarty
*
* @param string $name Form element to replace with HTMl editor by name
*/
function use_html_editor($name='', $editorhidebuttons='') {
echo ''."\n";
}
function display_edit_field($id, $mode=0) {
parent::display_edit_field($id, $mode);
}
function update($fieldobject) {
$fieldobject->param1 = trim($fieldobject->param1);
$fieldobject->param2 = trim($fieldobject->param2);
$fieldobject->param3 = trim($fieldobject->param3);
if (!update_record('data_fields',$fieldobject)){
notify ('upate failed');
}
}
/************************************
* store content of this field type *
************************************/
function store_data_content($fieldid, $recordid, $value, $name=''){
if ($value) {
$content = new object;
$content->fieldid = $fieldid;
$content->recordid = $recordid;
if ($oldcontent = get_record('data_content','fieldid', $fieldid, 'recordid', $recordid)) {
// This belongs to an existing data_content.
$content->id = $oldcontent->id;
$nameParts = explode('_', $name);
$column = $nameParts[count($nameParts) - 1]; // Format is field__content[1 to 4]
$content->$column = clean_param($value, PARAM_INT);
update_record('data_content', $content);
}
else {
// First (and maybe only) data content for this field for this record.
$content->content = clean_param($value, PARAM_CLEANHTML);
insert_record('data_content', $content);
}
}
}
/*************************************
* update content of this field type *
*************************************/
function update_data_content($fieldid, $recordid, $value, $name=''){
// If data_content already exists, we update.
if ($oldcontent = get_record('data_content','fieldid', $fieldid, 'recordid', $recordid)){
$content = new object;
$content->fieldid = $fieldid;
$content->recordid = $recordid;
$nameParts = explode('_', $name);
if (!empty($nameParts[2])) {
$content->$nameParts[2] = clean_param($value, PARAM_NOTAGS);
}
else {
$content->content = clean_param($value, PARAM_NOTAGS);
}
$content->id = $oldcontent->id;
update_record('data_content', $content);
}
else { //make 1 if there isn't one already
$this->store_data_content($fieldid, $recordid, $value, $name='');
}
}
}
?>