From 7fb46992661e2eb1fb7bfd1a6d3f7f1ccf8fd306 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 10 Oct 2011 13:50:01 +0100 Subject: [PATCH] MDL-29719 Option to display course shortname alongside fullname on course lists --- admin/settings/appearance.php | 5 ++++- course/category.php | 3 ++- course/lib.php | 23 +++++++++++++++++++++-- course/simpletest/testcourselib.php | 20 ++++++++++++++++++++ lang/en/admin.php | 2 ++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php index 1646cab08f2f5..7da57866803b6 100644 --- a/admin/settings/appearance.php +++ b/admin/settings/appearance.php @@ -118,8 +118,11 @@ $ADMIN->add('appearance', $temp); // coursecontact is the person responsible for course - usually manages enrolments, receives notification, etc. - $temp = new admin_settingpage('coursecontact', get_string('coursecontact', 'admin')); + $temp = new admin_settingpage('coursecontact', get_string('courses')); $temp->add(new admin_setting_special_coursecontact()); + $temp->add(new admin_setting_configcheckbox('courselistshortnames', + get_string('courselistshortnames', 'admin'), + get_string('courselistshortnames_desc', 'admin'), 0)); $ADMIN->add('appearance', $temp); $temp = new admin_settingpage('ajax', get_string('ajaxuse')); diff --git a/course/category.php b/course/category.php index 9ed7ebca867d7..0fb8ac571cf95 100644 --- a/course/category.php +++ b/course/category.php @@ -314,7 +314,8 @@ $linkcss = $acourse->visible ? '' : ' class="dimmed" '; echo ''; - echo ''. format_string($acourse->fullname) .''; + $coursename = get_course_display_name_for_list($course); + echo ''. format_string($coursename) .''; if ($editingon) { echo ''; if (has_capability('moodle/course:update', $coursecontext)) { diff --git a/course/lib.php b/course/lib.php index a00348f32698e..5fb1ae674ac33 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2173,6 +2173,22 @@ function make_categories_options() { return $cats; } +/** + * Gets the name of a course to be displayed when showing a list of courses. + * By default this is just $course->fullname but user can configure it. The + * result of this function should be passed through print_string. + * @param object $course Moodle course object + * @return string Display name of course (either fullname or short + fullname) + */ +function get_course_display_name_for_list($course) { + global $CFG; + if (!empty($CFG->courselistshortnames)) { + return $course->shortname . ' ' .$course->fullname; + } else { + return $course->fullname; + } +} + /** * Prints the category info in indented fashion * This function is only used by print_whole_category_list() above @@ -2231,7 +2247,8 @@ function print_category_info($category, $depth=0, $showcourses = false) { $linkcss = array('class'=>'dimmed'); } - $courselink = html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), $linkcss); + $coursename = get_course_display_name_for_list($course); + $courselink = html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($coursename), $linkcss); // print enrol info $courseicon = ''; @@ -2423,7 +2440,9 @@ function print_course($course, $highlightterms = '') { echo html_writer::start_tag('h3', array('class'=>'name')); $linkhref = new moodle_url('/course/view.php', array('id'=>$course->id)); - $linktext = highlight($highlightterms, format_string($course->fullname)); + + $coursename = get_course_display_name_for_list($course); + $linktext = highlight($highlightterms, format_string($coursename)); $linkparams = array('title'=>get_string('entercourse')); if (empty($course->visible)) { $linkparams['class'] = 'dimmed'; diff --git a/course/simpletest/testcourselib.php b/course/simpletest/testcourselib.php index 2104d84491e95..857d13e4ba83a 100644 --- a/course/simpletest/testcourselib.php +++ b/course/simpletest/testcourselib.php @@ -113,4 +113,24 @@ function testReorderSections() { $this->assertEqual(25, next($newsections_flipped)); $this->assertEqual(21, next($newsections_flipped)); } + + function test_get_course_display_name_for_list() { + global $CFG; + + $course = (object)array('shortname' => 'FROG101', + 'fullname' => 'Introduction to pond life'); + + // Store config value in case other tests rely on it + $oldcfg = $CFG->courselistshortnames; + + $CFG->courselistshortnames = 0; + $this->assertEqual('Introduction to pond life', + get_course_display_name_for_list($course)); + + $CFG->courselistshortnames = 1; + $this->assertEqual('FROG101 Introduction to pond life', + get_course_display_name_for_list($course)); + + $CFG->courselistshortnames = $oldcfg; + } } diff --git a/lang/en/admin.php b/lang/en/admin.php index 03c0ed706c83e..a1bdd6908f45d 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -352,6 +352,8 @@ $string['country'] = 'Default country'; $string['coursecontact'] = 'Course contacts'; $string['coursecontact_desc'] = 'This setting allows you to control who appears on the course description. Users need to have at least one of these roles in a course to be shown on the course description for that course.'; +$string['courselistshortnames'] = 'Display short names'; +$string['courselistshortnames_desc'] = 'Show short name as well as full name when displaying lists of courses.'; $string['coursemgmt'] = 'Add/edit courses'; $string['courseoverview'] = 'Course overview'; $string['courserequestnotify'] = 'Course request notification';