Custom Blocks on all courses by default

Custom Blocks on all courses by default

by Ed Beddows -
Number of replies: 4

I have just created a couple of blocks, i would like these to run through the whole site, not just the front page, and be placed in the same order on each course. We have already got alot of courses on our site, so manually adding these to each course is only a last option.

Can this be done?

Thanks.

Average of ratings: -
In reply to Ed Beddows

Re: Custom Blocks on all courses by default

by John Papaioannou -
Hi Ed,

First of all, what you need to do depends on what version of Moodle you use. I assume 1.4.x. Second, you will need to do a little customized programming to achieve the desired effect.

Every course in Moodle corresponds to one record in table mdl_course in the database. This table contains a "blockinfo" field which holds the information about the blocks in each course.

The blockinfo field in general has the format A,B,...,C:X,Y,...,Z where A, B, C, X, Y, Z etc are numbers which correspond to block ids from the mdl_block table (you can see in that table which number corresponds to which block). The double colon separates left:right blocks. If a number is negative, e.g. -3, this means that in that position we have the block with id = 3 but it is hidden.

Now what you need to do is write a small script that loops over all the records in mdl_course, takes the blockinfo field, does a little string manipulation in order to insert your new block ids in the required place, and then calls update_record or something equivalent to write the data back to the db.

I hope this helps to get you started!
In reply to John Papaioannou

Re: Custom Blocks on all courses by default

by Ed Beddows -

Thanks Jon, very useful information, i've now got it working exactly how i want.

Cheers.

In reply to John Papaioannou

Re: Custom Blocks on all courses by default

by Alan Zaitchik -
Please excuse the fact that I am replying to your posting from nearly 4 years ago-- it's the closest I found to my question!
I wish to programmatically set up a sticky block for various sets of courses (and the site as a whole) by modifying the relevant MySQL tables, but in 1.9 I find no blockinfo field in mdl_course.
Could you advise concerning the new linkages between courses and blocks? I should have no problem with the actual coding once I understand the tables involved.
Thanks so much,
Alan
(a.zaitchik@rcn.com)
In reply to Alan Zaitchik

Re: Custom Blocks on all courses by default

by Miguel Santos -
The mdl_block_instance is what you want to work with

blockid = the type of block you want, specified in mdl_block
pageid = the course id of the course you want the block displayed
position = r or l (right or left)
weight = display order 0 for the top, 1 for the second, etc
visible = 0 or 1 1=visible 0=hidden