From 31f4086460ccafd3998bfc27207d6b6307e34f88 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Tue, 20 Nov 2012 10:07:06 +0000 Subject: [PATCH] MDL-36259 course short names: ensure it displays when enabled in admin Fixed 5 Moodle pages that does not display the course short name even if "Display extended course names" setting is on. In fixing this, search.php also included related minor code to change to minimise DB calls to improve performance. --- course/search.php | 35 +++++++++++++++------------------ enrol/meta/addinstance_form.php | 7 +++++-- report/log/locallib.php | 12 +++++------ user/profile.php | 5 +++-- user/view.php | 5 +++-- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/course/search.php b/course/search.php index 5f91966ec74..5c569aecd7f 100644 --- a/course/search.php +++ b/course/search.php @@ -175,13 +175,11 @@ if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // S if (!empty($blocklist) and confirm_sesskey()) { $blockname = $DB->get_field('block', 'name', array('id' => $blocklist)); $courses = array(); - $courses = $DB->get_records_sql(" - SELECT * FROM {course} WHERE id IN ( - SELECT DISTINCT ctx.instanceid - FROM {context} ctx - JOIN {block_instances} bi ON bi.parentcontextid = ctx.id - WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)", - array($blockname)); + list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); + $sql = "SELECT C.* $select FROM {course} c + $join JOIN {block_instances} bi ON bi.parentcontextid = ctx.id + WHERE bi.blockname = ?"; + $courses = $DB->get_records_sql($sql, array($blockname)); $totalcount = count($courses); // Keep only chunk of array which you want to display if ($totalcount > $perpage) { @@ -193,26 +191,24 @@ if (!empty($blocklist) and confirm_sesskey()) { } } elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules $modulename = $modulelist; - $sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c" - ." WHERE module.course=c.id"; - - $courseids = $DB->get_records_sql($sql); + list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); + $sql = "SELECT c.* $select FROM {course} c $join + WHERE c.id IN (SELECT DISTINCT cc.id FROM {".$modulelist."} module, {course} cc + WHERE module.course = cc.id)"; + $courselist = $DB->get_records_sql($sql); $courses = array(); - if (!empty($courseids)) { + if (!empty($courselist)) { $firstcourse = $page*$perpage; $lastcourse = $page*$perpage + $perpage -1; $i = 0; - foreach ($courseids as $courseid) { + foreach ($courselist as $course) { if ($i >= $firstcourse && $i <= $lastcourse) { - $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id)); + $courses[$course->id] = $course; } $i++; } - $totalcount = count($courseids); - } - else { - $totalcount = 0; } + $totalcount = count($courselist); } else if (!empty($searchterm)) { // Donot do search for empty search request. $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount); @@ -294,6 +290,7 @@ if ($courses) { foreach ($courses as $course) { + context_helper::preload_from_record($course); $coursecontext = context_course::instance($course->id); $linkcss = $course->visible ? "" : " class=\"dimmed\" "; @@ -313,7 +310,7 @@ if ($courses) { echo "\n"; echo "id\">" - . highlight($search, format_string($course->fullname)) . "\n"; + . highlight($search, $coursecontext->get_context_name(false)) . "\n"; echo "".$displaylist[$course->category]."\n"; echo "\n"; diff --git a/enrol/meta/addinstance_form.php b/enrol/meta/addinstance_form.php index f2b9bbff49f..092e3fdc996 100644 --- a/enrol/meta/addinstance_form.php +++ b/enrol/meta/addinstance_form.php @@ -41,11 +41,14 @@ class enrol_meta_addinstance_form extends moodleform { // TODO: this has to be done via ajax or else it will fail very badly on large sites! $courses = array('' => get_string('choosedots')); - $rs = $DB->get_recordset('course', array(), 'sortorder ASC', 'id, fullname, shortname, visible'); + list ($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); + $sql = "SELECT c.id, c.fullname, c.shortname, c.visible $select FROM {course} c $join ORDER BY c.sortorder ASC"; + $rs = $DB->get_recordset_sql($sql); foreach ($rs as $c) { if ($c->id == SITEID or $c->id == $course->id or isset($existing[$c->id])) { continue; } + context_helper::preload_from_record($c); $coursecontext = context_course::instance($c->id); if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { continue; @@ -53,7 +56,7 @@ class enrol_meta_addinstance_form extends moodleform { if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) { continue; } - $courses[$c->id] = format_string($c->fullname). ' ['.format_string($c->shortname, true, array('context' => $coursecontext)).']'; + $courses[$c->id] = $coursecontext->get_context_name(false); } $rs->close(); diff --git a/report/log/locallib.php b/report/log/locallib.php index 7647c8e1242..4d5f1ae54be 100644 --- a/report/log/locallib.php +++ b/report/log/locallib.php @@ -190,12 +190,12 @@ function report_log_print_mnet_selector_form($hostid, $course, $selecteduser=0, $sites = array(); if ($CFG->mnet_localhost_id == $hostid) { if (has_capability('report/log:view', $sitecontext) && $showcourses) { - if ($ccc = $DB->get_records("course", null, "fullname","id,fullname,category")) { + if ($ccc = $DB->get_records("course", null, "fullname","id,shortname,fullname,category")) { foreach ($ccc as $cc) { if ($cc->id == SITEID) { $sites["$hostid/$cc->id"] = format_string($cc->fullname).' ('.get_string('site').')'; } else { - $courses["$hostid/$cc->id"] = format_string($cc->fullname); + $courses["$hostid/$cc->id"] = format_string(get_course_display_name_for_list($cc)); } } } @@ -312,7 +312,7 @@ function report_log_print_mnet_selector_form($hostid, $course, $selecteduser=0, echo html_writer::select($dropdown, "host_course", $hostid.'/'.$cid); } else { $courses = array(); - $courses[$course->id] = $course->fullname . ((empty($course->category)) ? ' ('.get_string('site').') ' : ''); + $courses[$course->id] = get_course_display_name_for_list($course) . ((empty($course->category)) ? ' ('.get_string('site').') ' : ''); echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide')); echo html_writer::select($courses,"id",$course->id, false); if (has_capability('report/log:view', $sitecontext)) { @@ -460,10 +460,10 @@ function report_log_print_selector_form($course, $selecteduser=0, $selecteddate= } if (has_capability('report/log:view', $sitecontext) && $showcourses) { - if ($ccc = $DB->get_records("course", null, "fullname", "id,fullname,category")) { + if ($ccc = $DB->get_records("course", null, "fullname", "id,shortname,fullname,category")) { foreach ($ccc as $cc) { if ($cc->category) { - $courses["$cc->id"] = format_string($cc->fullname); + $courses["$cc->id"] = format_string(get_course_display_name_for_list($cc)); } else { $courses["$cc->id"] = format_string($cc->fullname) . ' (Site)'; } @@ -561,7 +561,7 @@ function report_log_print_selector_form($course, $selecteduser=0, $selecteddate= } else { // echo ''; $courses = array(); - $courses[$course->id] = $course->fullname . (($course->id == SITEID) ? ' ('.get_string('site').') ' : ''); + $courses[$course->id] = get_course_display_name_for_list($course) . (($course->id == SITEID) ? ' ('.get_string('site').') ' : ''); echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide')); echo html_writer::select($courses,"id",$course->id, false); if (has_capability('report/log:view', $sitecontext)) { diff --git a/user/profile.php b/user/profile.php index 92f55cf4515..082eaab317e 100644 --- a/user/profile.php +++ b/user/profile.php @@ -321,15 +321,16 @@ if (!isset($hiddenfields['mycourses'])) { $courselisting = ''; foreach ($mycourses as $mycourse) { if ($mycourse->category) { + context_helper::preload_from_record($mycourse); + $ccontext = context_course::instance($mycourse->id); $class = ''; if ($mycourse->visible == 0) { - $ccontext = context_course::instance($mycourse->id); if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) { continue; } $class = 'class="dimmed"'; } - $courselisting .= "wwwroot}/user/view.php?id={$user->id}&course={$mycourse->id}\" $class >" . format_string($mycourse->fullname) . ", "; + $courselisting .= "wwwroot}/user/view.php?id={$user->id}&course={$mycourse->id}\" $class >" . $ccontext->get_context_name(false) . ", "; } $shown++; if($shown==20) { diff --git a/user/view.php b/user/view.php index c5aa4a21de6..0aeb9332277 100644 --- a/user/view.php +++ b/user/view.php @@ -288,8 +288,9 @@ if (!isset($hiddenfields['mycourses'])) { $courselisting = ''; foreach ($mycourses as $mycourse) { if ($mycourse->category) { - $ccontext = context_course::instance($mycourse->id);; - $cfullname = format_string($mycourse->fullname, true, array('context' => $ccontext)); + context_helper::preload_from_record($mycourse); + $ccontext = context_course::instance($mycourse->id); + $cfullname = $ccontext->get_context_name(false); if ($mycourse->id != $course->id){ $class = ''; if ($mycourse->visible == 0) {