MDL-37085 Substitute function print_section_add_menus() with core_course_renderer...
authorMarina Glancy <marina@moodle.com>
Fri, 14 Dec 2012 02:10:45 +0000 (10:10 +0800)
committerMarina Glancy <marina@moodle.com>
Mon, 14 Jan 2013 04:01:23 +0000 (15:01 +1100)
blocks/site_main_menu/block_site_main_menu.php
blocks/social_activities/block_social_activities.php
course/format/renderer.php
course/lib.php
course/renderer.php
index.php

index 03a5d4c..4cd681f 100644 (file)
@@ -134,7 +134,9 @@ class block_site_main_menu extends block_list {
             $this->content->icons[] = '';
         }
 
-        $this->content->footer = print_section_add_menus($course, 0, null, true, true);
+        $courserenderer = $this->page->get_renderer('core', 'course');
+        $this->content->footer = $courserenderer->course_section_add_cm_control($course,
+                0, null, array('inblock' => true));
 
         return $this->content;
     }
index d7d48c0..0de13b7 100644 (file)
@@ -137,7 +137,9 @@ class block_social_activities extends block_list {
             $this->content->icons[] = '';
         }
 
-        $this->content->footer = print_section_add_menus($course, 0, null, true, true);
+        $courserenderer = $this->page->get_renderer('core', 'course');
+        $this->content->footer = $courserenderer->course_section_add_cm_control($course,
+                0, null, array('inblock' => true));
 
         return $this->content;
     }
index befde23..dd0a6fe 100644 (file)
@@ -544,6 +544,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
 
         $modinfo = get_fast_modinfo($course);
         $course = course_get_format($course)->get_course();
+        $courserenderer = $this->page->get_renderer('core', 'course');
 
         // Can we view the section in question?
         if (!($sectioninfo = $modinfo->get_section_info($displaysection))) {
@@ -569,9 +570,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
             echo $this->start_section_list();
             echo $this->section_header($thissection, $course, true, $displaysection);
             print_section($course, $thissection, null, null, true, "100%", false, $displaysection);
-            if ($PAGE->user_is_editing()) {
-                print_section_add_menus($course, 0, null, false, false, $displaysection);
-            }
+            echo $courserenderer->course_section_add_cm_control($course, 0, $displaysection);
             echo $this->section_footer();
             echo $this->end_section_list();
         }
@@ -606,9 +605,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
         echo $completioninfo->display_help_icon();
 
         print_section($course, $thissection, null, null, true, '100%', false, $displaysection);
-        if ($PAGE->user_is_editing()) {
-            print_section_add_menus($course, $displaysection, null, false, false, $displaysection);
-        }
+        echo $courserenderer->course_section_add_cm_control($course, $displaysection, $displaysection);
         echo $this->section_footer();
         echo $this->end_section_list();
 
@@ -642,6 +639,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
         $course = course_get_format($course)->get_course();
 
         $context = context_course::instance($course->id);
+        $courserenderer = $this->page->get_renderer('core', 'course');
         // Title with completion help icon.
         $completioninfo = new completion_info($course);
         echo $completioninfo->display_help_icon();
@@ -659,9 +657,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
                 if ($thissection->summary or !empty($modinfo->sections[0]) or $PAGE->user_is_editing()) {
                     echo $this->section_header($thissection, $course, false, 0);
                     print_section($course, $thissection, null, null, true, "100%", false, 0);
-                    if ($PAGE->user_is_editing()) {
-                        print_section_add_menus($course, 0, null, false, false, 0);
-                    }
+                    echo $courserenderer->course_section_add_cm_control($course, 0);
                     echo $this->section_footer();
                 }
                 continue;
@@ -691,9 +687,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
                 echo $this->section_header($thissection, $course, false, 0);
                 if ($thissection->uservisible) {
                     print_section($course, $thissection, null, null, true, "100%", false, 0);
-                    if ($PAGE->user_is_editing()) {
-                        print_section_add_menus($course, $section, null, false, false, 0);
-                    }
+                    echo $courserenderer->course_section_add_cm_control($course, $section);
                 }
                 echo $this->section_footer();
             }
index 983b9fc..984b5e5 100644 (file)
@@ -1669,145 +1669,27 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
 /**
  * Prints the menus to add activities and resources.
  *
- * @param stdClass $course The course
+ * @deprecated since 2.5
+ *
+ * @param stdClass $course course object, must be the same as set on the page
  * @param int $section relative section number (field course_sections.section)
- * @param null|array $modnames An array containing the list of modules and their names
- *     if omitted will be taken from get_module_types_names()
+ * @param null|array $modnames (argument ignored) get_module_types_names() is used instead of argument
  * @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 = null, $vertical=false, $return=false, $sectionreturn=null) {
-    global $PAGE, $CFG, $OUTPUT;
-    if ($course->id != $PAGE->course->id) {
-        debugging('print_section_add_menus() can be called only for the course set on the page', DEBUG_DEVELOPER);
-        return;
-    }
-
-    if ($modnames === null) {
-        $modnames = get_module_types_names();
-    }
-
-    // check to see if user can add menus and there are modules to add
-    if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
-            || !$PAGE->user_is_editing()
-            || empty($modnames)) {
-        if ($return) {
-            return '';
-        } else {
-            return false;
-        }
-    }
-
-    // Retrieve all modules with associated metadata
-    $modules = get_module_metadata($course, $modnames, $sectionreturn);
-
-    // We'll sort resources and activities into two lists
-    $resources = array();
-    $activities = array();
-
-    foreach ($modules as $module) {
-        if (isset($module->types)) {
-            // This module has a subtype
-            // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
-            $subtypes = array();
-            foreach ($module->types as $subtype) {
-                $link = $subtype->link->out(true, array('section' => $section));
-                $subtypes[$link] = $subtype->title;
-            }
-
-            // Sort module subtypes into the list
-            if (!empty($module->title)) {
-                // This grouping has a name
-                if ($module->archetype == MOD_CLASS_RESOURCE) {
-                    $resources[] = array($module->title=>$subtypes);
-                } else {
-                    $activities[] = array($module->title=>$subtypes);
-                }
-            } else {
-                // This grouping does not have a name
-                if ($module->archetype == MOD_CLASS_RESOURCE) {
-                    $resources = array_merge($resources, $subtypes);
-                } else {
-                    $activities = array_merge($activities, $subtypes);
-                }
-            }
-        } else {
-            // This module has no subtypes
-            if ($module->archetype == MOD_ARCHETYPE_RESOURCE) {
-                $link = $module->link->out(true, array('section' => $section));
-                $resources[$link] = $module->title;
-            } else if ($module->archetype === MOD_ARCHETYPE_SYSTEM) {
-                // System modules cannot be added by user, do not add to dropdown
-            } else {
-                $link = $module->link->out(true, array('section' => $section));
-                $activities[$link] = $module->title;
-            }
-        }
-    }
-
-    $straddactivity = get_string('addactivity');
-    $straddresource = get_string('addresource');
-    $sectionname = get_section_name($course, $section);
-    $strresourcelabel = get_string('addresourcetosection', null, $sectionname);
-    $stractivitylabel = get_string('addactivitytosection', null, $sectionname);
-
-    $output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section));
-
-    if (!$vertical) {
-        $output .= html_writer::start_tag('div', array('class' => 'horizontal'));
-    }
-
-    if (!empty($resources)) {
-        $select = new url_select($resources, '', array(''=>$straddresource), "ressection$section");
-        $select->set_help_icon('resources');
-        $select->set_label($strresourcelabel, array('class' => 'accesshide'));
-        $output .= $OUTPUT->render($select);
-    }
-
-    if (!empty($activities)) {
-        $select = new url_select($activities, '', array(''=>$straddactivity), "section$section");
-        $select->set_help_icon('activities');
-        $select->set_label($stractivitylabel, array('class' => 'accesshide'));
-        $output .= $OUTPUT->render($select);
-    }
-
-    if (!$vertical) {
-        $output .= html_writer::end_tag('div');
-    }
-
-    $output .= html_writer::end_tag('div');
-
-    if (course_ajax_enabled($course)) {
-        $straddeither = get_string('addresourceoractivity');
-        // The module chooser link
-        $modchooser = html_writer::start_tag('div', array('class' => 'mdl-right'));
-        $modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser'));
-        $icon = $OUTPUT->pix_icon('t/add', '');
-        $span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text'));
-        $modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link'));
-        $modchooser.= html_writer::end_tag('div');
-        $modchooser.= html_writer::end_tag('div');
-
-        // Wrap the normal output in a noscript div
-        $usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault);
-        if ($usemodchooser) {
-            $output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown'));
-            $modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser'));
-        } else {
-            // If the module chooser is disabled, we need to ensure that the dropdowns are shown even if javascript is disabled
-            $output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown'));
-            $modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser'));
-        }
-        $courserenderer = $PAGE->get_renderer('core', 'course');
-        $output = $courserenderer->course_modchooser($modules, $course) . $modchooser . $output;
-    }
-
+    global $PAGE;
+    $output = '';
+    $courserenderer = $PAGE->get_renderer('core', 'course');
+    $output = $courserenderer->course_section_add_cm_control($course, $section, $sectionreturn,
+            array('inblock' => $vertical));
     if ($return) {
         return $output;
     } else {
         echo $output;
+        return !empty($output);
     }
 }
 
index 009fa34..2fb7c01 100644 (file)
@@ -390,4 +390,120 @@ class core_course_renderer extends plugin_renderer_base {
         $output .= html_writer::end_tag('span');
         return $output;
     }
+
+    /**
+     * Renders HTML for the menus to add activities and resources to the current course
+     *
+     * @param stdClass $course
+     * @param int $section relative section number (field course_sections.section)
+     * @param int $sectionreturn The section to link back to
+     * @param array $displayoptions additional display options, for example blocks add
+     *     option 'inblock' => true, suggesting to display controls vertically
+     * @return string
+     */
+    function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
+        global $CFG;
+
+        $vertical = !empty($displayoptions['inblock']);
+
+        // check to see if user can add menus and there are modules to add
+        if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
+                || !$this->page->user_is_editing()
+                || !($modnames = get_module_types_names()) || empty($modnames)) {
+            return '';
+        }
+
+        // Retrieve all modules with associated metadata
+        $modules = get_module_metadata($course, $modnames, $sectionreturn);
+        $urlparams = array('section' => $section);
+
+        // We'll sort resources and activities into two lists
+        $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
+
+        foreach ($modules as $module) {
+            if (!array_key_exists($module->archetype, $activities)) {
+                // System modules cannot be added by user, do not add to dropdown
+            } else if (isset($module->types)) {
+                // This module has a subtype
+                // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
+                $subtypes = array();
+                foreach ($module->types as $subtype) {
+                    $link = $subtype->link->out(true, $urlparams);
+                    $subtypes[$link] = $subtype->title;
+                }
+
+                // Sort module subtypes into the list
+                if (!empty($module->title)) {
+                    // This grouping has a name
+                    $activities[$module->archetype][] = array($module->title => $subtypes);
+                } else {
+                    // This grouping does not have a name
+                    $activities[$module->archetype] = array_merge($activities[$module->archetype], $subtypes);
+                }
+            } else {
+                // This module has no subtypes
+                $link = $module->link->out(true, $urlparams);
+                $activities[$module->archetype][$link] = $module->title;
+            }
+        }
+
+        $straddactivity = get_string('addactivity');
+        $straddresource = get_string('addresource');
+        $sectionname = get_section_name($course, $section);
+        $strresourcelabel = get_string('addresourcetosection', null, $sectionname);
+        $stractivitylabel = get_string('addactivitytosection', null, $sectionname);
+
+        $output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section));
+
+        if (!$vertical) {
+            $output .= html_writer::start_tag('div', array('class' => 'horizontal'));
+        }
+
+        if (!empty($activities[MOD_CLASS_RESOURCE])) {
+            $select = new url_select($activities[MOD_CLASS_RESOURCE], '', array(''=>$straddresource), "ressection$section");
+            $select->set_help_icon('resources');
+            $select->set_label($strresourcelabel, array('class' => 'accesshide'));
+            $output .= $this->output->render($select);
+        }
+
+        if (!empty($activities[MOD_CLASS_ACTIVITY])) {
+            $select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array(''=>$straddactivity), "section$section");
+            $select->set_help_icon('activities');
+            $select->set_label($stractivitylabel, array('class' => 'accesshide'));
+            $output .= $this->output->render($select);
+        }
+
+        if (!$vertical) {
+            $output .= html_writer::end_tag('div');
+        }
+
+        $output .= html_writer::end_tag('div');
+
+        if (course_ajax_enabled($course) && $course->id == $this->page->course->id) {
+            // modchooser can be added only for the current course set on the page!
+            $straddeither = get_string('addresourceoractivity');
+            // The module chooser link
+            $modchooser = html_writer::start_tag('div', array('class' => 'mdl-right'));
+            $modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser'));
+            $icon = $this->output->pix_icon('t/add', '');
+            $span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text'));
+            $modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link'));
+            $modchooser.= html_writer::end_tag('div');
+            $modchooser.= html_writer::end_tag('div');
+
+            // Wrap the normal output in a noscript div
+            $usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault);
+            if ($usemodchooser) {
+                $output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown'));
+                $modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser'));
+            } else {
+                // If the module chooser is disabled, we need to ensure that the dropdowns are shown even if javascript is disabled
+                $output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown'));
+                $modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser'));
+            }
+            $output = $this->course_modchooser($modules, $course) . $modchooser . $output;
+        }
+
+        return $output;
+    }
 }
index 2c727cb..bda0d53 100644 (file)
--- a/index.php
+++ b/index.php
 
             print_section($SITE, $section, $mods, $modnamesused, true);
 
-            if ($editing) {
-                print_section_add_menus($SITE, $section->section, $modnames);
-            }
+            echo $courserenderer->course_section_add_cm_control($SITE, $section->section);
             echo $OUTPUT->box_end();
         }
     }