Code to make block have a different title for each course ?

Code to make block have a different title for each course ?

by Jan Dierckx -
Number of replies: 15
Is there someone who can help me out on this one?
I'm trying to write a block that has separate configurations for each course it is displayed in.
The function get_content() reads items out of a databasetable and returns them so the block looks different depending on which course it is called from.
I would like one of the settings to be the title of the block itself, so each teacher could change the name of the block. I cannot figure out how to do this. There is of course a function get_title() in moodle.block.class.php but adding a separate function get_title() to replace the one in the moodle.block.class doesn't seem to work. Why?
Average of ratings: -
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by W Page -
Hi Jan!

I cannot help you with this but wanted to ask a question.

Is this going to be a block that a teacher/admin could put content or a script in  and can be given any name (similar to phpNuke)?  Of course in phpNuke you do not have multiple instances of phpNukes like you do courses in Moodle.

WP1
In reply to W Page

Re: Code to make block have a different title for each course ?

by Jan Dierckx -
Not really, to further clarify things I made a screenshot.
I wanted to add a block to my site that displays a new entry out of the glossary every day or week. You could use it to display things like  "Technical term of the day", "This day in history", "Interesting link of the day", "Did you know..."
Just a block that changes on a regular basis to make it more attractive for students to drop by from time to time after schoolhours.
I wanted to use the glossarymodule as a repository for these items because it offers lot of possibilities: adding pictures, letting studentrs contribute, etc...
As an example: I filled a glossary with some tips & tricks for my students using Moodle (how to disable forum autosubscription, how to use the HTML-editor, etc...) The block displays a new trick every day.
What's already working?
- teacher can select glossary from which entries have to be taken;
- teacher can select order of entries (random, oldest first, always the latest entry, etc...)
- teacher can select after how many days a new entry should be displayed
What's not?
- the title is a generic one: Random Quote, of course every user should be allowed to change this to reflect the use, like "Interesting webpage of the day", "Vocabulaire du jour" (etc...) 
  
Attachment topi.GIF
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by N Hansen -
This is a brilliant idea! I have for a long time been toying with the idea of a rotating content page on my Web site, long before I came to Moodle, but the idea of having it set up to change automatically and occupy a block is a great idea. I think it would be nice if it were an option both inside courses and on the main Moodle page. It would be a nice way to give potential students a "taste" of what they would get in full inside the actual courses if it could be displayed on the main pages. A good promotional tool for those of business users.
In reply to N Hansen

New block with "rotating content"

by Jan Dierckx -
blush 'rotating content' , that's what I mean with this Random Quote-talk-talk!
Should have thought about naming it that way myself! smile
I haven't yet solved the title-issue, but in the meantime if you want to test it, you can have a look at my site. (login as guest)
The code can be downloaded from there too.
Add a glossary to your course with whatever content you want to display and set the options in the blocks configuration page: rotate each day, or week, rotate everytime a page is viewed, only show the entry that was added last to the glossary, etc...
Configuration can be done for each course separately. Putting blocks on the frontpage will be possible in Moodle 1.4...
In reply to Jan Dierckx

Re: your faith calendar

by daniel ginerman -
hi Jan,
I very like your "faith calendar", and i'd like to use it in my site.
but the truth is that i need only it's "hebrew section", that is, the hebrew date. and i got into your code.... but didn't succeed to take the other two dates away and leave only the hebrew one. could you please send me the correct code to do that?
thanx so much,

daniEl
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by Jan Dierckx -
OK, I've read Janne's explanation of OOP in this post, and maybe now I can use the correct terminology smile to make this a little bit clearer.
In moodleblock.class.php there is a generic class, with different methods already defined, right?
When you extend this class with your own class (for making a new kind of block), you replace some of the methods with methods you create, right?
(e.g. the method get_content() should obviously be provided by your own extension of the class)
Now, I noticed there are methods like get_title() and even get_header() in the generic class.
(Which return nothing but $this->title) I tried replacing these with my own code so they would return the title that the teacher had defined, but Moodle nevertheless prints out the original title. It seems Moodle never uses the method get_header() to display the header of a block.
What am I doing wrong?
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by Chardelle Busch -
Picture of Core developers

Hi Jan,

I tried your block last week and thought it was great.  This feature is a great way to add stickiness to courses.  The new changes you have made will make it much more useable, but a couple of things:  You might already be aware of a problem, but, I just uploaded the newest version and when I clicked on the "Change settings for this block" link I get this error message:  Could not insert new record in database

Also, so we can keep track as you make changes, would you please add a version/date to your php files (and give yourself credit by adding your name too!) smile

Chardelle

In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by John Papaioannou -
Hi Jan,

What you want to do is edit your block's constructor (the function with the same name as the block), and instead of setting $this->title unconditionally, set it according to the $course parameter. $course is a db record of the course your block is going to be displayed in. You can see an actual example of this in the course_summary block, which has a different title depending on whether it's being displayed on the front page or in another course.

An important tip here is to remember that sometimes (when Moodle creates a block not to display it, but for internal testing purposes), $course may be NULL. You should take this into account and not write code like the following:
if($course->id == 2) { ... }

but instead do:

if(!empty($course) && $course->id == 2) { ... }

to avoid PHP warnings if $course is NULL and thus $course->id is not defined. Finally, it's very important to make absolutely sure that there is an unconditional branch that ensures your block is given a title no matter what $course is.

In reply to John Papaioannou

Re: Code to make block have a different title for each course ?

by Jan Dierckx -
Jon, thanks for explaining things. I've made some changes in the code and now the title can be changed as well.
Chardelle, thanks for pointing out I hadn't changed the version date inside the code. That's why the block didn't add the database table when you upgraded from an earlier version.
If you want to test this one, the code can be downloaded here
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by Steven Day -
This is a great contribution Jan. I just upgraded to the latest code but am still having some problems. Get the "Could not insert new record in database" warning. I checked, and both the block and file list the 8.15 version. I'll check and see if a database table was added.

Thanks for this. It will be great for both content and language courses. I will add a "mot du jour" block next semester.

Best,

Steven

PS It appeared the database was updated when I installed the latest version. But I'm still getting the same warning. Any ideas?
In reply to Steven Day

Re: Code to make block have a different title for each course ?

by Jan Dierckx -
Steven,
Thanks for trying the block.  Normally, when you login as a teacher, the footer of the block shows a link Change settings for this block. Can you access this page (where a teacher is able to configure the block) ?
If so, do your course glossaries show up in the drop down list? (see attached picture)
Do you have access to PHPMyAdmin? If so, can you make a screenshot of what exactly is stored in the fields of the table 'mdl_block_quote' ? (the green part can of course be different for your install...)
I really want to add more error checking to the block, so any information you can give me, would be very welcome.
Attachment random.GIF
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by Steven Day -
Jan,

I'm excited about getting your contribution operational. So my gratitude extends to you. For next term, I'd love to have the students create an on-going lexicon as part of their homework.

Back to the topic at hand; I do see the link for changing settings, but when I click on it, the "could not create new item in database" warning stops the process (as Chardelle noted above). The version is your 8/15 file, so I'm not sure what exactly the problem is. I will post a screenshot of my database.

Love your profile picture. Thanks again, and next time I'm in Belgium the ale is on me.

Steven

PS Well, there is no mdl_block_quote, which explains things. Can I create a new table by pasting the contents of the mysql.php file?
In reply to Steven Day

Re: Code to make block have a different title for each course ?

by Steven Day -
OK, I just re-installed everything and this time the table was set up correctly (I thought it was last time, too). In any case, it's working fine now. The quotes will be great for content courses, and for language classes I can have students go to work on building a lexicon. Testing them on it should provide sufficient motivation.

Thanks so much for doing this Jan!

Best,

Steven
In reply to Jan Dierckx

Re: Code to make block have a different title for each course ?

by Nicolas Martignoni -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
This is so gret I already translated the strings in french cool

Many thanks for this code!
Nicolas

PS Pay attention that your files have DOS line endings!
In reply to Nicolas Martignoni

Re: Code to make block have a different title for each course ?

by Jan Dierckx -
blush oops, I'll change the settings of my editor. I'll have to make sure it is using 4 spaces instead of tabs as well. Thanks for pointing this out to me.
I will put your translation in the zip of the block, together with a Dutch one and some typos I corrected in the English version.  
Feels great to have your block translated in French big grin Thanks!