FANDOM


--[[<pre> Shows Incubator course table
Debug console code
local frame = {args = { '1',['src']=8, 5 },getParent = function () local pArgs = {'1',['src']=8, 5 }  return { args = pArgs } end } mw.log('')
]]--
local p = {}
local libUtility = require('Module:Utility')
local libLang = require('Module:Lang')
local libTableBuilder = require('Module:Tablebuilder')
local libChart = require('Module:Chart')
local libFileLink = require('Module:File_link')
local libFlag = require('Module:Flag')
----
local tOrigCourse ={["fren"]=1,["enfr"]=1,["iten"]=1,["enit"]=1,["pten"]=1,["enpt"]=1,["esen"]=1,["enes"]=1,["deen"]=1,["ende"]=1}
local tCourses,tSrcCourses,tFastest,tCrowPrint = {},{},{},{}
local tCrowData = {}
 
--% Entry point for templates or pages, shows a table with courses and dates
--@ frame (table) A scribunto frame object
--: (string) An html table containing courses, dates, languages, and durations
function p.showcourses(frame)
    local tSortedRows   ={}
    local textsplit  = mw.text.split
    local tCourseCode,sFrom,sTo = "", "",""
    local sP1date, sP2date, sP3date = "", "",""
    local iPhase = 0
    local hStatRow 
    local tJsonCrow = mw.loadData("Module:Crow/json")
    local classCourse = require("Module:Crow/course")
    local courseId = 1;
 
    if tJsonCrow and type(tJsonCrow) == "table" then 
       tCrowData = tJsonCrow 
    else 
       tCrowData = mw.loadData('Module:Crow/data')
    end
 
    for sCourse,tTableData  in pairs(tCrowData) do
        tCourseCode = textsplit(sCourse,"#") -- find #
        sFrom = tCourseCode[1] -- get L1
        sTo = tCourseCode[2]   -- get L2
        sP1date = tTableData.P1date 
        sP2date = tTableData.P2date 
        sP3date = tTableData.P3date
        courseId = courseId + 1
        sortedAdd ( classCourse.new(sFrom, sTo, sP1date, sP2date, sP3date, tTableData.P1ref, tTableData.P2ref, tTableData.P3ref),tSortedRows,"desc"  )
    end
 
    local hTableCourses =  libTableBuilder.createTable("Liste des cours ("..courseId.. ")","", {"wikitable","sortable","courselist"})
    local hStatRow = libTableBuilder.createRow ()
    local mPhase1Link = getFileLink('phase1.png','25x30px','Incubator#Phase_1', 'Phase 1')
    local mPhase2Link = getFileLink('phase2.png','25x30px','Incubator#Phase_2', 'Phase 2')
    local mPhase3Link = getFileLink('phase3.png','25x30px','Incubator#Phase_3', 'Phase 3')
    local mPhasesLink = "" --getFileLink('','25x30px',Incubator,"Phases")
    --Adding header columns
    local tSortNumber = {["data-sort-type"]="number"}
    local tSortText = {["data-sort-type"]="text"}
    local tSortDate = {["data-sort-type"]="text"}
    local tCourseRow = {
    [1]={[1]=libTableBuilder.createCol("Langues",{["colspan"]="2"},true),
        [2]=libTableBuilder.createCol("Phase",{},true),
        [3]=libTableBuilder.createCol("Date♠ d'entrée dans la phase",{["colspan"]="3"},true),
        [4]=libTableBuilder.createCol("Jours dans la phase",{["colspan"]="3"},true)
        },
    [2]={
        [1]=libTableBuilder.createCol("Depuis (L1)",tSortText,true),
        [2]=libTableBuilder.createCol("Enseignant",tSortText,true),
        [3]=libTableBuilder.createCol(mPhasesLink,tSortNumber,true),
        [4]=libTableBuilder.createCol(mPhase1Link,tSortDate,true),
        [5]=libTableBuilder.createCol(mPhase2Link,tSortDate,true),
        [6]=libTableBuilder.createCol(mPhase3Link,tSortDate,true),
        [7]=libTableBuilder.createCol(mPhase1Link,tSortNumber,true),
        [8]=libTableBuilder.createCol(mPhase2Link,tSortNumber,true),
        [9]=libTableBuilder.createCol(mPhase1Link ..'+'.. mPhase2Link,tSortNumber,true)}
        }
    -- Header columns 
    for iRow, tCells in pairs(tCourseRow) do
        hStatRow = libTableBuilder.createRow ()
        hStatRow =libTableBuilder.appendColumns(hStatRow,tCells)
        hTableCourses = libTableBuilder.appendRow(hTableCourses,hStatRow) 
    end
    local tBackColor = {[1] = "courselist-phase1-row", [2] = "courselist-phase2-row"}
    local tCourseRow
    local tCourseData
    local tBackColorStyle = {[1] = "background-color:#E9E9E9", [2] = "background-color:#CCFFCC", [3] = "background-color:#AAFFAA"}
 
    for i=1,#tSortedRows  do 
        tCourseData = tSortedRows[i] 
        iPhase = tCourseData:getPhase()
        tCourseRow = {
            [1]=flagheader( tCourseData:getL1()) , -- From flag
            [2]=flagheader( tCourseData:getL2()) , -- To flag
            [3]=libTableBuilder.createCol(iPhase) , 
            [4]=frmtdate(tCourseData:getStartDate(),tCourseData:getRef(1)),
            [5]=frmtdate(tCourseData:getBetaDate(),tCourseData:getRef(2)),
            [6]=frmtdate(tCourseData:getReleaseDate(),tCourseData:getRef(3)),
            [7]=tCourseData:getDaysToCompletion(1) or "",                                         
            [8]=tCourseData:getDaysToCompletion(2) or "",   
            [9]=tCourseData:getDaysToCompletion(3) or ""   
        }
            hStatRow = libTableBuilder.createRow ({["style"]= tBackColorStyle[iPhase] })
--        hStatRow = libTableBuilder.createRow ({["class"]= tBackColor[iPhase] })
        libTableBuilder.appendColumns(hStatRow,tCourseRow)
        hTableCourses = libTableBuilder.appendRow(hTableCourses,hStatRow) 
    end
    return preprocess(tostring(hTableCourses))
end
--% Adds a new course to a sorted array based on phase and then language name
--@ tCourseDate (table) A course object containing information about a single course
--@ tSortedRows (table) An array containing course objects that will be sorted
--@ order (string) Ascending or descending order for the sort
--: (table) An tableBuilder table containing a formatted date
function sortedAdd (tCourseData, tSortedRows, order)
    local bAddedFlag = false
 
    local function sortedInsert(i)
        table.insert(tSortedRows,i,tCourseData)
        bAddedFlag = true
    end 
    local sortField , rowField 
    for i=1,#tSortedRows  do 
        if tSortedRows[i]  then
            sortField = tSortedRows[i]:getPhase()
            rowField = tCourseData:getPhase()
            if  sortField > rowField and order == "asc" then
                sortedInsert(i)
            break
            elseif sortField < rowField and order == "desc" then
                sortedInsert(i)
            break
            elseif sortField == rowField  then
                if tSortedRows[i]:getL1Name() > tCourseData:getL1Name() then
                    sortedInsert(i)
                break
                end
            end
        end
    end
 
    if not(bAddedFlag) then 
        table.insert(tSortedRows,tCourseData)
    end
    return tSortedRows
end
--% Formats a date
--@ sDate1 (string) A date in YYYY-MM-DD format
--@ ref (string) A reference for the date
--: (table) An tableBuilder table containing a formatted date
function frmtdate( sDate1, sRef)
    if not(sDate1) then 
        sDate1 =""
    end
    local frmDate = sDate1
 
    if (sDate1 == "0000-00-00") then
        sDate1 = ""
    end
 
    local dateSort = {["data-sort-value"]= frmDate}
    sDate1 = sDate1 .. ""..(sRef or "")
    -- -- Adds data sort to incomplete dates
    -- if sRef == "" or sDate1 == "" then
    --     dateSort = {}
    -- end
 
    return libTableBuilder.createCol(sDate1, dateSort)
end
--% Creates a flag header
--@ sLangCode (string) A language code for the flag
--: (table) An tableBuilder table containing a flag for the language
function flagheader( sLangCode)
    local sLangName = libLang.getLang(sLangCode) or "unknown"
    local sHeader
 
    sLangName =string.gsub(sLangName,"(%b())","")
    sHeader= getFlag(sLangCode,'15x15px') .. sLangName
 
    return libTableBuilder.createCol(sHeader,{["data-sort-value"]=string.lower(sLangName)})
end
--% Creates a flag header
--@ sLangCode (string) A language code for the flag
--@ sSize (string) The size of the flag, e.g. 20x20px
--: (string) An image for the flag (using markup)
function getFlag(sLangCode,sSize)
    return libFlag.getFlag(sLangCode, sSize  or '20x20px')
end
--% Creates the markup for an image or file
--@ sFileName (string) Name of the file
--@ sSize (string) Size of image e.g. 20x20px
--@ sLink (string) A link to a page
--@ sCaption (string) Caption of the file
--@ sAlt (string) Alternate text for the file
--: (string) Markup for generating a file link
function getFileLink(sFileName,sSize,sLink,sCaption,sAlt)
    local sFileLink = libFileLink.main{	file = sFileName,size = sSize,link = sLink,caption = sCaption,alt=sAlt or caption}
 
    return sFileLink
end
--% Parses text to change wikitext into html or simply html if debugging
--@ text (string) Text to parse
--: (string) Generated html or original text
function preprocess(text)
    if mw.getCurrentFrame() then
       return mw.getCurrentFrame():preprocess(text)
    end
    return text
end
 
return p