Block queries

Block queries

by M O'S -
Number of replies: 9
Hi,

I'm just wondering if the following are possible to do within a block, and how to go about doing them..

When a block is first installed, I want to generate a random string and insert it into a database table. Is it possible to detect when a block is first installed within a course and where would the code go for this?

Is it possible to output the time/date of when cron last ran on the main interface of a block? Ideally this would go in the get_content() function?

Thanks smile
Average of ratings: -
In reply to M O'S

Re: Block queries

by M O'S -
Update:
I solved the first query of generating a random string to insert into a table.

I am still trying to figure out the following:
Is it possible to output the time/date of when cron last ran on the main interface of a block?

How to determine when a course starts/ends? In the context of a course beginning every academic year (same taught course but different students/maybe different resources), how to determine this and to clean out database from previous year?

Hope this make sense.
In reply to M O'S

Re: Block queries

by Alejandro Michavila Pallarés -
Hi Megan, yeah it's possible to output the time/date. Check the data base table where the plugin's cron is store, then select the last run timestamp column. There are PHP functions to convert timestamp to a readable time/date. HTH
Average of ratings: Useful (1)
In reply to Alejandro Michavila Pallarés

Re: Block queries

by M O'S -
Hi Alejandro,

I found the mdl_block data base table which stores plugins and the last cron timestamp column, but my block is not listed in the table. How do I add it to the table? When I run my cron function at the moment, I am usually running it manually from cron.php for testing.

Thanks,

Megan
In reply to M O'S

Re: Block queries

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Megan,

What version of Moodle are we talking about? In Moodle 2.0 you can have a script called db/install.php in any plugin that runs after installation. I don't think there's such a thing in 1.9 unfortunately
Average of ratings: Useful (2)
In reply to Howard Miller

Re: Block queries

by M O'S -
I am using 1.9 and so are the party I'm developing for. Thanks anyway smile

In reply to Howard Miller

Re: Block queries

by Hubert Chathi -
In Moodle 1.9, blocks have an after_install method that is called when the block is installed, and an instance_create method that is called when a block instance is created.
Average of ratings: Useful (2)
In reply to Hubert Chathi

Re: Block queries

by Alejandro Michavila Pallarés -

Megan, remember to upgrade your block version (in version.php) when you modify the cron time. That will update the mdl_block table with your new cron time value.

In reply to Alejandro Michavila Pallarés

Re: Block queries

by M O'S -
Alejandro, I just have the following in my block.php

function init() {
$this->title = get_string('blockname', 'block_myblock');
$this->version = 2011013000;
$this->cron = 300;
}

I am using Moodle 1.9 so I am not sure I require a version.php