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);
TSV から課題の評点を転記します。
/**
 * Moodle の「課題」の「すべての提出を表示する」で
 * 姓名欄の TD 要素の先頭の空白までを id として
 * Modal dialog にペーストで与えた TSV
 * id<TAB>topicNo<TAB>score
 * ...
 * から値を参照して評点を埋める。
 */
javascript:(function(){
  function getTopicNumber() {
   	let pnbli = document.querySelectorAll("#page-navbar li");
    if (!pnbli || pnbli.length < 3) throw `Error: can't get #page-navbar li: ${pnbli}`;
    let topic = pnbli[pnbli.length - 3];
    let m = topic.textContent.match(/(topic|トピック)[^0-9]*([0-9]+)/i);
		if (!m) throw `Error: can't get topic string: ${topic}`;
    return parseInt(m[2]);
  }
  function getQuickGrades() {
    let submissions = document.querySelectorAll('tr[id^="mod_assign_grading_r"]');
    submissions = [].filter.call(submissions, e=>e.classList[0] != "emptyrow");
    let quickGrades = {};
    [].forEach.call(submissions, e=>{
      let id = e.querySelector("td label").textContent.replace(/^([0-9]+).*/, '$1');
      let grades = e.querySelectorAll(".quickgrade");
      quickGrades[id] = grades;
    });
	  return quickGrades;
  }
  function assignGrades(topicNo, quickGrades, tsv) {
    let data = [].map.call(tsv.split("\n"), e=>e.split("\t"));
    data.forEach(datum=>{
      console.log("------");
      console.log(topicNo);
      console.log(datum);
      if (topicNo == parseInt(datum[1]) && datum[0] in quickGrades) {
        console.log(quickGrades[datum[0]]);
        quickGrades[datum[0]][0].value = datum[2];
      } else {
        console.log(`quickGrade is not found for: ${datum[0]} ${datum[1]}`);
      }
    });
  }

  class Modal {
    constructor() {
      this.modal = document.body.appendChild(document.createElement("div"));
      Object.assign(this.modal.style, {
        position: "fixed",
        display: "fixed",
        top: "0",
        left: "0",
        width: "100%",
        height: "100%",
        backdropFilter: "blur(10px)",
        zIndex: "100000",
      });
    }
    appendChild(e) {
      return this.modal.appendChild(e);
    }
    remove() {
      this.modal.parentNode.removeChild(this.modal);
    }
  };
  class Paster {
    constructor(applyer,placeholder="") {
      this.applyer = applyer;
      let modal = new Modal();
      let textarea = modal.appendChild(document.createElement("textarea"));
      textarea.placeholder = placeholder;
      Object.assign(textarea.style, {
        width: "100%",
        height: "40em",
        display: "absolute"
      });
      let apply = modal.appendChild(document.createElement("button"));
      apply.textContent = "apply";
      apply.addEventListener("click", e=>{
        this.applyer && this.applyer(textarea.value);
        modal.remove();
      });
      let cancel = modal.appendChild(document.createElement("button"));
      cancel.textContent = "cancel";
      cancel.addEventListener("click", e=>{
        modal.remove();
      });
    }
  }

  let topicNumber = getTopicNumber();
  let quickGrades = getQuickGrades();
  let paster = new Paster(
    tsv=>assignGrades(topicNumber, quickGrades, tsv),
    placeholder="Write TSV as:\nid\ttopicNo\tscore\n...",
  ); 
})();

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();
  });
})();

2025-05-03: Uncheck all activity for import

/**
 * Moodle でインポートの際、一旦全アクティビティのチェックを外す
 */
(function() {
  let e = document.querySelectorAll('*[name^="setting_activity_"]');
  [].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);
})();

グループ番号とメールアドレスの TSV 取得

2025-10-10:

get_group_n_email

javascript:(function(){
  let t = document.querySelector("#participants tbody");
  let a = [].map.call(t.rows, row=>{
    let m = row.cells[4].textContent.match(/[0-9]+/);
    return m ? [m[0], row.cells[2].textContent] : undefined;
  }).filter(e=>e!=undefined).sort((a,b)=>a[0]<b[0]?-1:a[0]==b[0]?0:1);
  return a.map(e=>e.join("\t")).join("\n");
})();

コメントをかく


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

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

Wiki内検索

フリーエリア

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

広告募集中