Skip to content

Commit

Permalink
MDL-73683 core_courseformat: fix state hasavailability
Browse files Browse the repository at this point in the history
The course index lock icon depends on the state hasrestrictions
attributes on both section and course module. The lock icon
should be consistent with the content availability information
and it was not. With this patch the hasrestrictions attribute
uses a similar logic than the course content.
  • Loading branch information
ferranrecio committed Feb 15, 2022
1 parent d24a4ab commit c7a2e19
Show file tree
Hide file tree
Showing 4 changed files with 823 additions and 16 deletions.
9 changes: 7 additions & 2 deletions course/format/classes/output/local/state/cm.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ public function export_for_template(\renderer_base $output): stdClass {
// Check if restriction access are visible to the user.
$canviewhidden = has_capability('moodle/course:viewhiddenactivities', $cm->context);
if (!empty($CFG->enableavailability) && $canviewhidden) {
// Some users can see restrictions even if it does not apply to them.
$data->hascmrectrictions = !empty($cm->availableinfo);
if (has_capability('moodle/course:manageactivities', $cm->context)) {
// Course editors can see all restrictions.
$data->hascmrectrictions = !empty($cm->availability);
} else {
// Some users can see restrictions even if it does not apply to them.
$data->hascmrectrictions = !empty($cm->availableinfo);
}
} else {
$data->hascmrectrictions = !$data->accessvisible || !$cm->uservisible;
}
Expand Down
48 changes: 34 additions & 14 deletions course/format/classes/output/local/state/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace core_courseformat\output\local\state;

use core_availability\info_section;
use core_courseformat\base as course_format;
use section_info;
use renderable;
Expand Down Expand Up @@ -55,8 +56,6 @@ public function __construct(course_format $format, section_info $section) {
* @return array data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
global $CFG;

$format = $this->format;
$course = $format->get_course();
$section = $this->section;
Expand Down Expand Up @@ -87,20 +86,10 @@ public function export_for_template(\renderer_base $output): stdClass {
'sectionurl' => course_get_url($course, $section->section)->out(),
'current' => $format->is_section_current($section),
'indexcollapsed' => $indexcollapsed,
'contentcollapsed' => $contentcollapsed
'contentcollapsed' => $contentcollapsed,
'hasrestrictions' => $this->get_has_restrictions(),
];

// If the section availability restrictions must be displayed.
$canviewhidden = has_capability(
'moodle/course:viewhiddenactivities',
context_course::instance($course->id)
);
if (!empty($CFG->enableavailability) && $canviewhidden) {
$data->hasrestrictions = !empty($section->availableinfo);
} else {
$data->hasrestrictions = false;
}

if (empty($modinfo->sections[$section->section])) {
return $data;
}
Expand All @@ -114,4 +103,35 @@ public function export_for_template(\renderer_base $output): stdClass {

return $data;
}

/**
* Return if the section has a restrictions icon displayed or not.
*
* @return bool if the section has visible restrictions for the user.
*/
protected function get_has_restrictions(): bool {
global $CFG;

$section = $this->section;
$course = $this->format->get_course();
$context = context_course::instance($course->id);

// Hidden sections have no restriction indicator displayed.
if (!$section->visible || empty($CFG->enableavailability)) {
return false;
}
if (!has_capability('moodle/course:viewhiddenactivities', $context)) {
// If the section is visible but not user visible means it has some locking rule.
return empty($section->uservisible);
}

if (has_capability('moodle/course:manageactivities', $context)) {
// Course editors can see all restrictions if the section is visible.
$ci = new info_section($section);
return !empty($ci->get_full_information());
} else {
// Some users can see restrictions even if it does not apply to them.
return !empty($section->availableinfo);
}
}
}
Loading

0 comments on commit c7a2e19

Please sign in to comment.