Add download ranking table as csv file

This commit is contained in:
Zhao 2022-10-08 17:43:55 +07:00
parent 43ffca160b
commit e584c75991
2 changed files with 69 additions and 1 deletions

View file

@ -4,7 +4,7 @@
{% block after_rank_head %} {% block after_rank_head %}
{% if has_rating %} {% if has_rating %}
<th>{{ _('Rating') }}</th> <th class="rating-column">{{ _('Rating') }}</th>
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View file

@ -61,6 +61,73 @@
}); });
</script> </script>
{% endif %} {% endif %}
{% if tab == 'ranking' %}
<script type="text/javascript">
$.fn.ignore = function(sel) {
return this.clone().find(sel || '>*').remove().end();
};
function download_ranking_as_csv() {
function clean_text(text) {
// Remove new line and leading/trailing spaces
text = text.replace(/(\r\n|\n|\r)/gm, '').trim();
// Escape double-quote with double-double-quote
text = text.replace(/"/g, '""');
return '"' + text + '"';
}
var csv = [];
$('table#users-table thead tr').each(function () {
var header = [];
$(this).find('th').each(function () {
var $col = $(this);
if ($col.hasClass('rating-column')) {
return;
} else if ($col.hasClass('username')) {
header.push(clean_text($col.text()));
header.push("Name");
} else if ($col.hasClass('problem-score-col')) {
header.push($col.attr("title"));
} else {
header.push(clean_text($col.text()));
}
});
csv.push(header.join(','));
});
$('table#users-table tbody tr').each(function () {
var row_data = [];
$(this).find('td').each(function () {
var $col = $(this);
if ($col.hasClass('rating-column')) {
return;
} else if ($col.hasClass('user-name')) {
row_data.push(clean_text($col.ignore('.fullname').ignore('.organization').text()));
row_data.push(clean_text($col.ignore('.user').ignore('.organization').text()));
} else {
row_data.push(clean_text($col.ignore('.solving-time').text()));
}
});
csv.push(row_data.join(','));
});
csv = csv.join('\n');
var filename = '{{ contest.key }}_' + moment().format('MMMM Do YYYY, h:mm:ss a'); + '.csv';
var link = document.createElement('a');
link.style.display = 'none';
link.setAttribute('target', '_blank');
link.setAttribute('href', 'data:text/csv;charset=utf-8,\uFEFF' + encodeURIComponent(csv));
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
{% endif %}
{% include "contest/media-js.html" %} {% include "contest/media-js.html" %}
{% endblock %} {% endblock %}
@ -86,6 +153,7 @@
<input id="show-virtual-checkbox" type="checkbox" style="vertical-align: bottom;"> <input id="show-virtual-checkbox" type="checkbox" style="vertical-align: bottom;">
<label id="show-virtual-label" for="show-virtual-checkbox" style="vertical-align: bottom; margin-right: 1em;">{{ _('Show virtual participation') }}</label> <label id="show-virtual-label" for="show-virtual-checkbox" style="vertical-align: bottom; margin-right: 1em;">{{ _('Show virtual participation') }}</label>
<img src="{{static('loading.gif')}}" style="height: 1em; display:none;" id="loading-gif"></img> <img src="{{static('loading.gif')}}" style="height: 1em; display:none;" id="loading-gif"></img>
<a href="#" onclick="download_ranking_as_csv()" style="float: right;">{{ _('Download as CSV') }}</a>
</div> </div>
{% include "contest/ranking-table.html" %} {% include "contest/ranking-table.html" %}
{% endblock %} {% endblock %}