پرش به محتوا

پودمان:ماتریس آرای هیئت نظارت

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[نمایش] [ویرایش] [تاریخچه] [پاکسازی]

استفاده[ویرایش]

{{#invoke:ماتریس آرای هیئت نظارت|main|اِن‌اُم (شماره دور انتخابات)|فهرست نامزدان که با : از هم جدا شده‌اند|تاریخ پایان نظرخواهی به صورت yyyymmdd}}

function firstToUpper(str)
    return (str:gsub("^%l", string.upper))
end

function uniquecandidates(frame)
  local output = {}
  for candidate in mw.text.gsplit(frame.args[2], ':') do
    table.insert(output, candidate)
  end
  return output
end

function uniquevoters(frame)
  local output = {}
  local hash = {}
  local res = {}
  local voting = frame.args[1]
  for candidate in mw.text.gsplit(frame.args[2], ':') do
    local text = mw.title.new("انتخابات هیئت نظارت/دور " .. voting .. "/" .. candidate, "ویکی‌پدیا"):getContent()
    local iter = mw.ustring.gmatch(text, "{{رای هیئت نظارت " .. voting .. "|([^\n]+)|رای=[موافق مخالف]+[^\n]+c=[pپ]")
    while true do
      local m = iter()
      if m == nil then break end
      m = mw.ustring.gsub(m, "1=", "")
      m = firstToUpper(m)
      table.insert(output, m)
    end
  end
  for _,v in ipairs(output) do
    if (not hash[v]) then
      res[#res+1] = v
      hash[v] = true
    end
  end
  return res
end

return {
  main = function (frame)
    local output = ""
    local voting = frame.args[1]
    candidates = uniquecandidates(frame)
    voters = uniquevoters(frame)
    local pos_mat = {}
    local neg_mat = {}
    for i=1,table.getn(voters) do
      pos_mat[i] = {}
      neg_mat[i] = {}
      for j=1,table.getn(candidates) do
        pos_mat[i][j] = 0
        neg_mat[i][j] = 0
      end
    end
    
    for candidate in mw.text.gsplit(frame.args[2], ':') do
      local text = mw.title.new("انتخابات هیئت نظارت/دور " .. voting .. "/" .. candidate, "ویکی‌پدیا"):getContent()
      local vid = 0
      local cid = 0
      for i, c in ipairs(candidates) do
        if c == candidate then cid = i end
      end

      local iter = mw.ustring.gmatch(text, "{{رای هیئت نظارت " .. voting .. "|([^\n]+)|رای=موافق[^\n]+c=[pپ]")
      while true do
        local m = iter()
        if m == nil then break end
        local voter = mw.ustring.gsub(m, "1=", "")
        voter = firstToUpper(voter)
        for i, v in ipairs(voters) do
          if v == voter then vid = i end
        end
        pos_mat[cid][vid] = 1
      end

      local iter = mw.ustring.gmatch(text, "{{رای هیئت نظارت " .. voting .. "|([^\n]+)|رای=مخالف[^\n]+c=[pپ]")
      while true do
        local m = iter()
        if m == nil then break end
        local voter = mw.ustring.gsub(m, "1=", "")
        voter = firstToUpper(voter)
        for i, v in ipairs(voters) do
          if v == voter then vid = i end
        end
        neg_mat[cid][vid] = 1
      end

    end

    output = "{| class='wikitable sortable' style='font-size: small' \n!\n"
    for i=1,table.getn(candidates) do
      output = output .. "! " .. mw.ustring.gsub(candidates[i], '_', ' ') .. "\n"
    end
    for j=1,table.getn(voters) do
      output = output .. "|-\n|" .. voters[j] .. "\n"
      for i=1,table.getn(candidates) do
        output = output .. "|"
        if pos_mat[i][j] == 1 then
          output = output .. "{{بله}}"
        else
          if neg_mat[i][j] == 1 then
            output = output .. "{{خیر}}"
          end
        end
        output = output .. "\n"
      end
    end
    output = output .. "|-\n!آرای موافق\n"
    for i=1,table.getn(candidates) do
      local sum = 0
      for j=1,table.getn(voters) do
        if pos_mat[i][j] == 1 then sum = sum + 1 end
      end
      output = output .. '|' .. sum .. '\n'
    end
    output = output .. "|-\n!آرای مخالف\n"
    for i=1,table.getn(candidates) do
      local sum = 0
      for j=1,table.getn(voters) do
        if neg_mat[i][j] == 1 then sum = sum + 1 end
      end
      output = output .. '|' .. sum .. '\n'
    end
    output = output .. "|}"
    return frame:preprocess(output)
  end
}