From ae15c16a7a94e86d6debf3cc385e24fecac889e2 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 12 Jan 2018 17:44:01 +0530 Subject: Change in js, templates and custom_filters - Add js function to download csv data for course status - Show course status - Add new custom functions to view student status for a course --- yaksh/static/yaksh/js/course.js | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/course.js b/yaksh/static/yaksh/js/course.js index f0d03e2..162e0b8 100644 --- a/yaksh/static/yaksh/js/course.js +++ b/yaksh/static/yaksh/js/course.js @@ -66,4 +66,76 @@ $("#send_mail").click(function(){ return status; }); +// Download course status as csv +function exportTableToCSV($table, filename) { + var $headers = $table.find('tr:has(th)') + ,$rows = $table.find('tr:has(td)') + + // Temporary delimiter characters unlikely to be typed by keyboard + // This is to avoid accidentally splitting the actual contents + ,tmpColDelim = String.fromCharCode(11) // vertical tab character + ,tmpRowDelim = String.fromCharCode(0) // null character + + // actual delimiter characters for CSV format + ,colDelim = '","' + ,rowDelim = '"\r\n"'; + + // Grab text from table into CSV formatted string + var csv = '"'; + csv += formatRows($headers.map(grabRow)); + csv += rowDelim; + csv += formatRows($rows.map(grabRow)) + '"'; + + // Data URI + var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); + + // For IE (tested 10+) + if (window.navigator.msSaveOrOpenBlob) { + var blob = new Blob([decodeURIComponent(encodeURI(csv))], { + type: "text/csv;charset=utf-8;" + }); + navigator.msSaveBlob(blob, filename); + } else { + $(this) + .attr({ + 'download': filename,'href': csvData + }); + } + + function formatRows(rows){ + return rows.get().join(tmpRowDelim) + .split(tmpRowDelim).join(rowDelim) + .split(tmpColDelim).join(colDelim); + } + // Grab and format a row from the table + function grabRow(i,row){ + var $row = $(row); + var $cols = $row.find('td'); + if(!$cols.length) $cols = $row.find('th'); + + return $cols.map(grabCol) + .get().join(tmpColDelim); + } + // Grab and format a column from the table + function grabCol(j,col){ + var $col = $(col), + $text = $col.text(); + + return $text.replace('"', '""').replace("View Unit Status", ''); // escape double quotes + + } +} + + +$("#export").click(function (event) { + var outputFile = $("#course_name").val().replace(" ", "_") + '.csv'; + + exportTableToCSV.apply(this, [$('#course_table'), outputFile]); +}); + }); + +function view_status(unit){ + title_list = $(unit).attr("title").split("/"); + $(unit).attr("title", title_list.join("\n")); +} -- cgit