Undefined variable: PAGE

Undefined variable: PAGE

by Manuel Antonio Rivera Socarrás -
Number of replies: 9

Hi all, Moodle developers, I'm working on the lib.php and I have these lines,

$ Id = required_param ('id', PARAM_INT);
$ Pageid = optional_param ('pageid', '', PARAM_INT);
$ PAGE-> set_url ('/ mod / fm / import.php', array ('id' => $ id, 'pageid' => $ pageid));

And gives me the following error

Undefined variable: PAGE in /var/www/html/moodle/mod/fm/lib.php on line 21

Exception - Call to a member function set_url () on null
Debug info:
Error code: generalexceptionmessage
Stack trace:

    Line 21 of /mod/fm/lib.php: Error thrown

Could you help me?



Average of ratings: -
In reply to Manuel Antonio Rivera Socarrás

Re: Undefined variable: PAGE

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi.

Pretty sure you meant to use the global $PAGE variable. Not you need a leading "$".

By the way, in PHP, all of your variable names should start with "$".

mike

Average of ratings: Useful (1)
In reply to Mike Churchward

Re: Undefined variable: PAGE

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

To expand what Mike has said - you have a space between the $ and PAGE (and other variables)

Average of ratings: Useful (1)
In reply to Richard Oelmann

Re: Undefined variable: PAGE

by Manuel Antonio Rivera Socarrás -

This was a mistake copying the code but it is fine in the file, I show you a segment of the code and so I explain what I want to do.
  1. require_once($CFG->libdir . '/formslib.php');
    require_once("$CFG->libdir/filelib.php");

    $pageid = optional_param('pageid', '', PARAM_INT); // Page ID

    $data = new stdClass;
    $data->id = PAGE->cm->id;
    $data->pageid = $pageid;

    $mform = new lesson_import_form(null, array('formats'=>lesson_get_import_export_formats('import')));
    $mform->set_data($data);

    defined('MOODLE_INTERNAL') || die();
    define('fm_ULTIMATE_ANSWER', 42);

  2. function fm_add_instance(stdClass $fm, mod_fm_mod_form $mform = null)
    {
        global $DB;
        global $CFG, $PAGE;

        if (isset($_POST['from_RUNAH'])) {
            $fm_value = $_POST['ratingtime'];
            $fm_value_arr = explode("***", $fm_value);

            $fm->id_fed = $fm_value_arr['0'];
            $fm->name = $fm_value_arr['1'];
            $fm->object_name = $fm_value_arr['1'];
            $fm->timecreated = time();

        } elseif ($_POST['to_RUNAH']) {
  3.         if ($data = $mform->get_data()) {

                require_sesskey();

                $realfilename = $mform->get_new_filename('questionfile');
                //TODO: Leave all imported questions in Questionimport for now.
                $importfile = "{$CFG->tempdir}/questionimport/{$realfilename}";
                make_temp_directory('questionimport');
                if (!$result = $mform->save_file('questionfile', $importfile, true)) {
                    throw new moodle_exception('uploadproblem');
                }


            }}

    return $DB->insert_record('fm', $fm);
    }
What I want to do with all of this is to get the name of the file that I uploaded to a filemanager component that I created in the mod_form to save it, I've tried it in 3 different ways and it did not work for me, and the doc that gives me the Moodle API for files does not help me


In reply to Manuel Antonio Rivera Socarrás

Re: Undefined variable: PAGE

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Your original question was asking for help on why you got the error message:

Undefined variable: PAGE in /var/www/html/moodle/mod/fm/lib.php on line 21

Exception - Call to a member function set_url () on null
Debug info:
Error code: generalexceptionmessage
Stack trace:

    Line 21 of /mod/fm/lib.php: Error thrown
That was due to the missing "$" on your PAGE variable. If you are saying the "$" is there, then what its telling you is that it does not have $PAGE defined.

If this is a main level script, you will need to  make sure the script is including the root "config.php" file, so that have globals like $PAGE defined. If this is in a function, then you need to ensure that $PAGE is declared as a global variable.

mike

In reply to Manuel Antonio Rivera Socarrás

Re: Undefined variable: PAGE

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Well, in your original example you had

$ PAGE-> set_url

Which should be - without the spaces...

$PAGE->set_url

But that line isn't even in your later example - but there you have

$data->id = PAGE->cm->id;

with no $ at all - $PAGE->cm->id


Also, your function

function fm_add_instance(stdClass $fm, mod_fm_mod_form $mform = null)

As far as I am aware (and based on checking php.net) arguments passed to a function need to be comma delimited - http://php.net/manual/en/functions.arguments.php


I'd suggest a good place to start would be a straightforward check through your code for standards like that - the error messages you are getting are pointing you at the lines with the errors, so it shouldn't be too hard to pick up.


In reply to Mike Churchward

Re: Undefined variable: PAGE

by Manuel Antonio Rivera Socarrás -

Mike, thanks by answer, but , I remove "$" and I got this "Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)"

What can I do?

In reply to Manuel Antonio Rivera Socarrás

Re: Undefined variable: PAGE

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Is it inside a function? If so, you may have missed the global $PAGE part, so you might want something like this:


function my_function() {
    global $PAGE;
    $id = required_param('id', PARAM_INT);
    $pageid = optional_param('pageid', '', PARAM_INT);
    $PAGE->set_url('/mod/fm/import.php', array('id' => $id, 'pageid' => $pageid));
}

Alternatively, is the file included from somewhere else? If not, you might need to add require_once(__DIR__.'/../../config.php') to the start.

In reply to Davo Smith

Re: Undefined variable: PAGE

by Manuel Antonio Rivera Socarrás -

Not Davo, I'm not in a function, look at the second intervention in the post, and I show a little more code, I think it is better understood what I want to do