From: moodler Date: Thu, 23 Mar 2006 10:01:21 +0000 (+0000) Subject: Various tidy-ups X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=d118d06afceac7e0f702e0b587246568bbeb5655;p=moodle.git Various tidy-ups --- diff --git a/mod/data/add.php b/mod/data/add.php index f8f9b97a49..5ff4f92a36 100755 --- a/mod/data/add.php +++ b/mod/data/add.php @@ -31,6 +31,7 @@ $d = optional_param('d', 0, PARAM_INT); // database id $rid = optional_param('rid', 0, PARAM_INT); //record id $import = optional_param('import', 0, PARAM_INT); // show import form + $cancel = optional_param('cancel', ''); // cancel an add $mode ='addtemplate'; //define the mode for this page, only 1 mode available if ($id) { @@ -72,6 +73,10 @@ error (get_string('noaccess','data')); } } + + if ($cancel) { + redirect('view.php?d='.$data->id); + } /// Print the page header @@ -233,20 +238,18 @@ $field = data_get_field($eachfield, $data); $patterns[]="/\[\[".$field->field->name."\]\]/i"; $replacements[] = $field->display_add_field($rid); - - unset($g); } $newtext = preg_replace($patterns, $replacements, $data->{$mode}); - } - else { //if the add template is not yet defined, print the default form! - data_generate_empty_add_form($data->id, $rid); + + } else { //if the add template is not yet defined, print the default form! + echo data_generate_default_template($data, 'addtemplate', $rid, true, false); $newtext = ''; } echo $newtext; echo '
'; if ($rid){ - echo ' '; + echo ' '; } echo '
'; print_simple_box_end(); @@ -296,13 +299,10 @@ /// Finish the page // Print the stuff that need to come after the form fields. - $storedFields = get_records('data_fields', 'dataid', $data->id); - foreach ($storedFields as $sf) { - $fieldClass = 'data_field_' . $sf->type; - $fieldObj = new $fieldClass($sf->id); - - echo "\n\n"; - $fieldObj->print_after_form(); + $fields = get_records('data_fields', 'dataid', $data->id); + foreach ($fields as $eachfield) { + $field = data_get_field($eachfield, $data); + $field->print_after_form(); } print_footer($course); diff --git a/mod/data/lib.php b/mod/data/lib.php index dd05cc2222..8c3e39c04b 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -306,93 +306,60 @@ class data_field_base { /// Base class for Database Field Types (see field/* /***************************************************************************** -/* Given a mode and a dataid, generate a default case template * - * input @param mode - addtemplate, singletemplate, listtempalte, rsstemplate* +/* Given a template and a dataid, generate a default case template * + * input @param template - addtemplate, singletemplate, listtempalte, rsstemplate* * @param dataid * * output null * *****************************************************************************/ -function data_generate_default_form($dataid, $mode){ - if (!$dataid && !$mode){ +function data_generate_default_template($data, $template, $recordid=0, $form=false, $update=true) { + + if (!$data && !$template){ return false; } //get all the fields for that database - if ($fields = get_records('data_fields','dataid',$dataid)){ - $data->id = $dataid; - $str = ''; //the string to write to the given $data->{$mode} - //this only applies to add and single template + if ($fields = get_records('data_fields', 'dataid', $data->id)){ - $str .= '
'; + $str = '
'; $str .= ''; - foreach ($fields as $cfield){ + foreach ($fields as $field){ $str .= ''; $str .=''; - unset($g); } - if ($mode!='addtemplate' and $mode!='rsstemplate'){ //if not adding, we put tags in there + if ($template != 'addtemplate' and $template != 'rsstemplate') { //if not adding, we put tags in there $str .= ''; } $str .= '
'; - $str .= $cfield->name.':'; + $str .= $field->name.':'; $str .= ''; - $str .= '[['.$cfield->name.']]'; + if ($form) { /// Print forms instead of data + $fieldobj = data_get_field($field, $data); + $str .= $fieldobj->display_add_field($recordid); + + } else { /// Just print the tag + $str .= '[['.$field->name.']]'; + } $str .= '
##Edit## ##More## ##Delete## ##Approve##
'; $str .= '
'; - if ($mode == 'listtemplate'){ + if ($template == 'listtemplate'){ $str .= '
'; } - $data->{$mode} = $str; - //make the header and footer for listtempalte - update_record('data', $data); - } -} - -/********************************************************* - * generates an empty add form, if there is none. * - * input: @(int)id, id of the data * - * output: null * - *********************************************************/ -function data_generate_empty_add_form($id, $rid=0){ - $currentdata = get_record('data','id',$id); - //check if there is an add entry - if (!$currentdata->addtemplate){ - echo '
'.get_string('emptyadd', 'data').'

'; - //get all the field entry, and print studnet version - - if ($fields = get_records('data_fields','dataid',$currentdata->id)){ - $str = ''; //the string to write to the given $data->{$mode} - //this only applies to add and single template - - $str .= '
'; - $str .= ''; - - foreach ($fields as $cfield){ - - $str .= ''; - $str .= ''; - - $str .=''; - $str .= ''; - unset($g); + if ($update) { + $newdata->id = $data->id; + $newdata->{$mode} = $str; + if (!update_record('data', $newdata)) { + notify('Error updating template'); } - - $str .= '
'; - $str .= $cfield->name.':'; - $str .= ''; - $g = data_get_field($cfield, $currentdata); - $str .= $g->display_add_field($rid); - $str .= '
'; - $str .= '
'; - } - echo $str; + + return $str; } } @@ -594,11 +561,11 @@ function data_tags_check($dataid, $template){ $possiblefields = get_records('data_fields','dataid',$dataid); ///then we generate strings to replace $tagsok = true; //let's be optimistic - foreach ($possiblefields as $cfield){ - $pattern="/\[\[".$cfield->name."\]\]/i"; + foreach ($possiblefields as $field){ + $pattern="/\[\[".$field->name."\]\]/i"; if (preg_match_all($pattern, $template, $dummy)>1){ $tagsok = false; - notify ('[['.$cfield->name.']] - '.get_string('multipletags','data')); + notify ('[['.$field->name.']] - '.get_string('multipletags','data')); } } //else return true @@ -852,11 +819,10 @@ function data_print_template($records, $data, $search, $template, $sort, $page=0 $possiblefields = get_records('data_fields','dataid',$data->id); ///then we generate strings to replace for normal tags - foreach ($possiblefields as $cfield) { - $patterns[]='/\[\['.$cfield->name.'\]\]/i'; - $g = data_get_field($cfield, $data); - $replacement[] = highlight($search, $g->display_browse_field($record->id, $template)); - unset($g); + foreach ($possiblefields as $ff) { + $patterns[]='/\[\['.$ff->name.'\]\]/i'; + $field = data_get_field($ff, $data); + $replacement[] = highlight($search, $field->display_browse_field($record->id, $template)); } $record = get_record('data_records','id',$record->id); @@ -1269,4 +1235,4 @@ function data_convert_arrays_to_strings(&$fieldinput) { } -?> \ No newline at end of file +?> diff --git a/mod/data/templates.php b/mod/data/templates.php index 9f6186edf7..000d7be61e 100755 --- a/mod/data/templates.php +++ b/mod/data/templates.php @@ -89,13 +89,13 @@ // Generate default template. if (!empty($mytemplate->defaultform)){ - data_generate_default_form($data->id, $mode); + data_generate_default_templates($data->id, $mode); } else if (!empty($mytemplate->allforms)){ //generate all default templates - data_generate_default_form($data->id, 'singletemplate'); - data_generate_default_form($data->id, 'listtemplate'); - data_generate_default_form($data->id, 'addtemplate'); - data_generate_default_form($data->id, 'rsstemplate'); + data_generate_default_template($data, 'singletemplate'); + data_generate_default_template($data, 'listtemplate'); + data_generate_default_template($data, 'addtemplate'); + data_generate_default_template($data, 'rsstemplate'); add_to_log($course->id, 'data', 'templates def', "templates.php?id=$cm->id&d=$data->id", $data->id, $cm->id); } else {