MediaWiki:Gadget-syllabusTimetableLinks.js
From Warwick Wiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.hook('wikipage.content').add(function(){
const today = new Date();
$.getJSON( "https://warwick-wiki.containers.uwcs.co.uk/wiki/MediaWiki:Tabula-weeks.json?action=raw", function( data ) {
var thisWeekNum;
var thisWeekYear;
const weeks = data.weeks;
var i = 0;
var foundWeek = false;
while (i < weeks.length && !foundWeek) {
var startDate = new Date(weeks[i].start);
var endDate = new Date(weeks[i].end + "T24:00:00Z");
if (weeks[i].weekNumber > 0 && startDate < today && endDate >= today) {
thisWeekNum = weeks[i].weekNumber;
thisWeekYear = weeks[i].academicYear.replace("/", "");
foundWeek = true;
}
i++;
}
var nextWeekNum;
var nextWeekYear;
foundWeek = false;
while (i < weeks.length && !foundWeek) {
if (weeks[i].weekNumber > 0) {
nextWeekNum = weeks[i].weekNumber;
nextWeekYear = weeks[i].academicYear.replace("/", "");
foundWeek = true;
}
i++;
}
$('.syllabus-timetable-links').each(function(i, obj) {
var id = $(obj).data('syllabus-id');
var thisWeekTT = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=' + thisWeekNum + '">This week</a>';
var nextWeekTT = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' +nextWeekYear + '/roomtimetable.asp?id=' + id + '&week=' + nextWeekNum + '">Next week</a>';
var nextYear = String(Number(thisWeekYear.slice(0,2)) + 1) + String(Number(thisWeekYear.slice(2)) + 1);
var termHolidaysTTs;
if (1 <= thisWeekNum && 10 >= thisWeekNum) {
termHolidaysTTs = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=1-10">This term</a><br/><a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=11-14">Next vacation</a> <a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=15-24">Next term</a>';
} else if (11 <= thisWeekNum && 14 >= thisWeekNum) {
termHolidaysTTs = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=11-14">This vaccation</a> <a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=15-24">Next term</a>';
} else if (15 <= thisWeekNum && 24 >= thisWeekNum) {
termHolidaysTTs = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=15-24">This term</a><br/><a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=25-29">Next vacation</a> <a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=30-39">Next term</a>';
} else if (25 <= thisWeekNum && 29 >= thisWeekNum) {
termHolidaysTTs = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=25-29">This vaccation</a> <a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=30-39">Next term</a>';
} else if (30 <= thisWeekNum && 39 >= thisWeekNum) {
termHolidaysTTs = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=30-39">This term</a><br/><a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=40-53">Next vacation</a> <a href="https://timetablingmanagement.warwick.ac.uk/SWS' + nextYear + '/roomtimetable.asp?id=' + id + '&week=1-10">Next term</a>';
} else if (40 <= thisWeekNum && 53 >= thisWeekNum) {
termHolidaysTTs = '<a href="https://timetablingmanagement.warwick.ac.uk/SWS' + thisWeekYear + '/roomtimetable.asp?id=' + id + '&week=40-53">This vaccation</a> <a href="https://timetablingmanagement.warwick.ac.uk/SWS' + nextYear + '/roomtimetable.asp?id=' + id + '&week=1-10">Next term</a>';
}
$(obj).html('<b>Timetables</b><br/>' + thisWeekTT + ' ' + nextWeekTT + '<br/>' + termHolidaysTTs);
});
});
});