Skip to content

Instantly share code, notes, and snippets.

@x-magic
Created February 16, 2021 05:01
Show Gist options
  • Select an option

  • Save x-magic/890b491c1d800d87b528f07fbe91939e to your computer and use it in GitHub Desktop.

Select an option

Save x-magic/890b491c1d800d87b528f07fbe91939e to your computer and use it in GitHub Desktop.
User info finder block code by reusing Moodle Enrolment API
<div>
<input type="text" id="fndr_student_finder_field" class="form-control" placeholder="Type User ID/Email/..." role="textbox" style="max-width: 15em; float: left; margin-right: 1em;" />
<button id="fndr_student_finder_search" type="button" class="btn btn-primary" style="margin-right: 1em;">Search</button>
<div id="fndr_student_finder_results" style="font-family: monospace; float: right;">Click Search button to display results</div>
</div>
<script type="text/javascript">
// <![CDATA[
var fndr_sesskey = document.querySelector('#page-header input[name=sesskey]').value;
var fndr_endpoint = "/lib/ajax/service.php?sesskey=" + fndr_sesskey + "&info=core_enrol_get_potential_users";
var fndr_search_template = '[{"index":0,"methodname":"core_enrol_get_potential_users","args":{"courseid":"___PROVIDE_YOUR_COURSEID___","enrolid":"___PROVIDE_YOUR_ENROLID___","search":"___SEARCHME___","searchanywhere":true,"page":0,"perpage":10}}]';
document.getElementById("fndr_student_finder_search").addEventListener("click", function() {
var fndr_search_keyword = document.getElementById("fndr_student_finder_field").value;
if (fndr_search_keyword === "") {
document.getElementById("fndr_student_finder_results").innerText = "You must provide a keyword to search!";
return false;
}
var fndr_search_json = fndr_search_template.replace("___SEARCHME___", fndr_search_keyword);
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var data = JSON.parse(xmlHttp.responseText);
if (data[0].error === true) {
alert("Error with response: " + JSON.stringify(data));
return false;
}
var fndr_outputs = "";
if (data[0].data.length > 0) {
data[0].data.forEach(function(item, index) {
fndr_outputs += "#" + index + ": ID#: " + item.idnumber + " Email: " + item.email + " Name: " + item.fullname + "<br>";
});
} else {
fndr_outputs = "No results!";
}
document.getElementById("fndr_student_finder_results").innerHTML = fndr_outputs;
}
}
xmlHttp.open("post", fndr_endpoint);
xmlHttp.send(fndr_search_json);
});
// ]]>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment