moodle/course/format/renderer.php
Dan Poltawski 1abf12532d MDL-32508 course: Add basic 'add a section' button at the bottom of the course page
With thanks to LUNS who provided this code from when I did it working
there.
2012-05-04 10:53:01 +08:00

525 lines
20 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base renderer for outputting course formats.
*
* @package core
* @copyright 2012 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.3
*/
defined('MOODLE_INTERNAL') || die();
/**
* This renderer is used by section based formats
*
* @package core
* @copyright 2012 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.3
*/
abstract class format_renderer_base extends plugin_renderer_base {
/**
* Generate the starting container html for a list of sections
* @return string HTML to output.
*/
abstract public function start_section_list();
/**
* Generate the closing container html for a list of sections
* @return string HTML to output.
*/
abstract public function end_section_list();
/**
* Generate the title for this section page
* @return string the page title
*/
abstract public function page_title();
/**
* Generate the display of the header part of a section before
* course modules are included
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course The course entry from DB
* @param bool $onsectionpage true if being printed on a section page
* @return string HTML to output.
*/
public function section_header($section, $course, $onsectionpage) {
global $PAGE;
$o = '';
$currenttext = '';
$sectionstyle = '';
$rightcontent = $this->output->spacer();
$leftcontent = $this->output->spacer();
$linktitle = false;
if ($section->section != 0 ) {
// Only in the non-general sections.
if (!$section->visible) {
$sectionstyle = ' hidden';
} else if ($course->marker == $section->section) {
$sectionstyle = ' current';
$currenttext = get_accesshide(get_string('currentsection', 'format_'.$course->format));
}
$leftcontent = $currenttext.$section->section;
$controls = $this->section_edit_controls($course, $section, $onsectionpage);
if (!empty($controls)) {
$rightcontent = implode('<br />', $controls);
}
$linktitle = ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE);
}
$o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section,
'class' => 'section main clearfix'.$sectionstyle));
$o.= html_writer::tag('div', $leftcontent, array('class' => 'left side'));
$o.= html_writer::tag('div', $rightcontent, array('class' => 'right side'));
$o.= html_writer::start_tag('div', array('class' => 'content'));
if (!$onsectionpage) {
$title = get_section_name($course, $section);
if ($linktitle) {
$title = html_writer::link(course_get_url($course, $section->section), $title);
}
$o.= $this->output->heading($title, 3, 'sectionname');
}
$o.= html_writer::start_tag('div', array('class' => 'summary'));
$context = context_course::instance($section->course);
$summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php',
$context->id, 'course', 'section', $section->id);
$summaryformatoptions = new stdClass();
$summaryformatoptions->noclean = true;
$summaryformatoptions->overflowdiv = true;
$o.= format_text($summarytext, $section->summaryformat, $summaryformatoptions);
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $context)) {
$url = new moodle_url('/course/editsection.php', array('id'=>$section->id));
if ($onsectionpage) {
$url->param('sectionreturn', 1);
}
$o.= html_writer::link($url,
html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/edit'), 'class' => 'iconsmall edit')),
array('title' => get_string('editsummary')));
}
$o.= html_writer::end_tag('div');
return $o;
}
/**
* Generate the display of the footer part of a section
*
* @return string HTML to output.
*/
public function section_footer() {
$o = html_writer::end_tag('div');
$o.= html_writer::end_tag('li');
return $o;
}
/**
* Generate the edit controls of a section
*
* @param stdClass $course The course entry from DB
* @param stdClass $section The course_section entry from DB
* @param bool $onsectionpage true if being printed on a section page
* @return array of links with edit controls
*/
public function section_edit_controls($course, $section, $onsectionpage = false) {
global $PAGE;
if (!$PAGE->user_is_editing()) {
return array();
}
if (!has_capability('moodle/course:update', context_course::instance($course->id))) {
return array();
}
if ($onsectionpage) {
$baseurl = course_get_url($course, $section->section);
} else {
$baseurl = course_get_url($course);
}
$baseurl->param('sesskey', sesskey());
$controls = array();
$url = clone($baseurl);
if ($section->visible) { // Show the hide/show eye.
$strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
$url->param('hide', $section->section);
$controls[] = html_writer::link($url,
html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/hide'),
'class' => 'icon hide', 'alt' => $strhidefromothers)),
array('title' => $strhidefromothers, 'class' => 'editing_showhide'));
} else {
$strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
$url->param('show', $section->section);
$controls[] = html_writer::link($url,
html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/show'),
'class' => 'icon hide', 'alt' => $strshowfromothers)),
array('title' => $strshowfromothers, 'class' => 'editing_showhide'));
}
if (!$onsectionpage) {
$url = clone($baseurl);
if ($section->section > 1) { // Add a arrow to move section up.
$url->param('section', $section->section);
$url->param('move', -1);
$strmoveup = get_string('moveup');
$controls[] = html_writer::link($url,
html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/up'),
'class' => 'icon up', 'alt' => $strmoveup)),
array('title' => $strmoveup, 'class' => 'moveup'));
}
$url = clone($baseurl);
if ($section->section < $course->numsections) { // Add a arrow to move section down.
$url->param('section', $section->section);
$url->param('move', 1);
$strmovedown = get_string('movedown');
$controls[] = html_writer::link($url,
html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/down'),
'class' => 'icon down', 'alt' => $strmovedown)),
array('title' => $strmovedown, 'class' => 'movedown'));
}
}
return $controls;
}
/**
* Generate a summary of a section for display on the 'coruse index page'
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course The course entry from DB
* @return string HTML to output.
*/
public function section_summary($section, $course) {
$o = '';
$o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section));
$title = get_section_name($course, $section);
$o.= html_writer::start_tag('div', array('class' => 'section-summary'));
$o.= html_writer::start_tag('a', array('href' => course_get_url($course, $section->section)));
$o.= $this->output->heading($title, 3, 'header section-title');
$o.= html_writer::end_tag('a');
$o.= html_writer::start_tag('div', array('class' => 'summarytext'));
$o.= format_text($section->summary, $section->summaryformat);
$o.= html_writer::end_tag('div');
$o.= html_writer::end_tag('div');
$o.= html_writer::end_tag('li');
return $o;
}
/**
* Show if something is on on the course clipboard (moving around)
*
* @param stdClass $course The course entry from DB
* @param int $sectionno The section number in the coruse which is being dsiplayed
* @return string HTML to output.
*/
public function course_activity_clipboard($course, $sectionno = 0) {
global $USER;
$o = '';
// If currently moving a file then show the current clipboard.
if (ismoving($course->id)) {
$url = new moodle_url('/course/mod.php',
array('sesskey' => sesskey(),
'cancelcopy' => true,
'sr' => $sectionno,
)
);
$strcancel= get_string('cancel');
$o.= html_writer::start_tag('li', array('class' => 'clipboard'));
$o.= strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
$o.= ' ('.html_writer::link($url, get_string('cancel')).')';
$o.= html_writer::end_tag('li');
}
return $o;
}
/**
* Generate next/previous section links for naviation
*
* @param stdClass $course The course entry from DB
* @param array $sections The course_sections entries from the DB
* @param int $sectionno The section number in the coruse which is being dsiplayed
* @return string HTML to output.
*/
public function get_nav_links($course, $sections, $sectionno) {
// FIXME: This is really evil and should by using the navigation API.
$canviewhidden = has_capability('moodle/course:viewhiddensections', context_course::instance($course->id))
or !$course->hiddensections;
$links = array('previous' => '', 'next' => '');
$back = $sectionno - 1;
while ($back > 0 and empty($links['previous'])) {
if ($canviewhidden || $sections[$back]->visible) {
$links['previous'] = html_writer::link(course_get_url($course, $back),
$this->output->larrow().$this->output->spacer().get_section_name($course, $sections[$back]));
}
$back--;
}
$forward = $sectionno + 1;
while ($forward <= $course->numsections and empty($links['next'])) {
if ($canviewhidden || $sections[$forward]->visible) {
$links['next'] = html_writer::link(course_get_url($course, $forward),
get_section_name($course, $sections[$forward]).$this->output->spacer().$this->output->rarrow());
}
$forward++;
}
$o = '';
$o.= html_writer::start_tag('div', array('class' => 'section-navigation yui3-g'));
$o.= html_writer::tag('div', $links['previous'], array('class' => 'yui3-u'));
$o.= html_writer::tag('div', $links['next'], array('class' => 'right yui3-u'));
$o.= html_writer::end_tag('div');
return $o;
}
/**
* Generate the header html of a stealth section
*
* @param int $sectionno The section number in the coruse which is being dsiplayed
* @return string HTML to output.
*/
public function stealth_section_header($sectionno) {
$o = '';
$o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix orphaned hidden'));
$o.= html_writer::tag('div', '', array('class' => 'left side'));
$o.= html_writer::tag('div', '', array('class' => 'right side'));
$o.= html_writer::start_tag('div', array('class' => 'content'));
$o.= $this->output->heading(get_string('orphanedactivities'), 3, 'sectionname');
return $o;
}
/**
* Generate footer html of a stealth section
*
* @return string HTML to output.
*/
public function stealth_section_footer() {
$o = html_writer::end_tag('div');
$o.= html_writer::end_tag('li');
return $o;
}
/**
* Generate the html for a hidden section
*
* @param int $sectionno The section number in the coruse which is being dsiplayed
* @return string HTML to output.
*/
public function section_hidden($sectionno) {
$o = '';
$o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix hidden'));
$o.= html_writer::tag('div', '', array('class' => 'left side'));
$o.= html_writer::tag('div', '', array('class' => 'right side'));
$o.= html_writer::start_tag('div', array('class' => 'content'));
$o.= get_string('notavailable');
$o.= html_writer::end_tag('div');
$o.= html_writer::end_tag('li');
return $o;
}
/**
* Output the html for a single section page .
*
* @param stdClass $course The course entry from DB
* @param array $sections The course_sections entries from the DB
* @param array $mods used for print_section()
* @param array $modnames used for print_section()
* @param array $modnamesused used for print_section()
* @param int $displaysection The section number in the course which is being displayed
*/
public function print_single_section_page($course, $sections, $mods, $modnames, $modnamesused, $displaysection) {
global $PAGE;
// Section next/previous links.
$sectionnavlinks = $this->get_nav_links($course, $sections, $displaysection);
echo $sectionnavlinks;
// Can we view the section in question?
$context = context_course::instance($course->id);
$canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
if (!$sections[$displaysection]->visible && !$canviewhidden) {
if (!$course->hiddensections) {
echo $this->start_section_list();
echo $this->section_hidden($displaysection);
echo $this->end_section_list();
echo $sectionnavlinks;
}
// Can't view this section.
return;
}
// Title with completion help icon.
$completioninfo = new completion_info($course);
echo $completioninfo->display_help_icon();
$title = get_section_name($course, $sections[$displaysection]);
echo $this->output->heading($title, 2, 'headingblock header outline');
// Copy activity clipboard..
echo $this->course_activity_clipboard($course, $displaysection);
// Now the list of sections..
echo $this->start_section_list();
// General section if non-empty.
$thissection = $sections[0];
if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
echo $this->section_header($thissection, $course, true);
print_section($course, $thissection, $mods, $modnamesused, true);
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, 0, $modnames);
}
echo $this->section_footer();
}
// The requested section page.
$thissection = $sections[$displaysection];
echo $this->section_header($thissection, $course, true);
print_section($course, $thissection, $mods, $modnamesused, true);
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, $displaysection, $modnames);
}
echo $this->section_footer();
echo $sectionnavlinks;
echo $this->end_section_list();
}
/**
* Output the html for a multiple section page
*
* @param stdClass $course The course entry from DB
* @param array $sections The course_sections entries from the DB
* @param array $mods used for print_section()
* @param array $modnames used for print_section()
* @param array $modnamesused used for print_section()
*/
public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
global $PAGE;
$context = context_course::instance($course->id);
// Title with completion help icon.
$completioninfo = new completion_info($course);
echo $completioninfo->display_help_icon();
echo $this->output->heading($this->page_title(), 2, 'headingblock header outline');
// Copy activity clipboard..
echo $this->course_activity_clipboard($course);
// Now the list of sections..
echo $this->start_section_list();
// General section if non-empty.
$thissection = $sections[0];
unset($sections[0]);
if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
echo $this->section_header($thissection, $course, true);
print_section($course, $thissection, $mods, $modnamesused, true);
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, 0, $modnames);
}
echo $this->section_footer();
}
$canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
for ($section = 1; $section <= $course->numsections; $section++) {
if (!empty($sections[$section])) {
$thissection = $sections[$section];
} else {
// This will create a course section if it doesn't exist..
$thissection = get_course_section($section, $course->id);
}
$showsection = ($canviewhidden or $thissection->visible or !$course->hiddensections);
if (!$thissection->visible && !$canviewhidden) {
if (!$course->hiddensections) {
echo $this->section_hidden($section);
}
unset($sections[$section]);
continue;
}
if (!$PAGE->user_is_editing() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
// Display section summary only.
echo $this->section_summary($thissection, $course);
} else {
echo $this->section_header($thissection, $course, false);
print_section($course, $thissection, $mods, $modnamesused);
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, $section, $modnames);
}
echo $this->section_footer();
}
unset($sections[$section]);
}
if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
// Print stealth sections if present.
$modinfo = get_fast_modinfo($course);
foreach ($sections as $section => $thissection) {
if (empty($modinfo->sections[$section])) {
continue;
}
echo $this->stealth_section_header($section);
print_section($course, $thissection, $mods, $modnamesused);
echo $this->stealth_section_footer();
}
$straddsection = get_string('addanadditionalsection', 'moodle');
echo html_writer::start_tag('div', array('class' => 'mdl-align'));
echo $this->output->action_link(
new moodle_url('/course/addsection.php',
array('courseid' => $course->id, 'sesskey' => sesskey())
), $this->output->pix_icon('t/add', $straddsection).$straddsection, null,
array('class' => 'addsectionlink')
);
echo html_writer::end_tag('div');
}
echo $this->end_section_list();
}
}