diff --git a/blocks/site_main_menu/block_site_main_menu.php b/blocks/site_main_menu/block_site_main_menu.php index 03a5d4c936560..4cd681f7acd67 100644 --- a/blocks/site_main_menu/block_site_main_menu.php +++ b/blocks/site_main_menu/block_site_main_menu.php @@ -134,7 +134,9 @@ function get_content() { $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; } diff --git a/blocks/social_activities/block_social_activities.php b/blocks/social_activities/block_social_activities.php index d7d48c0384793..0de13b72ec248 100644 --- a/blocks/social_activities/block_social_activities.php +++ b/blocks/social_activities/block_social_activities.php @@ -137,7 +137,9 @@ function get_content() { $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; } diff --git a/course/format/renderer.php b/course/format/renderer.php index befde23c15abe..dd0a6fe8116fb 100644 --- a/course/format/renderer.php +++ b/course/format/renderer.php @@ -544,6 +544,7 @@ public function print_single_section_page($course, $sections, $mods, $modnames, $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 @@ public function print_single_section_page($course, $sections, $mods, $modnames, 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 @@ public function print_single_section_page($course, $sections, $mods, $modnames, 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 @@ public function print_multiple_section_page($course, $sections, $mods, $modnames $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 @@ public function print_multiple_section_page($course, $sections, $mods, $modnames 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 @@ public function print_multiple_section_page($course, $sections, $mods, $modnames 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(); } diff --git a/course/lib.php b/course/lib.php index 983b9fc8109f1..984b5e5f4e804 100644 --- a/course/lib.php +++ b/course/lib.php @@ -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); } } diff --git a/course/renderer.php b/course/renderer.php index 009fa344c4ed2..2fb7c01ccdf89 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -390,4 +390,120 @@ public function course_section_cm_edit_actions($actions) { $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; + } } diff --git a/index.php b/index.php index 2c727cbec362b..bda0d53c42682 100644 --- a/index.php +++ b/index.php @@ -144,9 +144,7 @@ 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(); } }