Skip to content

Commit

Permalink
MDL-37085 Substitute function print_section_add_menus() with core_cou…
Browse files Browse the repository at this point in the history
…rse_renderer::course_section_add_cm_controls()
  • Loading branch information
marinaglancy committed Jan 14, 2013
1 parent 697ff99 commit 9a6aa5c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 145 deletions.
4 changes: 3 additions & 1 deletion blocks/site_main_menu/block_site_main_menu.php
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion blocks/social_activities/block_social_activities.php
Expand Up @@ -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;
}
Expand Down
18 changes: 6 additions & 12 deletions course/format/renderer.php
Expand Up @@ -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))) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
138 changes: 10 additions & 128 deletions course/lib.php
Expand Up @@ -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);
}
}

Expand Down
116 changes: 116 additions & 0 deletions course/renderer.php
Expand Up @@ -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;
}
}
4 changes: 1 addition & 3 deletions index.php
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 9a6aa5c

Please sign in to comment.