Commit 15dce333 authored by Zac Wood's avatar Zac Wood
Browse files

identify sections by CRN instead of id

parent c9161a32
Pipeline #3492 passed with stages
in 26 minutes and 15 seconds
...@@ -33,7 +33,7 @@ class Cart { ...@@ -33,7 +33,7 @@ class Cart {
} }
async toggleSection(section) { async toggleSection(section) {
const resp = await fetch(`/sessions/cart?&section_id=${section.id}`, { cache: 'no-store' }); const resp = await fetch(`/sessions/cart?&crn=${section.crn}`, { cache: 'no-store' });
const json = await resp.json(); const json = await resp.json();
this.courses = json; this.courses = json;
} }
...@@ -41,7 +41,7 @@ class Cart { ...@@ -41,7 +41,7 @@ class Cart {
includesSection(obj) { includesSection(obj) {
for (const key in this._courses) { for (const key in this._courses) {
const list = this._courses[key]; const list = this._courses[key];
if (list.includes(obj.id)) return true; if (list.includes(obj.crn)) return true;
} }
return false; return false;
......
...@@ -31,18 +31,18 @@ const remove = async item => { ...@@ -31,18 +31,18 @@ const remove = async item => {
* and sets the link in the modal to it. * and sets the link in the modal to it.
*/ */
const setUrlInModal = () => { const setUrlInModal = () => {
document.getElementById('calendar-link').innerText = `${window.location.protocol}//${window.location.hostname}/api/schedules?section_ids=${window.cart._courses.join(',')}`; document.getElementById('calendar-link').innerText = `${window.location.protocol}//${window.location.hostname}/api/schedules?crns=${window.cart._courses.join(',')}`;
}; };
const downloadIcs = async () => { const downloadIcs = async () => {
const response = await fetch(`${window.location.protocol}//${window.location.hostname}/api/schedules?section_ids=${window.cart._courses.join(',')}`); const response = await fetch(`${window.location.protocol}//${window.location.hostname}/api/schedules?crns=${window.cart._courses.join(',')}`);
const text = await response.text(); const text = await response.text();
const blob = new Blob([text], { type: 'text/calendar;charset=utf-8' }); const blob = new Blob([text], { type: 'text/calendar;charset=utf-8' });
saveAs(blob, 'GMU Schedule.ics'); saveAs(blob, 'GMU Schedule.ics');
}; };
const addToSystemCalendar = () => { const addToSystemCalendar = () => {
window.open(`webcal://${window.location.hostname}/api/schedules?section_ids=${window.cart._courses.join(',')}`); window.open(`webcal://${window.location.hostname}/api/schedules?crns=${window.cart._courses.join(',')}`);
}; };
const initListeners = () => { const initListeners = () => {
...@@ -53,5 +53,5 @@ const initListeners = () => { ...@@ -53,5 +53,5 @@ const initListeners = () => {
document.getElementById('download-ics').onclick = downloadIcs; document.getElementById('download-ics').onclick = downloadIcs;
document.getElementById('add-to-system').onclick = addToSystemCalendar; document.getElementById('add-to-system').onclick = addToSystemCalendar;
document.getElementById('share-url').innerText = `${window.location.protocol}//${window.location.hostname}/schedule/view?section_ids=${window.cart._courses.join(',')}`; document.getElementById('share-url').innerText = `${window.location.protocol}//${window.location.hostname}/schedule/view?crns=${window.cart._courses.join(',')}`;
}; };
...@@ -5,10 +5,10 @@ class API::SchedulesController < ApplicationController ...@@ -5,10 +5,10 @@ class API::SchedulesController < ApplicationController
# Render an iCal file containing the schedules of all the # Render an iCal file containing the schedules of all the
# course sections with the given CRNs. # course sections with the given CRNs.
api :GET, '/schedules', 'Generate an iCal file with events for the given CRNs' api :GET, '/schedules', 'Generate an iCal file with events for the given CRNs'
param :section_ids, String, desc: 'Comma separated list of section ids to include as events in the calendar', required: true param :crns, String, desc: 'Comma separated list of crns to include as events in the calendar', required: true
def index def index
ids = params["section_ids"].split ',' crns = params["crns"].split ','
@schedule = Schedule.new ids @schedule = Schedule.new crns
render plain: @schedule.to_ical # render a plaintext iCal file render plain: @schedule.to_ical # render a plaintext iCal file
end end
end end
...@@ -16,13 +16,11 @@ class ApplicationController < ActionController::Base ...@@ -16,13 +16,11 @@ class ApplicationController < ActionController::Base
def set_cart def set_cart
@cart = JSON.parse(cookies[:cart]) @cart = JSON.parse(cookies[:cart])
@cart = @cart.reject { |id| CourseSection.find_by_id(id).nil? } @cart = @cart.reject { |crn| CourseSection.find_by_crn(crn).nil? }
cookies[:cart] = @cart.to_json cookies[:cart] = @cart.to_json
end end
def set_cookies def set_cookies
cookies[:crns] = "" if cookies[:crns].nil?
cookies[:section_ids] = "" if cookies[:section_ids].nil?
cookies[:cart] = "[]" if cookies[:cart].nil? cookies[:cart] = "[]" if cookies[:cart].nil?
end end
end end
...@@ -3,17 +3,23 @@ class SchedulesController < ApplicationController ...@@ -3,17 +3,23 @@ class SchedulesController < ApplicationController
include SchedulesHelper include SchedulesHelper
def show def show
valid_ids = @cart.reject { |id| valid_crns = @cart.reject { |crn|
s = CourseSection.find_by_id(id) s = CourseSection.find_by_crn(crn)
s.nil? || s.start_time == "TBA" || s.end_time == "TBA" s.nil? || s.start_time == "TBA" || s.end_time == "TBA"
} }
@all = valid_ids.map { |id| CourseSection.find_by_id id } @all = valid_crns.map { |crn|
@events = generate_fullcalender_events(valid_ids) a = CourseSection.where(crn: crn).sort_by { |s| s.course.semester.id }
a.first
}
@events = generate_fullcalender_events(@all)
end end
def view def view
@all = params[:section_ids].split(',').map { |id| CourseSection.find_by_id id.to_s } @all = params[:crns].split(',').map { |crn|
@events = generate_fullcalender_events(params[:section_ids].split(',')) a = CourseSection.where(crn: crn).sort_by { |s| s.course.semester.id }
a.first
}
@events = generate_fullcalender_events(@all)
end end
end end
...@@ -8,14 +8,15 @@ class SessionsController < ApplicationController ...@@ -8,14 +8,15 @@ class SessionsController < ApplicationController
end end
def cart def cart
section_id = params[:section_id] section_crn = params[:crn]
if @cart.include?(section_id) if @cart.include?(section_crn.to_s)
@cart.reject! { |id| section_id == id } @cart.reject! { |crn| section_crn.to_s == crn.to_s }
else else
@cart << section_id @cart << section_crn
end end
puts @cart
cookies[:cart] = @cart.to_json cookies[:cart] = @cart.to_json
render json: @cart.to_json render json: @cart.to_json
end end
...@@ -25,8 +26,7 @@ class SessionsController < ApplicationController ...@@ -25,8 +26,7 @@ class SessionsController < ApplicationController
crns.each { |crn| crns.each { |crn|
s = CourseSection.find_by_crn(crn) s = CourseSection.find_by_crn(crn)
next if s.nil? next if s.nil?
section_id = s.id.to_s @cart << crn.to_s unless @cart.include?(crn.to_s)
@cart << section_id unless @cart.include?(section_id)
} }
cookies[:cart] = @cart.to_json cookies[:cart] = @cart.to_json
redirect_to schedule_path redirect_to schedule_path
......
...@@ -9,9 +9,8 @@ module SchedulesHelper ...@@ -9,9 +9,8 @@ module SchedulesHelper
"U": Date.new(2019, 1, 20) "U": Date.new(2019, 1, 20)
}.freeze }.freeze
def generate_fullcalender_events(section_ids) def generate_fullcalender_events(sections)
section_ids.map do |id| sections.map do |s|
s = CourseSection.find_by_id id
s.days.split('').map do |day| s.days.split('').map do |day|
formatted_date = DAYS[day.to_sym].to_s.tr('-', '') formatted_date = DAYS[day.to_sym].to_s.tr('-', '')
time = Time.parse(s.start_time).strftime("%H%M%S") time = Time.parse(s.start_time).strftime("%H%M%S")
......
module SearchHelper module SearchHelper
def in_cart?(id) def in_cart?(crn)
@cart.include? id.to_s @cart.include? crn.to_s
end end
class GenericQueryData class GenericQueryData
......
...@@ -3,11 +3,11 @@ require 'time' ...@@ -3,11 +3,11 @@ require 'time'
# Creates a iCal object given a list of section ids # Creates a iCal object given a list of section ids
class Schedule class Schedule
def initialize(ids) def initialize(crns)
@cal = Icalendar::Calendar.new @cal = Icalendar::Calendar.new
@cal.x_wr_calname = 'GMU Schedule' @cal.x_wr_calname = 'GMU Schedule'
@course_sections = ids.map { |id| CourseSection.find_by_id id } @course_sections = crns.map { |crn| CourseSection.find_by_crn crn }
@course_sections.compact! @course_sections.compact!
load_events load_events
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<p><b class="subj"><%= "#{section.name}" %></b>: <%= section.title %></p> <p><b class="subj"><%= "#{section.name}" %></b>: <%= section.title %></p>
<% if editable %> <% if editable %>
<% if in_cart? section.id %> <% if in_cart? section.crn %>
<span class="float-right text-center add-remove-btn"><i id="icon" class="fas fa-minus"></i><br/><span class="text">Remove</span></span> <span class="float-right text-center add-remove-btn"><i id="icon" class="fas fa-minus"></i><br/><span class="text">Remove</span></span>
<% else %> <% else %>
<span class="float-right text-center add-remove-btn"><i id="icon" class="fas fa-plus"></i><br/><span class="text">Add</span></span> <span class="float-right text-center add-remove-btn"><i id="icon" class="fas fa-plus"></i><br/><span class="text">Add</span></span>
......
...@@ -112,7 +112,7 @@ def main ...@@ -112,7 +112,7 @@ def main
# wipe_db # wipe_db
parser = PatriotWeb::Parser.new parser = PatriotWeb::Parser.new
semesters = parser.parse_semesters[0..12] # expand to include however many semesters you want semesters = parser.parse_semesters[0..6] # expand to include however many semesters you want
courses = nil courses = nil
semesters.each do |semester| semesters.each do |semester|
......
...@@ -2,9 +2,9 @@ require 'test_helper' ...@@ -2,9 +2,9 @@ require 'test_helper'
class API::SchedulesControllerTest < ActionDispatch::IntegrationTest class API::SchedulesControllerTest < ActionDispatch::IntegrationTest
test "should generate schedule" do test "should generate schedule" do
ids = [course_sections(:cs112001).id, course_sections(:cs112002).id] crns = [course_sections(:cs112001).crn, course_sections(:cs112002).crn]
get api_schedules_path section_ids: ids.join(','), semester_id: semesters(:fall2018).id get api_schedules_path crns: crns.join(','), semester_id: semesters(:fall2018).id
# DTSTAMP and UID lines uniquely identify events, so we can't test against them. # DTSTAMP and UID lines uniquely identify events, so we can't test against them.
# so remove all the lines starting with them. # so remove all the lines starting with them.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment