Making Moodle more object oriented

Making Moodle more object oriented

by Greg Barnett -
Number of replies: 8

Hello,

First let me introduce myself briefly.  I am a system administrator / programmer / web developer for Crown College.  We are currently in the process of implementing moodle.  As part of the process of putting Moodle into place, I've had to use various Moodle functions in some of my other PHP scripts.  In order to clearly identify where those functions were coming from, I wanted to have them be a part of a class.  I added a few lines to moodlelib.php to accomplish this, in a manner that still allows the functions to be a part of the global namespace.

With my modified moodlelib.php, I can now use

moodle::isteacher($courseID);

instead of

isteacher($courseID);

Here is a diff with the changes I made, and I've attached a complete copy as well.

2a3,4
> #BEGIN_MOODLE_CLASS_FUNCTIONS#
>
2152a2155
> #END_MOODLE_CLASS_FUNCTIONS#
2153a2157,2171
> # Added by Greg Barnett for Crown College
> # !!! ugly magic value...
> $moodle_file_lines = file("/var/www/html/moodle/lib/moodlelib.php");
>
> $append = False;
> $moodle_functions = '';
> foreach ($moodle_file_lines as $line):
>     if(trim($line) == "#BEGIN_MOODLE_CLASS_FUNCTIONS#"):
>         $append = true;
>     elseif(trim($line) == "#END_MOODLE_CLASS_FUNCTIONS#"):
>         break;
>     elseif($append):
>         $moodle_functions .= $line;
>     endif;
> endforeach;
2154a2173
> eval ("class moodle {" .  $moodle_functions . "}");

If there is a preferred method of submitting patches, please let me know.  There are two other developers here at Crown College who may also have contributions.

 

Greg Barnett

gregb@crowncollege.edu

Average of ratings: -
In reply to Greg Barnett

Re: Making Moodle more object oriented

by Greg Barnett -
Since I've noticed at least one 404 from someone looking for /moodle/ at www.crowncollege.edu I thought I'd let everyone know that we are testing out Moodle on our intranet before we put it on one of our servers.
In reply to Greg Barnett

Re: Making Moodle more object oriented

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
Greg,

Great idea - nice work! Thanks for posting it, too. This forum is fine for such patches, since I find not many people are using the main bug tracker. This will hopefully change once I implement the new tracker module.

I would only suggest that your code idea is separated from the main moodlelib.php for efficiency (since it would get run with every page request). I've actually rewritten your code a bit and already checked it in as a separate file class.moodlelib.php ... you can see it here: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/moodle/moodle/lib/class.moodlelib.php?rev=1.1&content-type=text/vnd.viewcvs-markup

Note that I've changed the class name to moodlelib because I may use moodle for something else down the track. Hope this method still does the trick for you - if not let me know!

Cheers,
Martin

P.S. Yes, that was me causing the 404 - I'm very curious to know what other PHP scripts you are integrating with Moodle. smile Perhaps, if you have time, you might like to tell us about it.
In reply to Martin Dougiamas

Re: Making Moodle more object oriented

by Greg Barnett -
We aren't quite ready yet to post our moodle pages publicly, but I think you will be very interested in what we are doing.

Since you want the code in a separate file, I have modified the code even more, to make it a bit more general purpose, and probably slightly faster. Since the capabilities of the code have changed, I think it probably makes sense to rename the file from class.moodlelib.php to makeclass.php.

I also have 4 lines that I think might be useful in config-dist.php, related to this code.

///////////////////////////////////////////////////////////////////////////
// This is optional. Uncommenting this makes moodle functions available in
// an object oriented fashion
#require("$CFG->libdir/class.moodlelib.php");
#makeClassFromFile($CFG->libdir ."/moodlelib.php", 'moodlelib');

I have done some simple benchmarks, and on our test server (1.2Ghz Celeron), making a class out of moodlelib.php requires an extra 0.07 seconds.

running 'time php config.php' reports:
0.204 seconds with making the class
0.134 seconds without making the class
In reply to Greg Barnett

Re: Making Moodle more object oriented

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
Ah, yes, much more flexible - many thanks!

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/moodle/moodle/lib/makeclass.php?rev=1.2&content-type=text/vnd.viewcvs-markup

I'm not going to put it in config-dist.php, because I'm really trying to reduce that file rather than make it longer.

However I'll definitely add it to the developer docs.
In reply to Martin Dougiamas

Re: Making Moodle more object oriented

by Greg Barnett -
Putting it in the developer docs instead of config-dist.php makes sense.

Under the usage example I have a few changes to suggest

add one line:
require_once('moodle/config.php');

change the current require to require_once.

and change 'blah' to '$CFG->libdir'.

With the number of global variables required by many of the functions in moodlelib, trying to use those functions without the globals initialized is likely to cause all sorts of problems.
In reply to Greg Barnett

Re: Making Moodle more object oriented

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
Yes, you're right.

Hmmm, but since the variables and the Moodle functions are now in the global namespace anyway, there hardly seems much point in creating an extra class at all (except as a documentation device). ie if the application you're including from has a function with the same name as a Moodle function there will still be a conflict (function already defined).

I've made some changes to setup.php and makeclass.php in CVS to work around this ... can you give it a go?
In reply to Martin Dougiamas

Re: Making Moodle more object oriented

by Greg Barnett -
The changes you made to setup.php and makeclass.php almost worked right. I made a few changes, and it is now working, and make_class_from_file is now loosely coupled, so that it can make a class from files that come from outside of moodle.

The globals do generally have to be initialized, and functions be available in the global namespace for the classified/objectified versions of the libraries to work. So at this point, makeclass.php only aids in the ability to make an external program that uses moodle functions a bit more readable.

Due to the current limited usefulness, it may make sense to remove any mention of makeclass.php from setup, and move makeclass.php to the contrib directory.

In reply to Greg Barnett

Re: Making Moodle more object oriented

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
If you let me know your Sourceforge username I'll give your own contrib directory and you can use CVS to maintain this kind of stuff in there. smile