]> git.mjollnir.org Git - moodle.git/commitdiff
Various tidy-ups
authormoodler <moodler>
Thu, 23 Mar 2006 10:01:21 +0000 (10:01 +0000)
committermoodler <moodler>
Thu, 23 Mar 2006 10:01:21 +0000 (10:01 +0000)
mod/data/add.php
mod/data/lib.php
mod/data/templates.php

index f8f9b97a492a6bd1cd00b476088b98d813f4d9fd..5ff4f92a36f3a2465d3c5ca0e597892c124e00d8 100755 (executable)
@@ -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) {
             error (get_string('noaccess','data'));
         }
     }
+
+    if ($cancel) {
+        redirect('view.php?d='.$data->id);
+    }
   
 
 /// Print the page header
             $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 '<div align="center"><input type="submit" value="'.get_string('save','data').'" />';
     if ($rid){
-        echo '&nbsp;<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)">';
+        echo '&nbsp;<input type="submit" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)">';
     }
     echo '</div>';
     print_simple_box_end();
 /// 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);
index dd05cc2222cb4d988baa8f1ca44c853f0680fc05..8c3e39c04b613d09c453e5dfe5b0831a58faa875 100755 (executable)
@@ -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 .= '<div align="center">';
+        $str = '<div align="center">';
         $str .= '<table>';
 
-        foreach ($fields as $cfield){
+        foreach ($fields as $field){
 
             $str .= '<tr><td valign="top" align="right">';
-            $str .= $cfield->name.':';
+            $str .= $field->name.':';
             $str .= '</td>';
 
             $str .='<td>';
-            $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 .= '</td></tr>';
-            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 .= '<tr><td align="center" colspan="2">##Edit##  ##More##  ##Delete##  ##Approve##</td></tr>';
         }
 
         $str .= '</table>';
         $str .= '</div>';
 
-        if ($mode == 'listtemplate'){
+        if ($template == 'listtemplate'){
             $str .= '<br />';
         }
-        $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 '<div align="center">'.get_string('emptyadd', 'data').'</div><p></p>';
-        //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 .= '<div align="center">';
-            $str .= '<table>';
-
-            foreach ($fields as $cfield){
-                
-                $str .= '<tr>';
-                $str .= '<td valign="top" align="right">';
-                $str .= $cfield->name.':';
-                $str .= '</td>';
-
-                $str .='<td valign="top">';
-                $g = data_get_field($cfield, $currentdata);
-                $str .= $g->display_add_field($rid);
-                $str .= '</td>';
-                $str .= '</tr>';
-                unset($g);
+        if ($update) {
+            $newdata->id = $data->id;
+            $newdata->{$mode} = $str;
+            if (!update_record('data', $newdata)) {
+                notify('Error updating template');
             }
-
-            $str .= '</table>';
-            $str .= '</div>';
-
         }
-        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
+?>
index 9f6186edf7a16deb62b387872c7dd8747f6dfd5f..000d7be61ecb65ddd5af5942af82c8bd4c1ebad4 100755 (executable)
 
         // 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&amp;d=$data->id", $data->id, $cm->id);
         } else {