Adding calendar to main page

Adding calendar to main page

by Gustav W Delius -
Number of replies: 7

Jon, I have had a look at your code for the calendar and seen that it supplies the very convenient function calendar_print_sideblocks() for printing the calendar miniview together with an upcoming events box. So I put a calendar on my Moodle front page by calling this function at an appropriate place in index.html.

However unfortunately I do not know how to tell the function what events to include in the calendar. There appears to be some complicated mechanism involving SESSION variables for this purpose. I would like the calendar on the homepage to show all global events and, after the user is logged in, all the events in the user's group in all the courses the user is enrolled in and all the users's personal events, i.e., everything the user will ever see. How do I do that?

Average of ratings: -
In reply to Gustav W Delius

Re: Adding calendar to main page

by John Papaioannou -

Hi Gustav,

I haven't spent much time working with the calendar as a guest (that's probably why now that I logged in to check some things for this answer I got about five "I need to do this" thoughts in a row) but after having a look:

I think the existing code base will do just exactly what you describe. Are you getting different behavior, and if so, in what way? In order to do that kind of thing with calendar_get_side_blocks(), here's what needs to be done:

  1. Get the calendar_set_filters function call out of the first if (in fact, that's a bug waiting to happen right there... what if later, when preferences are expanded, that if doesn't execute but the next one does?)
  2. Give the function optional parameters with default values and alter it to use them, if present.

However, if you familiarize yourself with the code, you will see that I have intentionally made almost all layers of the calendar code take parameters for the courses, userids, groupids etc. whose events they should work on. The calendar_print_side_blocks() function is the very topmost layer, to provide an "interface" to "external" (to the calendar) parts of Moodle.

If displaying this kind of thing in the front page is going to be included in Moodle, then it would be worth to change or add a few functions like this one to provide external code with this functionality. Otherwise, I would suggest to do this programmatically for your site. It isn't difficult:

// You need to do this before the first call to any calendar_
// functions, so for the time being I call it from... everywhere.
calendar_session_vars();
// This destroys the course filtering that the user may have in
// place (if he's not a guest). A negligible inconvenience, and
// we DO have to show events from ALL courses.



$SESSION->cal_show_course = true;
// OverLib
echo calendar_overlib_html();
print_side_block_start(get_string('calendar', 'calendar'));
echo calendar_top_controls('course', array('id' => $course->id, 'm' => $_GET['cal_m'], 'y' => $_GET['cal_y']));
// The first three arguments mean: events from all courses,
// group events from all courses, and for this user only
echo calendar_get_mini(true, true, $USER->id, $_GET['cal_m'], $_GET['cal_y']);
echo calendar_filter_controls('course');
print_side_block_end();
In reply to John Papaioannou

Re: Adding calendar to main page

by Gustav W Delius -

Thank you for your code for displaying a calendar on the Moodle home page. However it displays all events from all courses which is not desirable. The calendar should only show the events from all courses and groups the user is enrolled in. How can I achieve that? I tried to do this by calling calendar_set_filters with a list of all the courses that I want shown in its 4th argument. But that didn't do anything.

In reply to Gustav W Delius

Re: Adding calendar to main page

by John Papaioannou -
1. Easy answer: to get the courses you want, use calendar_get_default_courses(). This returns an array of all courses that the current user is either a student or a teacher in. I 'm not sure if it includes the "system" course (courseid == 1), but that should be easy to check. Warning: if you want to use filtering and do it this way, you will end up writing calendar_set_filters() from scratch.

2. Calling calendar_set_filters() is exactly what you want to do. Since you said "it didn't do anything", I assume you used for the fourth argument something like:

array(4, 5, 6)

which in reality is

array(0 => 4, 1 => 5, 2 => 6)

and it is in the WRONG format. The correct way is:

array(4 => 1, 5 => 1, 6 => 1)

In fact this little detail caused me to do a lot of hair-pulling while debugging the filters. That's why I ended up writing in calendar_set_filters:

// WARNING: When calling this function, be VERY careful with the format for default courses arguments!
// Correct formatting is [courseid] => 1 to be concise with moodlelib.php functions.

You can obtain a correctly formatted array by calling calendar_get_default_courses() as described above.

Of course I cannot be sure that this is indeed your problem. It's just that I had it myself and it caused me no end of torment...

Cheers,
Jon
In reply to Gustav W Delius

Re: Adding calendar to main page

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Jon -

I've got the calendar working on my home page, as per this discussion, but I can't use the month forward and back nav. It assumes an URL of 'view.php', which isn't the case for the homepage.

Short of me hacking in some new arguments, is there a mechanism in place to allow this?

mike

In reply to Mike Churchward

Re: Adding calendar to main page

by John Papaioannou -
Mike,

No mechanism, sorry. But now you mention it, I think this deserves a solution in CVS. Hold on and wait for the mailing list to inform you! smile

Jon
In reply to Mike Churchward

Re: Adding calendar to main page

by John Papaioannou -
It should be ready now. Just calendar_print_side_blocks() from the site front page and tell me how things look.

Jon
In reply to John Papaioannou

Re: Adding calendar to main page

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hey Jon -

It works from the frontpage just fine.

However (there's always a however), I also use it in a lower-level, non-course page (category.php). This could be easily fixed if we provided a 2nd argument to 'calendar_print_side_blocks' that is the page to go to if specified.

So in my case, from 'category.php', I could have:
    calendar_print_side_block(null, $CFG->wwwroot.'/course/category.php');

When the calendar_print_side_block gets that argument, it would use it instead.

What I do is send the pagetocall argument through to calendar_top_controls in the 'type' argument. Then, in calendar_top_controls, if type isn't anything I expect I have a default for the case statement that passes it to the calendar_get_link_tag functions. Seems to work okay.

mike