@stop
@section('javascript')
// --------------------------------------------------------------------------
// Page load
// --------------------------------------------------------------------------
// Fetch criteria of first outcome
fetchCriteria($('#select-outcome'));
//
if($('#select-template').find(':selected').data('template-id') == '0')
{
// Hide table
$('#rubric-container').hide();
}
else
{
alert('else');
loadRubric();
}
// Find way to get the original name for use farther below. This doesn't work.
var originalName = $('#rubric-name').val();
alert(originalName);
// --------------------------------------------------------------------------
// Functions
// --------------------------------------------------------------------------
// Fetch criteria associated to a specific learning outcome
function fetchCriteria(outcome)
{
$.post
(
"{{ URL::route('fetchCriteria') }}",
{ id: outcome.find(':selected').data('outcome-id')},
function(data)
{
$('#select-criterion').empty();
data.forEach( function (arrayItem)
{
// If criterion does not belong to a user, display it
if(arrayItem.user_id==null)
{
$('#select-criterion')
.append('');
}
// If it does, but the user is the one logged on, display.
// Otherwise, ignore
else if(arrayItem.user_id!="" && {{{Auth::id()}}}==arrayItem.user_id )
{
$('#select-criterion')
.append('');
}
});
// Add custom option
$('#select-criterion')
.append('');
$('#select-criterion').prop('disabled', false);
$('#button-add-criterion').prop('disabled', false);
}
);
}
// Add a new criterion to the rubric
function addCriterion()
{
if(!$('tbody').children().length)
{
$('#rubric-container').show();
}
var id= parseInt($('#select-criterion').find(':selected').data('criterion-id'));
// If criterion already exists in the db
if(id!=0)
{
// Check for duplicates
var duplicates=false;
$('tbody tr').each(function()
{
if ($(this).data('criterion-id') == id)
{
duplicates=true;
}
});
if(duplicates)
{
$('#js-error-row').show();
$('#js-error-row').find('#error-message').text('Error: The selected criterion was already added.');
return;
}
else
{
$('#js-error-row').hide();
}
$.post
(
"{{ URL::route('fetchCriterion') }}",
{ id: id},
function(data)
{
// If saved criterion is not custom, it can be removed but not edited
if(data.user_id==null)
{
$('table tbody')
.append('
'
+'
'+data.name+'
'
+'
'+data.description12+'
'
+'
'+data.description34+'
'
+'
'+data.description56+'
'
+'
'+data.description78+'
'
+'
');
}
// If saved criterion is custom, it can be removed and edited
else
{
$('table tbody')
.append('
'
+'
Custom
'
+'
'
+'
'
+'
'
+'
'
+'
');
}
// Enable X-Edtable on this new row
$('.editable').editable({
unsavedclass: null,
rows: "4"
});
}
);
}
// If criterion is completely new and custom
else
{
$('table tbody')
.append('
'
+'
Custom
'
+'
'
+'
'
+'
'
+'
'
+'
');
// Enable X-Edtable on this new row
$('.editable').editable({
unsavedclass: null,
rows: "4"
});
}
}
// Fetch single criterion
function fetchCriterion(criterion)
{
$.post
(
"{{ URL::route('fetchCriterion') }}",
{ id: outcome.find(':selected').data('outcome-id')},
function(data)
{
$('#select-criterion').empty();
data.forEach( function (arrayItem)
{
$('#select-criterion')
.append('');
});
$('#select-criterion').append('');
}
);
}
// Load a template
function loadTemplate()
{
// Set expected values to the default 70 / 5
$('#expected_percentage option[value="70"]').prop('selected', true);
$('#expected_points option[value="5"]').prop('selected', true);
$.post
(
"{{ URL::to('loadTemplate') }}",
{ id: $('#select-template').find(':selected').data('template-id')},
function(data)
{
// Show the container and empty all rows
$('#rubric-container').show();
$('tbody').empty();
//Set the name of the rubric
$('#rubric-name').val(data.name);
// Set the contents of the rubric
var contents = JSON.parse(data.contents);
contents.forEach(function (criterion)
{
$('table tbody')
.append('
'
+'
'+criterion.name+'
'
+'
'+criterion.description12+'
'
+'
'+criterion.description34+'
'
+'
'+criterion.description56+'
'
+'
'+criterion.description78+'
'
+'
');
// Enable X-Edtable on this new row
$('.editable').editable({
unsavedclass: null,
rows: "4"
});
});
}
);
}
// Load a rubric
function loadRubric()
{
$.post
(
"{{ URL::to('professor/loadRubric') }}",
{ id: $('#select-template').find(':selected').data('rubric-id')},
function(data)
{
// Show the container and empty all rows
$('#rubric-container').show();
$('tbody').empty();
//Set the name of the rubric
$('#rubric-name').val(data.name);
// Set the contents of the rubric
var contents = JSON.parse(data.contents);
contents.forEach(function (criterion)
{
// If saved criterion is not custom, it can be removed but not edited
if(criterion.user_id==null)
{
$('table tbody')
.append('
'
+'
'+criterion.name+'
'
+'
'+criterion.description12+'
'
+'
'+criterion.description34+'
'
+'
'+criterion.description56+'
'
+'
'+criterion.description78+'
'
+'
');
}
// If saved criterion is custom, it can be removed and edited
else
{
$('table tbody')
.append('
'
+'
Custom
'
+'
'
+'
'
+'
'
+'
'
+'
');
}
// Enable X-Edtable on this new row
$('.editable').editable({
unsavedclass: null,
rows: "4"
});
});
}
);
}
// Checks whether the user wants to save a rubric with a name that already exists
function nameExists()
{
var duplicates = false;
$('#select-template option').each(function()
{
if($('#rubric-name').val().trim()===$(this).text().trim())
{
duplicates = true;
return;
}
});
return duplicates;
}
// --------------------------------------------------------------------------
// Events
// --------------------------------------------------------------------------
// When trying to change the template, ask the user to confirm and reset the
// rubric if s/he does
$('#select-template').on('change', function()
{
// If changing to custom template, reset
if($('#select-template').find(':selected').data('template-id')=='0')
{
$('#rubric-container').hide();
$('#rubric-name').val('');
$('tbody').empty();
}
// Otherwise, load the selected template/rubric
else
{
if($('#select-template').find(':selected').hasClass('template'))
{
loadTemplate();
}
else
{
loadRubric();
}
}
});
// When a learning outcome changes, update its criteria
$('#select-outcome').on('change', function()
{
fetchCriteria($(this));
});
// When the add button is clicked
$('#button-add-criterion').on('click', function(e)
{
//Prevent page refresh
e.preventDefault();
//Add new criterion
addCriterion();
});
// When save button is clicked
$('#button-save-rubric').on('click', function(e)
{
//Prevent page refresh
e.preventDefault();
// Empty field validation
var emptyFields=false;
$('td').each(function()
{
if ($(this).text() == "" || $(this).text() == "Empty")
{
emptyFields=true;
}
});
// If any fields are empty, display error and return
if(emptyFields || $.trim($('#rubric-name').val())=='')
{
$('#js-error-row').show();
$('#js-error-row').find('#error-message').text('Error: Please fill all the fields.');
return;
}
else
{
$('#js-error-row').hide();
}
//---------------------------------------------------vvv-------------------------------------------------------------------
//TODO update assessments
var criterionObject = new Object();
var criteriaArray = new Array();
// For each criterion in the rubric, get its value and put it into an array
$('tbody tr').each(function( index )
{
criterionObject.id = $(this).data('criterion-id');
criterionObject.outcome_id = $(this).data('assoc-outcome-id');
criterionObject.name = $(this).children('td:nth-child(1)').text();
criterionObject.description12 = $(this).children('td:nth-child(2)').text();
criterionObject.description34 = $(this).children('td:nth-child(3)').text();
criterionObject.description56 = $(this).children('td:nth-child(4)').text();
criterionObject.description78 = $(this).children('td:nth-child(5)').text();
// Clone the object and push it into the array
var clone = jQuery.extend({}, criterionObject);
criteriaArray.push(clone);
});
// If activity does not have a rubric, create it
if($('#assigned_rubric').length === 0)
{
// Check whether a rubric with the written name already exists
// If it does, display an error.
if(nameExists())
{
$('#js-error-row').show();
$('#js-error-row').find('#error-message').text('Error: A rubric or template with that name already exists.');
return;
}
$('#js-error-row').hide();
// Create
$.post
(
"{{ URL::to('professor/saveRubric') }}",
{
name: $('#rubric-name').val(),
activity_id: parseInt($('#activity_id').val()),
contents: JSON.stringify(criteriaArray),
expected_percentage: $('#expected_percentage').find(':selected').val(),
expected_points: $('#expected_points').find(':selected').val()
},
function(data)
{
location.reload(true);
}
);
}
// Else, update it
else
{
// Check whether a rubric with the written name already exists and if the
// selected template's id equals assigned id. If the name exists, but the
// ids don't match, show error
alert(originalName);
if(originalName != $('#rubric-name').val() && nameExists())
{
$('#js-error-row').show();
$('#js-error-row').find('#error-message').text('Error: You must change the name of the rubric.');
return;
}
$('#js-error-row').hide();
// Update database
$.post
(
"{{ URL::to('professor/updateRubric') }}",
{
id: $('#assigned_rubric').data('assigned-rubric'),
name: $('#rubric-name').val(),
contents: JSON.stringify(criteriaArray),
expected_percentage: $('#expected_percentage').find(':selected').val(),
expected_points: $('#expected_points').find(':selected').val()
},
function(data)
{
location.reload();
}
);
}
});
//-----------------------------------------------------^^^------------------------------------------------------------------
// TODO Move this somewhere else
// When the delete button is clicked
$('#button-delete-rubric').on('click', function(e)
{
// Delete from database
$.post
(
"{{ URL::to('professor/deleteRubric') }}",
{
id: $('#select-template').find(':selected').data('rubric-id')
},
function(data)
{
location.reload();
}
);
});
// Remove a criterion and hide the table if it was the only one
$('table').on('click', '.glyphicon-remove', function(e)
{
$(this).closest('tr').remove();
if(!$('tbody').children().length)
{
$('#rubric-container').hide();
}
});
@stop