From 583c0cf67fd43967fa4b0e2d0033581a3175a930 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 13 Sep 2012 11:00:34 +0800 Subject: [PATCH] MDL-35276 fix redirect to course/section pages --- course/format/renderer.php | 25 ++++++++++++------------ course/lib.php | 44 ++++++++++++++++++++++++++++++++---------- course/mod.php | 48 ++++++++++++++-------------------------------- course/modduplicate.php | 13 +++++++------ course/modedit.php | 6 +++--- lib/navigationlib.php | 4 +--- 6 files changed, 72 insertions(+), 68 deletions(-) diff --git a/course/format/renderer.php b/course/format/renderer.php index c758cacb00c..4bb5a3ac7e5 100644 --- a/course/format/renderer.php +++ b/course/format/renderer.php @@ -65,8 +65,9 @@ abstract class format_section_renderer_base extends plugin_renderer_base { */ public function section_title($section, $course) { $title = get_section_name($course, $section); - if ($section->section != 0 && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) { - $title = html_writer::link(course_get_url($course, $section->section), $title); + $url = course_get_url($course, $section->section, array('navigation' => true)); + if ($url) { + $title = html_writer::link($url, $title); } return $title; } @@ -125,7 +126,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base { * @param int $sectionreturn The section to return to after an action * @return string HTML to output. */ - protected function section_header($section, $course, $onsectionpage, $sectionreturn=0) { + protected function section_header($section, $course, $onsectionpage, $sectionreturn=null) { global $PAGE; $o = ''; @@ -413,7 +414,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base { * @param int $sectionno The section number in the coruse which is being dsiplayed * @return string HTML to output. */ - protected function course_activity_clipboard($course, $sectionno = 0) { + protected function course_activity_clipboard($course, $sectionno = null) { global $USER; $o = ''; @@ -642,7 +643,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base { echo $this->output->heading($this->page_title(), 2, 'accesshide'); // Copy activity clipboard.. - echo $this->course_activity_clipboard($course); + echo $this->course_activity_clipboard($course, 0); // Now the list of sections.. echo $this->start_section_list(); @@ -651,10 +652,10 @@ abstract class format_section_renderer_base extends plugin_renderer_base { $thissection = $sections[0]; unset($sections[0]); if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) { - echo $this->section_header($thissection, $course, false); - print_section($course, $thissection, $mods, $modnamesused, true); + echo $this->section_header($thissection, $course, false, 0); + print_section($course, $thissection, $mods, $modnamesused, true, "100%", false, 0); if ($PAGE->user_is_editing()) { - print_section_add_menus($course, 0, $modnames); + print_section_add_menus($course, 0, $modnames, false, false, 0); } echo $this->section_footer(); } @@ -693,11 +694,11 @@ abstract class format_section_renderer_base extends plugin_renderer_base { // Display section summary only. echo $this->section_summary($thissection, $course, $mods); } else { - echo $this->section_header($thissection, $course, false); + echo $this->section_header($thissection, $course, false, 0); if ($thissection->uservisible) { - print_section($course, $thissection, $mods, $modnamesused); + print_section($course, $thissection, $mods, $modnamesused, true, "100%", false, 0); if ($PAGE->user_is_editing()) { - print_section_add_menus($course, $section, $modnames); + print_section_add_menus($course, $section, $modnames, false, false, 0); } } echo $this->section_footer(); @@ -714,7 +715,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base { continue; } echo $this->stealth_section_header($section); - print_section($course, $thissection, $mods, $modnamesused); + print_section($course, $thissection, $mods, $modnamesused, true, "100%", false, $displaysection); echo $this->stealth_section_footer(); } diff --git a/course/lib.php b/course/lib.php index ac7a9572627..4ae8492a3c3 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1384,7 +1384,7 @@ function get_print_section_cm_text(cm_info $cm, $course) { * @param int $sectionreturn The section to return to * @return void */ -function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%", $hidecompletion=false, $sectionreturn=0) { +function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%", $hidecompletion=false, $sectionreturn=null) { global $CFG, $USER, $DB, $PAGE, $OUTPUT; static $initialised; @@ -1765,14 +1765,14 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, * Prints the menus to add activities and resources. * * @param stdClass $course The course - * @param stdClass $section The section + * @param int $section relative section number (field course_sections.section) * @param array $modnames An array containing the list of modules and their names * @param bool $vertical Vertical orientation * @param bool $return Return the menus or send them to output * @param int $sectionreturn The section to link back to * @return void|string depending on $return */ -function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false, $sectionreturn=0) { +function print_section_add_menus($course, $section, $modnames, $vertical=false, $return=false, $sectionreturn=null) { global $CFG, $OUTPUT; // check to see if user can add menus @@ -1781,7 +1781,7 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false, } // Retrieve all modules with associated metadata - $modules = get_module_metadata($course, $modnames); + $modules = get_module_metadata($course, $modnames, $sectionreturn); // We'll sort resources and activities into two lists $resources = array(); @@ -1894,7 +1894,7 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false, * @return array A list of stdClass objects containing metadata about each * module */ -function get_module_metadata($course, $modnames, $sectionreturn = 0) { +function get_module_metadata($course, $modnames, $sectionreturn = null) { global $CFG, $OUTPUT; // get_module_metadata will be called once per section on the page and courses may show @@ -3153,7 +3153,7 @@ function moveto_module($mod, $section, $beforemod=NULL) { * @param int $section The section to link back to * @return string XHTML for the editing buttons */ -function make_editing_buttons(stdClass $mod, $absolute_ignored = true, $moveselect = true, $indent=-1, $section=-1) { +function make_editing_buttons(stdClass $mod, $absolute_ignored = true, $moveselect = true, $indent=-1, $section=null) { global $CFG, $OUTPUT, $COURSE; static $str; @@ -3195,7 +3195,7 @@ function make_editing_buttons(stdClass $mod, $absolute_ignored = true, $movesele $baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey())); - if ($section >= 0) { + if ($section !== null) { $baseurl->param('sr', $section); } $actions = array(); @@ -4589,15 +4589,39 @@ function include_course_ajax($course, $usedmodules = array(), $enabledmodules = * * @param stdClass $course The course to get the section name for * @param int $sectionno The section number to return a link to + * if omitted the course view page is returned + * @param array $options options for view URL. At the moment core uses: + * 'navigation' (bool) if true and section has no separate page, the function returns null + * 'sr' (int) used by multipage formats to specify to which section to return * @return moodle_url The url of course */ -function course_get_url($course, $sectionno = null) { +function course_get_url($course, $sectionno = null, $options = array()) { + if ($course->id == SITEID) { + return new moodle_url('/'); + } $url = new moodle_url('/course/view.php', array('id' => $course->id)); - if (!is_null($sectionno)) { - if ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) { + $sr = null; + if (array_key_exists('sr', $options)) { + $sr = $options['sr']; + } + if ($sectionno !== null) { + if ($sr !== null) { + if ($sr) { + $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE; + $sectionno = $sr; + } else { + $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE; + } + } else { + $usercoursedisplay = $course->coursedisplay; + } + if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) { $url->param('section', $sectionno); } else { + if (!empty($options['navigation'])) { + return null; + } $url->set_anchor('section-'.$sectionno); } } diff --git a/course/mod.php b/course/mod.php index 181c6c9d21a..beb665c52f3 100644 --- a/course/mod.php +++ b/course/mod.php @@ -26,7 +26,7 @@ require("../config.php"); require_once("lib.php"); -$sectionreturn = optional_param('sr', 0, PARAM_INT); +$sectionreturn = optional_param('sr', null, PARAM_INT); $add = optional_param('add', '', PARAM_ALPHA); $type = optional_param('type', '', PARAM_ALPHA); $indent = optional_param('indent', 0, PARAM_INT); @@ -50,9 +50,7 @@ foreach (compact('indent','update','hide','show','copy','moveto','movetosection' $url->param($key, $value); } } -if ($sectionreturn) { - $url->param('sr', $sectionreturn); -} +$url->param('sr', $sectionreturn); if ($add !== '') { $url->param('add', $add); } @@ -109,7 +107,7 @@ if (!empty($add)) { get_string('continue'), 'post'), new single_button( - course_get_url($course, $sectionreturn), + course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)), get_string('cancel'), 'get') ); @@ -126,7 +124,7 @@ if (!empty($add)) { $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); require_capability('moodle/course:manageactivities', $modcontext); - $return = course_get_url($course, $sectionreturn); + $return = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)); if (!$confirm or !confirm_sesskey()) { $fullmodulename = get_string('modulename', $cm->modname); @@ -234,11 +232,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { rebuild_course_cache($section->course); - if (SITEID == $section->course) { - redirect($CFG->wwwroot); - } else { - redirect(course_get_url($course, $sectionreturn)); - } + redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn))); } else if (!empty($indent) and confirm_sesskey()) { $id = required_param('id', PARAM_INT); @@ -261,11 +255,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { rebuild_course_cache($cm->course); - if (SITEID == $cm->course) { - redirect($CFG->wwwroot); - } else { - redirect(course_get_url($course, $sectionreturn)); - } + redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } else if (!empty($hide) and confirm_sesskey()) { $cm = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST); @@ -280,11 +270,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { rebuild_course_cache($cm->course); - if (SITEID == $cm->course) { - redirect($CFG->wwwroot); - } else { - redirect(course_get_url($course, $sectionreturn)); - } + redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } else if (!empty($show) and confirm_sesskey()) { $cm = get_coursemodule_from_id('', $show, 0, true, MUST_EXIST); @@ -304,11 +290,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { rebuild_course_cache($cm->course); } - if (SITEID == $cm->course) { - redirect($CFG->wwwroot); - } else { - redirect(course_get_url($course, $sectionreturn)); - } + redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn))); } else if ($groupmode > -1 and confirm_sesskey()) { $id = required_param('id', PARAM_INT); @@ -325,11 +307,7 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { rebuild_course_cache($cm->course); - if (SITEID == $cm->course) { - redirect($CFG->wwwroot); - } else { - redirect(course_get_url($course, $sectionreturn)); - } + redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } else if (!empty($copy) and confirm_sesskey()) { // value = course module $cm = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST); @@ -347,18 +325,20 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $USER->activitycopyname = $cm->name; $USER->activitycopysectionreturn = $sectionreturn; - redirect(course_get_url($course, $sectionreturn)); + redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn))); } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module $courseid = $USER->activitycopycourse; $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, IGNORE_MISSING); + $sectionreturn = $USER->activitycopysectionreturn; unset($USER->activitycopy); unset($USER->activitycopycourse); unset($USER->activitycopyname); - - redirect(course_get_url($course, $sectionreturn)); + unset($USER->activitycopysectionreturn); + redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } else { print_error('unknowaction'); } diff --git a/course/modduplicate.php b/course/modduplicate.php index 30c9aafddd3..2f51ff99937 100644 --- a/course/modduplicate.php +++ b/course/modduplicate.php @@ -34,7 +34,7 @@ require_once($CFG->libdir . '/filelib.php'); $cmid = required_param('cmid', PARAM_INT); $courseid = required_param('course', PARAM_INT); -$sectionreturn = optional_param('sr', 0, PARAM_INT); +$sectionreturn = optional_param('sr', null, PARAM_INT); $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); $cm = get_coursemodule_from_id('', $cmid, $course->id, true, MUST_EXIST); @@ -61,8 +61,8 @@ $a->modtype = get_string('modulename', $cm->modname); $a->modname = format_string($cm->name); if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) { - $url = new moodle_url('/course/view.php#section-' . $cm->sectionnum, array('id' => $course->id)); - print_error('duplicatenosupport', 'core', $url, $a); + $url = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)); + print_error('duplicatenosupport', 'error', $url, $a); } // backup the activity @@ -91,7 +91,8 @@ if (!$rc->execute_precheck()) { echo $output->header(); echo $output->precheck_notices($precheckresults); - echo $output->continue_button(new moodle_url('/course/view.php', array('id' => $course->id))); + $url = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)); + echo $output->continue_button($url); echo $output->footer(); die(); } @@ -137,14 +138,14 @@ if ($newcmid) { get_string('duplicatecontedit'), 'get'), new single_button( - course_get_url($course, $sectionreturn), + course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)), get_string('duplicatecontcourse'), 'get') ); } else { echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess'); - echo $output->continue_button(course_get_url($course, $sectionreturn)); + echo $output->continue_button(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn))); } echo $output->footer(); diff --git a/course/modedit.php b/course/modedit.php index ff9861382d6..7aa8667a588 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -35,7 +35,7 @@ $add = optional_param('add', '', PARAM_ALPHA); // module name $update = optional_param('update', 0, PARAM_INT); $return = optional_param('return', 0, PARAM_BOOL); //return to course/view.php if false or mod/modname/view.php if true $type = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0 -$sectionreturn = optional_param('sr', 0, PARAM_INT); +$sectionreturn = optional_param('sr', null, PARAM_INT); $url = new moodle_url('/course/modedit.php'); $url->param('sr', $sectionreturn); @@ -264,7 +264,7 @@ if ($mform->is_cancelled()) { if ($return && !empty($cm->id)) { redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id"); } else { - redirect(course_get_url($course, $sectionreturn)); + redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn))); } } else if ($fromform = $mform->get_data()) { if (empty($fromform->coursemodule)) { @@ -633,7 +633,7 @@ if ($mform->is_cancelled()) { redirect($gradingman->get_management_url($returnurl)); } } else { - redirect(course_get_url($course, $sectionreturn)); + redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn))); } exit; diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 84ca77ffb9f..b24c1126db0 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -2026,9 +2026,7 @@ class global_navigation extends navigation_node { // pre 2.3 style format url $url = $urlfunction($course->id, $section->section); }else{ - if ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) { - $url = course_get_url($course, $section->section); - } + $url = course_get_url($course, $section->section, array('navigation' => true)); } $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id); $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; -- 2.11.4.GIT