diff --git a/course/classes/management/helper.php b/course/classes/management/helper.php index 9a02d19c83541..14de49b4d5a2f 100644 --- a/course/classes/management/helper.php +++ b/course/classes/management/helper.php @@ -221,12 +221,22 @@ public static function get_category_listitem_actions(\coursecat $category) { $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')) ); } diff --git a/course/classes/management_renderer.php b/course/classes/management_renderer.php index 530d507c108e9..6ed18dd0bb97c 100644 --- a/course/classes/management_renderer.php +++ b/course/classes/management_renderer.php @@ -407,8 +407,10 @@ public function category_bulk_actions(coursecat $category = null) { $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 @@ public function category_bulk_actions(coursecat $category = null) { $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 @@ public function course_listing_actions(coursecat $category, course_in_list $cour $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); diff --git a/course/lib.php b/course/lib.php index ffde87bd8f454..1915299f59fa4 100644 --- a/course/lib.php +++ b/course/lib.php @@ -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 diff --git a/course/management.php b/course/management.php index b87b74f599057..85a2494f3f453 100644 --- a/course/management.php +++ b/course/management.php @@ -354,10 +354,14 @@ // 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; } diff --git a/course/tests/behat/category_resort.feature b/course/tests/behat/category_resort.feature index 01d6857466d6d..781186ad72d76 100644 --- a/course/tests/behat/category_resort.feature +++ b/course/tests/behat/category_resort.feature @@ -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. diff --git a/course/tests/behat/course_category_management_listing.feature b/course/tests/behat/course_category_management_listing.feature index 818619b57ffb7..a07af93660103 100644 --- a/course/tests/behat/course_category_management_listing.feature +++ b/course/tests/behat/course_category_management_listing.feature @@ -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 "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" diff --git a/course/tests/behat/course_resort.feature b/course/tests/behat/course_resort.feature index b530e5b633a4e..9cb31a97fe0f0 100644 --- a/course/tests/behat/course_resort.feature +++ b/course/tests/behat/course_resort.feature @@ -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 "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 "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" diff --git a/lang/en/moodle.php b/lang/en/moodle.php index c74e820ee0783..61130db08e20f 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1513,12 +1513,9 @@ $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['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['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'; diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index d96f582da3b50..0bf08f6a18c1e 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -258,7 +258,7 @@ public static function get($id, $strictness = MUST_EXIST, $alwaysreturnhidden = return $coursecat; } else { if ($strictness == MUST_EXIST) { - throw new moodle_exception('unknowcategory'); + throw new moodle_exception('unknowncategory'); } } return null; @@ -2441,18 +2441,26 @@ public function can_restore_courses_into() { /** * 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 @@ public static function resort_categories_cleanup($includecourses = false) { /** * 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 @@ public function resort_courses($field, $cleanup = true) { 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 @@ public function resort_courses($field, $cleanup = true) { } // 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)); diff --git a/lib/tests/coursecatlib_test.php b/lib/tests/coursecatlib_test.php index b4b81809213b8..0aa637b6b6daf 100644 --- a/lib/tests/coursecatlib_test.php +++ b/lib/tests/coursecatlib_test.php @@ -395,25 +395,29 @@ public function test_resort_courses() { 'category' => $category->id, 'idnumber' => '006-01', 'shortname' => 'Biome Study', - 'fullname' => ''.'دراسة منطقة إحيائية'.'Biome Study' + 'fullname' => ''.'دراسة منطقة إحيائية'.'Biome Study', + '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' => 'Basic Scratch' + 'fullname' => 'Basic Scratch', + 'timecreated' => '10000000004' )); $c1 = (int)$course1->id; $c2 = (int)$course2->id; @@ -427,6 +431,8 @@ public function test_resort_courses() { $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.