Commit 41b46256 authored by Ferran Recio Calderó's avatar Ferran Recio Calderó
Browse files

MDL-72578 course: activity render to output classes

parent 70073fdc
......@@ -232,7 +232,8 @@ abstract class info {
* @return bool True if activity is available for all
*/
public function is_available_for_all() {
if (is_null($this->availability)) {
global $CFG;
if (is_null($this->availability) || !empty($CFG->enableavailability)) {
return true;
} else {
try {
......
......@@ -48,12 +48,19 @@ class block_site_main_menu extends block_list {
}
$course = get_site();
$format = course_get_format($course);
$courserenderer = $format->get_renderer($this->page);
require_once($CFG->dirroot.'/course/lib.php');
$context = context_course::instance($course->id);
$isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
$courserenderer = $this->page->get_renderer('core', 'course');
/// extra fast view mode
// Output classes.
$cmnameclass = $format->get_output_classname('content\\cm\\cmname');
$controlmenuclass = $format->get_output_classname('content\\cm\\controlmenu');
// Extra fast view mode.
if (!$isediting) {
$modinfo = get_fast_modinfo($course);
if (!empty($modinfo->sections[0])) {
......@@ -69,13 +76,17 @@ class block_site_main_menu extends block_list {
$indent = '';
}
if (!empty($cm->url)) {
$content = html_writer::div($courserenderer->course_section_cm_name($cm), 'activity');
if (empty($cm->url)) {
$content = html_writer::div(
$cm->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
'contentwithoutlink'
);
} else {
$content = $courserenderer->course_section_cm_text($cm);
$cmname = new $cmnameclass($format, $cm->get_section_info(), $cm, $isediting);
$content = $courserenderer->render($cmname);
}
$this->content->items[] = $indent . html_writer::div($content, 'main-menu-content');
$this->content->items[] = $indent . html_writer::div($content, 'main-menu-content activity');
}
}
return $this->content;
......@@ -106,18 +117,28 @@ class block_site_main_menu extends block_list {
continue;
}
if (!$ismoving) {
$actions = course_get_cm_edit_actions($mod, $mod->indent);
// Prepend list of actions with the 'move' action.
$actions = array('move' => new action_menu_link_primary(
new moodle_url('/course/mod.php', array('sesskey' => sesskey(), 'copy' => $mod->id)),
new pix_icon('t/move', $strmove, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$controlmenu = new $controlmenuclass(
$format,
$mod->get_section_info(),
$mod
);
$menu = $controlmenu->get_action_menu();
// Add a move primary action.
$menu->add(
new action_menu_link_primary(
new moodle_url('/course/mod.php', ['sesskey' => sesskey(), 'copy' => $mod->id]),
new pix_icon('t/move', $strmove, 'moodle', ['class' => 'iconsmall', 'title' => '']),
$strmove
)) + $actions;
)
);
$editbuttons = html_writer::tag('div',
$courserenderer->course_section_cm_edit_actions($actions, $mod, array('donotenhance' => true)),
array('class' => 'buttons')
$editbuttons = html_writer::tag(
'div',
$courserenderer->render($controlmenu),
['class' => 'buttons']
);
} else {
$editbuttons = '';
......@@ -138,9 +159,13 @@ class block_site_main_menu extends block_list {
$indent = '';
}
if (!$mod->url) {
$content = $courserenderer->course_section_cm_text($mod);
$content = html_writer::div(
$mod->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
'contentwithoutlink'
);
} else {
$content = html_writer::div($courserenderer->course_section_cm_name($mod), ' activity');
$cmname = new $cmnameclass($format, $mod->get_section_info(), $mod, $isediting);
$content = html_writer::div($courserenderer->render($cmname), 'activity');
}
$this->content->items[] = $indent . html_writer::div($content . $editbuttons, 'main-menu-content');
}
......@@ -159,5 +184,3 @@ class block_site_main_menu extends block_list {
return $this->content;
}
}
......@@ -48,7 +48,8 @@ class block_social_activities extends block_list {
}
$course = $this->page->course;
$courserenderer = $this->page->get_renderer('core', 'course');
$format = course_get_format($course);
$courserenderer = $format->get_renderer($this->page);
require_once($CFG->dirroot.'/course/lib.php');
......@@ -56,7 +57,11 @@ class block_social_activities extends block_list {
$isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
$modinfo = get_fast_modinfo($course);
/// extra fast view mode
// Output classes.
$cmnameclass = $format->get_output_classname('content\\cm\\cmname');
$controlmenuclass = $format->get_output_classname('content\\cm\\controlmenu');
// Extra fast view mode.
if (!$isediting) {
if (!empty($modinfo->sections[0])) {
foreach($modinfo->sections[0] as $cmid) {
......@@ -66,18 +71,21 @@ class block_social_activities extends block_list {
}
if (!$cm->url) {
$content = $courserenderer->course_section_cm_text($cm);
$content = html_writer::div(
$cm->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
'contentwithoutlink'
);
$this->content->items[] = $content;
$this->content->icons[] = '';
} else {
$this->content->items[] = html_writer::div($courserenderer->course_section_cm_name($cm), 'activity');
$cmname = new $cmnameclass($format, $cm->get_section_info(), $cm, $isediting);
$this->content->items[] = html_writer::div($courserenderer->render($cmname), 'activity');
}
}
}
return $this->content;
}
// Slow & hacky editing mode.
$ismoving = ismoving($course->id);
$section = $modinfo->get_section_info(0);
......@@ -102,18 +110,28 @@ class block_social_activities extends block_list {
continue;
}
if (!$ismoving) {
$actions = course_get_cm_edit_actions($mod, -1);
// Prepend list of actions with the 'move' action.
$actions = array('move' => new action_menu_link_primary(
new moodle_url('/course/mod.php', array('sesskey' => sesskey(), 'copy' => $mod->id)),
new pix_icon('t/move', $strmove, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$controlmenu = new $controlmenuclass(
$format,
$mod->get_section_info(),
$mod,
['disableindentation' => true]
);
$menu = $controlmenu->get_action_menu();
// Add a move primary action.
$menu->add(
new action_menu_link_primary(
new moodle_url('/course/mod.php', ['sesskey' => sesskey(), 'copy' => $mod->id]),
new pix_icon('t/move', $strmove, 'moodle', ['class' => 'iconsmall', 'title' => '']),
$strmove
)) + $actions;
)
);
$editbuttons = html_writer::tag('div',
$courserenderer->course_section_cm_edit_actions($actions, $mod, array('donotenhance' => true)),
array('class' => 'buttons')
$courserenderer->render($controlmenu),
['class' => 'buttons']
);
} else {
$editbuttons = '';
......@@ -129,12 +147,15 @@ class block_social_activities extends block_list {
$this->content->icons[] = '';
}
if (!$mod->url) {
$content = $courserenderer->course_section_cm_text($mod);
$content = html_writer::div(
$mod->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
'contentwithoutlink'
);
$this->content->items[] = $content . $editbuttons;
$this->content->icons[] = '';
} else {
$this->content->items[] = html_writer::div($courserenderer->course_section_cm_name($mod), 'activity') .
$editbuttons;
$cmname = new $cmnameclass($format, $mod->get_section_info(), $mod, $isediting);
$this->content->items[] = html_writer::div($courserenderer->render($cmname), 'activity') . $editbuttons;
}
}
}
......
......@@ -31,6 +31,7 @@ use cm_info;
/**
* Class to prepare a course module name for display and in-place editing
*
* @deprecated since Moodle 4.0
* @package core_course
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
......@@ -51,6 +52,10 @@ class course_module_name extends \core\output\inplace_editable {
* @param array $displayoptions
*/
public function __construct(cm_info $cm, $editable, $displayoptions = array()) {
debugging(
'course_section_cm_list is deprecated. Use core_courseformat\\output\\local\\cm\\cmname instead',
DEBUG_DEVELOPER
);
$this->cm = $cm;
$this->displayoptions = $displayoptions;
$value = $cm->name;
......
......@@ -1109,7 +1109,7 @@ abstract class base {
}
/**
* Returns instance of output compornent used by this plugin
* Returns instance of output component used by this plugin
*
* @throws coding_exception if the format class does not extends the original core one.
* @param string $outputname the element to render (section, activity...)
......
......@@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_courseformat\output\local\content\section;
namespace core_courseformat\output\local\content;
use cm_info;
use core\activity_dates;
......@@ -33,6 +33,7 @@ use renderable;
use section_info;
use stdClass;
use templatable;
use \core_availability\info_module;
/**
* Base class to render a course module inside a course format.
......@@ -55,6 +56,21 @@ class cm implements renderable, templatable {
/** @var array optional display options */
protected $displayoptions;
/** @var string activity link css classes */
protected $linkclasses = null;
/** @var string text css classes */
protected $textclasses = null;
/** @var string the activity name output class name */
protected $cmnameclass;
/** @var string the activity control menu class name */
protected $controlmenuclass;
/** @var string the activity availability class name */
protected $availabilityclass;
/**
* Constructor.
*
......@@ -68,6 +84,13 @@ class cm implements renderable, templatable {
$this->section = $section;
$this->mod = $mod;
$this->displayoptions = $displayoptions;
$this->load_classes();
// Get the necessary classes.
$this->cmnameclass = $format->get_output_classname('content\\cm\\cmname');
$this->controlmenuclass = $format->get_output_classname('content\\cm\\controlmenu');
$this->availabilityclass = $format->get_output_classname('content\\cm\\availability');
}
/**
......@@ -80,6 +103,7 @@ class cm implements renderable, templatable {
global $USER;
$format = $this->format;
$section = $this->section;
$mod = $this->mod;
$displayoptions = $this->displayoptions;
$course = $mod->get_course();
......@@ -94,6 +118,12 @@ class cm implements renderable, templatable {
$activitydates = activity_dates::get_dates_for_module($mod, $USER->id);
}
$displayoptions['linkclasses'] = $this->get_link_classes();
$displayoptions['textclasses'] = $this->get_text_classes();
// Grouping activity.
$groupinglabel = $mod->get_grouping_label($displayoptions['textclasses']);
$activityinfodata = null;
// - There are activity dates to be shown; or
// - Completion info needs to be displayed
......@@ -107,13 +137,32 @@ class cm implements renderable, templatable {
$activityinfodata = $activityinfo->export_for_template($output);
}
// Mod inplace name editable.
$cmname = new $this->cmnameclass(
$format,
$this->section,
$mod,
$format->show_editor(),
$this->displayoptions
);
// Mod availability.
$availability = new $this->availabilityclass(
$format,
$this->section,
$mod,
$this->displayoptions
);
$data = (object)[
'cmname' => $output->course_section_cm_name($mod, $displayoptions),
'cmname' => $cmname->export_for_template($output),
'grouping' => $groupinglabel,
'afterlink' => $mod->afterlink,
'altcontent' => $output->course_section_cm_text($mod, $displayoptions),
'availability' => $output->course_section_cm_availability($mod, $displayoptions),
'altcontent' => $mod->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
'modavailability' => $availability->export_for_template($output),
'url' => $mod->url,
'activityinfo' => $activityinfodata,
'textclasses' => $displayoptions['textclasses'],
];
if (!empty($mod->indent)) {
......@@ -131,22 +180,80 @@ class cm implements renderable, templatable {
}
$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;
}
$controlmenu = new $this->controlmenuclass(
$format,
$this->section,
$mod,
$this->displayoptions
);
$data->controlmenu = $controlmenu->export_for_template($output);
// Move and select options.
$data->moveicon = course_get_cm_move($mod, $returnsection);
}
if (!empty($data->extras)) {
$data->hasextras = true;
return $data;
}
return $data;
/**
* Returns the CSS classes for the activity name/content
*
* For items which are hidden, unavailable or stealth but should be displayed
* to current user ($mod->is_visible_on_course_page()), we show those as dimmed.
* Students will also see as dimmed activities names that are not yet available
* but should still be displayed (without link) with availability info.
*/
protected function load_classes() {
$mod = $this->mod;
$linkclasses = '';
$textclasses = '';
if ($mod->uservisible) {
$info = new info_module($mod);
$conditionalhidden = !$info->is_available_for_all();
$accessiblebutdim = (!$mod->visible || $conditionalhidden) &&
has_capability('moodle/course:viewhiddenactivities', $mod->context);
if ($accessiblebutdim) {
$linkclasses .= ' dimmed';
$textclasses .= ' dimmed_text';
if ($conditionalhidden) {
$linkclasses .= ' conditionalhidden';
$textclasses .= ' conditionalhidden';
}
}
if ($mod->is_stealth()) {
// Stealth activity is the one that is not visible on course page.
// It still may be displayed to the users who can manage it.
$linkclasses .= ' stealth';
$textclasses .= ' stealth';
}
} else {
$linkclasses .= ' dimmed';
$textclasses .= ' dimmed dimmed_text';
}
$this->linkclasses = $linkclasses;
$this->textclasses = $textclasses;
}
/**
* Get the activity link classes.
*
* @return string the activity link classes.
*/
public function get_link_classes(): string {
return $this->linkclasses;
}
/**
* Get the activity text/description classes.
*
* @return string the activity text classes.
*/
public function get_text_classes(): string {
return $this->textclasses;
}
}
<?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 availability information.
*
* @package core_courseformat
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_courseformat\output\local\content\cm;
use core_courseformat\output\local\content\section\availability as section_avalability;
use cm_info;
use core_courseformat\base as course_format;
use section_info;
use stdClass;
use core_availability\info_module;
use core_availability\info;
/**
* Base class to render a course module availability inside a course format.
*
* @package core_courseformat
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class availability extends section_avalability {
/** @var course_format the course format */
protected $format;
/** @var section_info the section object */
protected $section;
/** @var cm_info the course module instance */
protected $mod;
/** @var array optional display options */
protected $displayoptions;
/**
* 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;
}
/**
* 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 {
$data = (object)[
'info' => $this->get_info($output),
];
if (!empty($data->info)) {
$data->hasmodavailability = true;
}
return $data;
}
/**
* Get the availability HTML form the course renderer.
*
* @param \renderer_base $output typically, the renderer that's calling this function
* @return string the availability HTML
*/
protected function get_info(\renderer_base $output): array {
global $CFG;
$format = $this->format;
$mod = $this->mod;
$section = $this->section;
$info = [];
if (!$mod->is_visible_on_course_page()) {
// Nothing to be displayed to the user.
return $info;
}
if (!$mod->uservisible) {
// This is a student who is not allowed to see the module but might be allowed
// to see availability info (i.e. "Available from ...").
if (!empty($mod->availableinfo)) {
$formattedinfo = \core_availability\info::format_info(
$mod->availableinfo,
$mod->get_course()
);
$info[] = $this->availability_info($formattedinfo, 'isrestricted');
}
return $info;
}
// This is a teacher who is allowed to see module but still should see the
// information that module is not available to all/some students.
$modcontext = $mod->context;
$canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
if ($canviewhidden && !$mod->visible) {
// This module is hidden but current user has capability to see it.
// Do not display the availability info if the whole section is hidden.
if ($section->visible) {
$info[] = $this->availability_info(get_string('hiddenfromstudents'), 'ishidden');
}
} else if ($mod->is_stealth()) {
// This module is available but is normally not displayed on the course page
// (this user can see it because they can manage it).
$info[] = $this->availability_info(get_string('hiddenoncoursepage'), 'isstealth');
}
if ($canviewhidden && !empty($CFG->enableavailability)) {
// Display information about conditional availability.
// Don't add availability information if user is not editing and activity is hidden.
if ($mod->visible || $format->show_editor()) {
$hidinfoclass = 'isrestricted isfullinfo';
if (!$mod->visible) {
$hidinfoclass .= ' hide';
}
$ci = new info_module($mod);
$fullinfo = $ci->get_full_information();
if ($fullinfo) {
$formattedinfo = info::format_info(
$fullinfo,
$mod->get_course()
);
$info[] = $this->availability_info($formattedinfo, $hidinfoclass);