Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Calendar types

From MoodleDocs
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Overview

Multiple calendar support was introduced in Moodle 2.6. The aim was to allow multiple calendar types to define how dates are displayed throughout Moodle. Each calendar type sits in the location calendar/type as a plugin and installs much the same as other plugins in Moodle. The Gregorian calendar is included in core and is the default calendar. Each calendar must extend the base class located at calendar/classes/type_base.php

Another introduction in Moodle 2.6 was automatic class loading, which the calendar makes use of. There is no need to include the type_base.php file mentioned above in your class, simply use the namespace "core_calendar\type_base" and then simply extend type_base. You can see an example of this in calendar/type/gregorian/classes/structure.php.

UI changes

Choosing a calendar type

It's possible to set the calendar type preference on the user and course settings page, the same as the language preference. However, unlike the language select box, this option is only shown if there is more than one calendar type. The reason for this is because the majority of sites using Moodle will not want to use another calendar type other than Gregorian.

hijri select calendar type.png

Date selector form elements

The day, month and year are defined by the calendar as well as the order. When saved, these dates are then converted into Gregorian using a function defined in the calendar type, and then saved in the DB in unixtime.

hijri date selector.png

The calendar block

It is possible that a calendar may have less months in the year than the Gregorian, or more. This is also the case with days in a month. The calendar type takes care of this to ensure the date is converted correctly. In previous versions of Moodle the dates were passed as the variables cal_m and cal_y. This has been changed. For example, if someone was to pass cal_m as 12 and cal_y as 2013, and the calendar type has two months that fall in December, which month do we choose? To avoid this issue we now pass timestamps so that we can convert the date accurately. For backwards compatibility, if cal_m and cal_y are passed it is assumed it is on the 1st day of that month before it is converted.

The calendar type can define the name of the days, months and years. As well as how many days are in each month for a specific year, allowing for a fully flexible calendar block.

hijri calendar.png

Displaying dates

Dates saved in the DB now display throughout Moodle in the format specified by the calendar type being used.

hijri date displayed.png

Required files

These are the required files for your calendar type, located at calendar/type/<nameofcalendartype>.

classes/structure.php

This is where all the required functions are located. This is the bulk of the calendar type.

lang/en/calendartype_<nameofcalendartype>.php

You will need to define the name and pluginname of your calendar type here. Of course you can also put any other strings you may use here as well.

version.php

Much like any version.php file for other modules - see version.php.

API

The functions required in each calendar type are located at calendar/classes/type_base.php which also includes documentation. If you get stuck you can always use the Gregorian calendar as a reference point.