MDL-11699 - Make build_navigation more helpful by having it optionally build the activity and activityinstance links too, if you pass a $cm object. Note that this only does extra DB queries if you do pass a $cm object, and it does not have all the required fields, but in this case it will winge at you in developer debug mode. If you get your $cm using get_coursemodule_from_instance or get_coursemodule_from_id, which is best practice these days, then you won't have a problem.

This commit is contained in:
tjhunt 2007-10-10 14:00:41 +00:00
parent 8ddcdd86cb
commit 1ba3ad7df5

View file

@ -3375,14 +3375,30 @@ function print_navigation ($navigation, $separator=0, $return=false) {
/** /**
* This function will build the navigation string to be used by print_header * This function will build the navigation string to be used by print_header
* and others * and others.
*
* It will automatically add the site (and course level if appropriate) links.
*
* If you pass in a $cm object, the method will also generate the activity (e.g. Forums)
* and activityinstances (e.g. General Developer Forum) navigation levels.
*
* The fields used are $cm->modname, $cm->name and $cm->course. If you get the $cm object
* using the function get_coursemodule_from_instance or get_coursemodule_from_id (as recommended)
* then this will be done for you automatically. If you don't have $cm->modname or $cm->name,
* this fuction will attempt to find them using the $cm->module and $cm->instance fields, but
* this takes extra database queries, so a warning is printed in developer debug mode.
*
* @uses $CFG * @uses $CFG
* @uses $THEME * @uses $THEME
* @param $extranavlinks - array of associative arrays, keys: name, link, type *
* @param array $extranavlinks - array of associative arrays, keys: name, link, type
* @param mixed $cm - optionally the $cm object, if you want this function to generate the
* activity and activityinstance levels of navigation too.
*
* @return $navigation as an object so it can be differentiated from old style * @return $navigation as an object so it can be differentiated from old style
* navigation strings. * navigation strings.
*/ */
function build_navigation($extranavlinks) { function build_navigation($extranavlinks, $cm = null) {
global $CFG, $COURSE; global $CFG, $COURSE;
$navigation = ''; $navigation = '';
@ -3390,19 +3406,46 @@ function build_navigation($extranavlinks) {
//Site name //Site name
if ($site = get_site()) { if ($site = get_site()) {
$navlinks[] = array('name' => format_string($site->shortname), $navlinks[] = array(
'name' => format_string($site->shortname),
'link' => "$CFG->wwwroot/", 'link' => "$CFG->wwwroot/",
'type' => 'home'); 'type' => 'home');
} }
// Course name, if appropriate.
if ($COURSE) { if (isset($COURSE) && $COURSE->id != SITEID) {
if ($COURSE->id != SITEID) { $navlinks[] = array(
//Course 'name' => format_string($COURSE->shortname),
$navlinks[] = array('name' => format_string($COURSE->shortname),
'link' => "$CFG->wwwroot/course/view.php?id=$COURSE->id", 'link' => "$CFG->wwwroot/course/view.php?id=$COURSE->id",
'type' => 'course'); 'type' => 'course');
} }
// Activity type and instance, if appropriate.
if (is_object($cm)) {
if (!isset($cm->modname)) {
debugging('The field $cm->modname should be set if you call build_navigation with '.
'a $cm parameter. If you get $cm using get_coursemodule_from_instance or ',
'get_coursemodule_from_id, this will be done automatically.', DEBUG_DEVELOPER);
if (!$cm->modname = get_field('modules', 'name', 'id', $cm->module)) {
error('Cannot get the module type in build navigation.');
}
}
if (!isset($cm->name)) {
debugging('The field $cm->name should be set if you call build_navigation with '.
'a $cm parameter. If you get $cm using get_coursemodule_from_instance or ',
'get_coursemodule_from_id, this will be done automatically.', DEBUG_DEVELOPER);
if (!$cm->modname = get_field($cm->modname, 'name', 'id', $cm->instance)) {
error('Cannot get the module name in build navigation.');
}
}
$navlinks[] = array(
'name' => get_string('modulenameplural', $cm->modname),
'link' => $CFG->wwwroot . '/mod/' . $cm->modname . '/index.php?id=' . $cm->course,
'type' => 'activity');
$navlinks[] = array(
'name' => format_string($cm->name),
'link' => $CFG->wwwroot . '/mod/' . $cm->modname . '/view.php?id=' . $cm->id,
'type' => 'activityinstance');
} }
//Merge in extra navigation links //Merge in extra navigation links