Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function ($) {
- "use strict";
- var optionsDefaults = {
- /* action='download' options */
- filename: "table.csv",
- /* action='output' options */
- appendTo: "body",
- /* general options */
- separator: ",",
- newline: "\n",
- quoteFields: true,
- trimContent: true,
- excludeColumns: "",
- excludeRows: ""
- };
- var options = {};
- function quote(text) {
- return "\"" + text.replace(/"/g, "\"\"") + "\"";
- }
- function convert(table) {
- var output = "";
- var rows = table.find("tr").not(options.excludeRows);
- var numCols = rows.first().find("td,th").filter(":visible").not(options.excludeColumns).length;
- rows.each(function (ignore, elem) {
- $(elem).find("td,th").filter(":visible").not(options.excludeColumns).each(function (i, col) {
- var column = $(col);
- var content = options.trimContent ? $.trim(column.text()) : column.text();
- content = content.replace('Usuń', '').replace('(pokaż)', '');
- output += options.quoteFields ? quote(content) : content;
- if (i !== numCols - 1) {
- output += options.separator;
- } else {
- output += options.newline;
- }
- });
- });
- return output;
- }
- $.fn.table2csv = function (action, opt) {
- if (typeof action === "object") {
- opt = action;
- action = "download";
- } else if (action === undefined) {
- action = "download";
- }
- if (typeof action !== "string") {
- throw new Error("\"action\" argument must be a string");
- }
- if (opt !== undefined && typeof opt !== "object") {
- throw new Error("\"options\" argument must be an object");
- }
- options = $.extend({}, optionsDefaults, opt);
- var table = this.filter("table");
- if (table.length <= 0) {
- throw new Error("table2csv must be called on a <table> element");
- }
- if (table.length > 1) {
- throw new Error("converting multiple table elements at once is not supported yet");
- }
- return convert(table);
- };
- })(jQuery);
- function download(filename, sheets) {
- var csvContent = '';
- for (var sheetName in sheets) {
- if (sheets.hasOwnProperty(sheetName)) {
- csvContent += 'SheetName: ' + sheetName + '\n';
- csvContent += sheets[sheetName] + '\n\n';
- }
- }
- var element = document.createElement("a");
- element.setAttribute("href", "data:text/csv;charset=utf-8,\uFEFF" + encodeURIComponent(csvContent));
- element.setAttribute("download", filename + ".csv");
- element.style.display = "none";
- document.body.appendChild(element);
- element.click();
- document.body.removeChild(element);
- }
- jQuery(document).ready(function () {
- if (jQuery('#et-export-table').length < 1) {
- jQuery('.wpProQuiz_tab_wrapper').append('<button class="button-secondary" id="et-export-table">Eksportuj</button>');
- }
- jQuery('#et-export-table').on('click', function () {
- console.log('button clicked');
- jQuery('#wpProQuiz_historyPageLimit').val(5);
- function getDataAsSheets(callback) {
- var sheets = {};
- // Dodaj regułę CSS przed pobraniem danych
- var cssContent = '/* CSS for WP List Table */\n';
- cssContent += '.wp-list-table.widefat tr { display: block!important; }\n\n';
- sheets['css'] = cssContent;
- // Pobierz dane z historii wyników quizu
- var historyData = jQuery("#wpProQuiz_historyLoadContext > table").table2csv({
- separator: ',',
- newline: '\n',
- quoteFields: true,
- excludeColumns: '',
- excludeRows: '',
- trimContent: true,
- action: 'return'
- });
- sheets['quiz_results'] = removeEmptyRows(historyData);
- // Funkcja do usuwania pustych wierszy z arkusza
- function removeEmptyRows(data) {
- var lines = data.split('\n');
- var nonEmptyLines = lines.filter(function (line) {
- return line.trim() !== '';
- });
- return nonEmptyLines.join('\n');
- }
- // Pobierz dane dla każdego użytkownika
- var promises = [];
- jQuery('#wpProQuiz_statistics_form_data tr').each(function (index, element) {
- var to_click = jQuery(element).find('.user_statistic');
- var sheetName = to_click.text().trim(); // Pobierz tekst z elementu .user_statistic jako nazwę arkusza
- jQuery(to_click).click();
- var promise = new Promise(function (resolve, reject) {
- var intervalId = setInterval(function () {
- var tableLoaded = jQuery('#wpProQuiz_user_content table').length > 0;
- if (tableLoaded) {
- clearInterval(intervalId);
- var userData = jQuery("#wpProQuiz_user_content .wp-list-table").table2csv({
- separator: ',',
- newline: '\n',
- quoteFields: true,
- excludeColumns: '',
- excludeRows: '',
- trimContent: true,
- action: 'return'
- });
- sheets['user_results_' + sheetName] = removeEmptyRows(userData);
- resolve();
- }
- }, 100);
- });
- promises.push(promise);
- });
- Promise.all(promises).then(function () {
- callback(sheets);
- });
- }
- // Funkcja do pobierania danych po zakończeniu pobierania historii i eksportu
- function getHistoryDataAndExport() {
- getDataAsSheets(function (sheets) {
- // Wywołujemy tutaj eksport danych, gdy wszystkie dane zostały zebrane
- download('combined_data', sheets);
- });
- }
- // Funkcja do pobierania danych po zakończeniu pobierania historii
- function getHistoryData() {
- jQuery('#filter').trigger('click');
- setTimeout(function () {
- getHistoryDataAndExport();
- }, 2000);
- }
- getHistoryData();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement