Skip to content

Instantly share code, notes, and snippets.

@frumbert
Created August 9, 2017 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frumbert/b4fbb8e6f9a23c7233128a1f51df02b7 to your computer and use it in GitHub Desktop.
Save frumbert/b4fbb8e6f9a23c7233128a1f51df02b7 to your computer and use it in GitHub Desktop.
Moodle "next" and "previous" activity code
<?php
// you would probably use this within your theme or block - anywhere inside a course context.
// a moodle function for returning the url adjacent to the current page in a course
// tested in Moodle 3 and higher.
// sourced from within https://moodle.org/plugins/block_navbuttons then refactored a bit to my needs
// lets say you are on http://mymoodle.com/mod/page/view.php?id=302
// and you want to know what the activity immediately after this one is. You'd ask
// $url = get_activity_url(course_get_url($course, $cm->sectionnum),"next")
// $url would be the location of the next visible activity in the sequence, or the course/section if there wasn't a next activity.
// licence: GPL3, as is Moodle
function get_activity_url($default, $direction = "next") {
global $PAGE, $CFG, $COURSE, $DB;
require_once($CFG->libdir . '/modinfolib.php');
$cmid = $PAGE->cm->id;
$modinfo = get_fast_modinfo($COURSE);
$context = context_course::instance($COURSE->id);
$sections = $DB->get_records('course_sections', array('course' => $COURSE->id), 'section', 'section,visible,summary');
$next = null;
$prev = null;
$firstcourse = null;
$firstsection = null;
$lastcourse = null;
$lastsection = null;
$sectionnum = -1;
$thissection = null;
$firstthissection = null;
$flag = false;
$sectionflag = false;
$previousmod = null;
foreach ($modinfo->cms as $mod) {
if ($mod->modname == 'label') {
continue;
}
$format = course_get_format($COURSE);
if (method_exists($format, 'get_last_section_number')) {
$numsections = $format->get_last_section_number();
} else {
$opts = course_get_format($COURSE)->get_format_options();
$numsections = isset($opts['numsections']) ? $opts['numsections'] : 0;
}
if ($numsections && $mod->sectionnum > $numsections) {
break;
}
if (!$mod->uservisible) {
continue;
}
if ($mod->sectionnum > 0 && $sectionnum != $mod->sectionnum) {
$thissection = $sections[$mod->sectionnum];
if ($thissection->visible || !$COURSE->hiddensections ||
has_capability('moodle/course:viewhiddensections', $context)
) {
$sectionnum = $mod->sectionnum;
$firstthissection = false;
if ($sectionflag) {
if ($flag) { // Flag means selected mod was the last in the section.
$lastsection = 'none';
} else {
$lastsection = $previousmod;
}
$sectionflag = false;
}
} else {
continue;
}
}
$thismod = (object)[
'link' => new moodle_url('/mod/'.$mod->modname.'/view.php', array('id' => $mod->id)),
'name' => strip_tags(format_string($mod->name, true))
];
if ($flag) { // Current mod is the 'next' mod.
$next = $thismod;
$flag = false;
}
if ($cmid == $mod->id) {
$flag = true;
$sectionflag = true;
$prev = $previousmod;
$firstsection = $firstthissection;
}
if (!$firstthissection) {
$firstthissection = $thismod;
}
if (!$firstcourse) {
$firstcourse = $thismod;
}
$previousmod = $thismod;
}
$result = $default;
switch ($direction) {
case "next":
if ($next) {
$result = $next->link;
}
break;
case "prev":
if ($prev) {
$result = $prev->link;
}
break;
case "first":
if ($firstcourse) {
$result = $firstcourse->link;
}
break;
case "last":
if ($lastcourse) {
$resut = $lastcourse->link;
}
case "firstsection":
if ($firstsection) {
$result = $firstsection->link;
}
break;
case "lastsection":
if ($lastsection) {
$result = $lastsection->link;
}
break;
case "home":
$result = new moodle_url('/course/view.php', array('id' => $COURSE->id));
break;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment