Commit 9a4231e9 authored by Syxton's avatar Syxton
Browse files

MDL-45847 resort: Add more course sorting options

MDL-45847 resort: Add more course sorting options

Add descending sort options and course timecreated sort options.
Includes behat and unit test changes.
parent 5fd0df97
......@@ -221,12 +221,22 @@ class helper {
$actions['resortbyname'] = array(
'url' => new \moodle_url($baseurl, array('action' => 'resortcategories', 'resort' => 'name')),
'icon' => new \pix_icon('t/sort', new \lang_string('sort')),
'string' => new \lang_string('resortsubcategoriesbyname', 'moodle')
'string' => new \lang_string('resortsubcategoriesby', 'moodle' , get_string('categoryname'))
);
$actions['resortbynamedesc'] = array(
'url' => new \moodle_url($baseurl, array('action' => 'resortcategories', 'resort' => 'namedesc')),
'icon' => new \pix_icon('t/sort', new \lang_string('sort')),
'string' => new \lang_string('resortsubcategoriesbyreverse', 'moodle', get_string('categoryname'))
);
$actions['resortbyidnumber'] = array(
'url' => new \moodle_url($baseurl, array('action' => 'resortcategories', 'resort' => 'idnumber')),
'icon' => new \pix_icon('t/sort', new \lang_string('sort')),
'string' => new \lang_string('resortsubcategoriesbyidnumber', 'moodle')
'string' => new \lang_string('resortsubcategoriesby', 'moodle', get_string('idnumbercoursecategory'))
);
$actions['resortbyidnumberdesc'] = array(
'url' => new \moodle_url($baseurl, array('action' => 'resortcategories', 'resort' => 'idnumberdesc')),
'icon' => new \pix_icon('t/sort', new \lang_string('sort')),
'string' => new \lang_string('resortsubcategoriesbyreverse', 'moodle', get_string('idnumbercoursecategory'))
);
}
......
......@@ -407,8 +407,10 @@ class core_course_management_renderer extends plugin_renderer_base {
$form .= html_writer::div(
html_writer::select(
array(
'name' => get_string('sortcategoriesbyname'),
'idnumber' => get_string('sortcategoriesbyidnumber'),
'name' => get_string('sortbyx', 'moodle', get_string('categoryname')),
'namedesc' => get_string('sortbyxreverse', 'moodle', get_string('categoryname')),
'idnumber' => get_string('sortbyx', 'moodle', get_string('idnumbercoursecategory')),
'idnumberdesc' => get_string('sortbyxreverse' , 'moodle' , get_string('idnumbercoursecategory')),
'none' => get_string('dontsortcategories')
),
'resortcategoriesby',
......@@ -420,9 +422,14 @@ class core_course_management_renderer extends plugin_renderer_base {
$form .= html_writer::div(
html_writer::select(
array(
'fullname' => get_string('sortcoursesbyfullname'),
'shortname' => get_string('sortcoursesbyshortname'),
'idnumber' => get_string('sortcoursesbyidnumber'),
'fullname' => get_string('sortbyx', 'moodle', get_string('fullnamecourse')),
'fullnamedesc' => get_string('sortbyxreverse', 'moodle', get_string('fullnamecourse')),
'shortname' => get_string('sortbyx', 'moodle', get_string('shortnamecourse')),
'shortnamedesc' => get_string('sortbyxreverse', 'moodle', get_string('shortnamecourse')),
'idnumber' => get_string('sortbyx', 'moodle', get_string('idnumbercourse')),
'idnumberdesc' => get_string('sortbyxreverse', 'moodle', get_string('idnumbercourse')),
'timecreated' => get_string('sortbyx', 'moodle', get_string('timecreatedcourse')),
'timecreateddesc' => get_string('sortbyxreverse', 'moodle', get_string('timecreatedcourse')),
'none' => get_string('dontsortcourses')
),
'resortcoursesby',
......@@ -676,12 +683,38 @@ class core_course_management_renderer extends plugin_renderer_base {
$params['sesskey'] = sesskey();
$baseurl = new moodle_url('/course/management.php', $params);
$fullnameurl = new moodle_url($baseurl, array('resort' => 'fullname'));
$fullnameurldesc = new moodle_url($baseurl, array('resort' => 'fullnamedesc'));
$shortnameurl = new moodle_url($baseurl, array('resort' => 'shortname'));
$shortnameurldesc = new moodle_url($baseurl, array('resort' => 'shortnamedesc'));
$idnumberurl = new moodle_url($baseurl, array('resort' => 'idnumber'));
$idnumberdescurl = new moodle_url($baseurl, array('resort' => 'idnumberdesc'));
$timecreatedurl = new moodle_url($baseurl, array('resort' => 'timecreated'));
$timecreateddescurl = new moodle_url($baseurl, array('resort' => 'timecreateddesc'));
$menu = new action_menu(array(
new action_menu_link_secondary($fullnameurl, null, get_string('resortbyfullname')),
new action_menu_link_secondary($shortnameurl, null, get_string('resortbyshortname')),
new action_menu_link_secondary($idnumberurl, null, get_string('resortbyidnumber'))
new action_menu_link_secondary($fullnameurl,
null,
get_string('sortbyx', 'moodle', get_string('fullnamecourse'))),
new action_menu_link_secondary($fullnameurldesc,
null,
get_string('sortbyxreverse', 'moodle', get_string('fullnamecourse'))),
new action_menu_link_secondary($shortnameurl,
null,
get_string('sortbyx', 'moodle', get_string('shortnamecourse'))),
new action_menu_link_secondary($shortnameurldesc,
null,
get_string('sortbyxreverse', 'moodle', get_string('shortnamecourse'))),
new action_menu_link_secondary($idnumberurl,
null,
get_string('sortbyx', 'moodle', get_string('idnumbercourse'))),
new action_menu_link_secondary($idnumberdescurl,
null,
get_string('sortbyxreverse', 'moodle', get_string('idnumbercourse'))),
new action_menu_link_secondary($timecreatedurl,
null,
get_string('sortbyx', 'moodle', get_string('timecreatedcourse'))),
new action_menu_link_secondary($timecreateddescurl,
null,
get_string('sortbyxreverse', 'moodle', get_string('timecreatedcourse')))
));
$menu->set_menu_trigger(get_string('resortcourses'));
$actions[] = $this->render($menu);
......
......@@ -2517,7 +2517,8 @@ function create_course($data, $editoroptions = NULL) {
}
}
$data->timecreated = time();
// Check if timecreated is given.
$data->timecreated = !empty($data->timecreated) ? $data->timecreated : time();
$data->timemodified = $data->timecreated;
// place at beginning of any category
......
......@@ -354,10 +354,14 @@ if ($action !== false && confirm_sesskey()) {
// They're not sorting anything.
break;
}
if (!in_array($sortcategoriesby, array('idnumber', 'name'))) {
if (!in_array($sortcategoriesby, array('idnumber', 'idnumberdesc',
'name', 'namedesc'))) {
$sortcategoriesby = false;
}
if (!in_array($sortcoursesby, array('idnumber', 'fullname', 'shortname'))) {
if (!in_array($sortcoursesby, array('timecreated', 'timecreateddesc',
'idnumber', 'idnumberdesc',
'fullname', 'fullnamedesc',
'shortname', 'shortnamedesc'))) {
$sortcoursesby = false;
}
......
......@@ -25,8 +25,10 @@ Feature: Test we can resort categories in the management interface.
Examples:
| sortby | cat1 | cat2 | cat3 |
| "Sort categories by name" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort categories by ID number" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Category name ascending" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort by Category name descending" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "Sort by Category ID number ascending" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Category ID number descending" | "Applied sciences" | "Social studies" | "Extended social studies" |
Scenario Outline: Test bulk sorting current category.
Given the following "categories" exist:
......@@ -52,8 +54,10 @@ Feature: Test we can resort categories in the management interface.
Examples:
| sortby | cat1 | cat2 | cat3 |
| "Sort categories by name" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort categories by ID number" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Category name ascending" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort by Category name descending" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "Sort by Category ID number ascending" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Category ID number descending" | "Applied sciences" | "Social studies" | "Extended social studies" |
Scenario Outline: Test resorting subcategories.
Given the following "categories" exist:
......@@ -77,8 +81,10 @@ Feature: Test we can resort categories in the management interface.
Examples:
| sortby | cat1 | cat2 | cat3 |
| "resortbyname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "resortbyidnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "resortbyname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "resortbynamedesc" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "resortbyidnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "resortbyidnumberdesc" | "Applied sciences" | "Social studies" | "Extended social studies" |
@javascript
Scenario Outline: Test resorting subcategories with JS enabled.
......@@ -103,8 +109,10 @@ Feature: Test we can resort categories in the management interface.
Examples:
| sortby | cat1 | cat2 | cat3 |
| "resortbyname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "resortbyidnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "resortbyname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "resortbynamedesc" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "resortbyidnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "resortbyidnumberdesc" | "Applied sciences" | "Social studies" | "Extended social studies" |
# The scenario below this is the same but with JS enabled.
Scenario: Test moving categories up and down by one.
......
......@@ -255,8 +255,10 @@ Feature: Course category management interface performs as expected
Examples:
| sortby | cat1 | cat2 | cat3 |
| "Sort categories by name" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort categories by ID number" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Category name ascending" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort by Category name descending" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "Sort by Category ID number ascending" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Category ID number descending" | "Applied sciences" | "Social studies" | "Extended social studies" |
@javascript
Scenario Outline: Sub categories are displayed correctly when resorted
......@@ -281,8 +283,10 @@ Feature: Course category management interface performs as expected
Examples:
| sortby | cat1 | cat2 | cat3 |
| "resortbyname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "resortbyidnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "resortbyname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "resortbynamedesc" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "resortbyidnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "resortbyidnumberdesc" | "Applied sciences" | "Social studies" | "Extended social studies" |
@javascript
Scenario Outline: Test courses are displayed correctly after being resorted.
......@@ -290,10 +294,10 @@ Feature: Course category management interface performs as expected
| name | category 0| idnumber |
| Cat 1 | 0 | CAT1 |
And the following "courses" exist:
| category | fullname | shortname | idnumber | sortorder |
| CAT1 | Social studies | Senior school | Ext003 | 1 |
| CAT1 | Applied sciences | Middle school | Sci001 | 2 |
| CAT1 | Extended social studies | Junior school | Ext002 | 3 |
| category | fullname | shortname | idnumber | sortorder | timecreated |
| CAT1 | Social studies | Senior school | Ext003 | 1 | 10000000001 |
| CAT1 | Applied sciences | Middle school | Sci001 | 2 | 10000000002 |
| CAT1 | Extended social studies | Junior school | Ext002 | 3 | 10000000003 |
And I log in as "admin"
And I go to the courses management page
......@@ -302,9 +306,14 @@ Feature: Course category management interface performs as expected
# Redirect.
And I should see the "Course categories and courses" management page
And I click on "Sort courses" "link"
And I should see "By fullname" in the ".course-listing-actions" "css_element"
And I should see "By shortname" in the ".course-listing-actions" "css_element"
And I should see "By idnumber" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course full name ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course full name descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course short name ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course short name descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course ID number ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course ID number descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course time created ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course time created descending" in the ".course-listing-actions" "css_element"
And I click on <sortby> "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see the "Course categories and courses" management page
......@@ -313,9 +322,14 @@ Feature: Course category management interface performs as expected
Examples:
| sortby | course1 | course2 | course3 |
| "By fullname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "By shortname" | "Extended social studies" | "Applied sciences" | "Social studies" |
| "By idnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Course full name ascending" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort by Course full name descending" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "Sort by Course short name ascending" | "Extended social studies" | "Applied sciences" | "Social studies" |
| "Sort by Course short name descending" | "Social studies" | "Applied sciences" | "Extended social studies" |
| "Sort by Course ID number ascending" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Course ID number descending" | "Applied sciences" | "Social studies" | "Extended social studies" |
| "Sort by Course time created ascending" | "Social studies" | "Applied sciences" | "Extended social studies" |
| "Sort by Course time created descending" | "Extended social studies" | "Applied sciences" | "Social studies" |
@javascript
Scenario: Test course pagination
......@@ -344,7 +358,7 @@ Feature: Course category management interface performs as expected
# Redirect.
And I should see the "Course categories and courses" management page
And I click on "Sort courses" "link"
And I click on "By idnumber" "link" in the ".course-listing-actions" "css_element"
And I click on "Sort by Course ID number ascending" "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see "Per page: 20" in the ".course-listing-actions" "css_element"
And I should see course listing "Course 1" before "Course 2"
......@@ -527,7 +541,7 @@ Feature: Course category management interface performs as expected
# Redirect.
And I should see the "Course categories and courses" management page
And I click on "Sort courses" "link"
And I click on "By idnumber" "link" in the ".course-listing-actions" "css_element"
And I click on "Sort by Course ID number ascending" "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see "Per page: 20" in the ".course-listing-actions" "css_element"
And I should see course listing "Course 1" before "Course 2"
......@@ -592,7 +606,7 @@ Feature: Course category management interface performs as expected
# Redirect.
And I should see the "Course categories and courses" management page
And I click on "Sort courses" "link"
And I click on "By idnumber" "link" in the ".course-listing-actions" "css_element"
And I click on "Sort by Course ID number ascending" "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see the "Course categories and courses" management page
And I should see "Per page: 20" in the ".course-listing-actions" "css_element"
......
......@@ -10,10 +10,10 @@ Feature: Test we can resort course in the management interface.
| name | category 0| idnumber |
| Cat 1 | 0 | CAT1 |
And the following "courses" exist:
| category | fullname | shortname | idnumber | sortorder |
| CAT1 | Social studies | Senior school | Ext003 | 1 |
| CAT1 | Applied sciences | Middle school | Sci001 | 2 |
| CAT1 | Extended social studies | Junior school | Ext002 | 3 |
| category | fullname | shortname | idnumber | sortorder | timecreated |
| CAT1 | Social studies | Senior school | Ext003 | 1 | 10000000001 |
| CAT1 | Applied sciences | Middle school | Sci001 | 2 | 10000000002 |
| CAT1 | Extended social studies | Junior school | Ext002 | 3 | 10000000003 |
And I log in as "admin"
And I go to the courses management page
......@@ -22,9 +22,14 @@ Feature: Test we can resort course in the management interface.
# Redirect.
And I should see the "Course categories and courses" management page
And I should see "Sort courses" in the ".course-listing-actions" "css_element"
And I should see "By fullname" in the ".course-listing-actions" "css_element"
And I should see "By shortname" in the ".course-listing-actions" "css_element"
And I should see "By idnumber" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course full name ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course full name descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course short name ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course short name descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course ID number ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course ID number descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course time created ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course time created descending" in the ".course-listing-actions" "css_element"
And I click on <sortby> "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see the "Course categories and courses" management page
......@@ -33,9 +38,14 @@ Feature: Test we can resort course in the management interface.
Examples:
| sortby | course1 | course2 | course3 |
| "By fullname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "By shortname" | "Extended social studies" | "Applied sciences" | "Social studies" |
| "By idnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Course full name ascending" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort by Course full name descending" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "Sort by Course short name ascending" | "Extended social studies" | "Applied sciences" | "Social studies" |
| "Sort by Course short name descending" | "Social studies" | "Applied sciences" | "Extended social studies" |
| "Sort by Course ID number ascending" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Course ID number descending" | "Applied sciences" | "Social studies" | "Extended social studies" |
| "Sort by Course time created ascending" | "Social studies" | "Applied sciences" | "Extended social studies" |
| "Sort by Course time created descending" | "Extended social studies" | "Applied sciences" | "Social studies" |
@javascript
Scenario Outline: Resort courses with JavaScript enabled.
......@@ -43,10 +53,10 @@ Feature: Test we can resort course in the management interface.
| name | category 0| idnumber |
| Cat 1 | 0 | CAT1 |
And the following "courses" exist:
| category | fullname | shortname | idnumber | sortorder |
| CAT1 | Social studies | Senior school | Ext003 | 1 |
| CAT1 | Applied sciences | Middle school | Sci001 | 2 |
| CAT1 | Extended social studies | Junior school | Ext002 | 3 |
| category | fullname | shortname | idnumber | sortorder | timecreated |
| CAT1 | Social studies | Senior school | Ext003 | 1 | 10000000001 |
| CAT1 | Applied sciences | Middle school | Sci001 | 2 | 10000000002 |
| CAT1 | Extended social studies | Junior school | Ext002 | 3 | 10000000003 |
And I log in as "admin"
And I go to the courses management page
......@@ -55,13 +65,23 @@ Feature: Test we can resort course in the management interface.
# Redirect.
And I should see the "Course categories and courses" management page
And I should see "Sort courses" in the ".course-listing-actions" "css_element"
And I should not see "By fullname" in the ".course-listing-actions" "css_element"
And I should not see "By shortname" in the ".course-listing-actions" "css_element"
And I should not see "By idnumber" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course full name ascending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course full name descending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course short name ascending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course short name descending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course ID number ascending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course ID number descending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course time created ascending" in the ".course-listing-actions" "css_element"
And I should not see "Sort by Course time created descending" in the ".course-listing-actions" "css_element"
And I click on "Sort courses" "link"
And I should see "By fullname" in the ".course-listing-actions" "css_element"
And I should see "By shortname" in the ".course-listing-actions" "css_element"
And I should see "By idnumber" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course full name ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course full name descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course short name ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course short name descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course ID number ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course ID number descending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course time created ascending" in the ".course-listing-actions" "css_element"
And I should see "Sort by Course time created descending" in the ".course-listing-actions" "css_element"
And I click on <sortby> "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see the "Course categories and courses" management page
......@@ -70,9 +90,14 @@ Feature: Test we can resort course in the management interface.
Examples:
| sortby | course1 | course2 | course3 |
| "By fullname" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "By shortname" | "Extended social studies" | "Applied sciences" | "Social studies" |
| "By idnumber" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Course full name ascending" | "Applied sciences" | "Extended social studies" | "Social studies" |
| "Sort by Course full name descending" | "Social studies" | "Extended social studies" | "Applied sciences" |
| "Sort by Course short name ascending" | "Extended social studies" | "Applied sciences" | "Social studies" |
| "Sort by Course short name descending" | "Social studies" | "Applied sciences" | "Extended social studies" |
| "Sort by Course ID number ascending" | "Extended social studies" | "Social studies" | "Applied sciences" |
| "Sort by Course ID number descending" | "Applied sciences" | "Social studies" | "Extended social studies" |
| "Sort by Course time created ascending" | "Social studies" | "Applied sciences" | "Extended social studies" |
| "Sort by Course time created descending" | "Extended social studies" | "Applied sciences" | "Social studies" |
Scenario: Test moving courses up and down by one.
Given the following "categories" exist:
......@@ -93,7 +118,7 @@ Feature: Test we can resort course in the management interface.
And I should see "Course categories" in the "#category-listing h3" "css_element"
And I should see "Cat 1" in the "#category-listing" "css_element"
And I click on "Sort courses" "link"
And I click on "By idnumber" "link" in the ".course-listing-actions" "css_element"
And I click on "Sort by Course ID number ascending" "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see the "Course categories and courses" management page
And I should see course listing "Course 1" before "Course 2"
......@@ -130,7 +155,7 @@ Feature: Test we can resort course in the management interface.
And I should see "Course categories" in the "#category-listing h3" "css_element"
And I should see "Cat 1" in the "#category-listing" "css_element"
And I click on "Sort courses" "link"
And I click on "By idnumber" "link" in the ".course-listing-actions" "css_element"
And I click on "Sort by Course ID number ascending" "link" in the ".course-listing-actions" "css_element"
# Redirect.
And I should see the "Course categories and courses" management page
And I should see course listing "Course 1" before "Course 2"
......
......@@ -1513,12 +1513,9 @@ $string['resetstartdate'] = 'Reset start date';
$string['resetstatus'] = 'Status';
$string['resettask'] = 'Task';
$string['resettodefaults'] = 'Reset to defaults';
$string['resortsubcategoriesbyname'] = 'Sort subcategories by name';
$string['resortsubcategoriesbyidnumber'] = 'Sort subcategories by idnumber';
$string['resortsubcategoriesby'] = 'Sort subcategories by {$a} ascending';
$string['resortsubcategoriesbyreverse'] = 'Sort subcategories by {$a} descending';
$string['resortcourses'] = 'Sort courses';
$string['resortbyshortname'] = 'By shortname';
$string['resortbyfullname'] = 'By fullname';
$string['resortbyidnumber'] = 'By idnumber';
$string['resource'] = 'Resource';
$string['resourcedisplayauto'] = 'Automatic';
$string['resourcedisplaydownload'] = 'Force download';
......@@ -1724,11 +1721,6 @@ $string['sort'] = 'Sort';
$string['sortby'] = 'Sort by';
$string['sortbyx'] = 'Sort by {$a} ascending';
$string['sortbyxreverse'] = 'Sort by {$a} descending';
$string['sortcategoriesbyname'] = 'Sort categories by name';
$string['sortcategoriesbyidnumber'] = 'Sort categories by ID number';
$string['sortcoursesbyfullname'] = 'Sort courses by full name';
$string['sortcoursesbyshortname'] = 'Sort courses by short name';
$string['sortcoursesbyidnumber'] = 'Sort courses by ID number';
$string['sorting'] = 'Sorting';
$string['sourcerole'] = 'Source role';
$string['specifyname'] = 'You must specify a name.';
......@@ -1822,6 +1814,7 @@ $string['therearecourses'] = 'There are {$a} courses';
$string['thiscategory'] = 'This category';
$string['thiscategorycontains'] = 'This category contains';
$string['time'] = 'Time';
$string['timecreatedcourse'] = 'Course time created';
$string['timezone'] = 'Timezone';
$string['to'] = 'To';
$string['tocreatenewaccount'] = 'Skip to create new account';
......
......@@ -258,7 +258,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
return $coursecat;
} else {
if ($strictness == MUST_EXIST) {
throw new moodle_exception('unknowcategory');
throw new moodle_exception('unknowncategory');
}
}
return null;
......@@ -2441,18 +2441,26 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
/**
* Resorts the sub categories of this category by the given field.
*
* @param string $field
* @param string $field One of name, idnumber or descending values of each (appended desc)
* @param bool $cleanup If true cleanup will be done, if false you will need to do it manually later.
* @return bool True on success.
* @throws coding_exception
*/
public function resort_subcategories($field, $cleanup = true) {
global $DB;
$desc = false;
if (substr($field, -4) === "desc") {
$desc = true;
$field = substr($field, 0, -4); // Remove "desc" from field name.
}
if ($field !== 'name' && $field !== 'idnumber') {
throw new coding_exception('Invalid field requested');
}
$children = $this->get_children();
core_collator::asort_objects_by_property($children, $field, core_collator::SORT_NATURAL);
if (!empty($desc)) {
$children = array_reverse($children);
}
$i = 1;
foreach ($children as $cat) {
$i++;
......@@ -2481,14 +2489,19 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
/**
* Resort the courses within this category by the given field.
*
* @param string $field One of fullname, shortname or idnumber
* @param string $field One of fullname, shortname, idnumber or descending values of each (appended desc)
* @param bool $cleanup
* @return bool True for success.
* @throws coding_exception
*/
public function resort_courses($field, $cleanup = true) {
global $DB;
if ($field !== 'fullname' && $field !== 'shortname' && $field !== 'idnumber') {
$desc = false;
if (substr($field, -4) === "desc") {
$desc = true;
$field = substr($field, 0, -4); // Remove "desc" from field name.
}
if ($field !== 'fullname' && $field !== 'shortname' && $field !== 'idnumber' && $field !== 'timecreated') {
// This is ultra important as we use $field in an SQL statement below this.
throw new coding_exception('Invalid field requested');
}
......@@ -2497,8 +2510,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
FROM {course} c
LEFT JOIN {context} ctx ON ctx.instanceid = c.id
WHERE ctx.contextlevel = :ctxlevel AND
c.category = :categoryid
ORDER BY c.{$field}, c.sortorder";
c.category = :categoryid";
$params = array(
'ctxlevel' => CONTEXT_COURSE,
'categoryid' => $this->id
......@@ -2527,6 +2539,9 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate {
}
// Sort the courses.
core_collator::asort_objects_by_property($courses, 'sortby', core_collator::SORT_NATURAL);
if (!empty($desc)) {
$courses = array_reverse($courses);
}
$i = 1;
foreach ($courses as $course) {
$DB->set_field('course', 'sortorder', $this->sortorder + $i, array('id' => $course->id));
......
......@@ -395,25 +395,29 @@ class core_coursecatlib_testcase extends advanced_testcase {
'category' => $category->id,
'idnumber' => '006-01',
'shortname' => 'Biome Study',
'fullname' => '<span lang="ar" class="multilang">'.'دراسة منطقة إحيائية'.'</span><span lang="en" class="multilang">Biome Study</span>'
'fullname' => '<span lang="ar" class="multilang">'.'دراسة منطقة إحيائية'.'</span><span lang="en" class="multilang">Biome Study</span>',
'timecreated' => '10000000001'
));
$course2 = $generator->create_course(array(
'category' => $category->id,
'idnumber' => '007-02',
'shortname' => 'Chemistry Revision',
'fullname' => 'Chemistry Revision'
'fullname' => 'Chemistry Revision',
'timecreated' => '10000000002'
));
$course3 = $generator->create_course(array(
'category' => $category->id,
'idnumber' => '007-03',
'shortname' => 'Swiss Rolls and Sunflowers',
'fullname' => 'Aarkvarks guide to Swiss Rolls and Sunflowers'
'fullname' => 'Aarkvarks guide to Swiss Rolls and Sunflowers',
'timecreated' => '10000000003'
));
$course4 = $generator->create_course(array(
'category' => $category->id,
'idnumber' => '006-04',
'shortname' => 'Scratch',
'fullname' => '<a href="test.php">Basic Scratch</a>'
'fullname' => '<a href="test.php">Basic Scratch</a>',
'timecreated' => '10000000004'
));
$c1 = (int)$course1->id;
$c2 = (int)$course2->id;
......@@ -427,6 +431,8 @@ class core_coursecatlib_testcase extends advanced_testcase {
$this->assertTrue($coursecat->resort_courses('shortname'));
$this->assertSame(array($c1, $c2, $c4, $c3), array_keys($coursecat->get_courses()));
$this->assertTrue($coursecat->resort_courses('timecreated'));
$this->assertSame(array($c1, $c2, $c3, $c4), array_keys($coursecat->get_courses()));
try {
// Enable the multilang filter and set it to apply to headings and content.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment