MDL-12089 several search related problems fixed in data module; merged from MOODLE_19_STABLE

This commit is contained in:
skodak 2008-04-19 15:33:06 +00:00
parent 81c7f649e1
commit eeeb4f2ad9
3 changed files with 67 additions and 52 deletions

View file

@ -109,6 +109,8 @@ $string['filesnotgenerated'] = 'Not all files were generated: $a';
$string['filtername'] = 'Database Auto-linking'; $string['filtername'] = 'Database Auto-linking';
$string['footer'] = 'Footer'; $string['footer'] = 'Footer';
$string['forcelinkname'] = 'Forced name for the link'; $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['fromfile'] = 'Import from zip file';
$string['header'] = 'Header'; $string['header'] = 'Header';
$string['headeraddtemplate'] = 'Defines the interface when editing entries'; $string['headeraddtemplate'] = 'Defines the interface when editing entries';
@ -202,7 +204,7 @@ $string['requireapproval'] = 'Require approval?';
$string['requiredentries'] = 'Required entries'; $string['requiredentries'] = 'Required entries';
$string['requiredentriestoview'] = 'Entries required before viewing'; $string['requiredentriestoview'] = 'Entries required before viewing';
$string['resettemplate'] = 'Reset template'; $string['resettemplate'] = 'Reset template';
$string['resetsettings'] = 'Reset Fields'; $string['resetsettings'] = 'Reset filters';
$string['resizingimages'] = 'Resizing image thumbnails...'; $string['resizingimages'] = 'Resizing image thumbnails...';
$string['rows'] = 'rows'; $string['rows'] = 'rows';
$string['rssglobaldisabled'] = 'Disabled. See site configuration variables.'; $string['rssglobaldisabled'] = 'Disabled. See site configuration variables.';

View file

@ -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>'; .$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 { } else {
$replacement[] = ''; $replacement[] = '';
$replacement[] = '';
} }
$patterns[]='##more##'; $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>'; $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 format_text($newtext, FORMAT_HTML, $options);
echo '</td></tr>'; 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 '</table>';
echo '</div>'; echo '</div>';
echo '</div>'; echo '</div>';

View file

@ -106,6 +106,13 @@
$SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC'; $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); $advanced = optional_param('advanced', -1, PARAM_INT);
if ($advanced == -1) { if ($advanced == -1) {
$advanced = $SESSION->dataprefs[$data->id]['advanced']; $advanced = $SESSION->dataprefs[$data->id]['advanced'];
@ -118,7 +125,7 @@
} }
$search_array = $SESSION->dataprefs[$data->id]['search_array']; $search_array = $SESSION->dataprefs[$data->id]['search_array'];
if (!empty($advanced)) { if (!empty($advanced)) {
$search = ''; $search = '';
$vals = array(); $vals = array();
@ -356,7 +363,10 @@
} }
include('tabs.php'); include('tabs.php');
if ($mode != 'asearch') { if ($mode == 'asearch') {
$maxcount = 0;
} else {
/// Approve any requested records /// Approve any requested records
if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) { if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) {
@ -371,15 +381,15 @@
} }
} }
// Check the number of entries required against the number of entries already made (doesn't apply to teachers) // Check the number of entries required against the number of entries already made (doesn't apply to teachers)
$requiredentries_allowed = true; $requiredentries_allowed = true;
$numentries = data_numentries($data); $numentries = data_numentries($data);
if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) { if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) {
$data->entriesleft = $data->requiredentries - $numentries; $data->entriesleft = $data->requiredentries - $numentries;
$strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data); $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
notify($strentrieslefttoadd); notify($strentrieslefttoadd);
$requiredentries_allowed = false; $requiredentries_allowed = false;
} }
/// We need to examine the whole dataset to produce the correct paging /// We need to examine the whole dataset to produce the correct paging
@ -414,6 +424,7 @@
$what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname'; $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname';
$count = ' COUNT(DISTINCT c.recordid) '; $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 '; $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 $where = 'WHERE c.recordid = r.id
AND r.dataid = '.$data->id.' AND r.dataid = '.$data->id.'
@ -450,6 +461,7 @@
$what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, c.'.$sortcontent.', '.$sortcontentfull.' AS _order '; $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) '; $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 '; $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 $where = 'WHERE c.recordid = r.id
AND c.fieldid = '.$sort.' AND c.fieldid = '.$sort.'
@ -483,6 +495,7 @@
} else if ($search) { } else if ($search) {
$what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname '; $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname ';
$count = ' COUNT(DISTINCT c.recordid) '; $count = ' COUNT(DISTINCT c.recordid) ';
$rids = ' DISTINCT c.recordid ';
$tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u ';
$where = 'WHERE c.recordid = r.id $where = 'WHERE c.recordid = r.id
AND r.userid = u.id AND r.userid = u.id
@ -513,6 +526,7 @@
} else { } else {
$what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname '; $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname ';
$count = ' COUNT(r.id) '; $count = ' COUNT(r.id) ';
$rids = ' COUNT(r.id) ';
$tables = $CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; $tables = $CFG->prefix.'data_records r, '.$CFG->prefix.'user u ';
$where = 'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id '; $where = 'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id ';
$sortorder = ' ORDER BY r.timecreated '.$order. ' '; $sortorder = ' ORDER BY r.timecreated '.$order. ' ';
@ -527,49 +541,34 @@
/// To actually fetch the records /// To actually fetch the records
$fromsql = ' FROM '.$tables.$where.$groupselect.$approveselect.$searchselect; $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
$sqlselect = 'SELECT '.$what.$fromsql.$sortorder; /// Work out the paging numbers and counts
$sqlcount = 'SELECT '.$count.$fromsql; // Total number of records
/// Work out the paging numbers
$totalcount = count_records_sql($sqlcount); $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? if ($record) { // We need to just show one, so where is it in context?
$nowperpage = 1; $nowperpage = 1;
$mode = 'single'; $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; $page = 0;
if ($allrecords = get_records_sql($sqlselect)) { // Kludgey but accurate at least! if ($allrecords = get_records_sql($sqlrids)) { // Kludgey but accurate at least!
foreach ($allrecords as $key => $allrecord) { foreach ($allrecords as $key => $unused) {
if ($key == $record->id) { if ($key == $record->id) {
break; break;
} }
$page++; $page++;
} }
unset($allrecords);
} }
} else if ($mode == 'single') { // We rely on ambient $page settings } else if ($mode == 'single') { // We rely on ambient $page settings
@ -581,26 +580,39 @@
/// Get the actual records /// Get the actual records
$records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage); if (!$records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage)) {
// Nothing to show!
if (empty($records)) { // Nothing to show!
if ($record) { // Something was requested so try to show that at least (bug 5132) if ($record) { // Something was requested so try to show that at least (bug 5132)
if (has_capability('mod/data:manageentries', $context) || empty($data->approval) || if (has_capability('mod/data:manageentries', $context) || empty($data->approval) ||
$record->approved || (isloggedin() && $record->userid == $USER->id)) { $record->approved || (isloggedin() && $record->userid == $USER->id)) {
if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) { 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){ if (empty($records)) {
notify(get_string('nomatch','data')); 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 { } else {
notify(get_string('norecords','data')); 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 if ($mode == 'single') { // Single template
$baseurl = 'view.php?d='.$data->id.'&amp;mode=single&amp;'; $baseurl = 'view.php?d='.$data->id.'&amp;mode=single&amp;';
@ -645,7 +657,7 @@
} }
//Advanced search form doesn't make sense for single (redirects list view) //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); data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
} }