Skip to content

Commit

Permalink
"Added symsyscourselist template and courselistviewer to symsys module"
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Brockbank committed Aug 25, 2012
1 parent 4b7ad02 commit 698a52a
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 0 deletions.
197 changes: 197 additions & 0 deletions deme_django/modules/symsys/templates/symsyscourselist/show.html
@@ -0,0 +1,197 @@
{% extends layout %}
{% load item_tags %}

{% block title %} {% endblock %}

{% block content %}

<script type="text/javascript">
//NEW SCRIPT FOR SYMSYSCOURSELIST

function courseLink(query, elementId) {
//Facilitates opening/closing of course info boxes
if (document.getElementById(elementId).style.display=='block') {
//Close course info div if it is currently open
hideSingleCourse(elementId);
} else {
//Get relevant information and open div
showSingleCourse(query, elementId);
}
}

function showSingleCourse(query, elementId) {
//Initialize course info box
if(document.getElementById(elementId).style.display=='none') {
document.getElementById(elementId).innerHTML='<p>Loading...</p>';
courseInfoAjax(query, elementId);
document.getElementById(elementId).style.display='block'; //display options: 'block' or 'inline'

}
}

function hideSingleCourse(elementId) {
//Close course info div for the course box with id 'elementId'
document.getElementById(elementId).style.display='none';
}

function showAllCourseInfo () {
//Temporarily hide "hide all" button to allow ajax calls to complete
document.getElementById('hideAllCourses').style.display='none';
document.getElementById('hideLoading').style.display='block';
setTimeout(function() {
document.getElementById('hideLoading').style.display='none';
document.getElementById('hideAllCourses').style.display='block';
}, 10000);
var courseElems = document.getElementsByClassName('courselink');
for(i=0; i<courseElems.length; i++) {
var courseElemClasses = courseElems[i].className;
courseName = courseElemClasses.split(" ");
courseName = courseName[1];
courseId = courseElems[i].id;
showSingleCourse(courseName, courseId);
}
}

function hideAllCourseInfo() {
var courseElems = document.getElementsByClassName('courselink');
for(i=0; i<courseElems.length; i++) {
courseElems[i].style.display='none';
}
}

function courseInfoAjax(query, elementId) {
if(document.getElementById(elementId).style.display=='none') {
//Handle ampersand character in MS&E courses
//if(query.indexOf("&")!= -1) {
query = query.replace("&", "%26");
//}
//Make Ajax call to explore courses and fill in relevant info for course that was clicked
var xmlDoc = $.ajax({
url: "/viewing/symsyscourselist/show.ajax?query="+query,
dataType: "html text xml"
});
xmlDoc.done(successFn);
xmlDoc.fail(failFn);
}

function successFn(data, code, object) {
//Traverse xml doc and gather relevant information
if(query.indexOf("%26")!= -1) {
//Replace call handles MS&E
query = query.replace("%26", "&");
}
if(query.indexOf(" ")!= -1) {
queryArr = query.split(" ");
var subj = queryArr[0];
var index = queryArr[1];
} else {
//For "show all classes" button, no whitespace in 'query' so regexp needed to get class name and number
var subjExp = /[A-Z&]+/i; //Must include '&' to account for MS&E
var indexExp = /[0-9]+[A-Z]*/i;
var subj = subjExp.exec(query);
var index = indexExp.exec(query);
}
//Initialize relevant fields to display
var title = "";
var description = "";
var gers = "";
var grade = "";
var units = "";
var quarter = "";
var professor = "";
//Find the proper course in the xml doc, handle errors
$(data).find("course").each(function() {
//Note: 'if' statement MUST use eq() notation because tag names "subject" and "code" are not unique
if($(this).find("subject").eq(0).text()==subj && $(this).find("code").eq(0).text()==index) {
title = getTitle($(this));
description = getDescription($(this));
gers = getGers($(this));
grade = getGrade($(this));
units = getUnits($(this));
quarter = getQuarter($(this));
}
});
//Handle courses no longer offered or not offered during the current year
if(quarter=="") {
if(description=="" && gers=="" && units=="") {
description = "Course no longer offered."
} else {
quarter = "Not offered this year";
}
}
var exploreText = "For more info: visit ";
subj = subj.toString();
if (subj.indexOf("&")!= -1) subj = subj.replace("&", "%26");
var exploreLink = "http://explorecourses.stanford.edu/CourseSearch/search?view=catalog&q=" + subj + index;
//Initialize html markup for course info
var titleHtml = '<p><span style="font-weight:bold; text-decoration:underline;">Title: </span>';
var descriptionHtml = '<p><span style="font-weight:bold; text-decoration:underline;">Description: </span>';
var gersHtml = '<p><span style="font-weight:bold; text-decoration:underline;">GERs: </span>';
var unitsHtml = '<p><span style="font-weight:bold; text-decoration:underline;">Units: </span>';
var gradeHtml = '<p><span style="font-weight:bold; text-decoration:underline;">Grading basis: </span>';
var quarterHtml = '<p><span style="font-weight:bold; text-decoration:underline;">Quarters offered: </span>';
//Fill in appropriate data gathered above and insert it into query div
var courseInfo = titleHtml+title+'</p>'+descriptionHtml+description+'</p>'+gersHtml+gers+'</p>'+unitsHtml+units+'</p>'+gradeHtml+grade+'</p>'+quarterHtml+quarter+'</p><p>'+exploreText+'<a href='+exploreLink+'>'+exploreLink+'</a></p>';
document.getElementById(elementId).innerHTML=courseInfo;
}

function failFn() {
//alert("Sorry, unable to connect to explore courses. Try again later!");
hideSingleCourse(elementId);
}

//AJAX functions for retrieving course info
function getTitle(current) {
return current.find("title").text();
}
function getDescription(current) {
return current.find("description").text();
}
function getGers(current) {
return current.find("gers").text();
}
function getGrade(current) {
return current.find("grading").text();
}
function getUnits(current) {
return current.find("unitsMax").text();
}
function getQuarter(current) {
var quarter = "";
current.find("section").each(function() {
if($(this).find("component").text()=="LEC" || $(this).find("component").text()=="SEM") {
var tempQuarter = $(this).find("term").text();
if(quarter.search(tempQuarter)== -1) {
quarter += '<p>&nbsp;&nbsp;&nbsp;&nbsp;'+tempQuarter;
quarter += "; ";
$(this).find("schedule").each(function() {
quarter += $(this).find("days").text();
quarter += $(this).find("startTime").text() + '-' + $(this).find("endTime").text();
});
quarter += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;Professor(s): ";
$(this).find("instructor").each(function() {
var tempProf = $(this).find("name").text();
if(tempProf != "") {
quarter += tempProf + "; ";
}
});
quarter += '</p>';
}
}
});
return quarter;
}
}

</script>

{% ifagentcan 'view TextDocument.body' item %}
<button id="showAllCourses" title="Click to see course info including schedule and description for all courses below (allow some time for loading)" onclick="showAllCourseInfo(); return false;" href="#">Click to see course info for all courses below</button>
<br/>
<div id="hideLoading" style="display:none;">Loading ExploreCourses info...</div>
<button id="hideAllCourses" title="Click to hide course info for all courses below" onclick="hideAllCourseInfo(); return false;" href="#">Click to hide course info for all courses below</button>

<div id="docbody" style="padding: 10px;">{{ body|safe }}</div>
{% endifagentcan %}

{% endblock content %}
67 changes: 67 additions & 0 deletions deme_django/modules/symsys/views.py
Expand Up @@ -5,6 +5,9 @@
from modules.symsys.models import *
from django.db.models import Q
from django.core.urlresolvers import reverse
import re
import urllib


#class SymsysFacultyGroupViewer(SymsysGroupViewer):
# accepted_item_type = Collection
Expand Down Expand Up @@ -409,4 +412,68 @@ def type_internshipcreate_html(self):
redirect = self.request.GET.get('redirect', reverse('item_url', kwargs={'viewer': 'htmladvertisement', 'noun': new_ad.pk}))
return HttpResponseRedirect(redirect)

#NEW MATERIAL 7/31
class SymsysCourseListViewer(TextDocumentViewer):
#To be used for core class listing, concentration listings, and any other course listings
accepted_item_type = HtmlDocument
viewer_name = 'symsyscourselist'

def item_show_html(self):
from functools import partial
#Next lines taken from cms/views.py, class TextDocumentViewer, item_show_html
self.context['action_title'] = ''
self.context['in_textdocument_show'] = True
self.context['highlighted_transclusion_id'] = self.request.GET.get('highlighted_transclusion')
self.context['highlighted_comment_id'] = self.request.GET.get('highlighted_comment')
self.require_ability('view ', self.item, wildcard_suffix=True)
#Replace basic markup with clickable markup for each class listed
#depts list is alphabetized!
depts = ['ANTHRO', '(?<!N)(?<!HUM)BIO', 'BIOE', 'BIOMEDIN', 'CEE', 'CME', 'COMM', 'CS', 'ECON', 'EDUC', '(?<!C)EE', 'ENGR', 'ETHICSOC', 'GENE', 'HRP', 'HUMBIO', 'IHUM', 'IPS', 'LAW', 'LINGUIST', 'MATH', '(?<!C)ME', 'MED', 'MS&amp;E', 'MUSIC', 'NBIO', 'NENS', 'PHIL', 'POLISCI', 'PSYCH', 'PUBLPOL', 'SLE', '(?<!ETHIC)SOC', 'STATS', 'STS', 'SYMSYS', 'URBANST']
regex = '(%s [0-9]+[A-Z]*[^</]+)' #troublesome characters: /
#index must be a list because integers are not mutable (see its use in replFunc for more info)
index = [0]

def replFunc(index, dept, matchobj):
#index and temp used to create a unique id for the div that will hold course info for each course
index[0] += 1
temp = "a"+str(index[0])
classEx = '(%s [0-9]+[A-Z]*)' % dept
className = re.search(classEx, matchobj.group(0)).group(0)
#Click '+' or 'show/hide info'
return replace % (matchobj.group(1), className, temp, re.sub(' ', '', className), temp)
#Click whole course name etc.
#return replace % (className, temp, matchobj.group(1), re.sub(' ', '', className), temp)

#Highlight '+'
#replace = '%s <a href="#" title="Click to see course info including schedule and description." onclick="courseLink(\'%s\', \'%s\'); return false;"> + </a><div class="courselink %s" id="%s" style="display:none; border:5px solid; padding:5px;"></div>'
#Highlight whole course name
#replace = '<a href="#" title="Click to see course info including schedule and description." onclick="courseLink(\'%s\', \'%s\'); return false;">%s</a><div class="courselink %s" id="%s" style="display:none; border:5px solid; padding:5px;"></div>'
#Highlight 'show/hide info'
replace = '%s <a href="#" title="Click to see course info including schedule and description." style="font-weight:normal;" onclick="courseLink(\'%s\', \'%s\'); return false;"> show/hide info </a><div class="courselink %s" id="%s" style="display:none; border:5px solid; padding:5px; font-weight:normal;"></div>'

new_body = self.item.body
for dept in depts:
pattern = regex % dept
new_body = re.sub(pattern, partial(replFunc, index, dept), new_body)
self.context['body'] = new_body
template = loader.get_template('symsyscourselist/show.html')
self.context['is_html'] = True
return HttpResponse(template.render(self.context))

def type_show_ajax(self):
#Formulate url based on query, make ajax call, return xml doc
query = self.request.GET['query']
#if(' ' in query):
#query = query.upper().replace(' ', '')
#Replace call below handles MS&E
#if('&' in query):
#query = query.replace('&', '%26')
query = query.upper().replace(' ', '').replace('&', '%26')
url = 'http://explorecourses.stanford.edu/CourseSearch/search?view=xml-20120105&q=%s'
url = url % query
#Make ajax call for url data
opener = urllib.FancyURLopener({})
f = opener.open(url)
return HttpResponse(f)


0 comments on commit 698a52a

Please sign in to comment.