block functions

block functions

Thisum Buddhika發表於
Number of replies: 4

HI,

I'm developing a block and think of using the after_install() function, as I need so create a new table and insert some initial data into it. So as described, I tried to use this function, but found it is not being called form the block(as i think). can anyone please clarify the functionality of this function(possible causes to not working the function).

thanks...

評比平均分數: -
In reply to Thisum Buddhika

Re: block functions

Hubert Chathi發表於

Don't use after_install() to create tables and insert data. Use a db/install.xml in your block for that. Examples of db/install.xml are found throughout Moodle (for an example of an install.xml file that inserts data, look at the end of /lib/db/install.xml), and it can be created using the built-in XMLDB editor.

You also need to go to the Site Administration > Notifications page to install the block.

See the blocks development documentation for more information.

評比平均分數:Useful (1)
In reply to Hubert Chathi

Re: block functions

Thisum Buddhika發表於

yes,

I used XMLDB and created the table. As you mentioned, created the folder structure and the install.xml file and those work fine. but the problem is I tried to use after_install() to insert some data to the created table, when the bock is being installed.

but it seems the function is not working. I entered the following code:

function after_install(){

global $DB,$CFG;
$prf = $CFG->prefix;

$tables = array( $prf."assignment" , $prf."quiz" , $prf."resource" );

foreach ( $tables as $t ){

$tb1 = $DB->get_records_sql("SELECT id,timemodified FROM {$t}"); 
add_table_values( $tb1 , $t );
}
}

add_table_values() is created by me. but I found this piece of code is not working... could ou please exaplin why?

 

thanks for the quick reply....

In reply to Thisum Buddhika

Re: block functions

Hubert Chathi發表於

Which version of Moodle are you using? If you're using Moodle 2, there is no longer an after_install function. Instead, use db/install.php (see the search and feedback blocks for examples). If you're using Moodle 1.9, it does not use the $DB object.

Also, if you are using Moodle 2, do "$tb1 = $DB->get_records_sql("SELECT id,timemodified FROM {{$t}}");" (double the curly braces), and don't prepend the prefix to the table names.

評比平均分數:Useful (1)