sort by username in Grades

sort by username in Grades

by Brian Jones -
Number of replies: 1
Every professor and TA currently using Moodle in my department has requested that the grades page ($MOODLE/grade/index.php) include a column for the student's 'username' database field, and that they be allowed to sort by that mixed  I'm pretty sure that in 1.6.1+ there's no way to enable this.

I'm happy to try to add the feature myself for the benefit of the community at large. If others find this useful, let me know, and if anyone can give me a clue as to the functions I'll need to alter, I'd love to know, because as of now it doesn't appear that the columns are necessarily put together in one place to be referenced by later functions. There's a lot of code in grade/lib.php, though, so I probably missed something. Surely each function doesn't put together its own presentation?

thanks,
brian.
In reply to Brian Jones

Re: sort by username in Grades

by Brian Jones -
Well, I must say that extending the assignment base class was actually a bit nicer than simply adding a column to the grade book! tongueout 

Anyway, what I've done is instead of the grade table having a 'Student' column on the left *and* right columns, I've *replaced* the left 'Student' column with a 'Username' column, and borrowed the sort arrow buttons from another part of the code to lend that functionality to the 'username' column. I need to write a 'uname_sort_asc' function, but I did add one function and some variables already. Here's the diff, with the newest file on the right.

strudel:~/moodle16.bak/grade jonesy$ diff lib.php ~/csmoodle/grade/lib.php
1c1
< <?php // $Id: lib.php,v 1.31.2.8 2006/07/18 06:42:21 skodak Exp $
---
> <?php // $Id: lib.php,v 1.2 2006/08/28 19:03:08 bkj-moodle Exp $
266a267,268
>                 $grades_by_student["$userid"]['student_data']['username'] = $student->username;
>
612a615,617
>                 case 'username':  // added by bkjones@gmail.com
>                     uasort($grades_by_student, 'grade_sort_by_username');
>                     break;
833a839,850
> function grade_sort_by_username($x,$y)
> {
>     //$grades_by_student["$student->userid"]['student_data']['firstname'] = $student->firstname;
>     //$grades_by_student["$student->userid"]['student_data']['lastname'] = $student->lastname;
>     if (strnatcasecmp($x['student_data']['username'],$y['student_data']['username']) == 0) {
>         return strnatcasecmp($x['student_data']['firstname'],$y['student_data']['firstname']);
>     }
>     else {
>         return strnatcasecmp($x['student_data']['username'],$y['student_data']['username']);
>     }
> }
>
1718a1736
>                 $uname_heading_link = get_string('username', 'grades').'<br />';
1722a1741,1744
>                
>                     //// uname_sort_link added by bkjones@gmail.com to provide sortable username column instead of two fname/lname cols.
>                     $uname_heading_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=username&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('usernamedescending','grades').'" /></a>';
>                     $uname_heading_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=username_asc&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('usernameascending','grades').'" /></a>';
1725a1748,1749
>                     $uname_heading_link .= '<br /><a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;cview='.$cview.'">'.get_string('showallstudents','grades').'</a>';
>                
1730c1754
<                 $header = '<tr class="header"><th rowspan="2">'.$student_heading_link.'</th>';
---
>                 $header = '<tr class="header"><th rowspan="2">'.$uname_heading_link.'</th>';
1783a1808,1809
>                         $uname_link = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student.'&amp;course='.$course->id.'">';
>
1786a1813
>                         $uname_link = '<a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;user='.$student.'&amp;cview='.$cview.'">';
1789c1816,1818
<                     $row .= '<td class="fullname">'.$student_link.'</td>';
---
>                     $uname_link .= $grades_by_student[$student]['student_data']['username'].'</a>';
>                    
>                     $row .= '<td class="fullname">'.$uname_link.'</td>';
2007a2037
>             $uname_heading_link = get_string('username', 'grades').'<br />';
2010a2041,2045
>                
>                 //// uname_sort_link added by bkjones@gmail.com to provide sortable username column instead of two fname/lname cols.
>                 $uname_heading_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=username&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('usernamedescending','grades').'" /></a>';
>                 $uname_heading_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=username_asc&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('usernameascending','grades').'" /></a>';
>                
2013a2049,2050
>                 $uname_heading_link .= '<br /><a href="?id='.$course->id.'&amp;&amp;action=grades"><font size="-2">'.get_string('showallstudents','grades').'</font></a>';
>
2015c2052
<             $header = '<tr><th rowspan="2">'.$student_heading_link.'</th>';
---
>             $header = '<tr><th rowspan="2">'.$uname_heading_link.'</th>';
2041c2078,2079
<                     $studentviewlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student.'&amp;course='.$course->id.'">'.$grades_by_student[$student]['student_data']['lastname'].', '.$grades_by_student[$student]['student_data']['firstname'].'</a>';
---
>                 $unameviewlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student.'&amp;course='.$course->id.'">'.$grades_by_student[$student]['student_data']['username'].'</a>';
>                 $studentviewlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student.'&amp;course='.$course->id.'">'.$grades_by_student[$student]['student_data']['lastname'].', '.$grades_by_student[$student]['student_data']['firstname'].' </a>';
2043a2082
>                     $unameviewlink = '<a href="?id='.$course->id.'&amp;action=view_student_grades&amp;user='.$student.'">'.$grades_by_student[$student]['student_data']['username'].'</a>';
2046c2085
<                 $row .= '<td>'. $studentviewlink .'</td>';
---
>                 $row .= '<td>'. $unameviewlink .'</td>';