Skip to content

Commit

Permalink
MDL-36259 course short names: ensure it displays when enabled in admin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dtle authored and timhunt committed Dec 15, 2012
1 parent 2075186 commit 31f4086
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
35 changes: 16 additions & 19 deletions course/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@
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) {
Expand All @@ -193,26 +191,24 @@
}
} 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);
Expand Down Expand Up @@ -294,6 +290,7 @@

foreach ($courses as $course) {

context_helper::preload_from_record($course);
$coursecontext = context_course::instance($course->id);

$linkcss = $course->visible ? "" : " class=\"dimmed\" ";
Expand All @@ -313,7 +310,7 @@

echo "<tr>\n";
echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
. highlight($search, format_string($course->fullname)) . "</a></td>\n";
. highlight($search, $coursecontext->get_context_name(false)) . "</a></td>\n";
echo "<td>".$displaylist[$course->category]."</td>\n";
echo "<td>\n";

Expand Down
7 changes: 5 additions & 2 deletions enrol/meta/addinstance_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ function definition() {

// 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;
}
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();

Expand Down
12 changes: 6 additions & 6 deletions report/log/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)';
}
Expand Down Expand Up @@ -561,7 +561,7 @@ function report_log_print_selector_form($course, $selecteduser=0, $selecteddate=
} else {
// echo '<input type="hidden" name="id" value="'.$course->id.'" />';
$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)) {
Expand Down
5 changes: 3 additions & 2 deletions user/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,16 @@
$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 .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . format_string($mycourse->fullname) . "</a>, ";
$courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . $ccontext->get_context_name(false) . "</a>, ";
}
$shown++;
if($shown==20) {
Expand Down
5 changes: 3 additions & 2 deletions user/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@
$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) {
Expand Down

0 comments on commit 31f4086

Please sign in to comment.