Custom scripts does not work fine?

Custom scripts does not work fine?

by Jean Michel -
Number of replies: 4

Hi,

I want to use a new course view script, so I have done the next, as it's explained in the official Moodle documentation:

-I have created new folder customscripts, new folder course and I've added the view.php file (the same it's in the core of Moodle, but without the import of config.php). 

-I have this weird error: 

Coding error detected, it must be fixed by a programmer: The theme has already been set up for this page ready for output. Therefore, you can no longer change the theme, or anything that might affect what the current theme is, for example, the course.


-I have tried to find out the reason for the error. It seems simple: after my custom scripts, it's trying to execute the original script (view.php) after the execution of the custom script , so it tries to set theme twice (and call to set_header() twice, etc...) 

I don't know if this is the normal behaviour for custom scripts, but it seems it makes no sense. 

Finally I have included a "patch" in lib\setup.php to prevent the execution of the "original" script

if (($customscript = custom_script_path()) !== false) {
require ($customscript);
die(); //<--THIS IS THE PATCH
}

 

Can anybody tell me if I'm wrong? Thanks all.

Jean.

Average of ratings: -
In reply to Jean Michel

Re: Custom scripts does not work fine?

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

I'd not come across customscripts before (a link to the relevant page in the docs would have been helpful, but Google was able to find it very quickly anyway - http://docs.moodle.org/dev/customscripts ).

At the bottom of the docs page, was a link to this discussion: http://moodle.org/mod/forum/discuss.php?d=31895 which clearly states:

2. control will return to the original script unless you explicitly 'exit' or 'die' at the end of the custom script. This means you can either add functionality to a script or completely replace it.

Again, I've never come across this functionality before and the docs make it clear that this is not a good solution for long-term code changes (OK for very quick & dirty hacks, that are going to be fixed properly in a short space of time).

In reply to Davo Smith

Re: Custom scripts does not work fine?

by Jean Michel -

Thanks a lot Davo, I didn't imagine my answer was in that forum reference, maybe would be more efficient to add this detail in the documentation, saving the users the time to search in the forum thread, don't you think?

As you told me, I know this is not recommended way to extend functionality, so If it only can be used do do 'dirty and quick' hacks, maybe it has no sense to exist. Code should be clean always, no time excuses. So this could be seen as a "open door" to develope in the opposite way: rubbish code.

I occasionally found this functionality and get confused about its goal. I wanted to extend things as the standard forms in modules with my own forms. I don't want to write a new module, I want configure a new form with more fields for existing modules. I'm afraid this can not be achieved easily. What do you think Davo?

In reply to Jean Michel

Re: Custom scripts does not work fine?

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

Moodle docs is a wiki - sign up for an account and then you can edit any of the pages and add any details that you think are missing from them.

Bear in mind this 'custom scripts' code, as far as I can see from the forum dates, was written 7 years ago, when Moodle was very different to what it is now. I can't imagine that such a 'hack' would be introduced into the codebase if it was suggested today, but this is one of those rarely-used, obscure features that just hasn't been tidied out (either to avoid breaking existing sites, or, more likely, because it has been completely forgotten about).

Adding extra items to standard forms isn't going to do you much good unless you are also adding extra functionality to process those items and then make use of the new settings. To my mind, that means quite a few changes to whatever module you are working with.

At this point, you have 3 valid choices:

  1. Copy & rename the module, then add your changes - a nice clean solution, but time consuming and you miss out on any new features later added to the core module (unless you manually copy them across).
  2. Use git* to maintain your changes in a separate branch and merge or rebase your code with the official changes - gets a bit fiddly when changing Moodle versions (e.g. 2.2 => 2.3), but the solution that most people use when wanting to modify core code.
  3. Raise a tracker issue with a clear explanation of why your changes will be helpful, submit a patch (via github) and see if you can get your form changes accepted into Moodle core - this removes the need for you to maintain them separately, but may mean quite a bit of work to produce a patch that is acceptable (and to make a case for why it is generally useful).

* Note: other version control systems are available

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Custom scripts does not work fine?

by Jean Michel -

Very helpful answer Davo!. I think my preferred option is the second: using git to keep my changes updated.- I don't know git, but everybody tell me it's great, so I'll learn how to use it.

The case of functionality extension I commeted you is to extend the existing tag system in order to be used in all the modules. This way you can tag resources and activities Do you think I could create an issue to share this changes? In case of yes, who decide if this can be an interesting extension and when?

Best regards!

Jean