Skip to content

Commit

Permalink
MDL-68248 Blocks: My overview block: Add sort by short name
Browse files Browse the repository at this point in the history
  • Loading branch information
hiendinh committed Apr 27, 2020
1 parent df0e58a commit ad2ef9d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion admin/tool/usertours/lang/en/tool_usertours.php
Expand Up @@ -210,7 +210,7 @@
You can also choose to display the courses in a list, or with summary information, or the default \'card\' view.';
$string['tour3_title_displayoptions'] = 'Display options';
$string['tour3_content_displayoptions'] = 'Courses may be sorted by course name or by last access date.
$string['tour3_content_displayoptions'] = 'Courses may be sorted by course name, course short name or last access date.
You can also choose to display the courses in a list, with summary information, or the default \'card\' view.';

Expand Down
26 changes: 22 additions & 4 deletions blocks/myoverview/classes/output/main.php
Expand Up @@ -164,6 +164,7 @@ class main implements renderable, templatable {
* @throws \dml_exception
*/
public function __construct($grouping, $sort, $view, $paging, $customfieldvalue = null) {
global $CFG;
// Get plugin config.
$config = get_config('block_myoverview');

Expand All @@ -185,7 +186,18 @@ public function __construct($grouping, $sort, $view, $paging, $customfieldvalue
$this->customfieldvalue = $customfieldvalue;

// Check and remember the given sorting.
$this->sort = $sort ? $sort : BLOCK_MYOVERVIEW_SORTING_TITLE;
if ($sort) {
$this->sort = $sort;
} else if ($CFG->courselistshortnames) {
$this->sort = BLOCK_MYOVERVIEW_SORTING_SHORTNAME;
} else {
$this->sort = BLOCK_MYOVERVIEW_SORTING_TITLE;
}
// In case sorting remembered is shortname and display extended course names not checked,
// we should revert sorting to title.
if (!$CFG->courselistshortnames && $sort == BLOCK_MYOVERVIEW_SORTING_SHORTNAME) {
$this->sort = BLOCK_MYOVERVIEW_SORTING_TITLE;
}

// Check and remember the given view.
$this->view = $view ? $view : BLOCK_MYOVERVIEW_VIEW_CARD;
Expand Down Expand Up @@ -236,7 +248,6 @@ public function __construct($grouping, $sort, $view, $paging, $customfieldvalue
}
unset ($displaygroupingselectors, $displaygroupingselectorscount);
}

/**
* Determine the most sensible fallback grouping to use (in cases where the stored selection
* is no longer available).
Expand Down Expand Up @@ -393,7 +404,7 @@ public function get_customfield_values_for_export() {
*
*/
public function export_for_template(renderer_base $output) {
global $USER;
global $CFG, $USER;

$nocoursesurl = $output->image_url('courses', 'block_myoverview')->out();

Expand Down Expand Up @@ -422,12 +433,18 @@ public function export_for_template(renderer_base $output) {
}
$preferences = $this->get_preferences_as_booleans();
$availablelayouts = $this->get_formatted_available_layouts_for_export();
$sort = '';
if ($this->sort == BLOCK_MYOVERVIEW_SORTING_SHORTNAME) {
$sort = 'shortname';
} else {
$sort = $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc';
}

$defaultvariables = [
'totalcoursecount' => count(enrol_get_all_users_courses($USER->id, true)),
'nocoursesimg' => $nocoursesurl,
'grouping' => $this->grouping,
'sort' => $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc',
'sort' => $sort,
// If the user preference display option is not available, default to first available layout.
'view' => in_array($this->view, $this->layouts) ? $this->view : reset($this->layouts),
'paging' => $this->paging,
Expand All @@ -447,6 +464,7 @@ public function export_for_template(renderer_base $output) {
'customfieldvalue' => $this->customfieldvalue,
'customfieldvalues' => $customfieldvalues,
'selectedcustomfield' => $selectedcustomfield,
'showsortbyshortname' => $CFG->courselistshortnames,
];
return array_merge($defaultvariables, $preferences);

Expand Down
2 changes: 2 additions & 0 deletions blocks/myoverview/lang/en/block_myoverview.php
Expand Up @@ -44,6 +44,7 @@
$string['aria:title'] = 'Sort courses by course name';
$string['aria:past'] = 'Show past courses';
$string['aria:removefromfavourites'] = 'Remove star for';
$string['aria:shortname'] = 'Sort course by course short name';
$string['aria:summary'] = 'Switch to summary view';
$string['aria:sortingdropdown'] = 'Sorting drop-down menu';
$string['availablegroupings'] = 'Available filters';
Expand Down Expand Up @@ -73,6 +74,7 @@
$string['privacy:metadata:overviewgroupingpreference'] = 'The Course overview block grouping preference.';
$string['privacy:metadata:overviewpagingpreference'] = 'The Course overview block paging preference.';
$string['removefromfavourites'] = 'Unstar this course';
$string['shortname'] = 'Short name';
$string['summary'] = 'Summary';
$string['title'] = 'Course name';
$string['aria:hidecourse'] = 'Remove {$a} from view';
Expand Down
4 changes: 3 additions & 1 deletion blocks/myoverview/lib.php
Expand Up @@ -47,6 +47,7 @@
*/
define('BLOCK_MYOVERVIEW_SORTING_TITLE', 'title');
define('BLOCK_MYOVERVIEW_SORTING_LASTACCESSED', 'lastaccessed');
define('BLOCK_MYOVERVIEW_SORTING_SHORTNAME', 'shortname');

/**
* Constants for the user preferences view options
Expand Down Expand Up @@ -104,7 +105,8 @@ function block_myoverview_user_preferences() {
'type' => PARAM_ALPHA,
'choices' => array(
BLOCK_MYOVERVIEW_SORTING_TITLE,
BLOCK_MYOVERVIEW_SORTING_LASTACCESSED
BLOCK_MYOVERVIEW_SORTING_LASTACCESSED,
BLOCK_MYOVERVIEW_SORTING_SHORTNAME
)
);
$preferences['block_myoverview_user_view_preference'] = array(
Expand Down
10 changes: 9 additions & 1 deletion blocks/myoverview/templates/nav-sort-selector.mustache
Expand Up @@ -33,6 +33,7 @@
<span class="d-sm-inline-block" data-active-item-text>
{{#title}}{{#str}} title, block_myoverview {{/str}}{{/title}}
{{#lastaccessed}}{{#str}} lastaccessed, block_myoverview {{/str}}{{/lastaccessed}}
{{#shortname}}{{#str}} shortname, block_myoverview {{/str}}{{/shortname}}
</span>
</button>
<ul class="dropdown-menu" data-show-active-item aria-labelledby="sortingdropdown">
Expand All @@ -41,11 +42,18 @@
{{#str}} title, block_myoverview {{/str}}
</a>
</li>
{{#showsortbyshortname}}
<li>
<a class="dropdown-item {{#shortname}}active{{/shortname}}" href="#" data-filter="sort" data-pref="shortname" data-value="shortname" aria-label="{{#str}} aria:shortname, block_myoverview {{/str}}" aria-controls="courses-view-{{uniqid}}">
{{#str}} shortname, block_myoverview {{/str}}
</a>
</li>
{{/showsortbyshortname}}
<li>
<a class="dropdown-item {{#lastaccessed}}active{{/lastaccessed}}" href="#" data-filter="sort" data-pref="lastaccessed" data-value="ul.timeaccess desc" aria-label="{{#str}} aria:lastaccessed, block_myoverview {{/str}}" aria-controls="courses-view-{{uniqid}}">
{{#str}} lastaccessed, block_myoverview {{/str}}
</a>
</li>
</ul>
</div>
</div>
</div>
14 changes: 14 additions & 0 deletions blocks/myoverview/tests/behat/block_myoverview_dashboard.feature
Expand Up @@ -182,6 +182,20 @@ Feature: The my overview block allows users to easily access their courses
Then I should see "Last accessed" in the "Course overview" "block"
And "[data-sort='ul.timeaccess desc']" "css_element" in the "Course overview" "block" should be visible

Scenario: Short name sort persistence
Given I log in as "student1"
When I click on "sortingdropdown" "button" in the "Course overview" "block"
Then I should not see "Short name" in the "Course overview" "block"
When the following config values are set as admin:
| config | value |
| courselistshortnames | 1 |
And I reload the page
And I click on "sortingdropdown" "button" in the "Course overview" "block"
And I click on "Short name" "link" in the "Course overview" "block"
And I reload the page
Then I should see "Short name" in the "Course overview" "block"
And "[data-sort='shortname']" "css_element" in the "Course overview" "block" should be visible

Scenario: View inprogress courses with hide persistent functionality
Given I log in as "student1"
And I click on "All (except removed from view)" "button" in the "Course overview" "block"
Expand Down
3 changes: 2 additions & 1 deletion blocks/myoverview/tests/privacy_test.php
Expand Up @@ -72,6 +72,7 @@ public function user_preference_provider() {
return array(
array('block_myoverview_user_sort_preference', 'lastaccessed', ''),
array('block_myoverview_user_sort_preference', 'title', ''),
array('block_myoverview_user_sort_preference', 'shortname', ''),
array('block_myoverview_user_grouping_preference', 'allincludinghidden', ''),
array('block_myoverview_user_grouping_preference', 'all', ''),
array('block_myoverview_user_grouping_preference', 'inprogress', ''),
Expand Down Expand Up @@ -104,4 +105,4 @@ public function test_export_user_preferences_with_hidden_courses() {
$blockpreferences->{$name}->description
);
}
}
}

0 comments on commit ad2ef9d

Please sign in to comment.