Add a block to each course - NOT via the front page

Add a block to each course - NOT via the front page

by Paul Vaughan -
Number of replies: 5

Hi all.  I have a simple yet time-consuming task to achieve and I am sure that someone must have achieved something similar before, to save me reinventing the wheel.

I need to add a block to each and every course in our production Moodle (2.9).  I have tried the 'trick' where you add a block to the front page and change its settings to be available throughout the site, but in my case each block needs a different instance configuration and this is not possible using this method, as each block gets the same instance config as the front page block.

In general terms, the script would run something along these lines: For each course, add block with id 'x' (I am not bothered where, apart from 'left', but if there's a way of promoting it to the top, wonderful).  That's pretty much it!  There may be some additional configuration required too, based on text already present in each course's 'id number' field, but that's it.

Has anyone written a script to achieve anything like this already? Given time I could do it, but with the start of the new term fast approaching and two thirds of my team missing, time is not on my side.

Any help appreciated.

Average of ratings: -
In reply to Paul Vaughan

Re: Add a block to each course - NOT via the front page

by Rex Lorenzo -

You can create an event observer for the \core\event\course_created event. Details on how to create an event observer: https://docs.moodle.org/dev/Event_2

In the event observer, here is a code snippet that we use to add "Recent activity" for certain courses:

        // Add the "Recent activity" and "Upcoming events" blocks upon build and
        // place them above the "Administration" block.
        $coursecontext = context_course::instance($courseid);
        $page->set_context($coursecontext);
        $page->blocks->add_regions(array(BLOCK_POS_RIGHT), false);
        $page->blocks->add_block('recent_activity', BLOCK_POS_RIGHT, -10, 0, 'course-view-*');
        $page->blocks->add_block('calendar_upcoming', BLOCK_POS_RIGHT, -9, 0, 'course-view-*');

You will need to get the courseid from the event object that is passed to the function.

Or if you want to add in default blocks for every course for a given format, check out the $CFG->defaultblocks_<format> config: 

In reply to Paul Vaughan

Re: Add a block to each course - NOT via the front page

by Tony Box -

Hi Paul,

Funny you should ask this as I'm exploring the same thing and I haven't found any good answers on the forums or elsewhere.

I think using a database query is going to be the easiest solution. I'm trying to put one together right now.

My idea is to create a query that looks for all courses without block X and then adds block X to those courses. Sounds easy enough, but from my preliminary look into the DB, there are more supporting tables that will also need to be modified in order to successfully add the block to every course.

If I come up with a solution (or if someone else has a solution), I'll post it here.

In reply to Tony Box

Re: Add a block to each course - NOT via the front page

by Chris Pratt -

Did you get anywhere with this Tony?

In reply to Chris Pratt

Re: Add a block to each course - NOT via the front page

by Tony Box -

Hi Chris,

I never did get a query built for this. I started working on one and was able to build something that found all of the courses without a specific block, but I didn't get as far as adding a new block to them. I just ditched the idea and waited for a new semester to start, then added the block as a "default" block in moodle's config file. That way when new courses were created for the semester they would have it added automatically.

Despite that, this would still be a very useful query/script to have in the future.

In reply to Tony Box

Re: Add a block to each course - NOT via the front page

by Brian Merritt -
Picture of Particularly helpful Moodlers

If it's any help, I did an SQL query that pulls in all courses, and then if the blocks.id and blocks.blockname are null then that course needs to have the block added (the left join takes care of that)

SELECT DISTINCT course.id, course.shortname, course.fullname, blocks.id , blocks.blockname, blocks.parentcontextid  FROM database_name_here.mdl_course as course
	inner join moodle_2015.mdl_context as context
    on context.instanceid = course.id
	left join moodle_2015.mdl_block_instances as blocks
    on blocks.blockname = "choose_name_of_block"
       AND context.id = blocks.parentcontextid ;

Note this is pure SQL, if using Moodle to query it will be slightly different

Just update the database name and the name of the block to get the results