Index: lams_monitoring/web/includes/javascript/monitorLesson5.js =================================================================== diff -u -ra23b8d982ba3902fb9712d6cb7a4723c4f70c34c -r3ad491da0b1df01b13613f6106879d636dd7f132 --- lams_monitoring/web/includes/javascript/monitorLesson5.js (.../monitorLesson5.js) (revision a23b8d982ba3902fb9712d6cb7a4723c4f70c34c) +++ lams_monitoring/web/includes/javascript/monitorLesson5.js (.../monitorLesson5.js) (revision 3ad491da0b1df01b13613f6106879d636dd7f132) @@ -10,6 +10,8 @@ sequenceSearchedLearner = null, // for synchronisation purposes sequenceRefreshInProgress = false, +// currently opened EventSources + eventSources = [], // double tap support tapTimeout = 500, @@ -72,10 +74,18 @@ $(button).addClass('active'); } + clearEventSources(); + let tabContent = $('.monitoring-page-content .tab-content'); if (tabName == 'sequence') { tabContent.load(LAMS_URL + 'monitoring/monitoring/displaySequenceTab.do', function(){ + openEventSource(LAMS_URL + 'monitoring/monitoring/getLearnerProgressUpdateFlux.do?lessonId=' + lessonId, + function (event) { + if ("doRefresh" == event.data && $('#sequence-tab-content').length === 1){ + updateSequenceTab(); + } + }); refreshMonitor('sequence'); canvasFitScreen(learningDesignSvgFitScreen, true); $("#load-sequence-tab-btn").addClass('active'); @@ -86,6 +96,32 @@ }); } else if (tabName == 'gradebook') { tabContent.load(LAMS_URL + 'monitoring/monitoring/displayGradebookTab.do', function(){ + openEventSource(LAMS_URL + 'monitoring/monitoring/getGradebookUpdateFlux.do?lessonId=' + lessonId, + function (event) { + if ("doRefresh" == event.data && $('#gradebookDiv').length === 1){ + let expandedGridIds = [], + userGrid = $('#userView'), + activityGrid = $('#activityView'); + // do not update if grid is being edited by the teacher + if (userGrid.data('isCellEdited') === true || activityGrid.data('isCellEdited') === true) { + return; + } + + $("tr:has(.sgexpanded)", userGrid).each(function () { + let num = $(this).attr('id'); + expandedGridIds.push(num); + }); + userGrid.data('expandedGridIds', expandedGridIds).trigger("reloadGrid"); + + expandedGridIds = []; + $("tr:has(.sgexpanded)", activityGrid).each(function () { + let num = $(this).attr('id'); + expandedGridIds.push(num); + }); + activityGrid.data('expandedGridIds', expandedGridIds).trigger("reloadGrid"); + } + }); + refreshMonitor('gradebook'); }); } @@ -1326,13 +1362,6 @@ $('#forceBackwardsCloseButton', forceBackwardsDialogContents).click(function(){ $('#forceBackwardsDialog').modal('hide'); }); - - const learnerProgressUpdateSource = new EventSource(LAMS_URL + 'monitoring/monitoring/getLearnerProgressUpdateFlux.do?lessonId=' + lessonId); - learnerProgressUpdateSource.onmessage = function (event) { - if ("doRefresh" == event.data && $('#sequence-tab-content').length === 1){ - updateSequenceTab(); - } - } } function showIntroductionDialog(lessonId) { @@ -2397,23 +2426,17 @@ $('.accordion-collapse', item).attr('id', itemCollapseId).attr('data-bs-parent', '#learners-accordion') .on('show.bs.collapse', function () { - if (learnerProgressSource) { - try { - learnerProgressSource.close(); - } catch(e) { - console.error(e); - } - } + clearEventSources(); let learnerId = $(this).closest('.accordion-item').data('user-id'); - learnerProgressSource = new EventSource(LAMS_URL + 'learning/learner/getLearnerProgressUpdateFlux.do?lessonId=' - + lessonId + '&userId=' + learnerId); - - learnerProgressSource.onmessage = function (event) { - if ($('#learners-accordion-item-' + learnerId).length === 1) { - drawLearnerTimeline(learnerId, event.data); - } - } + openEventSource(LAMS_URL + 'learning/learner/getLearnerProgressUpdateFlux.do?lessonId=' + + lessonId + '&userId=' + learnerId, + function (event) { + if ($('#learners-accordion-item-' + learnerId).length === 1) { + drawLearnerTimeline(learnerId, event.data); + } + }); + }); }); } @@ -2541,32 +2564,6 @@ clearButton : 'btn btn-sm' } }); - - const gradebooksUpdateSource = new EventSource(LAMS_URL + 'monitoring/monitoring/getGradebookUpdateFlux.do?lessonId=' + lessonId); - gradebooksUpdateSource.onmessage = function (event) { - if ("doRefresh" == event.data && $('#gradebookDiv').length === 1){ - let expandedGridIds = [], - userGrid = $('#userView'), - activityGrid = $('#activityView'); - // do not update if grid is being edited by the teacher - if (userGrid.data('isCellEdited') === true || activityGrid.data('isCellEdited') === true) { - return; - } - - $("tr:has(.sgexpanded)", userGrid).each(function () { - let num = $(this).attr('id'); - expandedGridIds.push(num); - }); - userGrid.data('expandedGridIds', expandedGridIds).trigger("reloadGrid"); - - expandedGridIds = []; - $("tr:has(.sgexpanded)", activityGrid).each(function () { - let num = $(this).attr('id'); - expandedGridIds.push(num); - }); - activityGrid.data('expandedGridIds', expandedGridIds).trigger("reloadGrid"); - } - }; } /** @@ -3074,3 +3071,20 @@ lastTapTarget = event.currentTarget; }); } + +function openEventSource(url, onMessageFunction) { + const eventSource = new EventSource(url); + eventSources.push(eventSource); + eventSource.onmessage = onMessageFunction; +} + +function clearEventSources() { + eventSources.forEach(function(eventSource){ + try { + eventSource.close(); + } catch(e) { + console.error("Error while closing Event Source", e); + } + }); + eventSources = []; +} \ No newline at end of file