Would changing some of the quiz class names in Moodle 2.6 break your themes?

Would changing some of the quiz class names in Moodle 2.6 break your themes?

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

I want to take advantage of the new class auto-loading stuff in the quiz code (See http://docs.moodle.org/dev/Automatic_class_loading if you don't know about this.)

The problem is that to do this, I will have to rename some classes so the names start with mod_quiz_, and this may break code that refers to the old class names. In the long term, the proposed change is good for the maintainability of the quiz code, and performance, but in Moodle 2.6, that will probably break any themes that override the quiz renderers. I am wondering how painful that breakage would be for you? To fix it you woudl have to rename a bunch of things in your code, which is probably mostly a search-and-replace.

Basically, this is your opportunity to agree or disagree with this proposed change before I submit it for integration. Please comment in the tracker issue MDL-33071.

(Edited by Mary Evans - original submission Tuesday, 2 July 2013, 3:18 PM) Just fixed typos in subject header for clarity.

Average of ratings: -
In reply to Tim Hunt

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Tim,

Would it be possible to:

  1. Perform the rename.
  2. Create a set of classes with the old names which extend the new names.
  3. Have the classes in '2' call the parent methods in '1' and log a 'depreciated' warning.

?

To solve the issue and as a transition point in 2.6 before dropping the old names in 2.7.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

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

That only solves half the problem (as I commented in the bug). It solves code that references the class name (as in a contructor or static function call). It does not solve method declarations like

public function review_page(quiz_attempt $attemptobj, ...)

where in the base class the class name will change to mod_quiz_attempt.

 ... actually, I am wrong. As usual, PHP does not work the way you expect. I think


/**
 * @deprecated since Moodle 2.6. Use mod_quiz_new_name instead.
 */
class quiz_old_name extends mod_quiz_new_name { }

Will make almost everything work. It won't output developer debug warnings, but if your IDE highlights deprecated functions, that will show the problem. Yay! for PHP being rubbish.

In reply to Tim Hunt

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

But...

If the theme instantiates 'quiz_old_name' then:

public function review_page(quiz_attempt $attemptobj, ...) {
parent::review_page(quiz_attempt $attemptobj, ...);
// Output depreciated message.
}

will work smile because you said about referencing 'class names'.  But if it does not then can there be code in the 'old name' file that replaces the reference to the 'new name' class with an instance of itself?

Cheers,

Gareth

In reply to Gareth J Barnard

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

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

No, some part of the quiz will instantiate a mod_quiz_attempt, and pass it to the renderer as $attemptobj.

If the theme overrides the quiz renderer and declares 

public function review_page(quiz_attempt $attemptobj, ...)

Then you will get a fatal error object of type mod_quiz_attempt passed to theme_whatever_mod_quiz_renderer::review_page is not a quiz_attempt.

In reply to Tim Hunt

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

True, but referring to https://moodle.org/mod/forum/discuss.php?d=229474#p1006825...

As long as the new code uses mod_quiz referring objects that have methods with mod_quiz_attempt parameter declarations and therefore older code will use quiz referring objects that have methods with quiz_attempt referring methods assuming that quiz_attempt extends mod_quiz_attempt then you should have no problem.

There is only an issue if a reference to mod_quiz tries to call a method with a 'quiz' parameter as you say but will that happen as you will convert all your references to mod_quiz with the new parameters and existing legacy code will only know about quiz and quiz_attempt?

To be fair this is all in my mind and I probably need to create a script to justify it smile

Cheers,

Gareth

In reply to Gareth J Barnard

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Ok, I really see your point.  So internally can you use mod_quiz but then when the quiz passes it to the renderer it becomes a quiz object though the employment of a copy constructor.

In reply to Gareth J Barnard

Re: Would chaning some of the quiz class names in Moodle 2.6 break you themes?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Or alternatively you could turn it on its head and have mod_quiz extend quiz.  The new overridden methods call the parent methods with an option not to display the depreciated message (as downcasting works).  Place 'depreciated' message in the 'quiz' methods so it gets printed when the old reference is used to call the methods with the old parameter declarations.  Then if the theme renderers call the methods with the old parameter object with a returned mod_quiz reference then it will still work.  But you will use the methods with the new parameter objects.  And then strip out the quiz layer in M2.7 and implement all of the code.

I think I may need to create an example script.