From 714bec7418f6c0999dc927acb0b40cb84891f451 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 16 Apr 2008 11:51:50 +0000 Subject: [PATCH] MDL-9907 searching/sorting by author name; merged from MOODLE_19_STABLE --- lang/en_utf8/data.php | 2 + mod/data/lib.php | 33 ++++++-- mod/data/templates.php | 20 +++-- mod/data/view.php | 183 ++++++++++++++++++++++++++++------------- 4 files changed, 170 insertions(+), 68 deletions(-) diff --git a/lang/en_utf8/data.php b/lang/en_utf8/data.php index c210445607..73a55b515c 100644 --- a/lang/en_utf8/data.php +++ b/lang/en_utf8/data.php @@ -16,6 +16,8 @@ $string['approve'] = 'Approve'; $string['ascending'] = 'Ascending'; $string['asearchtemplate'] = 'Advanced search template'; $string['atmaxentry'] = 'You have entered the maximum number of entries allowed!'; +$string['authorfirstname'] = 'Author first name'; +$string['authorlastname'] = 'Author surname'; $string['autogenallforms'] = 'Generate all default templates'; $string['autolinkurl'] = 'Autolink the URL'; $string['availablefromdate'] = 'Available from'; diff --git a/mod/data/lib.php b/mod/data/lib.php index 87836f277c..05f51e9aca 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -26,6 +26,9 @@ define ('DATA_MAX_ENTRIES', 50); define ('DATA_PERPAGE_SINGLE', 1); +define ('DATA_FIRSTNAME', -1); +define ('DATA_LASTNAME', -2); + class data_field_base { /// Base class for Database Field Types (see field/*/field.class.php) var $type = 'unknown'; /// Subclasses must override the type with their name @@ -364,6 +367,9 @@ function data_generate_default_template(&$data, $template, $recordid=0, $form=fa $str .= '##edit## ##more## ##delete## ##approve##'; } else if ($template == 'singletemplate') { $str .= '##edit## ##delete## ##approve##'; + } else if ($template == 'asearchtemplate') { + $str .= ''.get_string('authorfirstname', 'data').': ##firstname##'; + $str .= ''.get_string('authorlastname', 'data').': ##lastname##'; } $str .= ''; @@ -1058,12 +1064,18 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order=' echo '    '; //foreach field, print the option $fields = get_records('data_fields','dataid',$data->id, 'name'); - echo ''; + foreach ($options as $key => $name) { + if ($key == $sort) { + echo ''; } else { - echo ''; + echo ''; } } echo ''; @@ -1118,7 +1130,7 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order=' // End --> //]]> '; - echo ' '.get_string('advancedsearch', 'data'); + echo ' '; echo ' '; echo '
'; @@ -1169,16 +1181,23 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order=' /// Then we generate strings to replace for normal tags foreach ($fields as $field) { $patterns[]='/\[\['.$field->field->name.'\]\]/i'; - $searchfield = data_get_field_from_id($field->field->id, $data); + $searchfield = data_get_field_from_id($field->field->id, $data); if (!empty($search_array[$field->field->id]->data)) { $replacement[] = $searchfield->display_search_field($search_array[$field->field->id]->data); } else { $replacement[] = $searchfield->display_search_field(); } } - + $fn = !empty($search_array[DATA_FIRSTNAME]->data) ? $search_array[DATA_FIRSTNAME]->data : ''; + $ln = !empty($search_array[DATA_LASTNAME]->data) ? $search_array[DATA_LASTNAME]->data : ''; + $patterns[] = '/##firstname##/'; + $replacement[] = ''; + $patterns[] = '/##lastname##/'; + $replacement[] = ''; + ///actual replacement of the tags $newtext = preg_replace($patterns, $replacement, $data->asearchtemplate); + $options = new object(); $options->para=false; $options->noclean=true; diff --git a/mod/data/templates.php b/mod/data/templates.php index 1855bedc94..3ad7c966b2 100755 --- a/mod/data/templates.php +++ b/mod/data/templates.php @@ -32,24 +32,24 @@ if ($id) { if (! $cm = get_coursemodule_from_id('data', $id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } if (! $course = get_record('course', 'id', $cm->course)) { - print_error('Course is misconfigured'); + error('Course is misconfigured'); } if (! $data = get_record('data', 'id', $cm->instance)) { - print_error('Course module is incorrect'); + error('Course module is incorrect'); } } else { if (! $data = get_record('data', 'id', $d)) { - print_error('Data ID is incorrect'); + error('Data ID is incorrect'); } if (! $course = get_record('course', 'id', $data->course)) { - print_error('Course is misconfigured'); + error('Course is misconfigured'); } if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } } @@ -237,6 +237,14 @@ } echo ''; } + + if ($mode == 'asearchtemplate') { + echo ''; + echo ''; + echo ''; + echo ''; + } + echo ''; echo '



'; if (can_use_html_editor()) { diff --git a/mod/data/view.php b/mod/data/view.php index c3ed5e3f71..d5b1a969a8 100755 --- a/mod/data/view.php +++ b/mod/data/view.php @@ -44,38 +44,38 @@ if ($id) { if (! $cm = get_coursemodule_from_id('data', $id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } if (! $course = get_record('course', 'id', $cm->course)) { - print_error('Course is misconfigured'); + error('Course is misconfigured'); } if (! $data = get_record('data', 'id', $cm->instance)) { - print_error('Course module is incorrect'); + error('Course module is incorrect'); } $record = NULL; } else if ($rid) { if (! $record = get_record('data_records', 'id', $rid)) { - print_error('Record ID is incorrect'); + error('Record ID is incorrect'); } if (! $data = get_record('data', 'id', $record->dataid)) { - print_error('Data ID is incorrect'); + error('Data ID is incorrect'); } if (! $course = get_record('course', 'id', $data->course)) { - print_error('Course is misconfigured'); + error('Course is misconfigured'); } if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } } else { // We must have $d if (! $data = get_record('data', 'id', $d)) { - print_error('Data ID is incorrect'); + error('Data ID is incorrect'); } if (! $course = get_record('course', 'id', $data->course)) { - print_error('Course is misconfigured'); + error('Course is misconfigured'); } if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { - print_error('Course Module ID was incorrect'); + error('Course Module ID was incorrect'); } $record = NULL; } @@ -133,40 +133,63 @@ else { $paging = true; } - if (!empty($fields)) { + if (!empty($fields)) { foreach($fields as $field) { $searchfield = data_get_field_from_id($field->id, $data); //Get field data to build search sql with. If paging is false, get from user. //If paging is true, get data from $search_array which is obtained from the $SESSION (see line 116). if(!$paging) { $val = $searchfield->parse_search_field(); - } - else { + } else { //Set value from session if there is a value @ the required index. - if(isset($search_array[$field->id])) { + if (isset($search_array[$field->id])) { $val = $search_array[$field->id]->data; - } - else { //If there is not an entry @ the required index, set value to blank. + } else { //If there is not an entry @ the required index, set value to blank. $val = ''; } - } - if (!empty($val)) { - $search_array[$field->id] = new stdClass; - $search_array[$field->id]->sql = $searchfield->generate_sql('c'.$field->id, $val); - $search_array[$field->id]->data = $val; - $search .= ' '.$val; } - else { - if (isset($search_array[$field->id])) { - // clear it out - unset($search_array[$field->id]); - } + if (!empty($val)) { + $search_array[$field->id] = new object(); + $search_array[$field->id]->sql = $searchfield->generate_sql('c'.$field->id, $val); + $search_array[$field->id]->data = $val; + $search .= ' '.$val; + } else { + // clear it out + unset($search_array[$field->id]); } } } + + if (!$paging) { + // name searching + $fn = optional_param('u_fn', '', PARAM_NOTAGS); + $ln = optional_param('u_ln', '', PARAM_NOTAGS); + } else { + $fn = isset($search_array[DATA_FIRSTNAME]) ? $search_array[DATA_FIRSTNAME] : ''; + $ln = isset($search_array[DATA_LASTNAME]) ? $search_array[DATA_LASTNAME] : ''; + } + if (!empty($fn)) { + $search_array[DATA_FIRSTNAME] = new object(); + $search_array[DATA_FIRSTNAME]->sql = ''; + $search_array[DATA_FIRSTNAME]->field = 'u.firstname'; + $search_array[DATA_FIRSTNAME]->data = $fn; + $search .= ' '.$fn; + } else { + unset($search_array[DATA_FIRSTNAME]); + } + if (!empty($ln)) { + $search_array[DATA_LASTNAME] = new object(); + $search_array[DATA_LASTNAME]->sql = ''; + $search_array[DATA_LASTNAME]->field = 'u.lastname'; + $search_array[DATA_LASTNAME]->data = $ln; + $search .= ' '.$ln; + } else { + unset($search_array[DATA_LASTNAME]); + } + $SESSION->dataprefs[$data->id]['search_array'] = $search_array; // Make it sticky - } - else { + + } else { $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS); //Paging variable not used for standard search. Set it to null. $paging = NULL; @@ -357,41 +380,86 @@ } /// Find the field we are sorting on - if ($sort and $sortfield = data_get_field_from_id($sort, $data)) { + if ($sort == DATA_FIRSTNAME or $sort == DATA_LASTNAME) { + + if ($sort == DATA_LASTNAME) { + $ordering = "u.lastname $order, u.firstname $order"; + } else { + $ordering = "u.firstname $order, u.lastname $order"; + } + + $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, 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 cs, '.$CFG->prefix.'user u '; + $where = 'WHERE c.recordid = r.id + AND r.dataid = '.$data->id.' + AND r.userid = u.id + AND cs.recordid = r.id '; + $sortorder = ' ORDER BY '.$ordering.', r.id ASC '; + $searchselect = ''; + + // If requiredentries is not reached, only show current user's entries + if (!$requiredentries_allowed) { + $where .= ' AND u.id = ' . $USER->id; + } + + if (!empty($advanced)) { //If advanced box is checked. + foreach($search_array as $key => $val) { //what does $search_array hold? + if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { + $searchselect .= " AND $val->field LIKE '%{$val->data}%'"; + continue; + } + $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; + $where .= ' AND c'.$key.'.recordid = r.id'; + $searchselect .= ' AND ('.$val->sql.') '; + } + } else if ($search) { + $ilike = sql_ilike(); //Be case-insensitive + $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) "; + } else { + $searchselect = ' '; + } + + } else if ($sort and $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, 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) '; - $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content c1, '.$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 AND c.fieldid = '.$sort.' AND r.dataid = '.$data->id.' AND r.userid = u.id - AND c1.recordid = r.id '; + AND cs.recordid = r.id '; $sortorder = ' ORDER BY _order '.$order.' , r.id ASC '; $searchselect = ''; - + // If requiredentries is not reached, only show current user's entries if (!$requiredentries_allowed) { $where .= ' AND u.id = ' . $USER->id; } - - if (!empty($advanced)) { //If advanced box is checked. - foreach($search_array as $key => $val) { //what does $search_array hold? + + if (!empty($advanced)) { //If advanced box is checked. + foreach($search_array as $key => $val) { //what does $search_array hold? + if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { + $searchselect .= " AND $val->field LIKE '%{$val->data}%'"; + continue; + } $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; $where .= ' AND c'.$key.'.recordid = r.id'; $searchselect .= ' AND ('.$val->sql.') '; } - } - elseif ($search) { - $searchselect = ' AND (c1.content ' . sql_ilike() . " '%$search%') "; //Be case-insensitive + } else if ($search) { + $ilike = sql_ilike(); //Be case-insensitive + $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) "; } else { $searchselect = ' '; - } + } + } else if ($search) { - $what = ' DISTINCT r.id, r.approved, 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) '; $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; $where = 'WHERE c.recordid = r.id @@ -399,24 +467,28 @@ AND r.dataid = '.$data->id; $sortorder = ' ORDER BY r.id ASC '; $searchselect = ''; - + // If requiredentries is not reached, only show current user's entries if (!$requiredentries_allowed) { $where .= ' AND u.id = ' . $USER->id; } - + if (!empty($advanced)) { //Advanced search box again. foreach($search_array as $key => $val) { + if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { + $searchselect .= " AND $val->field LIKE '%{$val->data}%'"; + continue; + } $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; $where .= ' AND c'.$key.'.recordid = r.id '; $searchselect .= ' AND ('.$val->sql.') '; } - } - else { - $searchselect = ' AND (c.content ' . sql_ilike() . " '%$search%') "; //Be case-insensitive + } else { + $ilike = sql_ilike(); //Be case-insensitive + $searchselect = " AND (c.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) "; } - + } else { $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname '; $count = ' COUNT(r.id) '; @@ -424,7 +496,7 @@ $where = 'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id '; $sortorder = ' ORDER BY r.timecreated '.$order. ' '; $searchselect = ' '; - + // If requiredentries is not reached, only show current user's entries if (!$requiredentries_allowed) { $where .= ' AND u.id = ' . $USER->id; @@ -469,13 +541,14 @@ # $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; + if ($allrecords = get_records_sql($sqlselect)) { // Kludgey but accurate at least! + foreach ($allrecords as $key => $allrecord) { + if ($key == $record->id) { + break; + } + $page++; } - $page++; } } else if ($mode == 'single') { // We rely on ambient $page settings -- 2.39.5