Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created April 4, 2013 19:07
Show Gist options
  • Save pamelafox/5313218 to your computer and use it in GitHub Desktop.
Save pamelafox/5313218 to your computer and use it in GitHub Desktop.
Course processing logic
processTimes: function() {
var startDate = '';
var startDisplay = 'Date to be announced';
var startDisplayShort = 'TBA';
var startCountdown = '';
var startTime = 4100745600000; // Change this in 2099
var startDiff = 99999999;
var duration = '';
var durationWeeks = 1000;
var today = moment(new Date());
var displayFormat = '';
var shortDisplayFormat = 'MMM Do';
var courseStatus = 'future';
var coursePercent = 0;
var endDisplayShort = '';
var startDateM = null;
// We can clean this up once our database is cleaner
if (this.get('duration_string')) {
var durationStr = this.get('duration_string');
var durationWeeksNum = parseInt(durationStr.split(' ')[0], 10);
if (!isNaN(durationWeeksNum) && durationWeeksNum >= 1) {
duration = durationStr;
durationWeeks = durationWeeksNum;
}
}
// Either they specify no fields (TBA),
// or they specify month and year,
// or they specify everything
if (this.get('self_study')) {
startDisplay = startDisplayShort = 'Self study';
} else if (this.get('start_year') && this.get('start_month') && !this.get('start_day')) {
startDateM = moment([this.get('start_year'), (this.get('start_month')-1)]);
startDisplay = startDateM.format('MMMM YYYY');
startDisplayShort = startDateM.format('MMM YY');
} else if (this.get('start_year') && this.get('start_month') && this.get('start_day')) {
startDateM = moment([this.get('start_year'), (this.get('start_month')-1), this.get('start_day')]);
startDisplay = startDateM.format('MMM Do YYYY');
startDisplayShort = startDateM.format('MMM Do');
if (durationWeeks != 1000) {
var endDateM = moment(startDateM).add('weeks', durationWeeks);
endDisplayShort = endDateM.format('MMM Do');
}
}
if (startDateM && startDateM.isValid()) {
startTime = startDateM.valueOf();
startCountdown = startDateM.fromNow();
startDiff = startDateM.diff(today, 'days');
coursePercent = Math.min(Math.abs(Math.ceil((startDiff / (durationWeeks * 7)) * 100)), 100);
if (startDiff === 0) {
startCountdown = 'Started today';
courseStatus = 'present';
} else if (startDiff < 0) {
if (Math.abs(startDiff) < ((durationWeeks + 3) * 7) && !this.get('certificates_ready')) {
startCountdown = 'Started ' + startCountdown;
courseStatus = 'present';
} else {
startCountdown = 'Ended ' + startDateM.add('weeks', durationWeeks).fromNow();
courseStatus = 'past';
}
} else {
startCountdown = 'Starts ' + startCountdown;
}
}
var timeProperties = {start_percent: coursePercent, start_status: courseStatus,
start_diff: startDiff, start_countdown: startCountdown,
start_display_short: startDisplayShort, end_display_short: endDisplayShort,
start_display: startDisplay, start_time: startTime,
duration: duration, duration_weeks: durationWeeks};
return timeProperties;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment