@@ -462,25 +462,32 @@ function moodleoverflow_pluginfile($course, $cm, $context, $filearea, $args, $fo
462
462
* Extends the settings navigation with the moodleoverflow settings.
463
463
*
464
464
* 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 .
466
466
*
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
469
473
*/
470
474
function moodleoverflow_extend_settings_navigation (settings_navigation $ settingsnav , ?navigation_node $ moodleoverflownode = null ) {
471
- global $ CFG , $ DB , $ PAGE , $ USER ;
475
+ global $ DB , $ USER ;
472
476
473
477
// 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 ]);
475
479
476
480
// 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 );
480
487
$ forcesubscribed = \mod_moodleoverflow \subscriptions::is_forcesubscribed ($ moodleoverflow );
481
488
$ subscdisabled = \mod_moodleoverflow \subscriptions::subscription_disabled ($ moodleoverflow );
482
489
$ cansubscribe = $ activeenrolled && (!$ subscdisabled || $ canmanage ) &&
483
- !($ forcesubscribed && has_capability ('mod/moodleoverflow:allowforcesubscribe ' , $ PAGE -> cm -> context ));
490
+ !($ forcesubscribed && has_capability ('mod/moodleoverflow:allowforcesubscribe ' , $ context ));
484
491
$ cantrack = \mod_moodleoverflow \readtracking::moodleoverflow_can_track_moodleoverflows ($ moodleoverflow );
485
492
486
493
// Display a link to the index.
@@ -502,7 +509,7 @@ function moodleoverflow_extend_settings_navigation(settings_navigation $settings
502
509
if ($ cansubscribe ) {
503
510
504
511
// 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 );
506
513
if ($ issubscribed ) {
507
514
$ linktext = get_string ('unsubscribe ' , 'moodleoverflow ' );
508
515
} else {
0 commit comments