MDL-18542:

Correctly deals with field types that have multiple parts (e.g. data and lat/long)
Thanks to Eloy for spotting this.
This commit is contained in:
thepurpleblob
2009-03-19 20:39:45 +00:00
parent 1316b059c9
commit e04eb3c786
4 changed files with 40 additions and 19 deletions
+17 -14
View File
@@ -139,7 +139,6 @@
}
include('tabs.php');
/// Process incoming data for adding/updating records
if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') 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});
+8 -4
View File
@@ -53,7 +53,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;
@@ -81,14 +88,11 @@ class data_field_date extends data_field_base {
}
function update_content($recordid, $value, $name='') {
$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;
+6
View File
@@ -68,6 +68,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;
$lats = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content');
+9 -1
View File
@@ -178,6 +178,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() {
@@ -378,7 +386,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.']]';
}