get current quiz number in PHP

get current quiz number in PHP

by Ben Smith -
Number of replies: 4

I am interested in passing a couple variables (userid and quizid) to an external bash script, so I have added the requisite code to processattempt.php in mod/quiz.  I can get the userid with the following code:

 

global $USER;

$userid=$USER->id;

 

But, I cannot figure out how to get the current quizid with similar code.

Thoughts? 

Average of ratings: -
In reply to Ben Smith

Re: get current quiz number in PHP

by Michal Novák -

If I understand well you need ID of quiz?

I think you need somethink like this

$quizobj = quiz::create($cm->instance, $USER->id);

$quiz = $quizobj->get_quiz();

If you will success and get questions and answers from quiz, please let me know I am so interested.

In reply to Michal Novák

Re: get current quiz number in PHP

by Ben Smith -

Michal,

Thanks for the reply.  What I am looking for is merely the ID of the current quiz once the quiz is complete (when processattempt.php called).  My script pulls the answers with its own SQL query, but I need the quizID to compare to know what quiz they have taken.  

When I insert your code it seems to break all test functionality.  I cannot advance past question 1 in my quiz.  Any other ideas?

 

Ben

In reply to Ben Smith

Re: get current quiz number in PHP

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

If you look at the code in 'processattempt.php', the first thing it retrieves is the '$attemptid'.

This is then used to create '$attemptobj', which contains all the details of the attempt.

Further down, '$attemptobj->get_quiz()' is called, which, presumably, gets the entry from the 'mdl_quiz' table. I suspect you could get the quizid from the '$attempobj->get_quiz()->id' (but I haven't tried it).

You may want to set up xdebug + suitable IDE (I use PHPStorm, but many others use netbeans / eclipse) - that will allow you to step through the code and see exactly what is being returned at any particular point.

 

In reply to Ben Smith

Re: get current quiz number in PHP

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There is no concept of 'current' quiz. For example, you could be looking at quiz 123 in your browser, and then follow a bookmark to quiz 456. What I mean is, HTTP is state-less.

So, if you want some task to process quiz id 123, you have to pass the id to that, e.g. in the URL.