Skip to content

Commit

Permalink
MUA500 course: new overview page
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranrecio committed Dec 3, 2024
1 parent d165d22 commit c181d5c
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 0 deletions.
100 changes: 100 additions & 0 deletions course/overview.php
Original file line number Diff line number Diff line change
@@ -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/>.

/**
* TODO describe file overview
*
* @package core_course
* @copyright 2024 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use core\url;

require_once('../config.php');
require_once('lib.php');
require_once($CFG->libdir . '/completionlib.php');

$courseid = required_param('id', PARAM_INT);

$PAGE->set_url('/course/overview.php', ['id' => $courseid]);

$course = get_course($courseid);

$context = context_course::instance($course->id, MUST_EXIST);

require_login($course);


$modinfo = get_fast_modinfo($course);
$modfullnames = [];
$archetypes = [];

foreach ($modinfo->cms as $cm) {
// Exclude activities that aren't visible or have no view link (e.g. label). Account for folder being displayed inline.
if (!$cm->uservisible || (!$cm->has_view() && strcmp($cm->modname, 'folder') !== 0)) {
continue;
}
if (array_key_exists($cm->modname, $modfullnames)) {
continue;
}
if (!array_key_exists($cm->modname, $archetypes)) {
$archetypes[$cm->modname] = plugin_supports('mod', $cm->modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
}
if ($archetypes[$cm->modname] == MOD_ARCHETYPE_RESOURCE) {
if (!array_key_exists('resources', $modfullnames)) {
$modfullnames['resources'] = get_string('resources');
}
} else {
$modfullnames[$cm->modname] = $cm->modplural;
}
}

core_collator::asort($modfullnames);

$elements = [];
foreach ($modfullnames as $modname => $modfullname) {
if ($modname === 'resources') {
$elements[] = (object)[
'overviewurl' => new url('/course/resources.php', ['id' => $course->id, 'forcedembedlayout' => 1]),
'icon' => $OUTPUT->pix_icon('monologo', '', 'mod_page', ['class' => 'icon']),
'name' => $modfullname,
'shortname' => 'resources',
];
} else {
$elements[] = (object)[
'overviewurl' => new url('/mod/' . $modname . '/index.php', ['id' => $course->id, 'forcedembedlayout' => 1]),
'icon' => $OUTPUT->image_icon('monologo', $modfullname, $modname),
'name' => $modfullname,
'shortname' => $modname,
];
}
}

$PAGE->set_pagelayout('incourse');

$output = $PAGE->get_renderer('format_' . $course->format);

$PAGE->set_title('Course overview');
$PAGE->set_heading('Course overview');
echo $output->header();

$data = (object) [
'elements' => $elements,
];
echo $output->render_from_template('core_course/local/overview', $data);

echo $OUTPUT->footer();
126 changes: 126 additions & 0 deletions course/templates/local/overview.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{{!
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/>.
}}
{{!
@template core_course/local/overview
TODO describe template overview
Example context (json):
{
"elements": [
{
"name": "Activity 1",
"shortname": "activity1",
"icon": "<i class='icon fa fa-graduation-cap'></i>",
"overviewurl": "https://moodle.org"
},
{
"name": "Activity 2",
"shortname": "activity2",
"icon": "<i class='icon fa fa-graduation-cap'></i>",
"overviewurl": "https://moodle.org"
}
]
}
}}
<div class="course-overview-content">
<div id="accordion">
{{#elements}}
<div class="card mb-3">
<div class="card-header border-0" id="heading{{shortname}}" data-mdl-region="overviewsection">
<h5 class="mb-0">
<button
class="btn btn-link"
data-toggle="collapse"
data-target="#collapse{{shortname}}"
aria-expanded="false"
aria-controls="collapse{{shortname}}"
>
{{{icon}}} {{{name}}}
</button>
</h5>
</div>

<div
id="collapse{{shortname}}"
class="collapse"
aria-labelledby="heading{{shortname}}"
>
<div class="card-body">
<div class="alert alert-warning" role="alert">
<div class="font-weight-bold">
This activity hasn't been migrated to the new format yet
</div>
<div>
Indicates that a specific activity is still using the old format and has not yet
transitioned to the updated version.
</div>
</div>
<iframe
class='w-100 alert alert-secondary alert-block fade in'
src='{{{overviewurl}}}'
width='560'
height='250'
frameborder='0'
style="min-height: 250px;"
allowfullscreen
></iframe>
</div>
</div>
</div>
{{/elements}}
</div>
</div>
{{#js}}
function executeCode() {
const iframes = document.querySelectorAll("iframe");
iframes.forEach(function(iframe) {
iframe.onload = function() {
// Auto scale iframes.
iframe.style.height = (iframe.contentWindow.document.body.scrollHeight + 30) + 'px';
// Add base target _parent to the iframes to force iframe links to refresh the page.
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
const baseElement = iframeDoc.createElement('base');
baseElement.target = '_parent';
iframeDoc.head.appendChild(baseElement);
};
});
document.querySelector('#accordion').addEventListener('click', function(event) {
const closestSection = event.target.closest('[data-mdl-region="overviewsection"]');
if (!closestSection) {
return;
}
const button = event.target;
const targetId = button.getAttribute('data-target');
const iframe = document.querySelector(targetId + ' iframe');
if (!iframe) {
return;
}
iframe.style.visibility = 'hidden';
setTimeout(function() {
iframe.style.height = (iframe.contentWindow.document.body.scrollHeight + 30) + 'px';
iframe.style.visibility = 'visible';
}, 250);
});
}

if (document.readyState === 'loading') {
document.addEventListener("DOMContentLoaded", executeCode);
} else {
executeCode();
}
{{/js}}

0 comments on commit c181d5c

Please sign in to comment.