Skip to content

Commit

Permalink
Allow different categories of handout exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
maxg committed Feb 2, 2016
1 parent fa6c55c commit 00a9eb8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
3 changes: 2 additions & 1 deletion web/handout/course-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
HANDOUT_SEMESTER = 'IAP 2000';
HANDOUT_AUTHORS = 'Copyright Alice and Bob, all imaginary rights reserved.';
HANDOUT_HOME = 'http://example.com/6.HANDX/www/ia00/';
HANDOUT_EXERCISES = [ 'reading-exercises' ];
// optional: handx server for server-checked exercises
// HANDOUT_EXERCISES = 'https://example.com/handx/ia00/';
// HANDOUT_HANDX = 'https://example.com/handx/ia00/';
2 changes: 1 addition & 1 deletion web/handout/deliver/deliver.phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ page.onCallback = function(hasExercises) {

// remove solutions from the page
page.evaluate(function(handoutID) {
$('.exercise-panel').attr('data-ex-remote', HANDOUT_EXERCISES + 'submit.php')
$('.exercise-panel').attr('data-ex-remote', HANDOUT_HANDX + 'submit.php')
.attr('data-ex-handout', handoutID);
$('.exercise-choice').removeAttr('data-ex-expected');
$('.exercise-choice').removeAttr('data-ex-regex');
Expand Down
31 changes: 20 additions & 11 deletions web/handout/handout-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// configuration
HANDOUT_PREVIEW = (document.location.protocol === 'file:') && ( ! window.callPhantom);

if (typeof HANDOUT_EXERCISES === "undefined") { HANDOUT_EXERCISES = false; }
if (typeof HANDOUT_HANDX === "undefined") { HANDOUT_HANDX = false; }

// load JavaScript by injecting a <script> tag
function require(url, callback) {
Expand Down Expand Up @@ -92,6 +92,11 @@ function render() {
$(this).addClass('converted');
});

// identify exercises
$(HANDOUT_EXERCISES.map(function(category) {
return '.' + category;
}).join(',')).addClass('exercises');

// assign IDs to all headers
[ 'h1', 'h2', 'h3' ].forEach(function(h) { // higher-level headers get cleaner IDs
$(h).each(function() {
Expand All @@ -102,13 +107,15 @@ function render() {
});

// convert exercise blocks
$('.exercises:not(.converted)').each(function() {
convertExercises(this);
$(this).addClass('converted');
HANDOUT_EXERCISES.forEach(function(category) {
$('.' + category + '.exercises:not(.converted)').each(function() {
convertExercises(this, category);
$(this).addClass('converted');
});
});
$('.exercises').first().each(function() {
if (HANDOUT_EXERCISES) {
$('main').prepend($('<iframe class="exercises-status">').attr('src', HANDOUT_EXERCISES + 'status.php'));
if (HANDOUT_HANDX) {
$('main').prepend($('<iframe class="exercises-status">').attr('src', HANDOUT_HANDX + 'status.php'));
}
});

Expand Down Expand Up @@ -263,13 +270,14 @@ function convertMarkdownTextFields(text) {
});
}

function convertExercises(node) {
function convertExercises(node, category) {
var categoryName = category.replace(/-/g, ' ');
var container = $(node).attr('id', uniqueIdentifier('id', 'ex'))
.addClass('panel-group')
.prepend('<h4 class="text-danger">Reading exercises</h4>');
.addClass('exercises panel-group')
.prepend('<h4 class="text-danger">' + categoryName + '</h4>');

$('h1', container).each(function() {
convertExercise(container, $(this).text(), $(this).nextUntil('hr'));
convertExercise(container, category, $(this).text(), $(this).nextUntil('hr'));
});

// remove original exercise titles and dividers
Expand All @@ -284,13 +292,14 @@ function convertExercises(node) {
});
}

function convertExercise(container, title, content) {
function convertExercise(container, category, title, content) {
var exerciseName = uniqueIdentifier('data-outline', title, container);
var exerciseId = container.attr('id') + '-' + exerciseName;
var panel = $('<div class="panel panel-danger">')
.append($('<div class="panel-collapse collapse exercise-panel">')
.attr('id', exerciseId)
.attr('data-outline', exerciseName)
.attr('data-ex-category', category)
.append('<div class="panel-body">'));
var body = content.wrapAll(panel).parent();

Expand Down
1 change: 1 addition & 0 deletions web/handout/handout-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function createExercise() {
var exercise = {
id: $(this).parents().map(function() { return $(this).data('outline'); })[0] +
'/' + $(this).data('outline'),
category: $(this).data('ex-category'),
node: $(this),
parts: $('.exercise-part', this).map(function() {
return {
Expand Down

0 comments on commit 00a9eb8

Please sign in to comment.