From 7900ecb02c3a75f8cbe43362eba06606b3642c92 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Mon, 26 Feb 2007 06:56:05 +0000 Subject: [PATCH] MDL-5958, code for advanced search for db module from Elijah (HSU) --- lang/en_utf8/data.php | 4 + mod/data/db/access.php | 10 + mod/data/db/install.xml | 5 +- mod/data/db/upgrade.php | 10 + mod/data/field/checkbox/field.class.php | 21 ++ mod/data/field/date/field.class.php | 30 ++ mod/data/field/file/field.class.php | 13 + mod/data/field/latlong/field.class.php | 27 +- mod/data/field/menu/field.class.php | 21 ++ mod/data/field/multimenu/field.class.php | 21 ++ mod/data/field/number/field.class.php | 13 + mod/data/field/picture/field.class.php | 12 + mod/data/field/radiobutton/field.class.php | 21 ++ mod/data/field/text/field.class.php | 13 + mod/data/field/textarea/field.class.php | 16 +- mod/data/field/url/field.class.php | 12 + mod/data/lib.php | 168 +++++++-- mod/data/tabs.php | 16 +- mod/data/templates.php | 8 +- mod/data/version.php | 2 +- mod/data/view.php | 407 +++++++++++++-------- theme/standard/styles_layout.css | 4 +- 22 files changed, 655 insertions(+), 199 deletions(-) diff --git a/lang/en_utf8/data.php b/lang/en_utf8/data.php index 18f558a4a5..bad779e32c 100644 --- a/lang/en_utf8/data.php +++ b/lang/en_utf8/data.php @@ -7,12 +7,14 @@ $string['add'] = 'Add entry'; $string['addafield'] = 'Add a field'; $string['addcomment'] = 'Add comment'; $string['addentries'] = 'Add entries'; +$string['advancedsearch'] = 'Advanced Search'; $string['addtemplate'] = 'Add template'; $string['allowcomments'] = 'Allow comments?'; $string['allowratings'] = 'Allow posts to be rated?'; $string['alttext'] = 'Alternative text'; $string['approve'] = 'Approve'; $string['ascending'] = 'Ascending'; +$string['asearchtemplate'] = 'Advanced Search Template'; $string['atmaxentry'] = 'You have entered the maximum number of entries allowed!'; $string['autogenallforms'] = 'Generate all default templates'; $string['autolinkurl'] = 'Autolink the URL'; @@ -102,6 +104,7 @@ $string['forcelinkname'] = 'Forced name for the link'; $string['fromfile'] = 'Import from zip file'; $string['header'] = 'Header'; $string['headeraddtemplate'] = 'Defines the interface when editing entries'; +$string['headerasearchtemplate'] = 'Defines the interface for Advanced Searches'; $string['headercsstemplate'] = 'Defines local CSS styles for the other templates'; $string['headerjstemplate'] = 'Defines custom Javascript for the other templates'; $string['headerlisttemplate'] = 'Defines browsing interface for multiple entries'; @@ -187,6 +190,7 @@ $string['requireapproval'] = 'Require approval?'; $string['requiredentries'] = 'Required entries'; $string['requiredentriestoview'] = 'Entries required before viewing'; $string['resettemplate'] = 'Reset template'; +$string['resetsettings'] = 'Reset Fields'; $string['resizingimages'] = 'Resizing image thumbnails...'; $string['rows'] = 'rows'; $string['rssglobaldisabled'] = 'Disabled. See site configuration variables.'; diff --git a/mod/data/db/access.php b/mod/data/db/access.php index 341f0dae17..29ba9fc2fc 100644 --- a/mod/data/db/access.php +++ b/mod/data/db/access.php @@ -42,6 +42,7 @@ $mod_data_capabilities = array( 'student' => CAP_ALLOW, 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -56,6 +57,7 @@ $mod_data_capabilities = array( 'student' => CAP_ALLOW, 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -70,6 +72,7 @@ $mod_data_capabilities = array( 'student' => CAP_ALLOW, 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -81,6 +84,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -92,6 +96,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -103,6 +108,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -116,6 +122,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -129,6 +136,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -142,6 +150,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), @@ -153,6 +162,7 @@ $mod_data_capabilities = array( 'legacy' => array( 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) ), diff --git a/mod/data/db/install.xml b/mod/data/db/install.xml index d71f92eafd..02e428a994 100644 --- a/mod/data/db/install.xml +++ b/mod/data/db/install.xml @@ -27,8 +27,9 @@ - - + + + diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index 94c9754c9f..f6b5aaa891 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -43,6 +43,16 @@ function xmldb_data_upgrade($oldversion=0) { $result = $result && add_field($table, $field); } + + if ($result && $oldversion < 2007022600) { + /// Define field asearchtemplate to be added to data + $table = new XMLDBTable('data'); + $field = new XMLDBField('asearchtemplate'); + $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'jstemplate'); + + /// Launch add field asearchtemplate + $result = $result && add_field($table, $field); + } return $result; } diff --git a/mod/data/field/checkbox/field.class.php b/mod/data/field/checkbox/field.class.php index 61afa0dc75..6574c3f417 100755 --- a/mod/data/field/checkbox/field.class.php +++ b/mod/data/field/checkbox/field.class.php @@ -65,6 +65,27 @@ class data_field_checkbox extends data_field_base { return $str; } + function display_search_field($value='') { + global $CFG; + $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content'); + $options = array(); + if(!empty($temp)) { + $options[''] = ''; //Make first index blank. + foreach ($temp as $key) { + $options[$key] = $key; //Build following indicies from the sql. + } + } + return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true); + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; + } + function update_content($recordid, $value, $name='') { $content = new object(); $content->fieldid = $this->field->id; diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index 9717faec04..bc35d4aea2 100755 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -22,6 +22,10 @@ // // /////////////////////////////////////////////////////////////////////////// +//2/19/07: Advanced search of the date field is currently disabled because it does not track +// pre 1970 dates and does not handle blank entrys. Advanced search functionality for this field +// type can be enabled once these issues are addressed in the core API. + class data_field_date extends data_field_base { var $type = 'date'; @@ -49,6 +53,32 @@ class data_field_date extends data_field_base { return $str; } + + //Enable the following three functions once core API issues have been addressed. + function display_search_field($value=0) { + return false; + //return print_date_selector('f_'.$this->field->id.'_d', 'f_'.$this->field->id.'_m', 'f_'.$this->field->id.'_y', $value, true); + } + + function generate_sql($tablealias, $value) { + return ' 1=1 '; + //return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; + } + + function parse_search_field() { + return ''; + /* + $day = optional_param('f_'.$this->field->id.'_d', 0, PARAM_INT); + $month = optional_param('f_'.$this->field->id.'_m', 0, PARAM_INT); + $year = optional_param('f_'.$this->field->id.'_y', 0, PARAM_INT); + if (!empty($day) && !empty($month) && !empty($year)) { + return make_timestamp($year, $month, $day, 12, 0, 0, 0, false); + } + else { + return 0; + } + */ + } function update_content($recordid, $value, $name='') { diff --git a/mod/data/field/file/field.class.php b/mod/data/field/file/field.class.php index 0376a3f804..37bb83065c 100755 --- a/mod/data/field/file/field.class.php +++ b/mod/data/field/file/field.class.php @@ -80,6 +80,19 @@ class data_field_file extends data_field_base { return $str; } + function display_search_field($value = '') { + return ''; + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; + } + + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + function display_browse_field($recordid, $template) { global $CFG; diff --git a/mod/data/field/latlong/field.class.php b/mod/data/field/latlong/field.class.php index 8e9d14af7d..b9e4e9d988 100755 --- a/mod/data/field/latlong/field.class.php +++ b/mod/data/field/latlong/field.class.php @@ -63,7 +63,7 @@ class data_field_latlong extends data_field_base { $long = $content->content1; } } - + $str = '
'; $str .= '
'.$this->field->name.''; $str .= '
'; @@ -75,6 +75,31 @@ class data_field_latlong extends data_field_base { return $str; } + + function display_search_field($value = '') { + global $CFG; + $lats = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content'); + $longs = get_records_sql_menu('SELECT id, content1 from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content'); + $options = array(); + if(!empty($lats) && !empty($longs)) { + $options[''] = ''; //Make first index blank. + foreach($lats as $key => $temp) { + $options[$temp.','.$longs[$key]] = $temp.','.$longs[$key]; + } + } + return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true); + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + $latlong[0] = ''; + $latlong[1] = ''; + $latlong = explode (',', $value, 2); + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$latlong[0]' AND {$tablealias}.content1 = '$latlong[1]') "; + } function display_browse_field($recordid, $template) { global $CFG; diff --git a/mod/data/field/menu/field.class.php b/mod/data/field/menu/field.class.php index ca26022390..9526519273 100755 --- a/mod/data/field/menu/field.class.php +++ b/mod/data/field/menu/field.class.php @@ -56,6 +56,27 @@ class data_field_menu extends data_field_base { return $str; } + + function display_search_field($value = '') { + global $CFG; + $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content'); + $options = array(); + if(!empty($temp)) { + $options[''] = ''; //Make first index blank. + foreach ($temp as $key) { + $options[$key] = $key; //Build following indicies from the sql. + } + } + return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true); + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; + } } diff --git a/mod/data/field/multimenu/field.class.php b/mod/data/field/multimenu/field.class.php index 28707e0b14..ec37adb8e5 100755 --- a/mod/data/field/multimenu/field.class.php +++ b/mod/data/field/multimenu/field.class.php @@ -60,6 +60,27 @@ class data_field_multimenu extends data_field_base { return $str; } + + function display_search_field($value = '') { + global $CFG; + $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content'); + $options = array(); + if(!empty($temp)) { + $options[''] = ''; //Make first index blank. + foreach ($temp as $key) { + $options[$key] = $key; //Build following indicies from the sql. + } + } + return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true); + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; + } function update_content($recordid, $value, $name='') { $content = new object; diff --git a/mod/data/field/number/field.class.php b/mod/data/field/number/field.class.php index 3bb34649b6..073a091ad6 100755 --- a/mod/data/field/number/field.class.php +++ b/mod/data/field/number/field.class.php @@ -43,7 +43,20 @@ class data_field_number extends data_field_base { return insert_record('data_content', $content); } } + + function display_search_field($value = '') { + return ''; + } + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + // need to cast? + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; + } + function get_sort_sql($fieldname) { global $CFG; diff --git a/mod/data/field/picture/field.class.php b/mod/data/field/picture/field.class.php index 4603881165..39cff6a016 100755 --- a/mod/data/field/picture/field.class.php +++ b/mod/data/field/picture/field.class.php @@ -72,6 +72,18 @@ class data_field_picture extends data_field_file { return $str; } + function display_search_field($value = '') { + return ''; + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; + } + function display_browse_field($recordid, $template) { global $CFG; diff --git a/mod/data/field/radiobutton/field.class.php b/mod/data/field/radiobutton/field.class.php index 9b40e9cff1..0ed463184f 100755 --- a/mod/data/field/radiobutton/field.class.php +++ b/mod/data/field/radiobutton/field.class.php @@ -66,6 +66,27 @@ class data_field_radiobutton extends data_field_base { $str .= ''; return $str; } + + function display_search_field($value = '') { + global $CFG; + $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content'); + $options = array(); + if(!empty($temp)) { + $options[''] = ''; //Make first index blank. + foreach ($temp as $key) { + $options[$key] = $key; //Build following indicies from the sql. + } + } + return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true); + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') "; + } } ?> diff --git a/mod/data/field/text/field.class.php b/mod/data/field/text/field.class.php index e613f764a3..ba13558a5f 100755 --- a/mod/data/field/text/field.class.php +++ b/mod/data/field/text/field.class.php @@ -29,6 +29,19 @@ class data_field_text extends data_field_base { function data_field_text($field=0, $data=0) { parent::data_field_base($field, $data); } + + function display_search_field($value = '') { + return ''; + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; + } + } ?> diff --git a/mod/data/field/textarea/field.class.php b/mod/data/field/textarea/field.class.php index 44ebf79798..92183f06a7 100755 --- a/mod/data/field/textarea/field.class.php +++ b/mod/data/field/textarea/field.class.php @@ -68,8 +68,20 @@ class data_field_textarea extends data_field_base { $str .= ''; return $str; } - - + + + function display_search_field($value = '') { + return ''; + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; + } + function gen_textarea($usehtmleditor, $text='') { return print_textarea($usehtmleditor, $this->field->param3, $this->field->param2, '', '', 'field_'.$this->field->id, $text, '', true, 'field_' . $this->field->id); diff --git a/mod/data/field/url/field.class.php b/mod/data/field/url/field.class.php index 124d9e3c0d..23c9138b4d 100755 --- a/mod/data/field/url/field.class.php +++ b/mod/data/field/url/field.class.php @@ -58,6 +58,18 @@ class data_field_url extends data_field_base { return $str; } + function display_search_field($value = '') { + return ''; + } + + function parse_search_field() { + return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); + } + + function generate_sql($tablealias, $value) { + return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; + } + function display_browse_field($recordid, $template) { if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){ $url = empty($content->content)? '':$content->content; diff --git a/mod/data/lib.php b/mod/data/lib.php index 8ed6196a34..ecfe84eec2 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -904,42 +904,44 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re } -function data_print_show_all_form($data, $perpage, $sort, $order, $mode) { - echo '
'; - echo '
'; - echo '
'; - echo ''; - echo ''; - echo ''; // clear search - echo ''; - echo ''; - echo ''; - echo ''; - - echo '
'; - echo '
'; - echo '
'; -} - - /************************************************************************ * function that takes in the current data, number of items per page, * * a search string and prints a preference box in view.php * + * * + * This preference box prints a searchable advanced search template if * + * a) A template is defined * + * b) The advanced search checkbox is checked. * + * * * input @param object $data * * @param int $perpage * * @param string $search * * output null * ************************************************************************/ -function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $mode='single'){ - echo '
'; +function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= ''){ + global $CFG; + + $cm = get_coursemodule_from_instance('data', $data->id); + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + echo '
'; echo '
'; - echo '
'; + echo '
'; echo ''; + if ($mode =='asearch') { + $advanced = 1; + echo ''; + } echo ' '; $pagesizes = array(2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9,10=>10,15=>15, 20=>20,30=>30,40=>40,50=>50,100=>100,200=>200,300=>300,400=>400,500=>500,1000=>1000); choose_from_menu($pagesizes, 'perpage', $perpage, '', '', '0', false, false, 0, 'pref_perpage'); - echo '    '; + echo ''; echo '    '; //foreach field, print the option $fields = get_records('data_fields','dataid',$data->id, 'name'); @@ -965,13 +967,120 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order=' echo ''; } echo ''; - //print ASC or DESC - echo '   '; - echo ''; - echo ''; - echo '
'; - echo '
'; + + if ($advanced) { + $checked = ' checked="checked" '; + } + else { + $checked = ''; + } + print ' + + '; + + echo ' '.get_string('advancedsearch', 'data'); + echo ' '; + + echo '
'; + echo '
'; + + echo ''; + + // print ASC or DESC + echo ''; + $i = 0; + + // Determine if we are printing all fields for advanced search, or the template for advanced search + // If a template is not defined, use the deafault template and display all fields. + if(empty($data->asearchtemplate)) { + data_generate_default_template($data, 'asearchtemplate'); + } + + static $fields = NULL; + static $isteacher; + static $dataid = NULL; + + if (empty($dataid)) { + $dataid = $data->id; + } else if ($dataid != $data->id) { + $fields = NULL; + } + + if (empty($fields)) { + $fieldrecords = get_records('data_fields','dataid', $data->id); + foreach ($fieldrecords as $fieldrecord) { + $fields[]= data_get_field($fieldrecord, $data); + } + + $isteacher = has_capability('mod/data:managetemplates', $context); + } + + /// Replacing tags + $patterns = array(); + $replacement = array(); + + /// 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); + 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(); + } + } + + ///actual replacement of the tags + $newtext = preg_replace($patterns, $replacement, $data->asearchtemplate); + $options->para=false; + $options->noclean=true; + echo ''; + + echo ''; + echo '
 
'; + echo format_text($newtext, FORMAT_HTML, $options); + echo '

'; echo '
'; + echo '
'; + echo ''; + echo '
'; + + } function data_print_ratings($data, $record) { @@ -980,8 +1089,7 @@ function data_print_ratings($data, $record) { $cm = get_coursemodule_from_instance('data', $data->id); $context = get_context_instance(CONTEXT_MODULE, $cm->id); - if ($data->assessed and !empty($USER->id) - and (has_capability('mod/data:rate', $context) or has_capability('mod/data:viewrating', $context) or data_isowner($record->id))) { + if ($data->assessed and !empty($USER->id) and (has_capability('mod/data:rate', $context) or has_capability('mod/data:viewrating', $context) or data_isowner($record->id))) { if ($ratingsscale = make_grades_menu($data->scale)) { $ratingsmenuused = false; diff --git a/mod/data/tabs.php b/mod/data/tabs.php index 037abeabb4..a3f9071cf8 100755 --- a/mod/data/tabs.php +++ b/mod/data/tabs.php @@ -35,7 +35,10 @@ $row = array(); $row[] = new tabobject('list', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id, get_string('list','data'), '', true); - + + //Add an advanced search tab. + $row[] = new tabobject('asearch', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&mode=asearch', get_string('advancedsearch', 'data'), '', true); + if (isset($record)) { $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id, get_string('single','data'), '', true); } else { @@ -52,6 +55,8 @@ $defaultemplate = 'listtemplate'; } else if ($currenttab == 'add') { $defaultemplate = 'addtemplate'; + } else if ($currenttab == 'asearch') { + $defaultemplate = 'asearchtemplate'; } else { $defaultemplate = 'singletemplate'; } @@ -70,8 +75,9 @@ * stolen code from quiz report *****************************/ if ($currenttab == 'templates' and isset($mode)) { + $inactive[] = 'templates'; - $templatelist = array ('listtemplate', 'singletemplate', 'addtemplate', 'rsstemplate', 'csstemplate', 'jstemplate'); + $templatelist = array ('listtemplate', 'asearchtemplate' , 'singletemplate', 'addtemplate', 'rsstemplate', 'csstemplate', 'jstemplate'); $row = array(); $currenttab =''; @@ -83,11 +89,13 @@ } } $tabs[] = $row; + $activetwo = array('templates'); + } else { + $activetwo = array(); } - /// Print out the tabs and continue! - print_tabs($tabs, $currenttab, $inactive); + print_tabs($tabs, $currenttab, $inactive, $activetwo); ?> diff --git a/mod/data/templates.php b/mod/data/templates.php index e6f09dde17..fc78f0d9d6 100755 --- a/mod/data/templates.php +++ b/mod/data/templates.php @@ -114,7 +114,6 @@ $currenttab = 'templates'; include('tabs.php'); - /// Processing submitted data, i.e updating form. $resettemplate = false; @@ -165,12 +164,13 @@ data_generate_default_template($data, 'singletemplate'); data_generate_default_template($data, 'listtemplate'); data_generate_default_template($data, 'addtemplate'); + data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches. data_generate_default_template($data, 'rsstemplate'); } echo '
'; - echo '
'; + echo '
'; echo ''; // Print button to autogen all forms, if all templates are empty @@ -225,7 +225,7 @@ } // Print special tags. fix for MDL-7031 - if ($mode != 'addtemplate') { + if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template. echo ''; echo ''; echo ''; @@ -291,7 +291,7 @@ print_simple_box_end(); - echo '
'; + echo ''; echo '
'; if ($usehtmleditor) { use_html_editor('template'); diff --git a/mod/data/version.php b/mod/data/version.php index 966e4ed0a2..edbb95be03 100644 --- a/mod/data/version.php +++ b/mod/data/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2007020200; +$module->version = 2007022600; $module->requires = 2007020200; // Requires this Moodle version $module->cron = 60; diff --git a/mod/data/view.php b/mod/data/view.php index 46037815f6..69e95ecafe 100755 --- a/mod/data/view.php +++ b/mod/data/view.php @@ -28,8 +28,8 @@ require_once("$CFG->libdir/rsslib.php"); require_once('pagelib.php'); - - + + /// One of these is necessary! $id = optional_param('id', 0, PARAM_INT); // course module id $d = optional_param('d', 0, PARAM_INT); // database id @@ -38,7 +38,7 @@ $mode = optional_param('mode', '', PARAM_ALPHA); // Force the browse mode ('single') $edit = optional_param('edit', -1, PARAM_BOOL); - + $page = optional_param('page', 0, PARAM_INT); /// These can be added to perform an action on a record $approve = optional_param('approve', 0, PARAM_INT); //approval recordid $delete = optional_param('delete', 0, PARAM_INT); //delete recordid @@ -110,10 +110,78 @@ if (!isset($SESSION->dataprefs[$data->id])) { $SESSION->dataprefs[$data->id] = array(); $SESSION->dataprefs[$data->id]['search'] = ''; + $SESSION->dataprefs[$data->id]['search_array'] = array(); $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort; + $SESSION->dataprefs[$data->id]['advanced'] = 0; $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC'; } - $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS); + $advanced = optional_param('advanced', $SESSION->dataprefs[$data->id]['advanced'], PARAM_INT); + $search_array = $SESSION->dataprefs[$data->id]['search_array']; + + if (!empty($advanced)) { + $search = ''; + $fields = get_records('data_fields', 'dataid', $data->id); + + //Added to ammend paging error. This error would occur when attempting to go from one page of advanced + //search results to another. All fields were reset in the page transfer, and there was no way of determining + //whether or not the user reset them. This would cause a blank search to execute whenever the user attempted + //to see any page of results past the first. + //This fix works as follows: + //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time. + //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the + //execution falls through to the second condition below, allowing paging to be set to true. + //Paging remains true and keeps getting passed though the URL until a new search is performed + //(even if page 0 is revisited). + //A false $paging flag generates advanced search results based on the fields input by the user. + //A true $paging flag generates davanced search results from the $SESSION global. + //(See lines 147-158) + + $paging = optional_param('paging', NULL, PARAM_BOOL); + if($page == 0 && !isset($paging)) { + $paging = false; + } + else { + $paging = true; + } + 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 { + //Set value from session if there is a value @ the required index. + 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. + $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]); + } + } + } + } + $SESSION->dataprefs[$data->id]['search_array'] = $search_array; // Make it sticky + } + 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; + } + $textlib = new textlib(); if ($textlib->strlen($search) < 2) { $search = ''; @@ -137,8 +205,6 @@ set_user_preference('data_perpage_'.$data->id, $perpage); } - $page = optional_param('page', 0, PARAM_INT); - add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id); @@ -249,205 +315,238 @@ if ($record or $mode == 'single') { $currenttab = 'single'; - } else { + } elseif($mode == 'asearch') { + $currenttab = 'asearch'; + } + else { $currenttab = 'list'; } include('tabs.php'); + if ($mode != 'asearch') { + /// Approve any requested records -/// Approve any requested records - - if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) { - 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'); + if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) { + 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'); + } } } } - } -// If not teacher, check whether user has sufficient records to view - if (!has_capability('mod/data:managetemplates', $context) and data_numentries($data) < $data->requiredentriestoview){ - notify (($data->requiredentriestoview - data_numentries($data)).' '.get_string('insufficiententries','data')); - echo '
'; - print_footer($course); - exit; - } + // If not teacher, check whether user has sufficient records to view + if (!has_capability('mod/data:managetemplates', $context) and data_numentries($data) < $data->requiredentriestoview){ + notify (($data->requiredentriestoview - data_numentries($data)).' '.get_string('insufficiententries','data')); + echo ''; + print_footer($course); + exit; + } -/// We need to examine the whole dataset to produce the correct paging + /// We need to examine the whole dataset to produce the correct paging - if ((!has_capability('mod/data:managetemplates', $context)) && ($data->approval)) { - if (isloggedin()) { - $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id.') '; + if ((!has_capability('mod/data:managetemplates', $context)) && ($data->approval)) { + if (isloggedin()) { + $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id.') '; + } else { + $approveselect = ' AND r.approved=1 '; + } } else { - $approveselect = ' AND r.approved=1 '; + $approveselect = ' '; } - } else { - $approveselect = ' '; - } - - if ($currentgroup) { - $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)"; - } else { - $groupselect = ' '; - } -/// Find the field we are sorting on - 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.' '; - $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 '; - $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 '.$sortcontentfull.' '.$order.' , r.id ASC '; - if ($search) { - $searchselect = " AND (c1.content LIKE '%$search%') "; + if ($currentgroup) { + $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)"; } else { - $searchselect = ' '; + $groupselect = ' '; } - } else if ($search) { - $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.'user u '; - $where = 'WHERE c.recordid = r.id - AND r.userid = u.id - AND r.dataid = '.$data->id; - $sortorder = ' ORDER BY r.id ASC '; - $searchselect = " AND (c.content LIKE '%$search%') "; + /// Find the field we are sorting on + 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.' '; + $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 '; + $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 '.$sortcontentfull.' '.$order.' , r.id ASC '; + $searchselect = ''; + + if (!empty($advanced)) { //If advanced box is checked. + foreach($search_array as $key => $val) { //what does $search_array hold? + $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; + $where .= ' AND c'.$key.'.recordid = r.id'; + $searchselect .= ' AND ('.$val->sql.') '; + } + } + elseif ($search) { + $searchselect = ' AND (dc.content LIKE "%'.$search.'%") '; + } else { + $searchselect = ' '; + } + } else if ($search) { + $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.'user u '; + $where = 'WHERE c.recordid = r.id + AND r.userid = u.id + AND r.dataid = '.$data->id; + $sortorder = ' ORDER BY r.id ASC '; + $searchselect = ''; + + if (!empty($advanced)) { //Advanced search box again. + foreach($search_array as $key => $val) { + $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; + $where .= ' AND c'.$key.'.recordid = r.id '; + $searchselect .= ' AND ('.$val->sql.') '; + } + } + else { + $searchselect = ' AND (c.content LIKE "%'.$search.'%") '; + } - } else { - $what = ' DISTINCT r.id, r.approved, r.timecreated, r.userid, u.firstname, u.lastname '; - $count = ' 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. ' '; - $searchselect = ' '; - } + + } else { + $what = ' DISTINCT r.id, r.approved, r.timecreated, r.userid, u.firstname, u.lastname '; + $count = ' 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. ' '; + $searchselect = ' '; + } -/// 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; + $sqlselect = 'SELECT '.$what.$fromsql.$sortorder; - $sqlcount = 'SELECT '.$count.$fromsql; // Total number of records + $sqlcount = 'SELECT '.$count.$fromsql; // Total number of records -/// Work out the paging numbers + /// Work out the paging numbers - $totalcount = count_records_sql($sqlcount); + $totalcount = count_records_sql($sqlcount); - if ($record) { // We need to just show one, so where is it in context? - $nowperpage = 1; - $mode = 'single'; + 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); + # 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); - $allrecords = get_records_sql($sqlselect); // Kludgey but accurate at least! - $page = 0; - foreach ($allrecords as $key => $allrecord) { - if ($key == $record->id) { - break; + $allrecords = get_records_sql($sqlselect); // Kludgey but accurate at least! + $page = 0; + foreach ($allrecords as $key => $allrecord) { + if ($key == $record->id) { + break; + } + $page++; } - $page++; - } - } else if ($mode == 'single') { // We rely on ambient $page settings - $nowperpage = 1; + } else if ($mode == 'single') { // We rely on ambient $page settings + $nowperpage = 1; - } else { - $nowperpage = $perpage; - } - -/// Get the actual records + } else { + $nowperpage = $perpage; + } - $records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage); + /// Get the actual records + + $records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage); - if (empty($records)) { // 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; + if (empty($records)) { // 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; + } } } - } - if ($records) { // OK, we can show this one - data_print_template('singletemplate', $records, $data, $search, $page); - } else if ($search){ - notify(get_string('nomatch','data')); - data_print_show_all_form($data, $perpage, $sort, $order, $mode); - } else { - notify(get_string('norecords','data')); - } + if ($records) { // OK, we can show this one + data_print_template('singletemplate', $records, $data, $search, $page); + } else if ($search){ + notify(get_string('nomatch','data')); + } else { + notify(get_string('norecords','data')); + } - } else { // We have some records to print + } else { // We have some records to print - if ($mode == 'single') { // Single template - $baseurl = 'view.php?d='.$data->id.'&mode=single&'; + if ($mode == 'single') { // Single template + $baseurl = 'view.php?d='.$data->id.'&mode=single&'; - print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); + print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); - if (empty($data->singletemplate)){ - notify(get_string('nosingletemplate','data')); - data_generate_default_template($data, 'singletemplate', 0, false, false); - } + if (empty($data->singletemplate)){ + notify(get_string('nosingletemplate','data')); + data_generate_default_template($data, 'singletemplate', 0, false, false); + } - data_print_template('singletemplate', $records, $data, $search, $page); + data_print_template('singletemplate', $records, $data, $search, $page); - print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); + print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); - } else { // List template - $baseurl = 'view.php?d='.$data->id.'&'; + } else { // List template + $baseurl = 'view.php?d='.$data->id.'&'; + //send the advanced flag through the URL so it is remembered while paging. + $baseurl .= 'advanced='.$advanced.'&'; + //pass variable to allow determining whether or not we are paging through results. + $baseurl .= 'paging='.$paging.'&'; - print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); + print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); - if (empty($data->listtemplate)){ - notify(get_string('nolisttemplate','data')); - data_generate_default_template($data, 'listtemplate', 0, false, false); + if (empty($data->listtemplate)){ + notify(get_string('nolisttemplate','data')); + data_generate_default_template($data, 'listtemplate', 0, false, false); + } + echo $data->listtemplateheader; + data_print_template('listtemplate', $records, $data, $search, $page); + echo $data->listtemplatefooter; + + print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); } - echo $data->listtemplateheader; - data_print_template('listtemplate', $records, $data, $search, $page); - echo $data->listtemplatefooter; - print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); } - + } + + $search = trim($search); + if (empty($records)) { + $records = array(); } - if ($records || $search || $page) { - data_print_preference_form($data, $perpage, $search, $sort, $order, $mode); + //Advanced search form doesn't make sense for single (redirects list view) + if ($records || $search || $page || $mode = 'asearch' && $mode != 'single') { + data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode); } /// If we have blocks, then print the left side here diff --git a/theme/standard/styles_layout.css b/theme/standard/styles_layout.css index 7e7f69aba8..6fb43f5a25 100644 --- a/theme/standard/styles_layout.css +++ b/theme/standard/styles_layout.css @@ -2490,7 +2490,9 @@ body#user-index .rolesform { .defaulttemplate { text-align:center; } - +.datapreferences { + text-align:center; +} /*** *** Modules: Forum ***/ -- 2.39.5