]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9907 searching/sorting by author name; merged from MOODLE_19_STABLE
authorskodak <skodak>
Wed, 16 Apr 2008 11:51:50 +0000 (11:51 +0000)
committerskodak <skodak>
Wed, 16 Apr 2008 11:51:50 +0000 (11:51 +0000)
lang/en_utf8/data.php
mod/data/lib.php
mod/data/templates.php
mod/data/view.php

index c210445607649c0bf33722f551c08a7588efc9ac..73a55b515c2e33b7d20dc26980fb3030238c7ca6 100644 (file)
@@ -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';
index 87836f277cb5a1eb3e911f2910d9737c65f45a28..05f51e9aca1cb610e9074b0405e07246e21daa5d 100755 (executable)
@@ -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 .= '<tr><td align="center" colspan="2">##edit##  ##more##  ##delete##  ##approve##</td></tr>';
         } else if ($template == 'singletemplate') {
             $str .= '<tr><td align="center" colspan="2">##edit##  ##delete##  ##approve##</td></tr>';
+        } else if ($template == 'asearchtemplate') {
+            $str .= '<tr><td valign="top" align="right">'.get_string('authorfirstname', 'data').': </td><td>##firstname##</td></tr>';
+            $str .= '<tr><td valign="top" align="right">'.get_string('authorlastname', 'data').': </td><td>##lastname##</td></tr>';
         }
 
         $str .= '</table>';
@@ -1058,12 +1064,18 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
     echo '&nbsp;&nbsp;&nbsp;<label for="pref_sortby">'.get_string('sortby').'</label> ';
     //foreach field, print the option
     $fields = get_records('data_fields','dataid',$data->id, 'name');
-    echo '<select name="sort" id="pref_sortby"><option value="0">'.get_string('dateentered','data').'</option>';
+    $options = array();
     foreach ($fields as $field) {
-        if ($field->id == $sort) {
-            echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
+        $options[$field->id] = $field->name;
+    }
+    $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
+    $options[DATA_LASTNAME] = get_string('authorlastname', 'data');
+    echo '<select name="sort" id="pref_sortby"><option value="0">'.get_string('dateentered','data').'</option>';
+    foreach ($options as $key => $name) {
+        if ($key == $sort) {
+            echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
         } else {
-            echo '<option value="'.$field->id.'">'.$field->name.'</option>';
+            echo '<option value="'.$key.'">'.$name.'</option>';
         }
     }
     echo '</select>';
@@ -1118,7 +1130,7 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
         //  End -->
         //]]>
         </script>';
-    echo '&nbsp;<input type="checkbox" name="advanced" value="1" '.$checked.' onchange="showHideAdvSearch(this.checked);" />'.get_string('advancedsearch', 'data');
+    echo '&nbsp;<input type="checkbox" id="advancedcheckbox" name="advanced" value="1" '.$checked.' onchange="showHideAdvSearch(this.checked);" /><label for="advancedcheckbox">'.get_string('advancedsearch', 'data').'</label>';
     echo '&nbsp;<input type="submit" value="'.get_string('savesettings','data').'" />';
     
     echo '<br />';
@@ -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[] = '<input type="text" size="16" name="u_fn" value="'.$fn.'">';
+    $patterns[]    = '/##lastname##/';
+    $replacement[] = '<input type="text" size="16" name="u_ln" value="'.$ln.'">';
+
     ///actual replacement of the tags
     $newtext = preg_replace($patterns, $replacement, $data->asearchtemplate);
+
     $options = new object();
     $options->para=false;
     $options->noclean=true;
index 1855bedc94a3bd7a4966b91e6c7401a756b9964b..3ad7c966b291386225b18dc2b7af1d0af56d8fe3 100755 (executable)
 
     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');
         }
     }
 
             }
             echo '</optgroup>';
         }
+
+        if ($mode == 'asearchtemplate') {
+            echo '<optgroup label="'.get_string('user').'">';
+            echo '<option value="##firstname##">' .get_string('firstname'). ' - ##firstname##</option>';
+            echo '<option value="##lastname##">' .get_string('lastname'). ' - ##lastname##</option>';
+            echo '</optgroup>';
+        }
+
         echo '</select>';
         echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
         if (can_use_html_editor()) {
index c3ed5e3f7106a182bff346be72acc9ab71c696d4..d5b1a969a85fa627a5ec90dc72ac40e83cad1177 100755 (executable)
 
     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;
     }
         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;
         }
 
     /// 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
                          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) ';
             $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;
     #        $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