Variables and functions scope

Variables and functions scope

by Mihai Nicolae -
Number of replies: 3
Hello,

I'm new to PHP, so I think that's the reason for some missunderstandings.

I've followed the begining of your tutorial "Development:Blocks" and I have reached the section where these 2 blocks of code are added:

<table cellpadding="9" cellspacing="0">
 <tr valign="top">
 <td align="right">
 <?php print_string('configcontent', 'block_simplehtml'); ?>:
 </td>
 <td>
 <?php print_textarea(true, 10, 50, 0, 0, 'text', $this->config->text); ?>
 </td>
 </tr>
 <tr>
 <td colspan="2" align="center">
 <input type="submit" value="<?php print_string('savechanges') ?>" />
 </td>
 </tr>
 </table>
 <?php use_html_editor(); ?>

and

$this->content = new stdClass;
 $this->content->text = $this->config->text;
 $this->content->footer = '';

But nothing happens :(. I see no edit button and no field where to enter the desired content for the block, as I've understood it would be possible using the above .html file.

My questions are:
1) where is defined (in which moodle project file) this "use_html_editor()" method ?
2) where is defined $CFG variable and what does it mean ?
2) I've noticed that in moodleblock.class.php file in the block_base class constructor is called $this->init(). where is defined this init() ? calling $this->init() I presume that init() is a method of the current class (block_base) since it is called using the current object ($this), but I can't find anywhere in this mooodleblock.class.php it's definition. And the block_base class definition is within this file, so it should be here.


Average of ratings: -
In reply to Mihai Nicolae

Re: Variables and functions scope

by Matt Gibson -
Hi Mihai, welcome to moodle smile

To get your head around Moodle code basics, have a look at http://xref.moodle.org and use the search function at the top right to find the functions and variables that don't make sense. This will show you where they are first defined, their source, and where else in Moodle they are referenced.

To find out about $CFG, follow the trail of includes from include(setup.php); which is in config.php. Its function is to fetch all of the configuration variables at the start of each page build so that if they are needed, they are in memory and don't have to be retrieved one by one from the database, which would be much slower. It basically gets everything from the mdl_config table as well as from config.php and holds it as an object.

As for $this->init(), as I remember, the block class extends the moodle base class for blocks (look near the top of the file for a line with 'extends' in it), so it will be in the class mentioned in that line. Use xref to find it.

Hope that helps and good luck with your coding!

Matt
In reply to Matt Gibson

Re: Variables and functions scope

by Mihai Nicolae -
Hi Matt,

Thank you for that link (http://xref.moodle.org) !

Well, init() is called in the constructor of the block_base class (class which is defined in moodleblock.class.php). This block_base class DOESN'T extend any other class.
Anyway, I've passed through this. Now my problem is that I want to create a table for my block. I've understood that this is done by creating an XML file in a directory db which must be placed within the directory for my block. I have already done that (well, not me, my colleagues did) and at the refresh of the page it appeared one just like the ones I've seen when I have installed moodle (notification about the database configuration). Now I have modified that XML file (because I wanted to change the table structure). And nothing happens sad. I've even erased the old table using the MySQL prompt. And it doesn't create a new one sad.
In reply to Mihai Nicolae

Re: Variables and functions scope

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers
"install.xml" is only used on the initial install of the block. Deleting the tables from the database doesn't delete the block, nor does it tell Moodle that the database tables are not there.

If you want to re-install the block (to test your new "install.xml" file for example), you need to go to the main blocks admin page of your site (admin / modules / blocks / manage blocks) and delete your block from that page. It will delete the tables for you and tell Moodle that the block is no longer present. If you go back to the notifications page after that, the block will be re-installed.

You can also manage additions to the database without de-installing the block. This is done using the "upgrade.php" function in the 'db' directory. In that file, you add statements for any version that requires a change to the database. Then, in your main block file, increase the version number. Moodle recognizes a newer version and will call the "upgrade.php" accordingly.

mike
Average of ratings: Useful (1)