MDL-37207 Add modchoosertoggle in renderer to allow themes to override
authorMarina Glancy <marina@moodle.com>
Wed, 12 Dec 2012 07:35:18 +0000 (15:35 +0800)
committerMarina Glancy <marina@moodle.com>
Mon, 14 Jan 2013 04:10:07 +0000 (15:10 +1100)
course/renderer.php
lib/navigationlib.php

index 7147c08..088df64 100644 (file)
@@ -49,6 +49,47 @@ class core_course_renderer extends plugin_renderer_base {
     public function __construct(moodle_page $page, $target) {
         $this->strings = new stdClass;
         parent::__construct($page, $target);
+        $this->add_modchoosertoggle();
+    }
+
+    /**
+     * Adds the item in course settings navigation to toggle modchooser
+     *
+     * Theme can overwrite as an empty function to exclude it (for example if theme does not
+     * use modchooser at all)
+     */
+    protected function add_modchoosertoggle() {
+        global $CFG;
+        static $modchoosertoggleadded = false;
+        // Add the module chooser toggle to the course page
+        if ($modchoosertoggleadded || $this->page->state > moodle_page::STATE_PRINTING_HEADER ||
+                $this->page->course->id == SITEID ||
+                !($coursenode = $this->page->settingsnav->find('courseadmin', navigation_node::TYPE_COURSE))) {
+            // too late or we are on site page or we could not find the course settings node
+            return;
+        }
+        $modchoosertoggleadded = true;
+        if ($this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
+            // We are on the course page, retain the current page params e.g. section.
+            $modchoosertoggleurl = clone($this->page->url);
+        } else {
+            // Edit on the main course page.
+            $modchoosertoggleurl = new moodle_url('/course/view.php', array('id' => $this->page->course->id,
+                'return' => $this->page->url->out_as_local_url(false)));
+        }
+        $modchoosertoggleurl->param('sesskey', sesskey());
+        if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) {
+            $modchoosertogglestring = get_string('modchooserdisable', 'moodle');
+            $modchoosertoggleurl->param('modchooser', 'off');
+        } else {
+            $modchoosertogglestring = get_string('modchooserenable', 'moodle');
+            $modchoosertoggleurl->param('modchooser', 'on');
+        }
+        $modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING);
+        $coursenode->add_node($modchoosertoggle, 'editsettings');
+        $modchoosertoggle->add_class('modchoosertoggle');
+        $modchoosertoggle->add_class('visibleifjs');
+        user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL);
     }
 
     /**
@@ -394,6 +435,9 @@ class core_course_renderer extends plugin_renderer_base {
     /**
      * Renders HTML for the menus to add activities and resources to the current course
      *
+     * Note, if theme overwrites this function and it does not use modchooser,
+     * see also {@link core_course_renderer::add_modchoosertoggle()}
+     *
      * @param stdClass $course
      * @param int $section relative section number (field course_sections.section)
      * @param int $sectionreturn The section to link back to
index 8458475..6073317 100644 (file)
@@ -3450,25 +3450,9 @@ class settings_navigation extends navigation_node {
             }
             $coursenode->add($editstring, $editurl, self::TYPE_SETTING, null, null, new pix_icon('i/edit', ''));
 
-            // Add the module chooser toggle
-            $modchoosertoggleurl = clone($baseurl);
-            if ($this->page->user_is_editing() && course_ajax_enabled($course)) {
-                if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) {
-                    $modchoosertogglestring = get_string('modchooserdisable', 'moodle');
-                    $modchoosertoggleurl->param('modchooser', 'off');
-                } else {
-                    $modchoosertogglestring = get_string('modchooserenable', 'moodle');
-                    $modchoosertoggleurl->param('modchooser', 'on');
-                }
-                $modchoosertoggle = $coursenode->add($modchoosertogglestring, $modchoosertoggleurl, self::TYPE_SETTING);
-                $modchoosertoggle->add_class('modchoosertoggle');
-                $modchoosertoggle->add_class('visibleifjs');
-                user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL);
-            }
-
             // Add the course settings link
             $url = new moodle_url('/course/edit.php', array('id'=>$course->id));
-            $coursenode->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/settings', ''));
+            $coursenode->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, 'editsettings', new pix_icon('i/settings', ''));
 
             // Add the course completion settings link
             if ($CFG->enablecompletion && $course->enablecompletion) {