Bug 5456

Summary: Create a link to opac-ics.pl
Product: Koha Reporter: Magnus Enger <magnus>
Component: OPACAssignee: Magnus Enger <magnus>
Status: CLOSED FIXED QA Contact: Bugs List <koha-bugs>
Severity: enhancement    
Priority: P5 - low CC: crohde, frederic, jandrews, jonathan.druart, kyle, veron
Version: master   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=17322
Change sponsored?: --- Patch complexity: Small patch
Who signed the patch off: Text to go in the release notes:
Now in opac user page there is a link to download user's issues dates as an ical Calendar. So the user can import the dates into his/her agenda.
Version(s) released in:
Bug Depends on:    
Bug Blocks: 18563, 17322    
Attachments: Bug 5456 - Create a link to opac-ics.pl
Bug 5456 - Create a link to opac-ics.pl
Bug 5456 - Create a link to opac-ics.pl
Bug 5456 - Create a link to opac-ics.pl

Description Magnus Enger 2010-11-30 08:51:17 UTC
Koha has a script called opac-ics.pl which can export the due dates for a logged in user in iCalendar/.ics format, but there does not seem to be a link to this script anywhere in the patron user interface. 

[09:26] chris: i think i wrote it in 2006, and it mostly still works 
[09:28] magnus: is there a link to it anywhere in the opac interface, or do you have to know the url?
[09:28] chris: there used to be
[09:28] chris: not sure when it got removed, we should test it all works well, fix it if it doesnt and put the link back
[09:31] magnus: hm, i get to download a calendar file - guess that means users have to remember to do it every so often... would it be possible to have an interface that e.g. iCal could "subscribe" to?
[09:31] magnus: guess that would mean making obscure URLs or putting username/password in the url...
[09:34] chris: magnus: yeah, that was my next plan an md5 hash
Comment 1 Chris Rohde 2014-05-21 19:16:01 UTC
Could this script be used to attach an iCal file of the date due to "checkout" emails?  Patrons could then drop the reminder into their choice of calendar software.
Comment 2 Magnus Enger 2016-07-23 18:33:05 UTC Comment hidden (obsolete)
Comment 3 Marc Véron 2016-07-24 05:15:40 UTC
Nice feature!

However, I can not import to thunderbird calendar.

This is what I get as koha.ics:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:Data::ICal 0.21
BEGIN:VEVENT
DESCRIPTION:Your copy of ""Pfützen schreien so laut ihr Licht" :" (barcode 
 1234567890) is due back at the library today
DTSTAMP:20160724T050512Z
DTSTART:TZID=Europe/Zurich:20160724T235900
SUMMARY:""Pfützen schreien so laut ihr Licht" :" is due
UID:issue-1@
END:VEVENT
END:VCALENDAR

If I fix the DTStart as follows, it imports OK:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:Data::ICal 0.21
BEGIN:VEVENT
DESCRIPTION:Your copy of ""Pfützen schreien so laut ihr Licht" :" (barcode 
 1234567890) is due back at the library today
DTSTAMP:20160724T050512Z
DTSTART:20160724T235900
TZID:Europe/Zurich
SUMMARY:""Pfützen schreien so laut ihr Licht" :" is due
UID:issue-1@
END:VEVENT
END:VCALENDAR

Additionally, I think there are to much quotes in DESCRIPTION
Comment 4 Magnus Enger 2016-07-24 22:05:16 UTC Comment hidden (obsolete)
Comment 5 Marc Véron 2016-07-25 05:37:36 UTC Comment hidden (obsolete)
Comment 6 Jonathan Druart 2016-07-25 17:10:07 UTC
(In reply to Marc Véron from comment #5)
> Update 2016-07-25
> - Force date_due to UTC to avoid problematic timezone info like this:
>   DTSTART:TZID=Europe/Zurich:20160724T235900

I am not sure this is correct.

From https://metacpan.org/pod/DateTime::Format::ICal:
"The iCal spec requires that datetimes be formatted either as floating times (no time zone), UTC (with a 'Z' suffix) or with a time zone id at the beginning ('TZID=America/Chicago;...'). If this method is asked to format a DateTime object that has an offset-only time zone, then the object will be converted to the UTC time zone internally before formatting."

So the TZID should be handled correctly by Thunderbird (I did not try).
I think we should either use a floating TZ or the tz returned by C4::Context->tz, but not arbitrarily set it to UTC
Comment 7 Marc Véron 2016-07-25 19:11:12 UTC
Hi Jonathan,

I tested Magnus' newest patch again (with 2 checkouts), that's what I get:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:Data::ICal 0.21
BEGIN:VEVENT
DESCRIPTION:Your copy of Pfützen schreien so laut ihr Licht (barcode glaupf
 u-01) is due back at the library today
DTSTAMP:20160725T185113Z
DTSTART:20160725T215900Z
SUMMARY:Pfützen schreien so laut ihr Licht is due
UID:issue-9@
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:Your copy of Der Rattenkönig : (barcode rakoe-01) is due back a
 t the library today
DTSTAMP:20160725T185113Z
DTSTART:20160725T215900Z
SUMMARY:Der Rattenkönig : is due
UID:issue-10@
END:VEVENT
END:VCALENDAR

If I pass it through http://icalendar.org/validator.html#results, it gives no errors (warnings though about the length of 2 lines).
I can import it to Thunderbird Calendar without problem, and Thunderbird shows what I expect regarding time (25.07.2016 23:59 local time)
Comment 8 Magnus Enger 2016-07-28 18:37:41 UTC
First of all let me say that I am no iCal expert! 

I created a small test script:

---

#!/usr/bin/perl

use Modern::Perl;
use DateTime::Format::ICal;
use Koha::DateUtils;

my $datestring = '2016-03-10 23:59:00';

my $dt = dt_from_string( $datestring );
say DateTime::Format::ICal->format_datetime( $dt );

my $dt2 = dt_from_string( $datestring );
$dt2->set_time_zone( C4::Context->tz );
say DateTime::Format::ICal->format_datetime( $dt2 );

my $dt3 = dt_from_string( $datestring );
$dt3->set_time_zone( 'UTC' );
say DateTime::Format::ICal->format_datetime( $dt3 );

---

This gives the following output:

TZID=Europe/Oslo:20160310T235900
TZID=Europe/Oslo:20160310T235900
20160310T225900Z

I tested the output from Koha in a couple of different validators. When I have a date like TZID=Europe/Oslo:20160310T235900 in the ICS I get errors from the validators: 

Error: Error was: Error at line 15:[DTSTART] Unparseable date: "TZID=Europe/Oslo:20160810T235900"
Cause: Caused by: [DTSTART] Unparseable date: "TZID=Europe/Oslo:20160810T235900"
http://severinghaus.org/projects/icv/

Invalid DTSTART value, must be a date or date-time value near line # 11
http://icalendar.org/validator.html

When I change the script so all dates look like 20160310T225900Z, I get no errors from the same validators. In addition, Marc has said that Thunderbird accepts this (but not TZID=Europe/Oslo:20160310T235900) *and* interprets the date and time correctly. My pragmatic side tells me this should be good enough. :-)

From https://metacpan.org/pod/DateTime::Format::ICal:
"The iCal spec requires that datetimes be formatted either as floating times (no time zone), UTC (with a 'Z' suffix) or with a time zone id at the beginning ('TZID=America/Chicago;...')."

The way I read this, "formatting as UTC" is just as good as having a "time zone id at the beginning".
Comment 9 Jonathan Druart 2016-08-01 11:11:51 UTC
Created attachment 53831 [details] [review]
Bug 5456 - Create a link to opac-ics.pl

opac-ics.pl was added to Koha back in 2007, but there has not been a link to it
from anywhere. This patch adds the link from the main view on
/cgi-bin/koha/opac-user.pl, and makes some changes to opac-ics.pl itself.

Changes to opac-ics.pl:
- Update the code to use date and time for when a loan is due
- Use dt_from_string to turn a MySQL time and date into a DateTime
- Add a timestamp and a UID (these were reported as necessary by a couple of
  iCal validators)
- Remove DTEND from events, since we now have time as well as date for when a
  loan is due
- For loans that are overdue, set the DTSTART to now(), to avoid creating
  calendar events in the past
- Use a template to generate text strings, to make them translatable

To test:
- Apply the patch
- Make sure you have at least a couple of loans, including one overdue
- Go to /cgi-bin/koha/opac-user.pl and click on the link labeled "Download as
  iCal/.ics file"
- Inspect the file in a text editor and/or view the result in a calendar
  appplication. Make sure the data makes sense.
- Sign off and go thee merrily on thine way.

Update 2016-07-25
- Force date_due to UTC to avoid problematic timezone info like this:
  DTSTART:TZID=Europe/Zurich:20160724T235900
- Remove quotes from summary and description

Followed test plan, koha.ics file successfully imported to Thunderbird calendar
Signed-off-by: Marc <veron@veron.ch>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Comment 10 Jonathan Druart 2016-08-01 11:12:37 UTC
(In reply to Magnus Enger from comment #8)

Ok, let's push it and we will see later if we need to adapt something :)
Comment 11 Kyle M Hall 2016-09-02 16:54:59 UTC
Pushed to master for 16.11, thanks Magnus!
Comment 12 Frédéric Demians 2016-09-08 20:06:09 UTC
Pushed in 16.05. Will be in 16.05.04.
Comment 13 John Andrews 2017-05-02 16:16:58 UTC
There's a problem with the lack of end time -- when importing this into my outlook calendar, because there's no end time, outlook automatically creates an hour long event. Which means that the due date reminder shows up both on the due date, and on the day AFTER the due date. In our case, the time due is 11:59 pm, so the appointment gets created in outlook as from 11:59 pm on the due date until 12:59 am on the next day. Is there a way to force the appointment to start and end at the same time?
Comment 14 Magnus Enger 2017-05-03 08:05:18 UTC
(In reply to John Andrews from comment #13)
> There's a problem with the lack of end time -- when importing this into my
> outlook calendar, because there's no end time, outlook automatically creates
> an hour long event. Which means that the due date reminder shows up both on
> the due date, and on the day AFTER the due date. In our case, the time due
> is 11:59 pm, so the appointment gets created in outlook as from 11:59 pm on
> the due date until 12:59 am on the next day. Is there a way to force the
> appointment to start and end at the same time?

Please report this as a new bug/issue.