Replacing weblib moodlelib phplib functions

Replacing weblib moodlelib phplib functions

by Timothy Takemoto -
Number of replies: 11

Thanks to Martin  and Bills' two fine  posts and the thread started by Marc Grayson about the content of the main Moodle libraries  and of course the developers guide have made Moodle's architecture a little clearer. Even so I am still very foggy. Please allow me to ask a quick question....

What is the accepted method of replacing functions that exist in the the big three libraries?

If there is a function in a new module which needs to replace say "print_header", then should it go into the module's "lib.php"? I guess so but I don't see such functions there. Perhaps it should go into a small localised and or renamed versions of the big three libraries such as "module_name/weblib.php? But I don't see those either, perhaps because replacing functions in the big three libraries is generally frowned upon, since it should be acheived by rewriting the that function, rather than by replicating it somewhere else?

Tim

Timothy Takemoto

Average of ratings: -
In reply to Timothy Takemoto

Re: Replacing weblib moodlelib phplib functions

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
You are of course free under the GPL to modify those files as you wish in your own installation, but Moodle 1.x does not support custom extensions to central functions.  Moodle 2.0 will, because these libraries will be implemented using classes.
In reply to Martin Dougiamas

Re: Replacing weblib moodlelib phplib functions

by Timothy Takemoto -

Classes....I need to look up the definition of classes. I am not sure what to do.

The background for to my quiery is that Mr. John Bristor's "exam" (which is the answer, if not to my dreams then at least, to my orders) contains modified central functions.

I think that I am pretty useless as a php coder, and believe that I would be well advised to just hand them over to the list, but it was Mr. Bristors suggestion that I looked into their implementation as a module. And after getting on his back it seemed the least I could do. But I was hoping that there would be some way of helping to do this for Moodle, as a possible extra module, rather than "free under the GPL."

Any idea of the Moodle 2.0 time scale?

Tim

In reply to Martin Dougiamas

Re: Replacing weblib moodlelib phplib functions

by Chris Luther -

Another approach to modifying existing or otherwise adding custom functions is to place these in an entirely seperate file and include this file at the end or the original file.  Take for example modifiying the Moodle function foo()

You could create a new file ../lib/weblib-custom.php and place your modified foo() function there.  At the end of weblib.php you add the following statement:

<?php
 
include(weblib-custom.php
);
?>

The custom library file will be read and the new foo definition will replace the standard definition.  When the site is upgraded the developer only needs to add the include statement to the distributed library and retest.

In reply to Chris Luther

Re: Replacing weblib moodlelib phplib functions

by Chris Luther -

erh, sorry typo in the above.  It should read:

include("../lib/weblib-custom.php");  // include custom lib.

In reply to Martin Dougiamas

Re: Replacing weblib moodlelib phplib functions

by Ger Tielemans -
Classes, does that mean that you will move to php5 or only prepare to move?
In reply to Timothy Takemoto

Re: Replacing weblib moodlelib phplib functions

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

Hi Timothy -

What I've done, is create 'local' versions of files where I need to replace functions. I use the naming convention, "'origname'-local.php". Then I copy the current version of the file into my local version, and apply any changes I want.

In the original version, at the point I want to replace the code, I place a line like:

if (file_exists("$CFG->dirroot/course/index-local.php")) {
     include_once("$CFG->dirroot/course/index-local.php");
    
exit();
}

Note, that if you want to completely disable the rest of the original module, use "exit". If you're only adding your own functions, take the 'exit' out.

This allows me to work with the Moodle release code by simply 'hiding' my local copy. The downside is that anytime I get new code from Moodle, I have to go through my local version manually and see where I have to update the code.

mike

In reply to Mike Churchward

Re: Replacing weblib moodlelib phplib functions

by W Page -
QUOTE

"...
The downside is that anytime I get new code from Moodle, I have to go through my local version manually and see where I have to update the code." Mike Churchward

Not too bad a "downside". It is always good practice to make sure you have original code in case one has to begin again with it for whatever reason. It may not be possible to get that code again (developer stopped working on it, etc....). I also change the name of original code before changing it or customizing it in some way. Once you get burned you learn (I sure did).

WP1

In reply to Timothy Takemoto

Re: Replacing mod/forum/lib.php function Frugal Forum Format

by Bruno Vernier -
this library function replacement concept applies to the lib.php files found in each mod (module type)

some colleagues and I prefer a more frugal look and feel when reading postings in forum... we wanted to have the edit/delete/reply and ratings widgets on the head of each posting at the far right instead of at the bottom of each posting ... resulting in a significant reduction of the size of longish conversations, especially conversations with lots of short postings

so I modified one function called forum_print_post from mod/forum/lib.php and put it in lib_local.php

1. I added include('lib_local.php'); at the bottom on lib.php

2. we cannot declare the same function twice, so either we rename the original function so it won't get used anymore, or we call the new function directly from the old one at the start of the function with return new_function(parameters)

i've attached the lib_local.php in case you want to check it out (it contains exact instructions at the top)

I call this the Frugal Forum Format smile

note: the attached file gets filtered a bit ... and I am not sure why... the extra PRE at the start should not be there and there is no closing ?> ...

In reply to Bruno Vernier

Re: Replacing mod/forum/lib.php function Frugal Forum Format

by Chris Luther -

we cannot declare the same function twice, so either we rename the original function so it won't get used anymore, or we call the new function directly from the old one at the start of the function with return new_function(parameters)

Hmmm.  Why not?  Won't declaring it in your local file obliterate the standard definition?  Last one declared wins!  Or at least that is what happens for me. smile  When you redefine in your local function I think it is unnecessay to rename the original function.

In reply to Chris Luther

Re: Replacing mod/forum/lib.php function Frugal Forum Format

by Bruno Vernier -
Chris,

 using PHP 4.3.3, I get an error message about not being allowed to re-declare function



In reply to Bruno Vernier

Re: Replacing mod/forum/lib.php function Frugal Forum Format

by Chris Luther -

OK, I stand corrected.  But the reason why is that the LIBS are included using Require_Once().  From the PHP docs....

require_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.

So the decison has to be made if we want to change Require_Once() to Require() when bringing in these resources.  That will enable quick enablement of function modifications. But I need to test more to see if it wacks out something else. 

====================

And to correct my earlier correction smile If we can get this to work the end statement should read:

include($CFG->dirroot.'\lib\weblib-custom.php');