hack のためのネタ帳, etc,,,

×
以下の図のような感じで、各セクションへのリンクを作る。

ラベルを HTML モードで編集して以下を貼り付け
<!-- Moodle jump to section with top and bottom -->
<div id="LinkToSection"></div>
<script>
    window.addEventListener("DOMContentLoaded", function() {
        let lts = document.querySelector("#LinkToSection");
        let sections = document.querySelectorAll('*[id^="section-"]');
        let label = lts.appendChild(document.createElement("span"));
        label.innerHTML = "&#x0001f680;";
        label.title = "Jump to section";
        function add_jump(section, label) {
            if (label == undefined) label = section.id.replace(/section-/, "");
            let a = lts.appendChild(document.createElement("a"));
            a.classList.add("LinkToSection");
            Object.assign(a, {
                href: "#" + section.id,
                textContent: label
            });
            if (section.classList.contains("current")) {
                a.classList.add("highlight");
            }
        }
        add_jump(document.querySelector("#page-course-view-topics"), "|<");
        [].forEach.call(sections, add_jump);
        add_jump(document.querySelector("#page-footer"), ">|");
        
        let style = document.head.appendChild(document.querySelector("#css4moodle-jump-to-section") || document.createElement("style"));
        Object.assign(style, {
            id: "css4moodle-jump-to-section",
            type: "text/css"
        });
        let heredoc = function(f){return f.toString().split("\n").slice(1,-1).join("\n");};
        style.textContent = heredoc(function(){/*
        a.LinkToSection {
            border: 1px solid lightgray;
            background: white;
            display: inline-block;
            width: 2em;
            text-align: center;
            text-decoration: none;
        }
        a.LinkToSection.highlight {
            background: #d9edf7;
            font-weight: bold;
        }
        #LinkToSection a:hover {
            border: 1px solid black;
            background: #fff0f0;
            text-decoration: none;
        }
        #LinkToSection a.highlight:hover {
            color: red;
            border: 1px solid red;
            background: #ffc0c0;
        }
        */});
        
        (function() {
            let e = document.querySelector("#LinkToSection");
            s = document.querySelectorAll('*[id^="section-"]');
            [].forEach.call(s, function(s) {
                s = s.querySelector(".summary");
                s.parentNode.insertBefore(e.cloneNode(true), s.nextSibling);
            });
            e.style.display = "none";
        })();
    });
</script>
以下の図のような感じで、各セクションへのリンクを作る。

ラベルを HTML モードで編集して以下を貼り付け

2022-06-23: 初版

2022-07-01: 各セクションに cloneNode append してみる

<!-- Moodle jump to section -->
<div id="LinkToSection"></div>
<script>
    window.addEventListener("DOMContentLoaded", function() {
        let lts = document.querySelector("#LinkToSection");
        let sections = document.querySelectorAll('*[id^="section-"]');
        let label = lts.appendChild(document.createElement("span"));
        label.innerHTML = "&#x0001f680;";
        label.title = "Jump to section";
        [].forEach.call(sections, function(section) {
            let a = lts.appendChild(document.createElement("a"));
            a.classList.add("LinkToSection");
            Object.assign(a, {
                href: "#" + section.id,
                textContent: section.id.replace(/section-/, "")
            });
            if (section.classList.contains("current")) {
                a.classList.add("highlight");
            }
        });
        
        let style = document.head.appendChild(document.querySelector("#css4moodle-jump-to-section") || document.createElement("style"));
        Object.assign(style, {
            id: "css4moodle-jump-to-section",
            type: "text/css"
        });
        let heredoc = function(f){return f.toString().split("\n").slice(1,-1).join("\n");};
        style.textContent = heredoc(function(){/*
        a.LinkToSection {
            border: 1px solid lightgray;
            background: white;
            display: inline-block;
            width: 2em;
            text-align: center;
            text-decoration: none;
        }
        a.LinkToSection.highlight {
            background: #d9edf7;
            font-weight: bold;
        }
        #LinkToSection a:hover {
            border: 1px solid black;
            background: #fff0f0;
            text-decoration: none;
        }
        #LinkToSection a.highlight:hover {
            color: red;
            border: 1px solid red;
            background: #ffc0c0;
        }
        */});
        
        (function() {
            let e = document.querySelector("#LinkToSection");
            s = document.querySelectorAll('*[id^="section-"]');
            [].forEach.call(s, function(s) {
                s = s.querySelector(".summary");
                s.parentNode.insertBefore(e.cloneNode(true), s.nextSibling);
            });
            e.style.display = "none";
        })();
    });
</script>
TSV から課題の評点を転記します。
/**
 * Moodle の「課題」の「すべての提出を表示する」で
 * 姓名欄の TD 要素の先頭の空白までを ID として
 * scores に Here Document として与えた TSV
 * ID<TAB>score
 * ...
 * から値を参照して評点を埋める。
 */
Function.prototype.hdoc =function(){return this.toString().split("\n").slice(1,-1).join("\n");};
var scores = function(){/*
    0000000001	-
    0000000002	100
    ...
*/}.hdoc();

// TSV から {ID: score, ...} の Object に変換
scores = scores.split("\n").map(s=>{
    let f =	s.split("\t");
    return [f[0].replace(/ /g, ""), (f[1].match(/[0-9]+/)||["0"])[0]];
}).reduce((r,v)=>{
    r[v[0]] = v[1];
    return r;
},{});

// ID で scores を参照して評点を埋める
var trs = document.querySelectorAll('tr[id^="mod_assign_grading_"]');
[].map.call(trs,tr=>{
    let s = tr.querySelector("td:nth-child(1)").textContent;
    let id = (s.match(/^[^ ]*/)||[""])[0];
    if (Object.hasOwn(scores, id)) {
        tr.querySelector("input.quickgrade").value = scores[id];
        console.log(id, scores[id]);
        delete scores[id];
    } else {
        console.log(id, "Not found!")
    }
});
console.log("Remains:");
console.log(scores);

2024-04-26: Uncheck all section for import

/**
 * Moodle でインポートの際、一旦全セクションのチェックを外す
 */
(function() {
  let e = document.querySelectorAll('*[name^="setting_section_section_"]');
  [].forEach.call(e, function(e){
    e.checked="checked";
    e.click();
  });
})();
ワークショップの評価フェーズにて、相互評価の未提出者と未提出数の一覧を得ます。

承前:

2024-11-21:

workshop_get_nulls

(function () {
  const getCellInfo = (function () {
    let cache = new WeakMap();

    function buildCellPositions(table) {
      const rows = table.rows;
      const cellPositions = Array.from({ length: rows.length }, () => []);

      for (let i = 0; i < rows.length; i++) {
        let currentColIndex = 0;

        for (let j = 0; j < rows[i].cells.length; j++) {
          const cell = rows[i].cells[j];
          const rowspan = cell.rowSpan || 1;
          const colspan = cell.colSpan || 1;

          while (cellPositions[i][currentColIndex]) {
            currentColIndex++;
          }

          for (let r = 0; r < rowspan; r++) {
            for (let c = 0; c < colspan; c++) {
              cellPositions[i + r][currentColIndex + c] = cell;
            }
          }

          currentColIndex += colspan;
        }
      }

      cache.set(table, cellPositions);
      return cellPositions;
    }

    function getCellPositions(table) {
      return cache.get(table) || buildCellPositions(table);
    }

    function getCellInfo(td) {
      const table = td.closest("table");
      if (!table) return null;

      const cellPositions = getCellPositions(table);
      for (let i = 0; i < cellPositions.length; i++) {
        for (let j = 0; j < cellPositions[i].length; j++) {
          if (cellPositions[i][j] === td) {
            return {
              rowIndex: i,
              colIndex: j,
              rowspan: td.rowSpan || 1,
              colspan: td.colSpan || 1,
            };
          }
        }
      }
      return null;
    }

    return getCellInfo
  })();

  let never = {};
  [].reduce.call(document.querySelectorAll(".grading-report tbody tr"), (r, e) => {
    let tds = e.querySelectorAll("td");
    if (tds.length == 4) {
      r = tds[0].textContent;
    }
    if (0 < tds.length && getCellInfo(tds[tds.length - 1]).colIndex == 3 && tds[tds.length - 1].classList.contains("null")) {
      never[r] = (never[r] ?? 0) + 1;
    }
    return r;
  }, null);
  let a = Object.keys(never).map(k => `${k}\t:\t${never[k]}`);
  console.log(a.join("\n"));
  console.log(a.length);
})();

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます