Skip to content

Commit cfc9114

Browse files
committed
Anjaly | BDSHR-1329 | Changing the manage reports page to
show only applicable facilities.
1 parent 1f9d9ce commit cfc9114

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

src/main/resources/templates/dhis.scheduleReports.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
$( document ).ready(function() {
2121
var formErrors = [[${formErrors}]];
2222
var success = [[${success}]];
23+
var orgUnits =[[${orgUnits}]]
2324
document.getElementById("periodSpan").innerHTML = getPeriodType();
24-
var scheduleOptions = new ReportScheduleOptions(formErrors,success);
25+
var scheduleOptions = new ReportScheduleOptions(orgUnits,formErrors,success);
2526
});
2627
</script>
2728
</head>
@@ -71,12 +72,7 @@ <h3 th:text="'Schedule Dataset for:'+ ${reportConfig.datasetName}"></h3>
7172
<div class="panel-body">
7273
<h4>Select Facilities and schedule submission</h4>
7374

74-
<div class="checkbox" th:each="org: ${orgUnits}">
75-
<label>
76-
<input type="checkbox" name="selectedFacilities" th:value="${org.facilityId}"
77-
th:attr="data-orgunit=${org.orgUnitId}"/>
78-
<span th:utext="${org.facilityName}">name</span>
79-
</label>
75+
<div id="applicableOrgUnits" hidden="true">
8076
</div>
8177
</div>
8278
</div>
@@ -265,6 +261,17 @@ <h4>Select Facilities and schedule submission</h4>
265261
{{/.}}
266262

267263
</script>
264+
<script id="template_applicable_org_units" type="x-tmpl-mustache">
265+
{{#.}}
266+
<div class="checkbox">
267+
<label>
268+
<input type="checkbox" name="selectedFacilities" value="{{facilityId}}"
269+
data-orgunit="{{orgUnitId}}"/>
270+
<span>{{facilityName}}</span>
271+
</label>
272+
</div>
273+
{{/.}}
274+
</script>
268275
</div>
269276
<div class="container">
270277
<div th:replace="fragments/footer :: footer"></div>

src/main/webapp/scripts/reportScheduleOptions.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function ReportScheduleOptions(formErrors,success) {
1+
function ReportScheduleOptions(orgUnits,formErrors,success) {
22
var periodId = "#periodType", startDtId = "#startDate", displayPeriodId = "#reportingPeriod";
33
var dtCh= "/", minYear=1900, maxYear=2100;
44
var applicableOrgUnits = [];
@@ -11,17 +11,6 @@ function ReportScheduleOptions(formErrors,success) {
1111
self.validateInput($(this).val());
1212
});
1313

14-
var checkboxes = $("input[type='checkbox']");
15-
checkboxes.click(function() {
16-
if(checkboxes.is(":checked")){
17-
isChecked = true;
18-
}
19-
else{
20-
isChecked = false;
21-
}
22-
self.disableSubmitAndPreview();
23-
});
24-
2514
$('#scheduleStartDate').datepicker({
2615
autoclose: true,
2716
onRender: function(date) {
@@ -115,24 +104,6 @@ function ReportScheduleOptions(formErrors,success) {
115104
});
116105
});
117106

118-
$("input[name=selectedFacilities]").bind("click", function(e) {
119-
clearErrors();
120-
clearSuccess();
121-
var orgUnitId = $(e.target).attr("data-orgunit");
122-
var arrayLength = self.applicableOrgUnits.length;
123-
var found = false;
124-
for (var i = 0; i < arrayLength; i++) {
125-
var orgUnit = self.applicableOrgUnits[i];
126-
if (orgUnit.id === orgUnitId) {
127-
found = true;
128-
}
129-
}
130-
if (!found) {
131-
showErrors("The selected organization is not applicable for this report.");
132-
e.preventDefault();
133-
}
134-
});
135-
136107
$("input[name=scheduleType]").bind("click", function(e) {
137108
var val = $('input[type=radio]:checked').val();
138109
if (val == "repeat") {
@@ -332,6 +303,7 @@ function ReportScheduleOptions(formErrors,success) {
332303
if (results) {
333304
if (results.hasOwnProperty('organisationUnits')) {
334305
self.applicableOrgUnits = results.organisationUnits;
306+
showApplicableOrgUnits(orgUnits);
335307
}
336308
}
337309
}).fail(function(){
@@ -340,6 +312,39 @@ function ReportScheduleOptions(formErrors,success) {
340312
});
341313
};
342314

315+
var showApplicableOrgUnits = function(availableOrgUnits){
316+
var availableApplicableOrgUnits = getAvailableApplicableOrgUnits(availableOrgUnits, self.applicableOrgUnits);
317+
if(availableApplicableOrgUnits.length==0){
318+
showErrors("This report is not applicable to any of the available facilities.");
319+
$('#createReportSchedule').attr('hidden', true);
320+
}
321+
else{
322+
var template = $('#template_applicable_org_units').html();
323+
Mustache.parse(template);
324+
var rendered = Mustache.render(template,availableApplicableOrgUnits);
325+
$('#applicableOrgUnits').html(rendered);
326+
$('#applicableOrgUnits').attr('hidden',false);
327+
var checkboxes = $("input[type='checkbox']");
328+
checkboxes.click(function() {
329+
if(checkboxes.is(":checked")){
330+
isChecked = true;
331+
}
332+
else{
333+
isChecked = false;
334+
}
335+
self.disableSubmitAndPreview();
336+
});
337+
}
338+
}
339+
340+
var getAvailableApplicableOrgUnits = function(availableOrgUnits, applicableOrgUnits){
341+
var filteredOrgUnits = availableOrgUnits.filter(function(availableUnit) {
342+
return applicableOrgUnits.find(function(applicableUnit) {
343+
return applicableUnit.id == availableUnit.orgUnitId})
344+
});
345+
return filteredOrgUnits;
346+
}
347+
343348
var getDhisNames = function(response) {
344349
var dataElements = {};
345350
var comboOptions = {};
@@ -397,7 +402,7 @@ function ReportScheduleOptions(formErrors,success) {
397402

398403
fetchOrgUnits();
399404
showErrors(formErrors);
400-
showSuccess(success)
405+
showSuccess(success);
401406
}
402407

403408
var toggleFacilityDetails = function(facilityId, show) {

0 commit comments

Comments
 (0)