General developer forum

require_login($course) not working in local plugin

 
Raymond
require_login($course) not working in local plugin
 

Hi guys,

I have a local plugin which handles a mandatory 'survey' for students enrolled into a specific course, among other things. Everything is working in this plugin except for when I try to use require_login($course) in one of the files.

Here is what I mean.

In lib.php, I have the following code:

defined('MOODLE_INTERNAL') || die;


function local_mbaentrepreneurship_extend_settings_navigation($settingsnav, $context) {
    global $CFG, $PAGE;
    // Only add this settings item on non-site course pages.
    if (!$PAGE->course or $PAGE->course->id == 1) {
        return;
    }
    // Only let users with the appropriate capability see this settings item.
    if (!has_capability('moodle/backup:backupcourse', context_course::instance($PAGE->course->id))) {
        // return;
    }
    if ($settingnode = $settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) {
        $mbaentrepreneurshipsettings = get_string('mbaentrepreneurshipsettings', 'local_mbaentrepreneurship');
        $url = new moodle_url('/local/mbaentrepreneurship/data.php', array('id' => $PAGE->course->id));
        if ($PAGE->course->id == 63) { // this is the module that I'm targeting
            $mbaentrepreneurship = navigation_node::create(
                            $mbaentrepreneurshipsettings, $url, navigation_node::NODETYPE_LEAF, 'mbaentrepreneurship', 'mbaentrepreneurship', new pix_icon('t/addcontact', $mbaentrepreneurshipsettings)
            );
            if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
                $mbaentrepreneurshipsettings->make_active();
            }
            $settingnode->add_node($mbaentrepreneurship);
        }
    }
}

I require this file in /local/mbaentrepreneurship/index.php, and then do this:

$courseid = optional_param('courseid', 0, PARAM_INT);
    $userid = optional_param('userid', 0, PARAM_INT);
    if ($courseid !== 63) {
        // throw an exception here
        throw new moodle_exception(' Invalid course has been passed to the page. Please make sure that you\'re on an MBA'
        . ' Entrepreneurship page.');
    }
    $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
    $student = $DB->get_record('user', array('id' => $userid));
    $context = context_course::instance($course->id, MUST_EXIST);
    require_login($course);
    if (!is_siteadmin()) {
        // well, not gonna bother with this!
    }
    $PAGE->set_context($context);
    $PAGE->set_url('/local/mbaentrepreneurship', array(
        'courseid' => $course->id,
        'userid' => $student->id
    ));
    $PAGE->set_heading($course->fullname);
    $PAGE->set_pagelayout('course');
    $PAGE->set_title('MBA Entrepreneurship: Choice');
    $PAGE->navbar->add(fullname($student) . ': Make Your Assessment Choice', new moodle_url('/local/mbaentrepreneurship', array(
        'courseid' => $course->id,
        'userid' => $student->id
    )));
    $PAGE->requires->js('/local/mbaentrepreneurship/mbaentrepreneurship.js');


This code in index.php works like a charm! But then I have another file called data.php, in the same plugin, and I do a call to require_login($course), and then it throws an error. Here is the code in data.php:


require_once('../../config.php');
require_once 'lib.php';
$id = optional_param('id', 0, PARAM_INT);
$course = $DB->get_record('course', array('id' => $id));
$context = context_course::instance($course->id, MUST_EXIST);
require_login($course); // THROWS AN ERROR HERE
$PAGE->set_context($context);
$PAGE->set_url('/local/mbaentrepreneurship/data.php', array(
    'id' => $course->id
));
$PAGE->set_pagetype('course-view-' . $course->format);
$PAGE->set_title($course->shortname);
$PAGE->set_pagelayout('course');
$PAGE->navbar->add('Programme', new moodle_url('/local/programme', array(
    'id' => 3
)));
$PAGE->navbar->add('MBA', new moodle_url('/course/index.php', array(
    'categoryid' => 3
)));
$PAGE->navbar->add($course->fullname, new moodle_url('/course/view.php', array(
    'id' => 63
)));
echo $OUTPUT->header();


The above code in data.php throws an error below, and I've noticed that if I removed the require_login($course) part, the error goes away. In fact, if I do require_login() alone, the page works. And its something to do with the navigation node.

Can you please point me in the right direction so that I can get this right and working well.


Exception - Call to a member function make_active() on string

Debug info: 
Error code: generalexceptionmessage
Stack trace:
  • line 34 of /local/mbaentrepreneurship/lib.php: Error thrown
  • line 4777 of /lib/navigationlib.php: call to local_mbaentrepreneurship_extend_settings_navigation()
  • line 3525 of /lib/navigationlib.php: call to settings_navigation->load_local_plugin_settings()
  • line 719 of /lib/pagelib.php: call to settings_navigation->initialise()
  • line 768 of /lib/pagelib.php: call to moodle_page->magic_get_settingsnav()
  • line 135 of /blocks/settings/block_settings.php: call to moodle_page->__get()
  • line 288 of /blocks/moodleblock.class.php: call to block_settings->get_content()
  • line 230 of /blocks/moodleblock.class.php: call to block_base->formatted_contents()
  • line 973 of /lib/blocklib.php: call to block_base->get_content_for_output()
  • line 1025 of /lib/blocklib.php: call to block_manager->create_block_contents()
  • line 335 of /lib/blocklib.php: call to block_manager->ensure_content_created()
  • line 28 of /theme/archaius/layout/partials/header.php: call to block_manager->region_has_content()
  • line 21 of /theme/archaius/layout/general.php: call to include()
  • line 1016 of /lib/outputrenderers.php: call to include()
  • line 946 of /lib/outputrenderers.php: call to core_renderer->render_page_layout()
  • line 1800 of /lib/setuplib.php: call to core_renderer->header()
  • line 33 of /local/mbaentrepreneurship/data.php: call to bootstrap_renderer->__call()













 
Average of ratings: -
Raymond
Re: require_login($course) not working in local plugin
 

Can someone please provide some help on this.

 
Average of ratings: -
Picture of Sam Chaffee
Re: require_login($course) not working in local plugin
Group Core developers

Hi,

Hard to tell exactly what is going on without seeing some other code, but it looks like $mbaentrepreneurshipsettings is a string: 

$mbaentrepreneurshipsettings = get_string('mbaentrepreneurshipsettings', 'local_mbaentrepreneurship');

My guess is that when you hit index.php that make_active() code isn't even getting called. Also thinking that what is meant for that line is this:

$mbaentrepreneurship->make_active();

That uses the navigation_node rather than the string, which is the object with the make_active method.

Good luck!

 
Average of ratings: -