Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active October 25, 2018 15:07
Show Gist options
  • Save kou1okada/c964ea53b93315d3c5fac8ebaf63dafe to your computer and use it in GitHub Desktop.
Save kou1okada/c964ea53b93315d3c5fac8ebaf63dafe to your computer and use it in GitHub Desktop.
Moodle hacks
/**
* Get route of current page.
* \return Hash object which has keys (module, controller and any optionals) and values.
* module is like "course", "assign", "data", etc...
* controller is like "view", "modedit", "edit", etc...
* optionals are like "add", "course", "section", "mode", etc...
*/
function getroute() {
let route = {};
if (location.href.match(/\/([^/]+)\/([^/]+)\.php(\?(.*))/)) {
route = {module: RegExp.$1, controller: RegExp.$2};
RegExp.$4.split("&").forEach(m=>{
m = m.split("=");
route[m[0]] = decodeURIComponent(m[1]);
});
}
return route;
}
/**
* Get fields at "edit" controller on "data" module.
* \return Hash object which has keys (field names) and values (HTML elements).
*/
function data_edit_getfields() {
return [].reduce.call(document.querySelectorAll('[for]'), (r, e) => (r[e.textContent] = document.querySelector("#"+e.getAttribute("for")), r),{});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment