jamesjonesmath / canvancement

Enhancements to the Canvas LMS
ISC License
215 stars 95 forks source link

sort-roster: Last Activity doesn't sort for non-US English languages #9

Closed greg-or closed 6 years ago

greg-or commented 6 years ago

Hi James,

The sort-roster filtering is a great little enhancement to Canvas! I've just tried this for a course set to non-US English (UK or Australian) and these languages format dates differently - this breaks the shortDateTime regex.

A simple fix for Australian English is to remove all of the logic around am/pm (as times are displayed in 24 hour time).

            var tm = '';
            var matches = shortDateTimeRegex.exec(s);
            if (matches) {
              var month = months.indexOf(matches[1]) / 4;
              var day = parseInt(matches[2]);
              var year = parseInt(matches[3]) || thisYear;
              var hour = parseInt(matches[4]);
              var min = parseInt(matches[5]);
              tm = new Date(year, month, day, hour, min, 0).toISOString();
            }

It looks like UK English formats their dates differently (dd MMM). I'm not sure whether this could be differentiated based on the lang html tag ($('html').attr('lang');) or whether it is just worth adding as a known issue?

Thanks, Greg

jamesjonesmath commented 6 years ago

Let me think about things I can do.

In general I don't support other locales when trying to read information from the web page directly. This roster sorter is one of those cases that has frustrated me when I was writing it because of the lack of classes that clearly identify the purpose of some of the columns and if someone already has to modify the config at the top to specify the heading text, they might be expected to modify the regular expression as well. Non-US English might be something I can work with because the text is likely the same and the date formats are limited.

I don't have any intent to add full detection of language because Canvas doesn't make their i18n internationalization library available to see what the text should be. Adding international support makes the script more complicated and subject to breakage if something changes. I thought about pulling in a library to parse the dates, but I try not to include external libraries if I don't have to. That's for people who want to include it in their custom global JavaScript so they don't have to mess with loading external libraries.

The best solution, short of Canvas adding this functionality, would be for Canvas to use a data attribute that stored the date and time in ISO format. That probably won't happen since they already have the data stored from when they downloaded it. From their perspective, it's not needed and they are discouraging us from adding things to their pages.

What I might be able to do is take a list of some common regular expressions and pass through the data looking for which ones match. If there is never an am/pm or some of the hours are more than 12 then I could use a 24 hour clock. If the numbers are dd MMM instead of MMM dd, that's easy enough to check. If the format is dd MMM but the MMM is a non-US abbreviation, then it becomes a question of how much do you try and support?

jamesjonesmath commented 6 years ago

Try version 5 that I just made available and let me know if it works for you. It worked for me with all four varieties of English handled by Canvas. I tested it in Firefox and Chrome with Tampermonkey.