]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12089 several search related problems fixed in data module; merged from MOODLE_19...
authorskodak <skodak>
Sat, 19 Apr 2008 15:33:06 +0000 (15:33 +0000)
committerskodak <skodak>
Sat, 19 Apr 2008 15:33:06 +0000 (15:33 +0000)
lang/en_utf8/data.php
mod/data/lib.php
mod/data/view.php

index 82db93600841219c0c4a310e66e236ad690cf819..22bd37f88e6338811e960fd0357096facd6a3be0 100644 (file)
@@ -109,6 +109,8 @@ $string['filesnotgenerated'] = 'Not all files were generated: $a';
 $string['filtername'] = 'Database Auto-linking';
 $string['footer'] = 'Footer';
 $string['forcelinkname'] = 'Forced name for the link';
+$string['foundrecords'] = 'Found records: $a->num/$a->max (<a href=\"$a->reseturl\">Reset filters</a>)';
+$string['foundnorecords'] = 'No records found (<a href=\"$a->reseturl\">Reset filters</a>)';
 $string['fromfile'] = 'Import from zip file';
 $string['header'] = 'Header';
 $string['headeraddtemplate'] = 'Defines the interface when editing entries';
@@ -202,7 +204,7 @@ $string['requireapproval'] = 'Require approval?';
 $string['requiredentries'] = 'Required entries';
 $string['requiredentriestoview'] = 'Entries required before viewing';
 $string['resettemplate'] = 'Reset template';
-$string['resetsettings'] = 'Reset Fields';
+$string['resetsettings'] = 'Reset filters';
 $string['resizingimages'] = 'Resizing image thumbnails...';
 $string['rows'] = 'rows';
 $string['rssglobaldisabled'] = 'Disabled. See site configuration variables.';
index b79577c04dacbee18f713538b728aafa68c75890..c0ecdd0150f7195c62ae2cd64af4c6c6f79a993c 100755 (executable)
@@ -961,6 +961,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
                              .$data->id.'&amp;delete='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>';
         } else {
             $replacement[] = '';
+            $replacement[] = '';
         }
         $patterns[]='##more##';
         $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$record->id.'"><img src="'.$CFG->pixpath.'/i/search.gif" class="iconsmall" alt="'.get_string('more', 'data').'" title="'.get_string('more', 'data').'" /></a>';
@@ -1205,7 +1206,7 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
     echo format_text($newtext, FORMAT_HTML, $options);
     echo '</td></tr>';
 
-    echo '<tr><td colspan="4" style="text-align: center;"><br/><input type="submit" value="'.get_string('savesettings','data').'" /><input type="reset" value="'.get_string('resetsettings','data').'" /></td></tr>';
+    echo '<tr><td colspan="4" style="text-align: center;"><br/><input type="submit" value="'.get_string('savesettings','data').'" /><input type="submit" name="resetadv" value="'.get_string('resetsettings','data').'" /></td></tr>';
     echo '</table>';
     echo '</div>';
     echo '</div>';
index 772a52058b032350ba41f5c0ae9d493666024dad..d17159619fd60645c39f931f45ef23320e8eecb4 100755 (executable)
         $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC';
     }
 
+    // reset advanced form
+    if (!is_null(optional_param('resetadv', null, PARAM_RAW))) {
+        $SESSION->dataprefs[$data->id]['search_array'] = array();
+        // we need the redirect to cleanup the form state properly
+        redirect("view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=1");
+    }
+
     $advanced = optional_param('advanced', -1, PARAM_INT);
     if ($advanced == -1) {
         $advanced = $SESSION->dataprefs[$data->id]['advanced'];
     }
 
     $search_array = $SESSION->dataprefs[$data->id]['search_array'];
-    
+
     if (!empty($advanced)) {
         $search = '';
         $vals = array();
     }
     include('tabs.php');
 
-    if ($mode != 'asearch') {
+    if ($mode == 'asearch') {
+        $maxcount = 0;
+        
+    } else {
     /// Approve any requested records
 
         if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) {
             }
         }
     
-    // Check the number of entries required against the number of entries already made (doesn't apply to teachers)
-    $requiredentries_allowed = true;
-    $numentries = data_numentries($data);
-    if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) {
-        $data->entriesleft = $data->requiredentries - $numentries;
-        $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
-        notify($strentrieslefttoadd); 
-        $requiredentries_allowed = false;
-    }
+        // Check the number of entries required against the number of entries already made (doesn't apply to teachers)
+        $requiredentries_allowed = true;
+        $numentries = data_numentries($data);
+        if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) {
+            $data->entriesleft = $data->requiredentries - $numentries;
+            $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
+            notify($strentrieslefttoadd);
+            $requiredentries_allowed = false;
+        }
 
     /// We need to examine the whole dataset to produce the correct paging
 
 
             $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname';
             $count = ' COUNT(DISTINCT c.recordid) ';
+            $rids  = ' DISTINCT c.recordid ';
             $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content cs, '.$CFG->prefix.'user u ';
             $where =  'WHERE c.recordid = r.id
                          AND r.dataid = '.$data->id.'
 
             $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, c.'.$sortcontent.', '.$sortcontentfull.' AS _order ';
             $count = ' COUNT(DISTINCT c.recordid) ';
+            $rids  = ' DISTINCT c.recordid ';
             $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content cs, '.$CFG->prefix.'user u ';
             $where =  'WHERE c.recordid = r.id
                          AND c.fieldid = '.$sort.'
         } else if ($search) {
             $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname ';
             $count = ' COUNT(DISTINCT c.recordid) ';
+            $rids  = ' DISTINCT c.recordid ';
             $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
         } else {
             $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname ';
             $count = ' COUNT(r.id) ';
+            $rids  = ' COUNT(r.id) ';
             $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. ' ';
 
     /// To actually fetch the records
 
-        $fromsql = ' FROM '.$tables.$where.$groupselect.$approveselect.$searchselect;
-
-        $sqlselect = 'SELECT '.$what.$fromsql.$sortorder;
-
-        $sqlcount  = 'SELECT '.$count.$fromsql;   // Total number of records
+        $fromsql    = "FROM $tables $where $groupselect $approveselect $searchselect";
+        $sqlselect  = "SELECT $what $fromsql $sortorder";
+        $sqlcount   = "SELECT $count $fromsql";   // Total number of records when searching
+        $sqlrids    = "SELECT $rids $fromsql";
+        $sqlmax     = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
 
-    /// Work out the paging numbers
+    /// Work out the paging numbers and counts
 
         $totalcount = count_records_sql($sqlcount);
+        if (empty($searchselect)) {
+            $maxcount = $totalcount;
+        } else {
+            $maxcount = count_records_sql($sqlmax);
+        }
 
         if ($record) {     // We need to just show one, so where is it in context?
             $nowperpage = 1;
             $mode = 'single';
 
-    #  Following code needs testing to make it work
-    #        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'
-    #                                   OR ($sortcontentfull = '$content' AND r.id < '$record->id') ";
-    #                } else {
-    #                    $lessthan = " AND $sortcontentfull > '$content'
-    #                                   OR ($sortcontentfull = '$content' AND r.id < '$record->id') ";
-    #                }
-    #            } 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);
-
-
             $page = 0;
-            if ($allrecords = get_records_sql($sqlselect)) {      // Kludgey but accurate at least!          
-                foreach ($allrecords as $key => $allrecord) {
+            if ($allrecords = get_records_sql($sqlrids)) {      // Kludgey but accurate at least!          
+                foreach ($allrecords as $key => $unused) {
                     if ($key == $record->id) {
                         break;
                     }
                     $page++;
                 }
+                unset($allrecords);
             }
 
         } else if ($mode == 'single') {  // We rely on ambient $page settings
 
     /// Get the actual records
         
-        $records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage);
-
-        if (empty($records)) {     // Nothing to show!
+        if (!$records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage)) {
+            // Nothing to show!
             if ($record) {         // Something was requested so try to show that at least (bug 5132)
                 if (has_capability('mod/data:manageentries', $context) || empty($data->approval) ||
                          $record->approved || (isloggedin() && $record->userid == $USER->id)) {
                     if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) {
-                        $records[] = $record;
+                        // OK, we can show this one
+                        $records = array($record->id => $record);
+                        $totalcount = 1;
                     }
                 }
             }
-            if ($records) {  // OK, we can show this one
-                data_print_template('singletemplate', $records, $data, $search, $page);
-            } else if ($search){
-                notify(get_string('nomatch','data'));
+        }
+
+        if (empty($records)) {
+            if ($maxcount){
+                $a = new object();
+                $a->max = $maxcount;
+                $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
+                notify(get_string('foundnorecords','data', $a));
             } else {
                 notify(get_string('norecords','data'));
             }
 
-        } else {                   //  We have some records to print
+        } else { //  We have some records to print
+
+            if ($maxcount != $totalcount) {
+                $a = new object();
+                $a->num = $totalcount;
+                $a->max = $maxcount;
+                $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
+                notify(get_string('foundrecords', 'data', $a), 'notifysuccess'); 
+            }
 
             if ($mode == 'single') {                  // Single template
                 $baseurl = 'view.php?d='.$data->id.'&amp;mode=single&amp;';
     }
 
     //Advanced search form doesn't make sense for single (redirects list view)
-    if (($records || $search || $page ||  $mode == 'asearch') && $mode != 'single') {
+    if (($maxcount || $mode == 'asearch') && $mode != 'single') {
         data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
     }