MDL-65974 course: move format renderers to outputs

This commit is contained in:
Ferran Recio 2020-10-22 09:22:54 +02:00
parent 1c839f9072
commit 8e7dc42440
49 changed files with 3553 additions and 1958 deletions

View file

@ -967,7 +967,6 @@ abstract class course_format {
public function editsection_form($action, $customdata = array()) {
global $CFG;
require_once($CFG->dirroot. '/course/editsection_form.php');
$context = context_course::instance($this->courseid);
if (!array_key_exists('course', $customdata)) {
$customdata['course'] = $this->get_course();
}
@ -1091,6 +1090,18 @@ abstract class course_format {
return ($sectionnum && ($course = $this->get_course()) && $course->marker == $sectionnum);
}
/**
* return true if the course editor must be displayed.
*
* @return bool true if edit controls must be displayed
*/
public function show_editor(): bool {
global $PAGE;
$course = $this->get_course();
$coursecontext = context_course::instance($course->id);
return $PAGE->user_is_editing() && has_capability('moodle/course:update', $coursecontext);
}
/**
* Allows to specify for modinfo that section is not available even when it is visible and conditionally available.
*
@ -1387,16 +1398,13 @@ abstract class course_format {
}
// Load the cmlist output.
$cmitemclass = $this->get_output_classname('section_format\\cmitem');
$renderer = $this->get_renderer($PAGE);
$coursesections = $modinfo->sections;
if (array_key_exists($section->section, $coursesections)) {
$completioninfo = new completion_info($course);
foreach ($coursesections[$section->section] as $cmid) {
$cm = $modinfo->get_cm($cmid);
$cmitem = new $cmitemclass($this, $section, $cm);
$modules[] = $renderer->render($cmitem);
$modules[] = $renderer->course_section_updated_cm_item($this, $section, $cm);
}
}

View file

@ -0,0 +1,134 @@
<?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/>.
/**
* Contains the default activity list from a section.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output;
use core_course\course_format;
use section_info;
use completion_info;
use renderable;
use templatable;
use cm_info;
use stdClass;
/**
* Base class to render a course module inside a course format.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cm_format implements renderable, templatable {
/** @var course_format the course format */
protected $format;
/** @var section_info the section object */
private $section;
/** @var cm_info the course module instance */
protected $mod;
/** @var array optional display options */
protected $displayoptions;
/** @var completion_info the course completion */
protected $completioninfo;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
* @param completion_info $completioninfo the course completion info
* @param cm_info $mod the course module ionfo
* @param array $displayoptions optional extra display options
*/
public function __construct(course_format $format, section_info $section, completion_info $completioninfo,
cm_info $mod, array $displayoptions = []) {
$this->format = $format;
$this->section = $section;
$this->completioninfo = $completioninfo;
$this->mod = $mod;
$this->displayoptions = $displayoptions;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;
$course = $format->get_course();
$mod = $this->mod;
$displayoptions = $this->displayoptions;
$data = (object)[
'cmname' => $output->course_section_cm_name($mod, $displayoptions),
'afterlink' => $mod->afterlink,
'altcontent' => $output->course_section_cm_text($mod, $displayoptions),
'availability' => $output->course_section_cm_availability($mod, $displayoptions),
'url' => $mod->url,
'completion' => $output->course_section_cm_completion(
$course, $this->completioninfo, $mod, $displayoptions
),
];
if (!empty($mod->indent)) {
$data->indent = $mod->indent;
if ($mod->indent > 15) {
$data->hugeindent = true;
}
}
if (!empty($data->cmname)) {
$data->hasname = true;
}
if (!empty($data->url)) {
$data->hasurl = true;
}
$returnsection = $format->get_section_number();
$data->extras = [];
if ($format->show_editor()) {
// Edit actions.
$editactions = course_get_cm_edit_actions($mod, $mod->indent, $returnsection);
$data->extras[] = $output->course_section_cm_edit_actions($editactions, $mod, $displayoptions);
if (!empty($mod->afterediticons)) {
$data->extras[] = $mod->afterediticons;
}
// Move and select options.
$data->moveicon = course_get_cm_move($mod, $returnsection);
}
if (!empty($data->completion) || !empty($data->extras)) {
$data->hasextras = true;
}
return $data;
}
}

View file

@ -0,0 +1,185 @@
<?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/>.
/**
* Contains the main course format out class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output;
use core_course\course_format as course_format_base;
use course_modinfo;
use renderable;
use templatable;
use stdClass;
/**
* Base class to render a course format.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_format implements renderable, templatable {
/** @var core_course\course_format the course format class */
protected $format;
/** @var string the section format class */
protected $sectionclass;
/** @var string the add section output class name */
protected $addsectionclass;
/** @var string section navigation class name */
protected $sectionnavigationclass;
/** @var string section selector class name */
protected $sectionselectorclass;
/**
* Constructor.
*
* @param course_format_base $format the coruse format
*/
public function __construct(course_format_base $format) {
$this->format = $format;
// Load output classes names from format.
$this->sectionclass = $format->get_output_classname('section_format');
$this->addsectionclass = $format->get_output_classname('course_format\\addsection');
$this->sectionnavigationclass = $format->get_output_classname('course_format\\sectionnavigation');
$this->sectionselectorclass = $format->get_output_classname('course_format\\sectionselector');
}
/**
* Export this data so it can be used as the context for a mustache template (core/inplace_editable).
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output) {
$format = $this->format;
$addsection = new $this->addsectionclass($format);
// Most formats uses section 0 as a separate section so we remove from the list.
$sections = $this->export_sections($output);
$initialsection = '';
if (!empty($sections)) {
$initialsection = array_shift($sections);
}
$data = (object)[
'title' => $format->page_title(), // This method should be in the course_format class.
'initialsection' => $initialsection,
'sections' => $sections,
'numsections' => $addsection->export_for_template($output),
'format' => $format->get_format(),
];
// The single section format has extra navigation.
$singlesection = $this->format->get_section_number();
if ($singlesection) {
$sectionnavigation = new $this->sectionnavigationclass($format, $singlesection);
$data->sectionnavigation = $sectionnavigation->export_for_template($output);
$sectionselector = new $this->sectionselectorclass($format, $sectionnavigation);
$data->sectionselector = $sectionselector->export_for_template($output);
$data->hasnavigation = true;
$data->singlesection = array_shift($data->sections);
}
return $data;
}
/**
* Export sections array data.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
protected function export_sections(\renderer_base $output): array {
$format = $this->format;
$course = $format->get_course();
$modinfo = $this->format->get_modinfo();
// Generate section list.
$sections = [];
$stealthsections = [];
$numsections = $format->get_last_section_number();
foreach ($this->get_sections_to_display($modinfo) as $sectionnum => $thissection) {
// The course/view.php check the section existence but the output can be called
// from other parts so we need to check it.
if (!$thissection) {
print_error('unknowncoursesection', 'error', course_get_url($course), format_string($course->fullname));
}
$section = new $this->sectionclass($format, $thissection);
if ($sectionnum > $numsections) {
// Activities inside this section are 'orphaned', this section will be printed as 'stealth' below.
if (!empty($modinfo->sections[$sectionnum])) {
$stealthsections[] = $section->export_for_template($output);
}
continue;
}
// Show the section if the user is permitted to access it, OR if it's not available
// but there is some available info text which explains the reason & should display,
// OR it is hidden but the course has a setting to display hidden sections as unavilable.
$showsection = $thissection->uservisible ||
($thissection->visible && !$thissection->available && !empty($thissection->availableinfo)) ||
(!$thissection->visible && !$course->hiddensections);
if (!$showsection) {
continue;
}
$sections[] = $section->export_for_template($output);
}
if (!empty($stealthsections)) {
$sections = array_merge($sections, $stealthsections);
}
return $sections;
}
/**
* Return an array of sections to display.
*
* This method is used to differentiate between display a specific section
* or a list of them.
*
* @param course_modinfo $modinfo the current course modinfo object
* @return section_info[] an array of section_info to display
*/
private function get_sections_to_display(course_modinfo $modinfo): array {
$singlesection = $this->format->get_section_number();
if ($singlesection) {
return [
$modinfo->get_section_info(0),
$modinfo->get_section_info($singlesection),
];
}
return $modinfo->get_section_info_all();
}
}

View file

@ -0,0 +1,131 @@
<?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/>.
/**
* Contains the default section course format output class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\course_format;
use core_course\course_format;
use renderable;
use templatable;
use moodle_url;
use stdClass;
/**
* Base class to render a course add section buttons.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class addsection implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/**
* Constructor.
*
* @param course_format $format the course format
*/
public function __construct(course_format $format) {
$this->format = $format;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;
$course = $format->get_course();
$options = $format->get_format_options();
$lastsection = $format->get_last_section_number();
$maxsections = $format->get_max_sections();
$data = new stdClass();
// If no editor must be displayed, just retun an empty structure.
if (!$format->show_editor()) {
return $data;
}
$supportsnumsections = array_key_exists('numsections', $options);
if ($supportsnumsections) {
// Current course format has 'numsections' option, which is very confusing and we suggest course format
// developers to get rid of it (see MDL-57769 on how to do it).
if ($lastsection < $maxsections) {
$data->increase = (object) [
'url' => new moodle_url(
'/course/changenumsections.php',
['courseid' => $course->id, 'increase' => true, 'sesskey' => sesskey()]
),
];
}
if ($course->numsections > 0) {
$data->decrease = (object) [
'url' => new moodle_url(
'/course/changenumsections.php',
['courseid' => $course->id, 'increase' => false, 'sesskey' => sesskey()]
),
];
}
} else if (course_get_format($course)->uses_sections() && $lastsection < $maxsections) {
// Current course format does not have 'numsections' option but it has multiple sections suppport.
// Display the "Add section" link that will insert a section in the end.
// Note to course format developers: inserting sections in the other positions should check both
// capabilities 'moodle/course:update' and 'moodle/course:movesections'.
if (get_string_manager()->string_exists('addsections', 'format_'.$course->format)) {
$addstring = get_string('addsections', 'format_'.$course->format);
} else {
$addstring = get_string('addsections');
}
$params = ['courseid' => $course->id, 'insertsection' => 0, 'sesskey' => sesskey()];
$singlesection = $this->format->get_section_number();
if ($singlesection) {
$params['sectionreturn'] = $singlesection;
}
$data->addsections = (object) [
'url' => new moodle_url('/course/changenumsections.php', $params),
'title' => $addstring,
'newsection' => $maxsections - $lastsection,
];
}
if (count((array)$data)) {
$data->showaddsection = true;
}
return $data;
}
}

View file

@ -0,0 +1,101 @@
<?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/>.
/**
* Contains the default frontpage section displayer.
*
* The frontpage has a different wat of rendering the main topic.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\course_format;
use core_course\course_format;
use renderable;
use templatable;
use section_info;
use context_course;
use moodle_url;
use stdClass;
/**
* Represents the frontpage section 1.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class frontpagesection implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_info the course section class */
protected $section;
/** @var string the section output class name */
protected $sectionclass;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
*/
public function __construct(course_format $format, section_info $section) {
$this->format = $format;
$this->section = $section;
// Get the necessary classes.
$this->sectionclass = $format->get_output_classname('section_format');
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
global $USER;
$format = $this->format;
$course = $format->get_course();
$context = context_course::instance($course->id);
$section = $this->section;
$sectionoutput = new $this->sectionclass($format, $section);
$sectionoutput->hide_controls();
if (trim($section->name) == '') {
$sectionoutput->hide_title();
}
$data = (object)[
'sections' => [$sectionoutput->export_for_template($output)],
];
if ($format->show_editor() && has_capability('moodle/course:update', $context)) {
$data->showsettings = true;
$data->settingsurl = new moodle_url('/course/editsection.php', ['id' => $section->id]);
}
return $data;
}
}

View file

@ -0,0 +1,123 @@
<?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/>.
/**
* Contains the default section navigation output class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\course_format;
use core_course\course_format;
use renderable;
use templatable;
use context_course;
use stdClass;
/**
* Base class to render a course add section navigation.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sectionnavigation implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var int the course displayed section */
protected $sectionno;
/** @var stdClass the calculated data to prevent calculations when rendered several times */
private $data = null;
/**
* Constructor.
*
* @param course_format $format the course format
* @param int $sectionno the section number
*/
public function __construct(course_format $format, int $sectionno) {
$this->format = $format;
$this->sectionno = $sectionno;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
global $USER;
if ($this->data !== null) {
return $this->data;
}
$format = $this->format;
$course = $format->get_course();
$context = context_course::instance($course->id);
$modinfo = $this->format->get_modinfo();
$sections = $modinfo->get_section_info_all();
// FIXME: This is really evil and should by using the navigation API.
$canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER) || !$course->hiddensections;
$data = (object)[
'previousurl' => '',
'nexturl' => '',
'larrow' => $output->larrow(),
'rarrow' => $output->rarrow(),
'currentsection' => $this->sectionno,
];
$back = $this->sectionno - 1;
while ($back > 0 and empty($data->previousurl)) {
if ($canviewhidden || $sections[$back]->uservisible) {
if (!$sections[$back]->visible) {
$data->previoushidden = true;
}
$data->previousname = get_section_name($course, $sections[$back]);
$data->previousurl = course_get_url($course, $back);
$data->hasprevious = true;
}
$back--;
}
$forward = $this->sectionno + 1;
$numsections = course_get_format($course)->get_last_section_number();
while ($forward <= $numsections and empty($data->nexturl)) {
if ($canviewhidden || $sections[$forward]->uservisible) {
if (!$sections[$forward]->visible) {
$data->nexthidden = true;
}
$data->nextname = get_section_name($course, $sections[$forward]);
$data->nexturl = course_get_url($course, $forward);
$data->hasnext = true;
}
$forward++;
}
$this->data = $data;
return $data;
}
}

View file

@ -0,0 +1,100 @@
<?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/>.
/**
* Contains the default section selector.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\course_format;
use core_course\course_format;
use renderable;
use templatable;
use url_select;
use stdClass;
/**
* Represents the section selector.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sectionselector implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var sectionnavigation the main section navigation class */
protected $navigation;
/**
* Constructor.
*
* In the current imeplementaiton the seciton selector is almost a variation of the section navigator
* but in the 4.0 this selector will be a kind of dropdown menu. When this happens the construct params
* will change.
*
* @param course_format $format the course format
* @param sectionnavigation $navigation the current section navigation
*/
public function __construct(course_format $format, sectionnavigation $navigation) {
$this->format = $format;
$this->navigation = $navigation;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;
$course = $format->get_course();
$modinfo = $this->format->get_modinfo();
$data = $this->navigation->export_for_template($output);
// Add the section selector.
$sectionmenu = [];
$sectionmenu[course_get_url($course)->out(false)] = get_string('maincoursepage');
$section = 1;
$numsections = $format->get_last_section_number();
while ($section <= $numsections) {
$thissection = $modinfo->get_section_info($section);
$showsection = $thissection->uservisible || !$course->hiddensections;
$url = course_get_url($course, $section);
if ($showsection && $url && $section != $data->currentsection) {
$sectionmenu[$url->out(false)] = get_section_name($course, $section);
}
$section++;
}
$select = new url_select($sectionmenu, '', ['' => get_string('jumpto')]);
$select->class = 'jumpmenu';
$select->formid = 'sectionmenu';
$data->selector = $output->render($select);
return $data;
}
}

View file

@ -0,0 +1,211 @@
<?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/>.
/**
* Contains the default section course format output class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output;
use core_course\course_format;
use completion_info;
use renderable;
use templatable;
use section_info;
use stdClass;
/**
* Base class to render a course section.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class section_format implements renderable, templatable {
/** @var course_format the course format */
protected $format;
/** @var section_info the section info */
protected $thissection;
/** @var section header output class */
protected $headerclass;
/** @var cm list output class */
protected $cmlistclass;
/** @var section summary output class */
protected $summaryclass;
/** @var activities summary output class */
protected $cmsummaryclass;
/** @var section control menu output class */
protected $controlclass;
/** @var section availability output class */
protected $availabilityclass;
/** @var optional move here output class */
protected $movehereclass;
/** @var bool if the title is hidden for some reason */
protected $hidetitle = false;
/** @var bool if the title is hidden for some reason */
protected $hidecontrols = false;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $thissection the section info
*/
public function __construct(course_format $format, section_info $thissection) {
$this->format = $format;
$this->thissection = $thissection;
// Load output classes names from format.
$this->headerclass = $format->get_output_classname('section_format\\header');
$this->cmlistclass = $format->get_output_classname('section_format\\cmlist');
$this->summaryclass = $format->get_output_classname('section_format\\summary');
$this->cmsummaryclass = $format->get_output_classname('section_format\\cmsummary');
$this->controlmenuclass = $format->get_output_classname('section_format\\controlmenu');
$this->availabilityclass = $format->get_output_classname('section_format\\availability');
$this->movehereclass = $format->get_output_classname('section_format\\movehere');
}
/**
* Hide the section title.
*
* This is used on blocks or in the home page where an isolated section is displayed.
*/
public function hide_title(): void {
$this->hidetitle = true;
}
/**
* Hide the section controls.
*
* This is used on blocks or in the home page where an isolated section is displayed.
*/
public function hide_controls(): void {
$this->hidecontrols = true;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;
$course = $format->get_course();
$thissection = $this->thissection;
$singlesection = $format->get_section_number();
$summary = new $this->summaryclass($format, $thissection);
$availability = new $this->availabilityclass($format, $thissection);
$data = (object)[
'num' => $thissection->section ?? '0',
'id' => $thissection->id,
'sectionreturnid' => $singlesection,
'summary' => $summary->export_for_template($output),
'availability' => $availability->export_for_template($output),
];
// Check if it is a stealth sections (orphaned).
if ($thissection->section > $format->get_last_section_number()) {
$data->isstealth = true;
$data->ishidden = true;
}
if ($format->show_editor()) {
if (empty($this->hidecontrols)) {
$controlmenu = new $this->controlmenuclass($format, $thissection);
$data->controlmenu = $controlmenu->export_for_template($output);
}
if (empty($data->isstealth)) {
$data->cmcontrols = $output->course_section_add_cm_control($course, $thissection->section, 0);
}
}
if ($thissection->section == 0) {
// Section zero is always visible only as a cmlist.
$cmlist = new $this->cmlistclass($format, $thissection);
$data->cmlist = $cmlist->export_for_template($output);
// Section 0 could have a completion help icon.
$completioninfo = new completion_info($course);
$data->completioninfo = $completioninfo->display_help_icon();
return $data;
}
// When a section is displayed alone the title goes over the section, not inside it.
$header = new $this->headerclass($format, $thissection);
if ($thissection->section == $singlesection) {
if (empty($this->hidetitle)) {
$data->singleheader = $header->export_for_template($output);
}
} else {
if (empty($this->hidetitle)) {
$data->header = $header->export_for_template($output);
}
// Add activities summary if necessary.
if (!$format->show_editor() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
$cmsummary = new $this->cmsummaryclass($format, $thissection);
$data->cmsummary = $cmsummary->export_for_template($output);
$data->onlysummary = true;
if (!$format->is_section_current($thissection)) {
// In multipage, only the current section (and the section zero) has elements.
return $data;
}
}
}
// Add the cm list.
if ($thissection->uservisible) {
$cmlist = new $this->cmlistclass($format, $thissection);
$data->cmlist = $cmlist->export_for_template($output);
}
if (!$thissection->visible) {
$data->ishidden = true;
}
if ($format->is_section_current($thissection)) {
$data->iscurrent = true;
$data->currentlink = get_accesshide(
get_string('currentsection', 'format_'.$format->get_format())
);
}
return $data;
}
}

View file

@ -0,0 +1,123 @@
<?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/>.
/**
* Contains the default section availability output class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use renderable;
use templatable;
use core_availability\info_section;
use core_availability\info;
use context_course;
use stdClass;
/**
* Base class to render section availability.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class availability implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_info the section object */
protected $section;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
*/
public function __construct(course_format $format, section_info $section) {
$this->format = $format;
$this->section = $section;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* If section is not visible, display the message about that ('Not available
* until...', that sort of thing). Otherwise, returns blank.
*
* For users with the ability to view hidden sections, it shows the
* information even though you can view the section and also may include
* slightly fuller information (so that teachers can tell when sections
* are going to be unavailable etc). This logic is the same as for
* activities.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdclass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
global $CFG, $USER;
$format = $this->format;
$section = $this->section;
$course = $format->get_course();
$context = context_course::instance($section->course);
$canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER);
$info = '';
if (!$section->visible) {
if ($canviewhidden) {
$info = $output->availability_info(get_string('hiddenfromstudents'), 'ishidden');
} else {
// We are here because of the setting "Hidden sections are shown in collapsed form".
// Student can not see the section contents but can see its name.
$info = $output->availability_info(get_string('notavailable'), 'ishidden');
}
} else if (!$section->uservisible) {
if ($section->availableinfo) {
// Note: We only get to this function if availableinfo is non-empty,
// so there is definitely something to print.
$formattedinfo = info::format_info($section->availableinfo, $section->course);
$info = $output->availability_info($formattedinfo, 'isrestricted');
}
} else if ($canviewhidden && !empty($CFG->enableavailability)) {
// Check if there is an availability restriction.
$ci = new info_section($section);
$fullinfo = $ci->get_full_information();
if ($fullinfo) {
$formattedinfo = info::format_info($fullinfo, $section->course);
$info = $output->availability_info($formattedinfo, 'isrestricted isfullinfo');
}
}
$data = (object)[
'info' => $info,
];
if (!empty($info)) {
$data->hasavailability = true;
}
return $data;
}
}

View file

@ -0,0 +1,101 @@
<?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/>.
/**
* Contains the default activity item from a section.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use completion_info;
use renderable;
use templatable;
use cm_info;
use stdClass;
/**
* Base class to render a section activity in the activities list.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cmitem implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_format the course section class */
protected $section;
/** @var cm_info the course module to display */
protected $mod;
/** @var array optional display options */
protected $displayoptions;
/** @var string the cm output class name */
protected $cmclass;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
* @param cm_info $mod the course module ionfo
* @param array $displayoptions optional extra display options
*/
public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
$this->format = $format;
$this->section = $section;
$this->mod = $mod;
$this->displayoptions = $displayoptions;
// Get the necessary classes.
$this->cmclass = $format->get_output_classname('cm_format');
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;
$course = $format->get_course();
$completioninfo = new completion_info($course);
$mod = $this->mod;
$data = new stdClass();
$data->cms = [];
$item = new $this->cmclass($format, $this->section, $completioninfo, $mod, $this->displayoptions);
return (object)[
'id' => $mod->id,
'module' => $mod->modname,
'extraclasses' => $mod->extraclasses,
'cmformat' => $item->export_for_template($output),
];
}
}

View file

@ -0,0 +1,132 @@
<?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/>.
/**
* Contains the default activity list from a section.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use renderable;
use templatable;
use moodle_url;
use stdClass;
/**
* Base class to render a section activity list.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cmlist implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_format the course section class */
protected $section;
/** @var array optional display options */
protected $displayoptions;
/** @var string the item output class name */
protected $itemclass;
/** @var optional move here output class */
protected $movehereclass;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
* @param array $displayoptions optional extra display options
*/
public function __construct(course_format $format, section_info $section, array $displayoptions = []) {
$this->format = $format;
$this->section = $section;
$this->displayoptions = $displayoptions;
// Get the necessary classes.
$this->itemclass = $format->get_output_classname('section_format\cmitem');
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
global $USER;
$format = $this->format;
$section = $this->section;
$course = $format->get_course();
$modinfo = $format->get_modinfo();
$user = $USER;
$data = new stdClass();
$data->cms = [];
// By default, non-ajax controls are disabled but in some places like the frontpage
// it is necessary to display them. This is a temporal solution while JS is still
// optional for course editing.
$showmovehere = ismoving($course->id);
if ($showmovehere) {
$data->hascms = true;
$data->showmovehere = true;
$data->strmovefull = strip_tags(get_string("movefull", "", "'$user->activitycopyname'"));
$data->movetosectionurl = new moodle_url('/course/mod.php', ['movetosection' => $section->id, 'sesskey' => sesskey()]);
$data->movingstr = strip_tags(get_string('activityclipboard', '', $user->activitycopyname));
$data->cancelcopyurl = new moodle_url('/course/mod.php', ['cancelcopy' => 'true', 'sesskey' => sesskey()]);
}
if (empty($modinfo->sections[$section->section])) {
return $data;
}
foreach ($modinfo->sections[$section->section] as $modnumber) {
$mod = $modinfo->cms[$modnumber];
// If the old non-ajax move is necessary, we do not print the selected cm.
if ($showmovehere && $USER->activitycopy == $mod->id) {
continue;
}
if ($mod->is_visible_on_course_page()) {
$item = new $this->itemclass($format, $section, $mod, $this->displayoptions);
$data->cms[] = (object)[
'cmitem' => $item->export_for_template($output),
'moveurl' => new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey())),
];
}
}
if (!empty($data->cms)) {
$data->hascms = true;
}
return $data;
}
}

View file

@ -0,0 +1,128 @@
<?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/>.
/**
* Contains the default activities summary (used for singlesection format).
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use completion_info;
use renderable;
use templatable;
use stdClass;
/**
* Base class to render a course section summary.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cmsummary implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_info the course section class */
protected $section;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
*/
public function __construct(course_format $format, section_info $section) {
$this->format = $format;
$this->section = $section;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
list($mods, $complete, $total) = $this->calculate_section_stats();
if (empty($mods)) {
return new stdClass();
}
$data = (object)[
'total' => $total,
'complete' => $complete,
'mods' => array_values($mods),
];
$data->modprogress = get_string('progresstotal', 'completion', $data);
return $data;
}
/**
* Calculate the activities count of the current section.
*
* @return array with [[count by activity type], completed activities, total of activitites]
*/
private function calculate_section_stats(): array {
$format = $this->format;
$course = $format->get_course();
$section = $this->section;
$modinfo = $format->get_modinfo();
$completioninfo = new completion_info($course);
$mods = [];
$total = 0;
$complete = 0;
$cmids = $modinfo->sections[$section->section] ?? [];
$cancomplete = isloggedin() && !isguestuser();
foreach ($cmids as $cmid) {
$thismod = $modinfo->cms[$cmid];
if ($thismod->uservisible) {
if (isset($mods[$thismod->modname])) {
$mods[$thismod->modname]['name'] = $thismod->modplural;
$mods[$thismod->modname]['count']++;
} else {
$mods[$thismod->modname]['name'] = $thismod->modfullname;
$mods[$thismod->modname]['count'] = 1;
}
if ($cancomplete && $completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) {
$total++;
$completiondata = $completioninfo->get_data($thismod, true);
if ($completiondata->completionstate == COMPLETION_COMPLETE ||
$completiondata->completionstate == COMPLETION_COMPLETE_PASS) {
$complete++;
}
}
}
}
return [$mods, $complete, $total];
}
}

View file

@ -0,0 +1,232 @@
<?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/>.
/**
* Contains the default section controls output class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use renderable;
use templatable;
use context_course;
use moodle_url;
use pix_icon;
use action_menu_link_secondary;
use action_menu;
use stdClass;
/**
* Base class to render section controls.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class controlmenu implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_info the course section class */
protected $section;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
*/
public function __construct(course_format $format, section_info $section) {
$this->format = $format;
$this->section = $section;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$section = $this->section;
$controls = $this->section_control_items();
if (empty($controls)) {
return new stdClass();
}
// Convert control array into an action_menu.
$menu = new action_menu();
$menu->set_menu_trigger(get_string('edit'));
$menu->attributes['class'] .= ' section-actions';
foreach ($controls as $value) {
$url = empty($value['url']) ? '' : $value['url'];
$icon = empty($value['icon']) ? '' : $value['icon'];
$name = empty($value['name']) ? '' : $value['name'];
$attr = empty($value['attr']) ? array() : $value['attr'];
$class = empty($value['pixattr']['class']) ? '' : $value['pixattr']['class'];
$al = new action_menu_link_secondary(
new moodle_url($url),
new pix_icon($icon, '', null, array('class' => "smallicon " . $class)),
$name,
$attr
);
$menu->add($al);
}
$data = (object)[
'menu' => $output->render($menu),
'hasmenu' => true,
'id' => $section->id,
];
return $data;
}
/**
* Generate the edit control items of a section.
*
* It is not clear this kind of controls are still available in 4.0 so, for now, this
* method is almost a clone of the previous section_control_items from the course/renderer.php.
*
* This method must remain public until the final deprecation of section_edit_control_items.
*
* @return array of edit control items
*/
public function section_control_items() {
global $USER;
$format = $this->format;
$section = $this->section;
$course = $format->get_course();
$sectionreturn = $format->get_section_number();
$user = $USER;
$coursecontext = context_course::instance($course->id);
$numsections = $format->get_last_section_number();
$isstealth = $section->section > $numsections;
$baseurl = course_get_url($course, $sectionreturn);
$baseurl->param('sesskey', sesskey());
$controls = [];
if (!$isstealth && has_capability('moodle/course:update', $coursecontext, $user)) {
if ($section->section > 0
&& get_string_manager()->string_exists('editsection', 'format_'.$format->get_format())) {
$streditsection = get_string('editsection', 'format_'.$format->get_format());
} else {
$streditsection = get_string('editsection');
}
$controls['edit'] = array(
'url' => new moodle_url('/course/editsection.php', array('id' => $section->id, 'sr' => $sectionreturn)),
'icon' => 'i/settings',
'name' => $streditsection,
'pixattr' => array('class' => ''),
'attr' => array('class' => 'icon edit'));
}
if ($section->section) {
$url = clone($baseurl);
if (!$isstealth) {
if (has_capability('moodle/course:sectionvisibility', $coursecontext, $user)) {
if ($section->visible) { // Show the hide/show eye.
$strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
$url->param('hide', $section->section);
$controls['visiblity'] = array(
'url' => $url,
'icon' => 'i/hide',
'name' => $strhidefromothers,
'pixattr' => array('class' => ''),
'attr' => array('class' => 'icon editing_showhide',
'data-sectionreturn' => $sectionreturn, 'data-action' => 'hide'));
} else {
$strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
$url->param('show', $section->section);
$controls['visiblity'] = array(
'url' => $url,
'icon' => 'i/show',
'name' => $strshowfromothers,
'pixattr' => array('class' => ''),
'attr' => array('class' => 'icon editing_showhide',
'data-sectionreturn' => $sectionreturn, 'data-action' => 'show'));
}
}
if (!$sectionreturn) {
if (has_capability('moodle/course:movesections', $coursecontext, $user)) {
$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['moveup'] = array(
'url' => $url,
'icon' => 'i/up',
'name' => $strmoveup,
'pixattr' => array('class' => ''),
'attr' => array('class' => 'icon moveup'));
}
$url = clone($baseurl);
if ($section->section < $numsections) { // Add a arrow to move section down.
$url->param('section', $section->section);
$url->param('move', 1);
$strmovedown = get_string('movedown');
$controls['movedown'] = array(
'url' => $url,
'icon' => 'i/down',
'name' => $strmovedown,
'pixattr' => array('class' => ''),
'attr' => array('class' => 'icon movedown'));
}
}
}
}
if (course_can_delete_section($course, $section)) {
if (get_string_manager()->string_exists('deletesection', 'format_'.$course->format)) {
$strdelete = get_string('deletesection', 'format_'.$course->format);
} else {
$strdelete = get_string('deletesection');
}
$url = new moodle_url('/course/editsection.php', array(
'id' => $section->id,
'sr' => $sectionreturn,
'delete' => 1,
'sesskey' => sesskey()));
$controls['delete'] = array(
'url' => $url,
'icon' => 'i/delete',
'name' => $strdelete,
'pixattr' => array('class' => ''),
'attr' => array('class' => 'icon editing_delete'));
}
}
return $controls;
}
}

View file

@ -0,0 +1,99 @@
<?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/>.
/**
* Contains the default section header format output class.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use renderable;
use templatable;
use stdClass;
/**
* Base class to render a section header.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class header implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_info the course section class */
protected $section;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
*/
public function __construct(course_format $format, section_info $section) {
$this->format = $format;
$this->section = $section;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;
$section = $this->section;
$course = $format->get_course();
$data = (object)[
'num' => $section->section,
'id' => $section->id,
];
if ($section->section > $format->get_last_section_number()) {
// Stealth sections (orphaned) has special title.
$data->title = get_string('orphanedactivitiesinsectionno', '', $section->section);
} else if ($section->section == $format->get_section_number()) {
// Regular section title.
$data->title = $output->section_title_without_link($section, $course);
$data->issinglesection = true;
} else {
// Regular section title.
$data->title = $output->section_title($section, $course);
}
if (!$section->visible) {
$data->ishidden = true;
}
if (!$format->show_editor() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE && empty($data->issinglesection)) {
$data->url = course_get_url($course, $section->section);
$data->name = get_section_name($course, $section);
}
return $data;
}
}

View file

@ -0,0 +1,94 @@
<?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/>.
/**
* Contains the default section summary (used for multipage format).
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output\section_format;
use core_course\course_format;
use section_info;
use renderable;
use templatable;
use context_course;
use stdClass;
/**
* Base class to render a course section summary.
*
* @package core_course
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class summary implements renderable, templatable {
/** @var course_format the course format class */
protected $format;
/** @var section_info the course section class */
private $section;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
*/
public function __construct(course_format $format, section_info $section) {
$this->format = $format;
$this->section = $section;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
$section = $this->section;
$data = new stdClass();
if ($section->uservisible || $section->visible) {
$data->summarytext = $this->format_summary_text();
}
return $data;
}
/**
* Generate html for a section summary text
*
* @return string HTML to output.
*/
public function format_summary_text(): string {
$section = $this->section;
$context = context_course::instance($section->course);
$summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php',
$context->id, 'course', 'section', $section->id);
$options = new stdClass();
$options->noclean = true;
$options->overflowdiv = true;
return format_text($summarytext, $section->summaryformat, $options);
}
}