Merge branch 'MDL-71070' of git://github.com/paulholden/moodle

This commit is contained in:
abgreeve 2021-08-04 10:14:18 +08:00
commit 26125cf670
10 changed files with 406 additions and 419 deletions

View file

@ -84,6 +84,9 @@ class flexible_table {
var $baseurl = NULL;
var $request = array();
/** @var string[] Columns that are expected to contain a users fullname. */
protected $userfullnamecolumns = ['fullname'];
/**
* @var bool Whether or not to store table properties in the user_preferences table.
*/
@ -599,6 +602,17 @@ class flexible_table {
return self::construct_order_by($this->get_sort_columns(), $this->column_textsort);
}
/**
* Whether the current table contains any fullname columns
*
* @return bool
*/
private function contains_fullname_columns(): bool {
$fullnamecolumns = array_intersect_key($this->columns, array_flip($this->userfullnamecolumns));
return !empty($fullnamecolumns);
}
/**
* Get the columns to sort by, in the form required by {@link construct_order_by()}.
* @return array column name => SORT_... constant.
@ -611,13 +625,11 @@ class flexible_table {
if (empty($this->prefs['sortby'])) {
return array();
}
foreach ($this->prefs['sortby'] as $column => $notused) {
if (isset($this->columns[$column])) {
continue; // This column is OK.
}
if (in_array($column, \core_user\fields::get_name_fields()) &&
isset($this->columns['fullname'])) {
if (in_array($column, \core_user\fields::get_name_fields()) && $this->contains_fullname_columns()) {
continue; // This column is OK.
}
// This column is not OK.
@ -656,7 +668,7 @@ class flexible_table {
$conditions = array();
$params = array();
if (isset($this->columns['fullname'])) {
if ($this->contains_fullname_columns()) {
static $i = 0;
$i++;
@ -973,8 +985,7 @@ class flexible_table {
$ilast = '';
}
if ((!empty($ifirst) || !empty($ilast) ||$this->use_initials)
&& isset($this->columns['fullname'])) {
if ((!empty($ifirst) || !empty($ilast) || $this->use_initials) && $this->contains_fullname_columns()) {
$prefixfirst = $this->request[TABLE_VAR_IFIRST];
$prefixlast = $this->request[TABLE_VAR_ILAST];
echo $OUTPUT->initials_bar($ifirst, 'firstinitial', get_string('firstname'), $prefixfirst, $this->baseurl);
@ -1231,44 +1242,43 @@ class flexible_table {
}
switch ($column) {
case 'fullname':
// Check the full name display for sortable fields.
if (has_capability('moodle/site:viewfullnames', $this->get_context())) {
$nameformat = $CFG->alternativefullnameformat;
} else {
$nameformat = $CFG->fullnamedisplay;
}
if ($nameformat == 'language') {
$nameformat = get_string('fullnamedisplay');
}
$requirednames = order_in_string(\core_user\fields::get_name_fields(), $nameformat);
if (!empty($requirednames)) {
if ($this->is_sortable($column)) {
// Done this way for the possibility of more than two sortable full name display fields.
$this->headers[$index] = '';
foreach ($requirednames as $name) {
$sortname = $this->sort_link(get_string($name),
$name, $primarysortcolumn === $name, $primarysortorder);
$this->headers[$index] .= $sortname . ' / ';
}
$helpicon = '';
if (isset($this->helpforheaders[$index])) {
$helpicon = $OUTPUT->render($this->helpforheaders[$index]);
}
$this->headers[$index] = substr($this->headers[$index], 0, -3). $helpicon;
}
}
break;
case 'userpic':
// do nothing, do not display sortable links
break;
break;
default:
if ($this->is_sortable($column)) {
if (array_search($column, $this->userfullnamecolumns) !== false) {
// Check the full name display for sortable fields.
if (has_capability('moodle/site:viewfullnames', $this->get_context())) {
$nameformat = $CFG->alternativefullnameformat;
} else {
$nameformat = $CFG->fullnamedisplay;
}
if ($nameformat == 'language') {
$nameformat = get_string('fullnamedisplay');
}
$requirednames = order_in_string(\core_user\fields::get_name_fields(), $nameformat);
if (!empty($requirednames)) {
if ($this->is_sortable($column)) {
// Done this way for the possibility of more than two sortable full name display fields.
$this->headers[$index] = '';
foreach ($requirednames as $name) {
$sortname = $this->sort_link(get_string($name),
$name, $primarysortcolumn === $name, $primarysortorder);
$this->headers[$index] .= $sortname . ' / ';
}
$helpicon = '';
if (isset($this->helpforheaders[$index])) {
$helpicon = $OUTPUT->render($this->helpforheaders[$index]);
}
$this->headers[$index] = substr($this->headers[$index], 0, -3) . $helpicon;
}
}
} else if ($this->is_sortable($column)) {
$helpicon = '';
if (isset($this->helpforheaders[$index])) {
$helpicon = $OUTPUT->render($this->helpforheaders[$index]);
@ -1328,7 +1338,7 @@ class flexible_table {
$isvalidsort = $sortby && $this->is_sortable($sortby);
$isvalidsort = $isvalidsort && empty($this->prefs['collapse'][$sortby]);
$isrealcolumn = isset($this->columns[$sortby]);
$isfullnamefield = isset($this->columns['fullname']) && in_array($sortby, $usernamefields);
$isfullnamefield = $this->contains_fullname_columns() && in_array($sortby, $usernamefields);
return $isvalidsort && ($isrealcolumn || $isfullnamefield);
}, ARRAY_FILTER_USE_KEY);