From cfd799821690f6f8cab5ea3e801f1181c877e805 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Thu, 19 Mar 2009 20:45:40 +0000 Subject: [PATCH] MDL-18542: Correctly deals with field types that have multiple parts (e.g. data and lat/long) Thanks to Eloy for spotting this. Merged from STABLE_19 --- mod/data/edit.php | 31 ++++++++++++++------------ mod/data/field/date/field.class.php | 11 ++++++--- mod/data/field/latlong/field.class.php | 6 +++++ mod/data/lib.php | 10 ++++++++- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/mod/data/edit.php b/mod/data/edit.php index db985c091a..d7646a1676 100755 --- a/mod/data/edit.php +++ b/mod/data/edit.php @@ -139,7 +139,6 @@ } include('tabs.php'); - /// Process incoming data for adding/updating records if ($datarecord = data_submitted() and confirm_sesskey()) { @@ -161,14 +160,16 @@ /// Update all content $field = NULL; foreach ($fieldids as $fieldid) { - $name = "field_$fieldid"; - $value = optional_param( $name,'' ); - if (empty($field->field) || ($fieldid != $field->field->id)) { // Try to reuse classes - $field = data_get_field_from_id($fieldid, $data); - } - if ($field) { - $field->update_content($rid, $value, $name); - } + $bits = explode('_',$fieldid); + $justid = $bits[0]; + $name = "field_$fieldid"; + $value = optional_param( $name,'' ); + if (empty($field->field) || ($justid != $field->field->id)) { // Try to reuse classes + $field = data_get_field_from_id($fieldid, $data); + } + if ($field) { + $field->update_content($rid, $value, $name); + } } add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&rid=$rid", $data->id, $cm->id); @@ -195,9 +196,11 @@ $emptyform = true; // assume the worst foreach ($fieldids as $fieldid) { + $bits = explode('_',$fieldid); + $justid = $bits[0]; $name = "field_$fieldid"; $value = optional_param( $name,'' ); - if (empty($field->field) || ($fieldid != $field->field->id)) { // Try to reuse classes + if (empty($field->field) || ($justid != $field->field->id)) { // Try to reuse classes $field = data_get_field_from_id($fieldid, $data); } if ($field->notemptyfield($value, $name)) { @@ -205,7 +208,6 @@ break; // if anything has content, this form is not empty, so stop now! } } - if ($emptyform){ //nothing gets written to database notify(get_string('emptyaddform','data')); } @@ -222,16 +224,17 @@ //for each field in the add form, add it to the data_content. foreach ($fieldids as $fieldid) { + $bits = explode('_',$fieldid); + $justid = $bits[0]; $name = "field_$fieldid"; $value = optional_param( $name,'' ); - if (empty($field->field) || ($fieldid != $field->field->id)) { // Try to reuse classes + if (empty($field->field) || ($justid != $field->field->id)) { // Try to reuse classes $field = data_get_field_from_id($fieldid, $data); } if ($field) { $field->update_content($recordid, $value, $name); } } - add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&rid=$recordid", $data->id, $cm->id); notify(get_string('entrysaved','data')); @@ -278,7 +281,7 @@ $replacements[] = $field->display_add_field($rid); $patterns[]="[[".$field->field->name."#id]]"; $replacements[] = 'field_'.$field->field->id; - $data->fieldids[] = $field->field->id; + $field->list_add_field( $data->fieldids ); } $newtext = str_ireplace($patterns, $replacements, $data->{$mode}); diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index 6d7297cd11..c90b2d4a75 100755 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -50,7 +50,14 @@ class data_field_date extends data_field_base { return $str; } - + + function list_add_field(&$fields) { + $fields[] = $this->field->id.'_day'; + $fields[] = $this->field->id.'_month'; + $fields[] = $this->field->id.'_year'; + return true; + } + //Enable the following three functions once core API issues have been addressed. function display_search_field($value=0) { return false; @@ -82,11 +89,9 @@ class data_field_date extends data_field_base { $names = explode('_',$name); $name = $names[2]; // day month or year - $this->$name = $value; if ($this->day and $this->month and $this->year) { // All of them have been collected now - $content = new object; $content->fieldid = $this->field->id; $content->recordid = $recordid; diff --git a/mod/data/field/latlong/field.class.php b/mod/data/field/latlong/field.class.php index 87ed62a7d4..81c047c5f8 100755 --- a/mod/data/field/latlong/field.class.php +++ b/mod/data/field/latlong/field.class.php @@ -65,6 +65,12 @@ class data_field_latlong extends data_field_base { return $str; } + function list_add_field( &$fields ) { + $fields[] = $this->field->id.'_0'; + $fields[] = $this->field->id.'_1'; + return true; + } + function display_search_field($value = '') { global $CFG, $DB; $lats = $DB->get_records_sql_menu('SELECT id, content FROM {data_content} WHERE fieldid=? GROUP BY content ORDER BY content', array($this->field->id)); diff --git a/mod/data/lib.php b/mod/data/lib.php index 89b7605fd9..c875b8c9f5 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -205,6 +205,14 @@ class data_field_base { // Base class for Database Field Types (see field/*/ return $str; } + +// add the field ids to an existing array to track added form fields +// override if anything with multiple fields (e.g. date) + function list_add_field( &$fields ) { + $fields[] = $this->field->id; + return true; + } + // Print the relevant form element to define the attributes for this field // viewable by teachers only. function display_edit_field() { @@ -407,7 +415,7 @@ function data_generate_default_template(&$data, $template, $recordid=0, $form=fa if ($form) { // Print forms instead of data $fieldobj = data_get_field($field, $data); $str .= $fieldobj->display_add_field($recordid); - $data->fieldids[] = $fieldobj->field->id; + $fieldobj->list_add_field( $data->fieldids ); } else { // Just print the tag $str .= '[['.$field->name.']]'; } -- 2.39.5