Skip to content

Commit

Permalink
MDL-74800 core_courseformat: fix accessibility checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranrecio committed Aug 8, 2022
1 parent 39af611 commit dc63d64
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 181 deletions.
4 changes: 2 additions & 2 deletions blocks/site_main_menu/block_site_main_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function get_content() {
'contentwithoutlink activity-item activity'
);
} else {
$cmname = new $cmnameclass($format, $cm->get_section_info(), $cm, $isediting);
$cmname = new $cmnameclass($format, $cm->get_section_info(), $cm);
$activitybasis = html_writer::div(
$indent . $courserenderer->render($cmname),
'activity-basis d-flex align-items-center');
Expand Down Expand Up @@ -214,7 +214,7 @@ function get_content() {
'contentwithoutlink activity-item activity'
);
} else {
$cmname = new $cmnameclass($format, $mod->get_section_info(), $mod, $isediting);
$cmname = new $cmnameclass($format, $mod->get_section_info(), $mod);
$activitybasis = html_writer::div(
$moveaction .
$indent .
Expand Down
4 changes: 2 additions & 2 deletions blocks/social_activities/block_social_activities.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function get_content() {
$this->content->items[] = $content;
$this->content->icons[] = '';
} else {
$cmname = new $cmnameclass($format, $cm->get_section_info(), $cm, $isediting);
$cmname = new $cmnameclass($format, $cm->get_section_info(), $cm);
$activitybasis = html_writer::div(
$courserenderer->render($cmname),
'activity-basis d-flex align-items-center');
Expand Down Expand Up @@ -205,7 +205,7 @@ function get_content() {
$this->content->items[] = $content;
$this->content->icons[] = '';
} else {
$cmname = new $cmnameclass($format, $mod->get_section_info(), $mod, $isediting);
$cmname = new $cmnameclass($format, $mod->get_section_info(), $mod);
$activitybasis = html_writer::div(
$courserenderer->render($cmname) .
$editbuttons,
Expand Down
32 changes: 17 additions & 15 deletions course/format/classes/output/local/content/cm.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ class cm implements named_templatable, renderable {
/** @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;

Expand All @@ -88,10 +82,8 @@ public function __construct(course_format $format, section_info $section, cm_inf
$this->mod = $mod;

// Add extra display options.
$this->load_classes();
$displayoptions['linkclasses'] = $this->get_link_classes();
$displayoptions['textclasses'] = $this->get_text_classes();
$this->displayoptions = $displayoptions;
$this->load_classes();

// Get the necessary classes.
$this->cmnameclass = $format->get_output_classname('content\\cm\\cmname');
Expand Down Expand Up @@ -148,11 +140,11 @@ protected function add_cm_name_data(stdClass &$data, renderer_base $output): boo
$this->format,
$this->section,
$this->mod,
$this->format->show_editor(),
null,
$this->displayoptions
);
$data->cmname = $cmname->export_for_template($output);
$data->hasname = !empty($data->cmname['displayvalue']);
$data->hasname = $cmname->has_name();
return $data->hasname;
}

Expand Down Expand Up @@ -323,8 +315,9 @@ protected function load_classes() {
$textclasses .= ' conditionalhidden';
}
}
$this->linkclasses = $linkclasses;
$this->textclasses = $textclasses;
$this->displayoptions['linkclasses'] = $linkclasses;
$this->displayoptions['textclasses'] = $textclasses;
$this->displayoptions['onclick'] = htmlspecialchars_decode($mod->onclick, ENT_QUOTES);;
}

/**
Expand All @@ -333,7 +326,7 @@ protected function load_classes() {
* @return string the activity link classes.
*/
public function get_link_classes(): string {
return $this->linkclasses;
return $this->displayoptions['linkclasses'] ?? '';
}

/**
Expand All @@ -342,6 +335,15 @@ public function get_link_classes(): string {
* @return string the activity text classes.
*/
public function get_text_classes(): string {
return $this->textclasses;
return $this->displayoptions['textclasses'] ?? '';
}

/**
* Get the activity onclick code.
*
* @return string the activity onclick.
*/
public function get_onclick_code(): string {
return $this->displayoptions['onclick'];
}
}
94 changes: 34 additions & 60 deletions course/format/classes/output/local/content/cm/cmname.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@
namespace core_courseformat\output\local\content\cm;

use cm_info;
use context_module;
use core\output\inplace_editable;
use core\output\named_templatable;
use core_courseformat\base as course_format;
use core_courseformat\output\local\courseformat_named_templatable;
use external_api;
use lang_string;
use renderable;
use section_info;
use stdClass;
Expand All @@ -43,7 +39,7 @@
* @copyright 2020 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cmname extends inplace_editable implements named_templatable, renderable {
class cmname implements named_templatable, renderable {

use courseformat_named_templatable;

Expand All @@ -56,9 +52,6 @@ class cmname extends inplace_editable implements named_templatable, renderable {
/** @var cm_info the course module instance */
protected $mod;

/** @var editable if the title is editable */
protected $editable;

/** @var array optional display options */
protected $displayoptions;

Expand All @@ -71,40 +64,23 @@ class cmname extends inplace_editable implements named_templatable, renderable {
* @param course_format $format the course format
* @param section_info $section the section info
* @param cm_info $mod the course module ionfo
* @param bool $editable if it is editable
* @param bool|null $editable if it is editable (not used)
* @param array $displayoptions optional extra display options
*/
public function __construct(
course_format $format,
section_info $section,
cm_info $mod,
bool $editable,
?bool $editable = null,
array $displayoptions = []
) {
$this->format = $format;
$this->section = $section;
$this->mod = $mod;
$this->displayoptions = $displayoptions;

$this->editable = $editable && has_capability(
'moodle/course:manageactivities',
$mod->context
);

// Get the necessary classes.
$this->titleclass = $format->get_output_classname('content\\cm\\title');

// Setup inplace editable.
parent::__construct(
'core_course',
'activityname',
$mod->id,
$this->editable,
$mod->name,
$mod->name,
new lang_string('edittitle'),
new lang_string('newactivityname', '', $mod->get_formatted_name())
);
}

/**
Expand All @@ -114,51 +90,49 @@ public function __construct(
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): array {
global $PAGE;
$mod = $this->mod;
$displayoptions = $this->displayoptions;

// Inplace editable uses core renderer by default. However, course elements require
// the format specific renderer.
$courseoutput = $this->format->get_renderer($PAGE);
if (!$this->has_name()) {
// Nothing to be displayed to the user.
return [];
}

$data = [
'url' => $mod->url,
'icon' => $mod->get_icon_url(),
'modname' => $mod->modname,
'pluginname' => get_string('pluginname', 'mod_' . $mod->modname),
'textclasses' => $displayoptions['textclasses'] ?? '',
'purpose' => plugin_supports('mod', $mod->modname, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER),
'activityname' => $this->get_title_data($output),
];

return $data;
}

// Inplace editable uses pre-rendered elements and does not allow line beaks in the UI value.
/**
* Get the title data.
*
* @param \renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
protected function get_title_data(\renderer_base $output): array {
$title = new $this->titleclass(
$this->format,
$this->section,
$this->mod,
$this->displayoptions
);
$this->displayvalue = str_replace("\n", "", $courseoutput->render($title));

if (trim($this->displayvalue) == '') {
$this->editable = false;
}
$data = parent::export_for_template($output);

return $data;
return (array) $title->export_for_template($output);
}

/**
* Updates course module name
* Return if the activity has a visible name.
*
* @param int $itemid course module id
* @param string $newvalue new name
* @return static
* @return bool if the title is visible.
*/
public static function update($itemid, $newvalue) {
$context = context_module::instance($itemid);
// Check access.
external_api::validate_context($context);
require_capability('moodle/course:manageactivities', $context);

// Trim module name and Update value.
set_coursemodule_name($itemid, trim($newvalue));
$coursemodulerecord = get_coursemodule_from_id('', $itemid, 0, false, MUST_EXIST);
// Return instance.
$modinfo = get_fast_modinfo($coursemodulerecord->course);
$cm = $modinfo->get_cm($itemid);
$section = $modinfo->get_section_info($cm->sectionnum);

$format = course_get_format($cm->course);
return new static($format, $section, $cm, true);
public function has_name(): bool {
return $this->mod->is_visible_on_course_page() && $this->mod->url;
}
}
Loading

0 comments on commit dc63d64

Please sign in to comment.