Skip to content

Commit c42df20

Browse files
Fix for single Activity format (#198)
* use right page variable * fix phpdoc error in lib.php * fix nullable declaration for php8.4 * fix index.php codechecker error including config.php --------- Co-authored-by: Thomas Niedermaier <[email protected]>
1 parent 2fa7dc6 commit c42df20

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

index.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,15 @@
2121
* @copyright 2017 Kennet Winter <[email protected]>
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
24-
defined('MOODLE_INTERNAL') || die();
25-
26-
global $CFG, $DB, $PAGE, $USER, $SESSION, $OUTPUT;
27-
28-
// Require needed files.
29-
use core\context\course;
3024

3125
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
3226
global $CFG, $DB, $PAGE, $USER, $SESSION, $OUTPUT;
3327
require_once(dirname(__FILE__) . '/locallib.php');
3428
require_once($CFG->dirroot . '/course/lib.php');
3529

30+
// Require needed files.
31+
use core\context\course;
32+
3633
// Fetch submitted parameters.
3734
$id = required_param('id', PARAM_INT);
3835
$subscribe = optional_param('subscribe', null, PARAM_INT);

lib.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -462,25 +462,32 @@ function moodleoverflow_pluginfile($course, $cm, $context, $filearea, $args, $fo
462462
* Extends the settings navigation with the moodleoverflow settings.
463463
*
464464
* This function is called when the context for the page is a moodleoverflow module. This is not called by AJAX
465-
* so it is safe to rely on the $PAGE.
465+
* so it is safe to rely on the page variable.
466466
*
467-
* @param settings_navigation $settingsnav complete settings navigation tree
468-
* @param navigation_node|null $moodleoverflownode moodleoverflow administration node
467+
* @param settings_navigation $settingsnav complete settings navigation tree
468+
* @param navigation_node|null $moodleoverflownode moodleoverflow administration node
469+
* @throws \core\exception\moodle_exception
470+
* @throws coding_exception
471+
* @throws dml_exception
472+
* @throws moodle_exception
469473
*/
470474
function moodleoverflow_extend_settings_navigation(settings_navigation $settingsnav, ?navigation_node $moodleoverflownode = null) {
471-
global $CFG, $DB, $PAGE, $USER;
475+
global $DB, $USER;
472476

473477
// Retrieve the current moodle record.
474-
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $PAGE->cm->instance]);
478+
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $settingsnav->get_page()->cm->instance]);
475479

476480
// Check if the user can subscribe to the instance.
477-
$enrolled = is_enrolled($PAGE->cm->context, $USER, '', false);
478-
$activeenrolled = is_enrolled($PAGE->cm->context, $USER, '', true);
479-
$canmanage = has_capability('mod/moodleoverflow:managesubscriptions', $PAGE->cm->context);
481+
if (!$context = context_module::instance($settingsnav->get_page()->cm->id)) {
482+
throw new \moodle_exception('badcontext');
483+
}
484+
$enrolled = is_enrolled($context, $USER, '', false);
485+
$activeenrolled = is_enrolled($context, $USER, '', true);
486+
$canmanage = has_capability('mod/moodleoverflow:managesubscriptions', $context);
480487
$forcesubscribed = \mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow);
481488
$subscdisabled = \mod_moodleoverflow\subscriptions::subscription_disabled($moodleoverflow);
482489
$cansubscribe = $activeenrolled && (!$subscdisabled || $canmanage) &&
483-
!($forcesubscribed && has_capability('mod/moodleoverflow:allowforcesubscribe', $PAGE->cm->context));
490+
!($forcesubscribed && has_capability('mod/moodleoverflow:allowforcesubscribe', $context));
484491
$cantrack = \mod_moodleoverflow\readtracking::moodleoverflow_can_track_moodleoverflows($moodleoverflow);
485492

486493
// Display a link to the index.
@@ -502,7 +509,7 @@ function moodleoverflow_extend_settings_navigation(settings_navigation $settings
502509
if ($cansubscribe) {
503510

504511
// Choose the linktext depending on the current state of subscription.
505-
$issubscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, $PAGE->cm->context);
512+
$issubscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, $context);
506513
if ($issubscribed) {
507514
$linktext = get_string('unsubscribe', 'moodleoverflow');
508515
} else {

0 commit comments

Comments
 (0)