Select Git revision
-
Stuart Gathman authoredStuart Gathman authored
jmodule.js 6.02 KiB
define(['jquery'], function ($) {
function getRootWebSitePath() {
var _location = document.location.toString();
var applicationNameIndex = _location.indexOf('/', _location.indexOf('://') + 3);
var applicationName = _location.substring(0, applicationNameIndex) + '/';
var webFolderIndex = _location.indexOf('/', _location.indexOf(applicationName) + applicationName.length);
var webFolderFullPath = _location.substring(0, webFolderIndex);
return webFolderFullPath;
}
function search(searchstring, reloadBool, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester) {
if (reloadBool) {
$("#hshcourseprogress").show();
$.ajax({
type: 'GET',
url: getRootWebSitePath() + '/blocks/hshcourselist/hshcourselist.php',
data: 'course=' + searchstring + '&instanceid=' + instanceid
+ '&sesskey=' + sesskey + '&contextid=' + contextid
+ '&idSearching=' + idSearching + '&orderbysemester=' + orderbysemester,
dataType: "json",
success: function (response) {
var plainJSON = JSON.stringify(response);
var courses = JSON.parse(plainJSON);
var list = $("<ul class='list-group' id='hshcourselistul'></ul>");
if (courses.length > 0) {
$.each(courses, function (index, course) {
switch (displaymode) {
case '0':
displaystr = course.shortname;
break;
case '1':
displaystr = course.fullname;
break;
case '2':
displaystr = course.shortname + ': ' + course.fullname;
break;
default:
displaystr = course.fullname;
break;
}
$('<li class="list-group-item hshcoursesearchlistitem " value="' + course.id + '">'
+ '<a href="' + getRootWebSitePath() + '/course/view.php?id=' + course.id + '">' + displaystr + '</a> </li>')
.appendTo(list);
});
}
$("#hshcourselistul").replaceWith(list);
$("#hshcourseprogress").hide();
},
error: function () {
console.error("error");
if (o.statusText !== 'abort') {
$("#hshcourseprogress").hide();
if (o.statusText !== undefined) {
console.error("ABORT + STATUS : " + o.statusText);
}
}
}
});
} else {
}
}
return {
init: function (jsdataobject) {
var jsdataobject = jsdataobject;
var instanceid = jsdataobject.instanceid;
var sesskey = jsdataobject.sesskey;
var displaymode = jsdataobject.displaymode;
var contextid = jsdataobject.contextid;
var reloadList = false;
var idSearching = false;
var searchstring;
var orderbysemester = jsdataobject.orderbysemester;
var displaystr;
$("#hshcourseprogress").hide();
$(".idSarchingLabel").hide();
$('#orderbysemestercheckbox_id').on('change', function () {
orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
reloadList = true;
searchstring = document.getElementById('hshcourselistsearch').value;
if (searchstring.slice(0, 1) === '#') {
$(".idSarchingLabel").show();
idSearching = true;
searchstring = searchstring.slice(1);
} else {
$(".idSarchingLabel").hide();
idSearching = false;
}
search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester);
});
$('#hshcourselistsearch').on('keyup', function (e) {
var key = e.keyCode;
orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
if (key !== 40 && key !== 38 && key !== 13) {
reloadList = true;
searchstring = document.getElementById('hshcourselistsearch').value;
if (searchstring.slice(0, 1) === '#') {
$(".idSarchingLabel").show();
idSearching = true;
searchstring = searchstring.slice(1);
} else {
$(".idSarchingLabel").hide();
idSearching = false;
}
search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester);
}
});
$('#hshcourseform').on('submit', function (e) {
e.preventDefault;
reloadList = true;
idSearching = false;
orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
searchstring = document.getElementById('hshcourselistsearch').value;
if (searchstring.slice(0, 1) === '#') {
idSearching = true;
searchstring = searchstring.slice(1);
} else {
idSearching = false;
}
search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester);
});
},
}
});