Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Antall svar: 20
Hi All:

Since I have upgraded to 1.7, my students are receiving the message: "Fatal error: Call to a member function on a non-object in [my server's path]/moodle/question/type/random/questiontype.php on line 242" when taking a quiz.

The error seems to occur upon switching to the second question when random questions are used to make the quiz. Making a new quiz does not solve the problem.

I looked at the code, but I have no idea what to do about it.

Thanks!

Carl
Gjennomsnittlig vurdering: -
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av Plugin developers
What types of question is the random question randomly selecting from?

Can you look in your database to see what is in the rows relating to this random question, and the students attempts at it?
Som svar til Tim Hunt

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Thanks for the reply.

I looked at the database, but I see nothing unusual. I have tried a few more trials, and I have discovered that the error is only happening when the quiz in in Adaptive mode and then only when at least one question has been submitted individually before the Submit All and Finish Button has been pressed!
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av Plugin developers
Probably the next thing to try is to replace the lines:

        return $QTYPES[$wrappedquestion->qtype]
->compare_responses($wrappedquestion, $state, $teststate);


(Lines 241 and 242 or question/type/random/questiontype.php) with:

$qtype = $QTYPES[$wrappedquestion->qtype];
if (!is_object($qtype)) {
echo '<p>Strange error with random questions.</p>';
print_object($wrappedquestion);
print_object($state);
print_object($teststate);
}
return $qtype->compare_responses($wrappedquestion, $state, $teststate);


and see if that reveals any useful information.
Som svar til Tim Hunt

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Hi Tim:

I appreciate your help more than you know! Below is an example of what I get back after your proposed code change. My path is changed to myserver in the last line of response.

Strange error with random questions.

stdClass Object
(
 [event] => 2
 [responses] => Array
 (
 [] => 3633
 )

 [timestamp] => 1163680632
 [options] => stdClass Object
 (
 [question] =>
 )

)
stdClass Object
(
 [id] => 179946
 [attempt] => 4037
 [originalquestion] => 0
 [seq_number] => 1
 [timestamp] => 1163680629
 [event] => 3
 [grade] => 1
 [raw_grade] => 1
 [penalty] => 1
 [sumpenalty] => 1
 [manualcomment] =>
 [question] => 4341
 [responses] => Array
 (
 [] => 3633
 )

 [changed] =>
 [options] => stdClass Object
 (
 [question] => stdClass Object
 (
 [id] => 1897
 [category] => 42
 [parent] => 0
 [name] => Unit tests are considered Assessment activities according to the grading system used in this class.
 [questiontext] => Unit tests are considered Assessment activities according to the grading system used in this class.
 [questiontextformat] => 1
 [image] =>
 [generalfeedback] =>
 [defaultgrade] => 1
 [penalty] => 0.1
 [qtype] => truefalse
 [length] => 1
 [stamp] => www.iwant2learn.net+050815133835+NZy8MS
 [version] => www.iwant2learn.net+060830225738+n1IcvC
 [hidden] => 0
 [options] => stdClass Object
 (
 [id] => 939
 [question] => 1897
 [trueanswer] => 3633
 [falseanswer] => 3634
 [answers] => Array
 (
 [3633] => stdClass Object
 (
 [question] => 1897
 [answer] => True
 [fraction] => 1
 [feedback] => You got it. Unit tests are considered Assessment activities according to the grading system used in this class.
 [id] => 3633
 )

 [3634] => stdClass Object
 (
 [question] => 1897
 [answer] => False
 [fraction] => 0
 [feedback] => Sorry. Unit tests are considered Assessment activities according to the grading system used in this class.
 [id] => 3634
 )

 )

 )

 [name_prefix] => resp4341_
 [maxgrade] => 1
 )

 )

 [last_graded] => stdClass Object
 (
 [id] => 179946
 [attempt] => 4037
 [originalquestion] => 0
 [seq_number] => 1
 [timestamp] => 1163680629
 [event] => 3
 [grade] => 1
 [raw_grade] => 1
 [penalty] => 1
 [sumpenalty] => 1
 [manualcomment] =>
 [question] => 4341
 [responses] => Array
 (
 [] => 3633
 )

 [changed] =>
 [options] => stdClass Object
 (
 [question] => stdClass Object
 (
 [id] => 1897
 [category] => 42
 [parent] => 0
 [name] => Unit tests are considered Assessment activities according to the grading system used in this class.
 [questiontext] => Unit tests are considered Assessment activities according to the grading system used in this class.
 [questiontextformat] => 1
 [image] =>
 [generalfeedback] =>
 [defaultgrade] => 1
 [penalty] => 0.1
 [qtype] => truefalse
 [length] => 1
 [stamp] => www.iwant2learn.net+050815133835+NZy8MS
 [version] => www.iwant2learn.net+060830225738+n1IcvC
 [hidden] => 0
 [options] => stdClass Object
 (
 [id] => 939
 [question] => 1897
 [trueanswer] => 3633
 [falseanswer] => 3634
 [answers] => Array
 (
 [3633] => stdClass Object
 (
 [question] => 1897
 [answer] => True
 [fraction] => 1
 [feedback] => You got it. Unit tests are considered Assessment activities according to the grading system used in this class.
 [id] => 3633
 )

 [3634] => stdClass Object
 (
 [question] => 1897
 [answer] => False
 [fraction] => 0
 [feedback] => Sorry. Unit tests are considered Assessment activities according to the grading system used in this class.
 [id] => 3634
 )

 )

 )

 [name_prefix] => resp4341_
 [maxgrade] => 1
 )

 )

 )

)

Fatal error: Call to a member function on a non-object in /home/myserver/public_html/moodle/question/type/random/questiontype.php on line 249
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av Plugin developers
Thank you very much for trying that, and sorry for not replying sooner, I had too many meetings today.

What your testing shows is that the random question is not getting initialised properly, as it should have been by the time the error you see occurs.

Unfortunately, we don't yet have enough information to know exactly what is happening. Now we need to try replacing the debug code I gave you before with:

if (!is_object($qtype)) {
echo '<p>Strange error with random questions. Wrapped question:</p>';
print_object($wrappedquestion);
echo '<p>Strange error with random questions. Wrapped question:</p>';
debug_print_backtrace();
}


That will only work on PHP5. If you are on PHP4, use

var_dump(debug_backtrace()); instead of debug_print_backtrace();

Hopefully, if you try that, it will give us enough information to work out what is going wrong.
Som svar til Tim Hunt

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Thanks again Tim!

My ISP runs php 4.4.2

Replacing lines 241 & 242 in questiontype.php with this code...
var_dump(debug_backtrace()); instead of debug_print_backtrace();

I get this response... Parse error: syntax error, unexpected '&', expecting '(' in /home/myserver/public_html/moodle/question/type/random/questiontype.php on line 33

I am not good at php, but line 33 looks corrupt to me!

function save_question_opt祤Ļns��qu

What should it be?
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Hi Tim:

FYI. If fix line 33 from my first install of 1.7 and replace 241 & 242 with the original, and I am right back to where I started!

33 function save_question_options($question) {
241 return $QTYPES[$wrappedquestion->qtype]
242 ->compare_responses($wrappedquestion, $state, $teststate);
Right back to:
Fatal error: Call to a member function on a non-object in /home/myserver/public_html/moodle/question/type/random/questiontype.php on line 242


If I fix line 33 and replace lines 241 and 242 with your substitue code for php5, I get this...

33 function save_question_options($question) {
241-242 replaced with:
if (!is_object($qtype)) {
 echo '<p>Strange error with random questions. Wrapped question:</p>';
 print_object($wrappedquestion);
 echo '<p>Strange error with random questions. Wrapped question:</p>';
 debug_print_backtrace();
}

Response...

Notice: Undefined variable: qtype in /home/myserver/public_html/moodle/question/type/random/questiontype.php on line 241

Strange error with random questions. Wrapped question:

Strange error with random questions. Wrapped question:


Fatal error: Call to undefined function: debug_print_backtrace() in /home/myserver/public_html/moodle/question/type/random/questiontype.php on line 249


If I fix 33 and replace 241 and 242 with your substitute for php4, I get this...

33 function save_question_options($question) {
241-242 var_dump(debug_backtrace()); instead of debug_print_backtrace();

Result:
Parse error: syntax error, unexpected T_STRING in /home/myserver/public_html/moodle/question/type/random/questiontype.php on line 241


Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Hi All:

I am still unable to track down the cause of problem in this thread.

I have also noticed that the first time I save an new "Add a Resource... Link to a File or Website" with the box checked to open the resource in a new window the resource does not open in a new window after closing the create window. When I edit the new resource, it appears that my initial choice to open the resource in a new window was not saved. If I again choose to open the resource in a new window and re-save, it works properly after the second save. Could the two issues be related?

Any assistance - suggestions - would be greatly appreciated!

Thanks!

Carl Keller
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Jean-Michel Védrine -
What Tim was saying is that you should use:
var_dump(debug_backtrace());
if your server is running PHP4
and use:
debug_print_backtrace();
if your server is running PHP5.
But you should not type the "instead of debug_print_backtrace();" part of the line
Som svar til Jean-Michel Védrine

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Thanks Jean-Michel:

I inserted the modifed code, and I got a large amount of code back. It starts with:

Strange error with random questions. Wrapped question:

Strange error with random questions. Wrapped question:
array(2) { [0]=> array(6) { ["file"]=> string(51) "/home/myhomedirectory/public_html/moodle/lib/questionlib.php" ["line"]=> int(993) ["function"]=> string(17)...

I have attached a copy in .txt format to this post.

Any assistance you might offer will be greatly appreciated!
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av Plugin developers
All that output is unreadable withought linebreaks, which seem to have got lost.

Can you change it to

echo '<pre>';
var_dump(debug_backtrace());
echo '</pre>';

or

echo '<pre>';
debug_print_backtrace();
echo '</pre>';

Som svar til Tim Hunt

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Thanks for the reply Tim:

I have inserted the code

echo '<pre>';
var_dump(debug_backtrace());
echo '</pre>';

for the line
var_dump(debug_backtrace());

The result is attached.
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av Plugin developers
Brilliant, now we have the information we need. (Crosses fingers.)

What all the junk means is that (remember we are looking at Moodle 1.7+):

1. It started in mod/quiz/attempt.php, and got as far as line 374, where it called question_process_responses() from questionlib.php. (And it tells us exactly what arguments got passed to the function).

2. Then in questionlib.php, it got to line 993, where it called the method compare_responses() from the class random_qtype which is in question/type/random/questiontype.php. And again it tells us what arguments were passed.

3. Then the error message tell that in that method, the object $wrappedquestion = &$state->options->question is not defined.

Looking at the arguments passed, we see the problem. $state, the second argument is only

 
&object(stdClass)(3) {
["event"]=>
string(1) "2"
["responses"]=>
array(1) {
[""]=>
string(5) "11485"
}
["timestamp"]=>
int(1165865260)
}


And there is no $state->options. Instead it should be using $teststate->options, which does exist.

Checking the documentation for the compare_responses() function in the base class (lines 836-855 of question/type/questiontpe.php) we see that the documentation is actually wrong (sigh). It says that we should only use $teststate->responses, whereas, if we look at the code in questionlib.php, we see that it is safe to use $teststate->options.

Finally I check all the places where compare_responses is called, to make sure that it always gets the state and teststate parameters the right way round, which it does.

So to fix the problem I fix that comment question/type/questiontpe.php, and fix compare_responses() in question/type/random/questiontype.php, and make that change in all of 1.6.x, 1.7.x and 1.8dev.

Oh, and I create a bug report in the tracker (MDL-7896), because it is good practice to record the reson for every change. Then I marked it fixed.


P.S. I wrote out the long explanation since I was staring at the debug output, and could not see what was wrong. It was only after I typed the explanation that I saw the problem. Then I decided to finish the forum post in case anyone was interested.
Som svar til Tim Hunt

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Daniel Hinostroza -

Thank you Tim,

The verbose explanation has been quite helpful and above all reassuring.  I'm waiting for the next build (about 45 min to go) in hopes of patching the files.  If it doesn't solve the issue, I'll go through all the steps you have previously recommended and post the bug with greater detail.

All the very best,

Daniel

Som svar til Tim Hunt

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -

TIM!!!

Upgraded.

Tried it.

Works great!

Your the best!

Thanks for all of your effort on this problem!

Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av Plugin developers
And thank you for your help. And your patience when I ignored this thread for a few weeks.
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Peter Richardson -
We are also getting the same error. This is what I know.

1) If we do not use the adaptive mode, the error does not appear, but the quiz freezes.

2) It does not matter if the quiz is a pre-1.7 quiz, if the quiz is new and the questions are pre- 1.7 questions, or if both the questions and the quiz are new.

3) Allowing kids to take multiple quizzes messes up the system.

4) Adaptive mode seems to be part of the problem.

I hope this is helpful to someone in uncovering the cause of this problem. My Chem teacher is quite frustrated as he was using the quiz function quite extensively and the upgrade to 1.7 seems to have created issues.


Som svar til Peter Richardson

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Carl Keller -
Hi Peter:

My install works fine if I don't use Adaptive mode, but I always get the problem with it. Both new and old quizzes fail on mine also. Multiple quizzes do not cause a problem for me.

I have also discovered that I have the same exact problem on a different installation of Moodle 1.7+ on a different domain with a different database. They are however on the same server at the same ISP!

On the second install, I have never even created a question in the quiz database because I use it for other purposes. When I went on to it and made up a few questions, however, I instantly experienced the same problem as I have on the other install after creating a quiz with random questions. I upgraded both sites within hours of one another a couple of weeks ago.

I have contacted the ISP, but of course they claim they are not at fault.

This problem has really cramped my style too. I would very much like to try to get to the bottom of it.
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Peter Richardson -
Carl,

Thanks for the reply. What do you suggest as a next step?

FYI, we are hosting on our own Dell Server running SUSE 9. Our install of Apache, mySQL and PHP are from XAMPP.

I don' t know much about PHP, but if you or anyone else has any ideas, I would be happy to try them out.

Peter
Som svar til Carl Keller

Re: Fatal error: Call to a member function on a non-object in moodle/question/type/random/questiontype.php on line 242

av Daniel Hinostroza -

Hi Carl,

I'm having the same problem ever since I installed 1.7+

It's a problem as my students are giving their finals this week.

This is the error code:


Fatal error: Call to a member function on a non-object in /home/sitename/public_html/evirtual/question/type/random/questiontype.php on line 242

If you sniffed out the bug, please let me know.

All the very best,

Daniel