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

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

by Chris Pratt -
Number of replies: 2

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