DB insert file priority on initial setup of block (Moodle2)

DB insert file priority on initial setup of block (Moodle2)

Stuart Buck發表於
Number of replies: 6

If I try calling my own functions within init() of a block it would appear that this prevents install.php being called.

I have a custom block that I want to brand depending on some values set by the administrator. I want to set these by calling my own function within init() as the block could be branded with several different values. In 1.9x These functions worked fine as the default inserts were within the statements tags in the install.xml but in 2.0 it's all moved to install.php.

If I run the code below, the install.php is completely missed. Anyone else notice this?


include_once(myfunctions.php);
class block_myblock extends block_base {


function init() {


global $CFG, $DB, $USER;
callmyownfunction();

}


}

However if I comment out my own function call from the init() then the install.php is called and the tables all set up. The include file is loaded because once the tables are setup the functions works fine.

I understood that the init() was called after the install.xml and install.php if they are present.

Any ideas as I'm at a loss after about a week. It's the same on wamp and IIS fastcgi which makes me think might be a Moodle2 issue.

評比平均分數: -
In reply to Stuart Buck

Re: DB insert file priority on initial setup of block (Moodle2)

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

Have you got Debugging set to developer level?

In reply to Tim Hunt

Re: DB insert file priority on initial setup of block (Moodle2)

Stuart Buck發表於

Hi Tim,

Yes, debugging is on and with extra dev. It appears it's trying to initialise the block before it has installed.

The function call is trying to get the default inserted record which should have been added by install.php. The install.php and the install.xml are not being called first so it is failing as the table does not exist at the point which the block is being initialised.

In reply to Tim Hunt

Re: DB insert file priority on initial setup of block (Moodle2)

Stuart Buck發表於

I'm still confused as to why Moodle is trying to initialise the block before the block install is complete.

I get the obvious error: table does not exist from the debug info, of course it doesn't, it hasn't set it up because it's ignoring the install.xml and install.php files.

I've just wiped everything today and set up the latest clean WAMP to see if there was an issue with my existing server setup.

Now running WAMP 2.1:
Apache 2.2.17
PHP 5.3.5
MYSSQL 5.5.8

It is still ignoring the install files if I call my own function within the init() of the block. Remove the function from the init() and the tables are setup fine as it must be adding the tables after initialising the block which makes no sense as it removes loat of functionality.

The only other solution I can think of is to look at writing a check to see if the table exists first and then call the functions but using the $DB object api all checks throw an error which stops the install.

I'll continue to experiement a little more. Any ideas welcome.

In reply to Stuart Buck

Re: DB insert file priority on initial setup of block (Moodle2) -SOLVED-

Stuart Buck發表於

I solved the issue with a simple logic check after I found the ddl functions documentation.

$dbman = $DB->get_manager();
$table = 'block_blockname';
if (
$dbman->table_exists($table)){
//call you own functions for standard behaviour
}else{
//setup default values to allow install to complete (don't hit db yet)
}

More docs can be found here if your doing something a little more customised with default records:
http://docs.moodle.org/en/Development:DDL_functions

I still think this is odd behaviour of Moodle 2.0.2, I'm using the latest build as of today.

 

Hope this helps someone.

In reply to Stuart Buck

Re: DB insert file priority on initial setup of block (Moodle2) Not SOLVED!

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

Acutally, that is not the right way to do it. Why can't you set things up in install.php? Or create a settings.php file to let admins configure your block?

In reply to Tim Hunt

Re: DB insert file priority on initial setup of block (Moodle2) Not SOLVED!

Stuart Buck發表於

I do set everything up in install.xml and install.php but even with all the latest versions, when I install a basic block and try to call my own functions, Moodle 2.0.2 is calling the init() to initialise the block before completing the install of the block so it returns a table does not exist error. my functions don't do any of the setup just the branding of the title and some images.

Install.xml contains the xmldb to set up the table and install.php contains the default record I need to have inserted. After placing the files as documented in the blocks folder when I attempt to run the plugins check it states "about to be installed" continue with the install and I get the error. If i remove my function calls in the init() the block installs correctly and sets up the tables.

It appears to me block installation has a problem with priority of running the install files as it works fine in 1.9.x

Its late here, I'll try to put a basic problem block together and post the zip if it helps.