]> git.mjollnir.org Git - moodle.git/commitdiff
Various fixes that have accumulated
authormoodler <moodler>
Wed, 5 Apr 2006 01:38:06 +0000 (01:38 +0000)
committermoodler <moodler>
Wed, 5 Apr 2006 01:38:06 +0000 (01:38 +0000)
mod/data/edit.php [moved from mod/data/add.php with 93% similarity]
mod/data/field/date/field.class.php
mod/data/index.php
mod/data/lib.php
mod/data/rsslib.php
mod/data/tabs.php
mod/data/templates.php
mod/data/view.php

similarity index 93%
rename from mod/data/add.php
rename to mod/data/edit.php
index b1548d2642e24b4767d4440578a239ec3ad65cb4..ff156854ccc678f9490b710f0d3349215c714487 100755 (executable)
     }
     
     require_course_login($course, false, $cm);
+    
+    if (!isloggedin() or isguest()) {
+        redirect('view.php?d='.$data->id);
+    }
 
 /// If it's hidden then it's don't show anything.  :)
     if (empty($cm->visible) and !isteacher($course->id)) {
 /// Check to see if groups are being used here
 
     if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, 'add.php?d='.$data->id.'&amp;sesskey='.sesskey().'&amp;');
+        $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id.'&amp;sesskey='.sesskey().'&amp;');
     } else {
         $currentgroup = 0;
     }
 
 /// Process incoming data for adding/updating records
 
-    if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/add.php') and confirm_sesskey()) {
+    if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) {
 
         $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid');  // strings to be ignored in input data
 
                 add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);
 
                 notify(get_string('entrysaved','data'));
+
+                if (!empty($datarecord->saveandview)) {
+                    redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$rid);
+                }
             }
         }
     }  // End of form processing
     $replacement = array();    //html to replace those yucky tags
 
     //form goes here first in case add template is empty
-    echo '<form enctype="multipart/form-data" action="add.php" method="post">';
+    echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
     echo '<input name="d" value="'.$data->id.'" type="hidden" />';
     echo '<input name="rid" value="'.$rid.'" type="hidden" />';
     echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
     }
    
     echo $newtext;
-    echo '<div align="center"><input type="submit" value="'.get_string('save','data').'" />';
-    if ($rid){
-        echo '&nbsp;<input type="submit" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)">';
+    echo '<div align="center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
+    if ($rid) {
+        echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)">';
+    } else {
+        echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
     }
     echo '</div>';
     print_simple_box_end();
             print_simple_box_end();
         } else {
             echo '<div align="center">';
-            echo '<a href="add.php?d='.$data->id.'&amp;import=1">'.get_string('uploadrecords', 'data').'</a>';
+            echo '<a href="edit.php?d='.$data->id.'&amp;import=1">'.get_string('uploadrecords', 'data').'</a>';
             echo '</div>';
         }
     }
index 1039f19022297a13234664a9085b9c4d1fd96f13..9b2824ea75b75ad2189e629d462d68fd56523e7e 100755 (executable)
@@ -82,6 +82,10 @@ class data_field_date extends data_field_base {
         }
     }
 
+    function get_sort_sql($fieldname) {
+        return 'CAST('.$fieldname.' AS unsigned)';
+    }
+
 
 }
 
index 88bb958ccfd52da5b1aafa41838059d3475d4764..a418cc79c4c187f70c7aedfca369683355124969 100755 (executable)
         $table->align = array ('center', 'center', 'center', 'center');
     }
 
+    $rss = (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds));
+
+    if ($rss) {
+        require_once($CFG->libdir."/rsslib.php");
+        array_push($table->head, 'RSS');
+        array_push($table->align, 'center');
+    }
+
     $currentgroup = get_current_group($course->id);
     if ($currentgroup and isteacheredit($course->id)) {
         $group = get_record("groups", "id", $currentgroup);
         } else {
             $numunapprovedrecords = get_string('noapprovalrequired', 'data');
         }
+        
+        $rsslink = '';
+        if ($rss && $data->rssarticles > 0) {
+            $rsslink = rss_get_link($course->id, $USER->id, 'data', $data->id, 'RSS');
+        }
 
         if ($course->format == 'weeks' or $course->format == 'topics') {
             if ($data->section !== $currentsection) {
                 }
                 $currentsection = $data->section;
             }
+            $row = array ($printsection, $link, $data->intro, $numrecords, $numunapprovedrecords);
     
-            $table->data[] = array ($printsection, $link, $data->intro, $numrecords, $numunapprovedrecords);
         } else {
-            $table->data[] = array ($link, $data->intro, $numrecords, $numunapprovedrecords);
+            $row = array ($link, $data->intro, $numrecords, $numunapprovedrecords);
         }
+
+        if ($rss) {
+            array_push($row, $rsslink);
+        }
+
+        $table->data[] = $row;
     }
 
     echo "<br />";
index 62f546257869b170d0b0288c3e0b183f95e4e9f4..a6278627a885c83e409c1e275a0575d2a034d6d7 100755 (executable)
@@ -289,6 +289,11 @@ class data_field_base {     /// Base class for Database Field Types (see field/*
         return 'content';
     }
 
+/// Returns the SQL needed to refer to the column.  Some fields may need to CAST() etc.
+    function get_sort_sql($fieldname) {
+        return $fieldname;
+    }
+
 /// Returns the name/type of the field
     function name(){
         return get_string('name'.$this->type, 'data');
@@ -810,7 +815,7 @@ function data_get_coursemodule_info($coursemodule) {
  *       @param string $template                                        *
  * output null                                                          *
  ************************************************************************/
-function data_print_template($template, $records, $data, $search='',$page=0, $return=false){
+function data_print_template($template, $records, $data, $search='',$page=0, $return=false) {
     global $CFG;
 
     static $fields = NULL;
@@ -824,7 +829,11 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
         $isteacher = isteacher($data->course);
     }
 
-    foreach ($records as $record) {    //only 1 record for single mode
+    if (empty($records)) {
+        return;
+    }
+
+    foreach ($records as $record) {   /// Might be just one for the single template
 
     /// Replacing tags
         $patterns = array();
@@ -840,7 +849,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
         $patterns[]='/\#\#Edit\#\#/i';
         $patterns[]='/\#\#Delete\#\#/i';
         if ($isteacher or data_isowner($record->id)) {
-            $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/add.php?d='
+            $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/edit.php?d='
                              .$data->id.'&amp;rid='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/edit.gif" height="11" width="11" border="0" alt="'.get_string('edit').'" /></a>';
             $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='
                              .$data->id.'&amp;delete='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/delete.gif" height="11" width="11" border="0" alt="'.get_string('delete').'" /></a>';
@@ -854,18 +863,21 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
         $patterns[]='/\#\#MoreURL\#\#/i';
         $replacement[] = $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$record->id;
 
+        $patterns[]='/\#\#User\#\#/i';
+        $replacement[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$record->userid.
+                               '&amp;course='.$data->course.'">'.fullname($record).'</a>';
 
         $patterns[]='/\#\#Approve\#\#/i';
         if ($isteacher && ($data->approval) && (!$record->approved)){
-            $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;approve='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/i/show.gif" height="11" width="11" border="0" alt="'.get_string('approve').'" /></a>';
+            $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;approve='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/i/approve.gif" height="11" width="11" border="0" alt="'.get_string('approve').'" /></a>';
         } else {
             $replacement[] = '';
         }
         
-        $patterns[]='/\#\#Comment\#\#/i';
+        $patterns[]='/\#\#Comments\#\#/i';
         if (($template == 'listtemplate') && ($data->comments)) {
             $comments = count_records('data_comments','recordid',$record->id);
-            $replacement[] = '<a href="comment.php?rid='.$record->id.'&amp;page='.$page.'">'.$comments.' '.get_string('comment','data').'</a>';
+            $replacement[] = '<a href="view.php?rid='.$record->id.'#comments">'.get_string('comments','data', $comments).'</a>';
         } else {
             $replacement[] = '';
         }
@@ -1089,6 +1101,8 @@ function data_get_ratings($recordid, $sort="u.firstname ASC") {
 //prints all comments + a text box for adding additional comment
 function data_print_comments($data, $record, $page=0) {
 
+    echo '<a name="comments"></a>';
+
     if ($comments = get_records('data_comments','recordid',$record->id)) {
         foreach ($comments as $comment) {
             data_print_comment($data, $comment, $page);
index 8451ee7de394ee1b507304fbc7c5fad213de3144..cb82a1b42ff31dbda4941f564807563dc59e8053 100644 (file)
                         if (!$firstfield = get_record_sql('SELECT id,name from '.$CFG->prefix.'data_fields WHERE dataid = '.$data->id.' ORDER by id', true)) {
                             continue;
                         }
-                        
+
+
                         // Get the data_records out.
+                        $approved = ($data->approval) ? ' AND dr.approved = 1 ' : ' ';
+
                         $sql = 'SELECT dr.* ' .
                                     "FROM {$CFG->prefix}data_records AS dr " .
-                                    "WHERE dr.dataid = {$data->id} " .
+                                    "WHERE dr.dataid = {$data->id} " .$approved.
                                     'ORDER BY dr.timecreated DESC ' .
                                     "LIMIT {$data->rssarticles}";
                         
index 370939b20b98850296dcaf1540b3366ed123eb79..6a868c63852fd10d69b1d4f9484326812f7b35a9 100755 (executable)
         $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&mode=single', get_string('single','data'), '', true);
     }
 
-    if (isteacher($course->id) or ($data->participants == DATA_STUDENTS_ONLY) or ($data->participants == DATA_TEACHERS_AND_STUDENTS)){
-        $addstring = empty($editentry) ? get_string('add', 'data') : get_string('editentry', 'data');
-        $row[] = new tabobject('add', $CFG->wwwroot.'/mod/data/add.php?d='.$data->id, $addstring, '', true);
-    }
-    if (isteacher($course->id)) {
-        if ($currenttab == 'list') {
-            $defaultemplate = 'listtemplate';
-        } else if ($currenttab == 'add') {
-            $defaultemplate = 'addtemplate';
-        } else {
-            $defaultemplate = 'singletemplate';
+    if (isloggedin() and !isguest()) {
+        if (isteacher($course->id) or ($data->participants == DATA_STUDENTS_ONLY) or 
+                                      ($data->participants == DATA_TEACHERS_AND_STUDENTS)){
+            $addstring = empty($editentry) ? get_string('add', 'data') : get_string('editentry', 'data');
+            $row[] = new tabobject('add', $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id, $addstring, '', true);
+        }
+        if (isteacher($course->id)) {
+            if ($currenttab == 'list') {
+                $defaultemplate = 'listtemplate';
+            } else if ($currenttab == 'add') {
+                $defaultemplate = 'addtemplate';
+            } else {
+                $defaultemplate = 'singletemplate';
+            }
+    
+            $row[] = new tabobject('templates', $CFG->wwwroot.'/mod/data/templates.php?d='.$data->id.
+                                                '&amp;mode='.$defaultemplate, get_string('templates','data'));
+            $row[] = new tabobject('fields', $CFG->wwwroot.'/mod/data/field.php?d='.$data->id, 
+                                             get_string('fields','data'), '', true);
         }
-
-        $row[] = new tabobject('templates', $CFG->wwwroot.'/mod/data/templates.php?d='.$data->id.'&amp;mode='.$defaultemplate, get_string('templates','data'));
-        $row[] = new tabobject('fields', $CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('fields','data'), '', true);
     }
 
     $tabs[] = $row;
index 5b1231e8083a75da527ef8fb13f39e363e627c7a..24aacdabb1288656456fd9431fe81c49e0bfc4b5 100755 (executable)
     echo '<option value="##delete##">##' .get_string('delete', 'data'). '##</option>';
     echo '<option value="##approve##">##' .get_string('approve', 'data'). '##</option>';
     echo '<option value="##comments##">##' .get_string('comments', 'data'). '##</option>';
+    echo '<option value="##user##">##' .get_string('user'). '##</option>';
     echo '</select>';
     echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
     echo '</td>';
index f627017214d7cec956fcd7f58182566afdd52110..fa90edd054c71bc8a779b256879f4cb8807bae62 100755 (executable)
 
 /// RSS and CSS meta
     $meta = '';
-    if (isset($CFG->enablerssfeeds) && isset($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
+    if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
         $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
         $meta .= '<link rel="alternate" type="application/rss+xml" ';
         $meta .= 'title ="'.$course->shortname.': %fullname%" href="'.$rsspath.'" />';
     print_heading(format_string($data->name));
     
     // Do we need to show a link to the RSS feed for the records?
-    if (isset($CFG->enablerssfeeds) && isset($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
+    if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
         echo '<div style="float:right;">';
         rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype'));
         echo '</div>';
         echo '<div style="clear:both;"></div>';
     }
     
-    if ($data->intro and empty($sort) and empty($search) and empty($page) and empty($record)) {
+    if ($data->intro and empty($page) and empty($record)) {
         print_simple_box(format_text($data->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
     }
 
         $currentgroup = 0;
     }
 
-
-/// Print the tabs
-
-    if ($record or $mode == 'single') {
-        $currenttab = 'single';
-    } else {
-        $currenttab = 'list';
-    }
-    include('tabs.php'); 
-
-
-/// Approve any requested records
-
-    if ($approve && confirm_sesskey() && isteacher($course->id)) {
-        if ($approverecord = get_record('data_records', 'id', $approve)) {   // Need to check this is valid
-            if ($approverecord->dataid == $data->id) {                       // Must be from this database
-                $newrecord->id = $approverecord->id;
-                $newrecord->approved = 1;
-                if (update_record('data_records', $newrecord)) {
-                    notify(get_string('recordapproved','data'), 'notifysuccess');
-                }
-            }
-        }
-    }
-
 /// Delete any requested records
 
     if ($delete && confirm_sesskey() && (isteacher($course->id) or data_isowner($delete))) {
             }
 
         } else {   // Print a confirmation page
-            notice_yesno(get_string('confirmdeleterecord','data'), 
-                         'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
-                         'view.php?d='.$data->id);
+            if ($deleterecord = get_record('data_records', 'id', $delete)) {   // Need to check this is valid
+                if ($deleterecord->dataid == $data->id) {                       // Must be from this database
+                    notice_yesno(get_string('confirmdeleterecord','data'), 
+                            'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
+                            'view.php?d='.$data->id);
 
-            print_footer($course);
-            exit;
+                    $records[] = $deleterecord;
+                    data_print_template('singletemplate', $records, $data);
+
+                    print_footer($course);
+                    exit;
+                }
+            }
+        }
+    }
+
+
+
+/// Print the tabs
+
+    if ($record or $mode == 'single') {
+        $currenttab = 'single';
+    } else {
+        $currenttab = 'list';
+    }
+    include('tabs.php'); 
+
+
+/// Approve any requested records
+
+    if ($approve && confirm_sesskey() && isteacher($course->id)) {
+        if ($approverecord = get_record('data_records', 'id', $approve)) {   // Need to check this is valid
+            if ($approverecord->dataid == $data->id) {                       // Must be from this database
+                $newrecord->id = $approverecord->id;
+                $newrecord->approved = 1;
+                if (update_record('data_records', $newrecord)) {
+                    notify(get_string('recordapproved','data'), 'notifysuccess');
+                }
+            }
         }
     }
 
         $groupselect = ' ';
     }
 
-
 /// Find the field we are sorting on
     if ($sort) {
         $sortfield = data_get_field_from_id($sort, $data);
         $sortcontent = $sortfield->get_sort_field();
+        $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent);
 
-        $what = ' DISTINCT r.id, r.approved ';
+        $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname ';
         $count = ' COUNT(DISTINCT c.recordid) ';
-        $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content c1 ';
+        $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content c1, '.$CFG->prefix.'user u ';
         $where =  'WHERE c.recordid = r.id 
                      AND c.fieldid = '.$sort.' 
                      AND r.dataid = '.$data->id.' 
+                     AND r.userid = u.id 
                      AND c1.recordid = r.id ';
-        $sortorder = ' ORDER BY c.'.$sortcontent.' '.$order.' ';
+        $sortorder = ' ORDER BY '.$sortcontentfull.' '.$order.' ';
+        if ($search) {
+            $searchselect = ' AND (c1.content LIKE "%'.$search.'%") ';
+        } else {
+            $searchselect = ' ';
+        }
 
     } else if ($search) { 
-        $what = ' DISTINCT r.id, r.approved ';
+        $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname ';
         $count = ' COUNT(DISTINCT c.recordid) ';
-        $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r ';
+        $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u ';
         $where =  'WHERE c.recordid = r.id 
+                     AND r.userid = u.id 
                      AND r.dataid = '.$data->id;
         $sortorder = ' ORDER BY r.id ';
+        $searchselect = ' AND (c.content LIKE "%'.$search.'%") ';
 
     } else {
-        $what = ' DISTINCT r.id, r.approved ';
+        $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname ';
         $count = ' COUNT(r.id) ';
-        $tables = $CFG->prefix.'data_records r ';
-        $where =  'WHERE r.dataid = '.$data->id;
-        $sortorder = ' ORDER BY r.id ';
-    }
-
-/// Restrict by a search if we have one
-
-    if ($search) {
-        $searchselect = ' AND (c.content LIKE "%'.$search.'%") ';
-    } else {
+        $tables = $CFG->prefix.'data_records r, '.$CFG->prefix.'user u ';
+        $where =  'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id ';
+        $sortorder = ' ORDER BY r.timecreated '.$order. ' ';
         $searchselect = ' ';
     }
 
+
 /// To actually fetch the records
 
     $fromsql = ' FROM '.$tables.$where.$groupselect.$approveselect.$searchselect;
     if ($record) {     // We need to just show one, so where is it in context?
         $nowperpage = 1;
         $mode = 'single';
-        if ($sort) {   // We need to search by that field
-            if ($content = get_field('data_content', 'content', 'recordid', $record->id, 'fieldid', $sort)) {
-                $content = addslashes($content);
-                if ($order == 'ASC') {
-                    $lessthan = " AND c.$sortcontent < '$content' ";
-                } else {
-                    $lessthan = " AND c.$sortcontent > '$content' ";
-                }
-            } else {   // Failed to find data (shouldn't happen), so fall back to something easy
-                $lessthan = " r.id < '$record->id' ";
+#        if ($sort) {   // We need to search by that field
+#            if ($content = get_field('data_content', 'content', 'recordid', $record->id, 'fieldid', $sort)) {
+#                $content = addslashes($content);
+#                if ($order == 'ASC') {
+#                    $lessthan = " AND $sortcontentfull < '$content' ";
+#                } else {
+#                    $lessthan = " AND $sortcontentfull > '$content' ";
+#                }
+#            } else {   // Failed to find data (shouldn't happen), so fall back to something easy
+#                $lessthan = " r.id < '$record->id' ";
+#            }
+#        } else {
+#            $lessthan = " r.id < '$record->id' ";
+#        }
+#        $sqlindex = 'SELECT COUNT(DISTINCT c.recordid) '.$fromsql.$lessthan.$sortorder;
+#        $page = count_records_sql($sqlindex);
+
+        $allrecords = get_records_sql($sqlselect);      // Kludgey but accurate at least!
+        $page = 0;
+        foreach ($allrecords as $key => $allrecord) {
+            if ($key == $record->id) {
+                break;
             }
-        } else {
-            $lessthan = " r.id < '$record->id' ";
+            $page++;
         }
-        $sqlindex = 'SELECT COUNT(DISTINCT c.recordid) '.$fromsql.$lessthan.$sortorder;
-        $page = count_records_sql($sqlindex);
 
     } else if ($mode == 'single') {  // We rely on ambient $page settings
         $nowperpage = 1;
 
             print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
         }
-    }
 
-    data_print_preference_form($data, $perpage, $search, $sort, $order);
+        data_print_preference_form($data, $perpage, $search, $sort, $order);
+    }
 
     print_footer($course);
 ?>