MDL-46921 lib: Sorting on additional fields, admin, enrolment pages.
authorAdrian Greeve <adrian@moodle.com>
Mon, 8 Sep 2014 08:21:53 +0000 (16:21 +0800)
committerAdrian Greeve <adrian@moodle.com>
Thu, 9 Oct 2014 00:19:49 +0000 (08:19 +0800)
admin/user.php
enrol/locallib.php
enrol/renderer.php
enrol/users.php

index 184ada6..88e37a4 100644 (file)
@@ -46,6 +46,8 @@
 
     $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
 
+    // The $user variable is also used outside of these if statements.
+    $user = null;
     if ($confirmuser and confirm_sesskey()) {
         require_capability('moodle/user:update', $sitecontext);
         if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id))) {
     // Carry on with the user listing
     $context = context_system::instance();
     $extracolumns = get_extra_user_fields($context);
-    $columns = array_merge(array('firstname', 'lastname'), $extracolumns,
-            array('city', 'country', 'lastaccess'));
+    // Get all user name fields as an array.
+    $allusernamefields = get_all_user_name_fields(false, null, null, null, true);
+    $columns = array_merge($allusernamefields, $extracolumns, array('city', 'country', 'lastaccess'));
 
     foreach ($columns as $column) {
         $string[$column] = get_user_field_name($column);
         $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
     }
 
-    $override = new stdClass();
-    $override->firstname = 'firstname';
-    $override->lastname = 'lastname';
-    $fullnamelanguage = get_string('fullnamedisplay', '', $override);
-    if (($CFG->fullnamedisplay == 'firstname lastname') or
-        ($CFG->fullnamedisplay == 'firstname') or
-        ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
-        $fullnamedisplay = "$firstname / $lastname";
-        if ($sort == "name") { // If sort has already been set to something else then ignore.
-            $sort = "firstname";
-        }
-    } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname').
-        $fullnamedisplay = "$lastname / $firstname";
-        if ($sort == "name") { // This should give the desired sorting based on fullnamedisplay.
-            $sort = "lastname";
-        }
+    // We need to check that alternativefullnameformat is not set to '' or language.
+    // We don't need to check the fullnamedisplay setting here as the fullname function call further down has
+    // the override parameter set to true.
+    $fullnamesetting = $CFG->alternativefullnameformat;
+    // If we are using language or it is empty, then retrieve all of the user's names.
+    if ($fullnamesetting == 'language' || empty($fullnamesetting)) {
+        $fullnamesetting = implode(' ', $allusernamefields);
+    }
+
+    // Order in string will ensure that the name columns are in the correct order.
+    $usernames = order_in_string($allusernamefields, $fullnamesetting);
+    $fullnamedisplay = array();
+    foreach ($usernames as $name) {
+        // Use the link from $$column for sorting on the user's name.
+        $fullnamedisplay[] = ${$name};
+    }
+    // All of the names are in one column. Put them into a string and separate them with a /.
+    $fullnamedisplay = implode(' / ', $fullnamedisplay);
+    // If $sort = name then it is the default for the setting and we should use the first name to sort by.
+    if ($sort == "name") {
+        // Use the first item in the array.
+        $sort = reset($usernames);
     }
 
     list($extrasql, $params) = $ufiltering->get_sql_filter();
index 5e2a928..433f685 100644 (file)
@@ -243,16 +243,8 @@ class course_enrolment_manager {
                       JOIN {enrol} e ON (e.id = ue.enrolid)
                  LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = u.id)
                  LEFT JOIN {groups_members} gm ON u.id = gm.userid
-                     WHERE $filtersql";
-            if ($sort === 'firstname') {
-                $sql .= " ORDER BY u.firstname $direction, u.lastname $direction";
-            } else if ($sort === 'lastname') {
-                $sql .= " ORDER BY u.lastname $direction, u.firstname $direction";
-            } else if ($sort === 'email') {
-                $sql .= " ORDER BY u.email $direction, u.lastname $direction, u.firstname $direction";
-            } else if ($sort === 'lastseen') {
-                $sql .= " ORDER BY ul.timeaccess $direction, u.lastname $direction, u.firstname $direction";
-            }
+                     WHERE $filtersql
+                  ORDER BY u.$sort $direction";
             $this->users[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage);
         }
         return $this->users[$key];
index e957b8a..2240199 100644 (file)
@@ -420,8 +420,8 @@ class course_enrolment_table extends html_table implements renderable {
      * @static
      * @var array
      */
-    protected static $sortablefields = array('firstname', 'lastname', 'idnumber', 'email',
-            'phone1', 'phone2', 'institution', 'department' );
+    protected static $sortablefields = array('firstname', 'lastname', 'firstnamephonetic', 'lastnamephonetic', 'middlename',
+            'alternatename', 'idnumber', 'email', 'phone1', 'phone2', 'institution', 'department' );
 
     /**
      * Constructs the table
@@ -709,7 +709,8 @@ class course_enrolment_users_table extends course_enrolment_table {
      * @static
      * @var array
      */
-    protected static $sortablefields = array('firstname', 'lastname', 'email', 'lastaccess');
+    protected static $sortablefields = array('firstname', 'lastname', 'firstnamephonetic', 'lastnamephonetic', 'middlename',
+            'alternatename', 'email', 'lastaccess');
 }
 
 /**
index 49421ef..fb585b4 100644 (file)
@@ -174,11 +174,25 @@ if ($action) {
 
 
 $renderer = $PAGE->get_renderer('core_enrol');
-$userdetails = array (
-    'picture' => false,
-    'firstname' => get_string('firstname'),
-    'lastname' => get_string('lastname'),
-);
+$userdetails = array('picture' => false);
+// Get all the user names in a reasonable default order.
+$allusernames = get_all_user_name_fields(false, null, null, null, true);
+// Initialise the variable for the user's names in the table header.
+$usernameheader = null;
+// Get the alternative full name format for users with the viewfullnames capability.
+$fullusernames = $CFG->alternativefullnameformat;
+// If fullusernames is empty or accidentally set to language then fall back on the $allusernames set up.
+if ($fullusernames == 'language' || empty($fullusernames)) {
+    $usernameheader = $allusernames;
+} else {
+    // If everything is as expected then put them in the order specified by the alternative full name format setting.
+    $usernameheader = order_in_string($allusernames, $fullusernames);
+}
+
+// Loop through each name and return the language string.
+foreach ($usernameheader as $key => $username) {
+    $userdetails[$username] = get_string($username);
+}
 $extrafields = get_extra_user_fields($context);
 foreach ($extrafields as $field) {
     $userdetails[$field] = get_user_field_name($field);