Module:Indicator datasets table: Difference between revisions

From Visual Data Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Reverted
Line 51: Line 51:
     -- headerRow:tag('th'):wikitext(dataset.schemas.Dataset['date'])
     -- headerRow:tag('th'):wikitext(dataset.schemas.Dataset['date'])
     local formattedDate = p.formatDate(dataset.schemas.Dataset['date'])
     local formattedDate = p.formatDate(dataset.schemas.Dataset['date'])
     headerRow:tag('th'):wikitext(formattedDate .. '<br/>'):wikitext('<small>{{int:webmo-survey}}{{int:webmo-' .. dataset.schemas.Dataset['dataset type'] .. '}}</small>')
     headerRow:tag('th'):wikitext(formattedDate .. '<br/>'):wikitext('<small>\'\'Test\'\'{{int:webmo-' .. dataset.schemas.Dataset['dataset type'] .. '}}</small>')
     end
     end



Revision as of 14:53, 6 May 2024

Documentation for this module may be created at Module:Indicator datasets table/doc

local p = {}

function p.getDatasets(frame)
    local articleids = frame.args.articleids
    if not articleids then
        return "Error: articleids not provided"
    end
    
    -- Entferne alle Kommas und Leerzeichen am Ende von articleids
    articleids = mw.ustring.gsub(articleids, '[,%s]*$', '')
    
    local ids = mw.text.split(articleids, ",")
    local datasets = {}
    local titleobject
    
    for _, id in ipairs(ids) do
        titleobject = mw.title.new(tonumber(id))
        local existspage = titleobject.exists
        
        if existspage then
            local content = titleobject:getContent()
            local dataset = mw.text.jsonDecode(content)
            -- Überprüfe, ob belongs to der aktuellen PageID entspricht
            if tonumber(dataset.schemas.Dataset['belongs to']) == mw.title.getCurrentTitle().id then
                table.insert(datasets, dataset)
            end
        else
            return "Error: Page not found for ID " .. id
        end
    end
    return datasets
end

function p.generateTable(frame)
    local datasets = p.getDatasets(frame)
    if not datasets or #datasets == 0 then
        return "" -- Keine Datensätze gefunden, gib leeren String zurück
    end
    
    -- Sortiere die Datensätze nach dem Datum
    table.sort(datasets, function(a, b) return a.schemas.Dataset['date'] < b.schemas.Dataset['date'] end)

    local html = mw.html.create()
    local tableNode = html:tag('table'):addClass('wikitable')

    -- Erstelle die Tabelle mit Legenden aus dem ersten Datensatz
    local headerRow = tableNode:tag('tr')
    headerRow:tag('th'):wikitext('Class')
    headerRow:tag('th'):wikitext('Sub-class')
    for _, dataset in ipairs(datasets) do
    	-- headerRow:tag('th'):wikitext(dataset.schemas.Dataset['date'])
    	local formattedDate = p.formatDate(dataset.schemas.Dataset['date'])
    	headerRow:tag('th'):wikitext(formattedDate .. '<br/>'):wikitext('<small>\'\'Test\'\'{{int:webmo-' .. dataset.schemas.Dataset['dataset type'] .. '}}</small>')
    end

    -- Füge die Datenzeilen hinzu
    for _, classData in ipairs(datasets[1].schemas.Dataset.classes) do
        local class = classData['class name']
        for _, subClassData in ipairs(classData['sub-classes']) do
            local subClass = subClassData['sub-class name']
            local row = tableNode:tag('tr')
            row:tag('td'):wikitext(class)
            row:tag('td'):wikitext(subClass)
            for _, dataset in ipairs(datasets) do
                local value = "N/A"
                for _, currentClassData in ipairs(dataset.schemas.Dataset.classes) do
                    if currentClassData['class name'] == class then
                        for _, currentSubClassData in ipairs(currentClassData['sub-classes']) do
                            if currentSubClassData['sub-class name'] == subClass then
                                value = ""
                                for _, currentSubSubClassData in ipairs(currentSubClassData['sub-sub-classes']) do
                                    value = value .. (currentSubSubClassData.value or "N/A") .. ", "
                                end
                                value = value:sub(1, -3) -- Entferne das letzte Komma und Leerzeichen
                                break
                            end
                        end
                        break
                    end
                end
                row:tag('td'):wikitext(value)
            end
        end
    end
    return tostring(html)
end

-- Funktion zur Formatierung des Datums gemäß den Nutzereinstellungen
function p.formatDate(date)
    return mw.language.getContentLanguage():formatDate(date)
end

return p