From: tjhunt Date: Wed, 10 Oct 2007 14:00:41 +0000 (+0000) Subject: MDL-11699 - Make build_navigation more helpful by having it optionally build the... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=1ba3ad7df5fc02bfebfbf947e9549d694ead1281;p=moodle.git 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. --- diff --git a/lib/weblib.php b/lib/weblib.php index 8bd0632c35..197f921eb0 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -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 - * 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 $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 * navigation strings. */ -function build_navigation($extranavlinks) { +function build_navigation($extranavlinks, $cm = null) { global $CFG, $COURSE; $navigation = ''; @@ -3390,19 +3406,46 @@ function build_navigation($extranavlinks) { //Site name if ($site = get_site()) { - $navlinks[] = array('name' => format_string($site->shortname), - 'link' => "$CFG->wwwroot/", - 'type' => 'home'); - } - - - if ($COURSE) { - if ($COURSE->id != SITEID) { - //Course - $navlinks[] = array('name' => format_string($COURSE->shortname), - 'link' => "$CFG->wwwroot/course/view.php?id=$COURSE->id", - 'type' => 'course'); + $navlinks[] = array( + 'name' => format_string($site->shortname), + 'link' => "$CFG->wwwroot/", + 'type' => 'home'); + } + + // Course name, if appropriate. + if (isset($COURSE) && $COURSE->id != SITEID) { + $navlinks[] = array( + 'name' => format_string($COURSE->shortname), + 'link' => "$CFG->wwwroot/course/view.php?id=$COURSE->id", + '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