Skip to content

Commit 666310d

Browse files
tailetantai.letan
andauthored
Questionnaire author to sort free response on name and date/time in summary view (#596)
Co-authored-by: tai.letan <[email protected]>
1 parent a71b56c commit 666310d

File tree

8 files changed

+294
-10
lines changed

8 files changed

+294
-10
lines changed

amd/build/table_sort.min.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/table_sort.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/table_sort.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
/**
17+
* JavaScript library for questionnaire response table sorting.
18+
*
19+
* @module mod_questionnaire/table_sort
20+
* @copyright
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
define(['jquery'], function($) {
24+
25+
var t = {
26+
/**
27+
* Initialise the event listeners.
28+
*
29+
*/
30+
init: function() {
31+
M.util.js_pending('mod_questionnaire_tablesort');
32+
$(".qn-handcursor").on('click', t.sortcolumn);
33+
M.util.js_complete('mod_questionnaire_tablesort');
34+
},
35+
36+
/**
37+
* Javascript for sorting s'Text Box' Response.
38+
* @param {Event} e
39+
*/
40+
sortcolumn: function(e) {
41+
e.preventDefault();
42+
var col = $(this).index();
43+
var id = $(this).closest('table').attr('id');
44+
var sortOrder = 1;
45+
$(this).siblings().find('span[class^="icon-container-"]').hide();
46+
$(this).siblings().removeClass('asc desc');
47+
$(this).find('span[class^="icon-container-"]').removeAttr('style');
48+
if ($(this).is('.asc')) {
49+
$(this).removeClass('asc').addClass('desc');
50+
sortOrder = -1;
51+
} else {
52+
$(this).addClass('asc').removeClass('desc');
53+
}
54+
var arrData = $(this).closest('table').find('tbody >tr:has(td.cell)').get();
55+
arrData.sort(function (a, b) {
56+
var val1 = $(a).children('td').eq(col).text();
57+
var val2 = $(b).children('td').eq(col).text();
58+
// Regex to check for date sorting.
59+
var dateregx = /^\d{2}.*\d{4},/;
60+
if (dateregx.test(val1) && dateregx.test(val2)) {
61+
val1 = new Date(val1);
62+
val2 = new Date(val2);
63+
return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
64+
} else if ($.isNumeric(val1) && $.isNumeric(val2)) {
65+
return sortOrder == 1 ? val1 - val2 : val2 - val1;
66+
} else {
67+
return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
68+
}
69+
});
70+
/* Append the sorted rows to tbody*/
71+
$.each(arrData, function (index, row) {
72+
var tableid = $('#' + id + ' tbody');
73+
tableid.append(row);
74+
});
75+
},
76+
};
77+
return t;
78+
});

classes/responsetype/text.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function get_results($rids=false, $anonymous=false) {
116116
'WHERE question_id=' . $this->question->id . $rsql .
117117
' AND t.response_id = r.id' .
118118
' AND u.id = r.userid ' .
119-
'ORDER BY u.lastname, u.firstname, r.submitted';
119+
'ORDER BY r.submitted DESC';
120120
}
121121
return $DB->get_records_sql($sql, $params);
122122
}
@@ -204,10 +204,15 @@ public function get_results_tags($weights, $participants, $respondents, $showtot
204204
}
205205
// The 'evencolor' attribute is used by the PDF template.
206206
$response->evencolor = $evencolor;
207+
$response->date = userdate($row->submitted, get_string('strftimedatetime'));
207208
$pagetags->responses[] = (object)['response' => $response];
208209
$evencolor = !$evencolor;
209210
}
210-
211+
// sort table only when row count is greater than one.
212+
if (count($weights) > 1) {
213+
$pagetags->sortresponse = true;
214+
}
215+
$pagetags->tableid = $this->question->id;
211216
if ($showtotals == 1) {
212217
$pagetags->total = new \stdClass();
213218
$pagetags->total->total = "($respondents)";

styles.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,23 @@ td.selected {
442442
#page-mod-questionnaire-report .generaltable.questionnairereport td {
443443
border: 1px solid silver;
444444
}
445+
#page-mod-questionnaire-report .generaltable[id] th.qn-handcursor {
446+
cursor: pointer;
447+
}
448+
449+
#page-mod-questionnaire-report th.c0 a span[class^='icon-container-'],
450+
#page-mod-questionnaire-report th.c1 a span.icon-container-asc,
451+
#page-mod-questionnaire-report th.asc a span.icon-container-desc,
452+
#page-mod-questionnaire-report th.desc a span.icon-container-asc {
453+
display: none;
454+
}
455+
456+
#page-mod-questionnaire-report th.asc a span.icon-container-asc,
457+
#page-mod-questionnaire-report th.desc a span.icon-container-desc {
458+
display: inline-block;
459+
}
445460

461+
#page-mod-questionnaire-report .frtlst,
446462
.qn-container .smalltext {
447463
font-size: 0.75em;
448464
}

templates/reportpage.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,11 @@
8383
{{#responses}}{{{.}}}{{/responses}}
8484
{{#bottomnavigationbar}}<div class="box respondentsnavbar">{{{.}}}</div>{{/bottomnavigationbar}}
8585
</div>
86-
</div>
86+
</div>
87+
{{#js}}
88+
require(['jquery', 'mod_questionnaire/table_sort'], function($, TableSort) {
89+
$(document).ready(function() {
90+
TableSort.init();
91+
});
92+
});
93+
{{/js}}

templates/results_text.mustache

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,51 @@
5050
}}
5151
<!-- Begin HTML generated from results_text template. -->
5252
{{#responses.0}}
53-
<table class="generaltable">
53+
<table class="generaltable" {{#tableid}}id="{{tableid}}"{{/tableid}}>
5454
<thead>
5555
<tr>
56-
<th class="header c0" style="text-align:left;" scope="col">{{#str}} respondent, mod_questionnaire {{/str}}</th>
57-
<th class="header c1 lastcol" style="text-align:right;" scope="col">{{#str}} response, mod_questionnaire {{/str}}</th>
56+
<th class="header c0 {{#tableid}}qn-handcursor{{/tableid}}" style="text-align:left;" scope="col" data-value="{{#str}} respondent, mod_questionnaire {{/str}}">
57+
{{#sortresponse}}
58+
<a href="#"
59+
title="{{#str}} respondent, mod_questionnaire {{/str}}"
60+
role="link"
61+
aria-label="{{#str}} respondent, mod_questionnaire {{/str}}" onclick="return false;">
62+
{{#str}} respondent, mod_questionnaire {{/str}}
63+
<span class="icon-container-asc">
64+
{{#pix}} t/sort_asc, moodle, {{#str}} sortbyx {{/str}}{{/pix}}
65+
</span>
66+
<span class="icon-container-desc">
67+
{{#pix}} t/sort_desc, moodle, {{#str}} sortbyxreverse {{/str}}{{/pix}}
68+
</span>
69+
</a>
70+
{{/sortresponse}}
71+
{{^sortresponse}}
72+
{{#str}} respondent, mod_questionnaire {{/str}}
73+
{{/sortresponse}}
74+
{{#tableid}}
75+
<div class="frtlst">{{#str}} firstname {{/str}}/{{#str}} lastname {{/str}}</div>
76+
<th class="header c1 {{#tableid}}qn-handcursor{{/tableid}} desc" style="text-align:center;" scope="col" data-value="{{#str}} date {{/str}}">
77+
{{#sortresponse}}
78+
<a href="#"
79+
title="{{#str}} date {{/str}}"
80+
role="link"
81+
aria-label="{{#str}} date {{/str}}" onclick="return false;">
82+
{{#str}} date {{/str}}
83+
<span class="icon-container-asc">
84+
{{#pix}} t/sort_asc, moodle, {{#str}} sortbyx {{/str}}{{/pix}}
85+
</span>
86+
<span class="icon-container-desc">
87+
{{#pix}} t/sort_desc, moodle, {{#str}} sortbyxreverse {{/str}}{{/pix}}
88+
</span>
89+
</a>
90+
{{/sortresponse}}
91+
{{^sortresponse}}
92+
{{#str}} date {{/str}}
93+
{{/sortresponse}}
94+
</th>
95+
{{/tableid}}
96+
</th>
97+
<th class="header c2 lastcol" style="text-align:right;" scope="col">{{#str}} response, mod_questionnaire{{/str}}</th>
5898
</tr>
5999
</thead>
60100
<tbody>
@@ -63,21 +103,29 @@
63103
{{#response}}
64104
<tr class="">
65105
<td class="cell c0" style="text-align:left;">{{{response.respondent}}}</td>
66-
<td class="cell c1 lastcol" style="text-align:right;">{{{response.text}}}</td>
106+
{{#tableid}}
107+
<td class="cell c1" style="text-align:center;">{{{response.date}}}</td>
108+
{{/tableid}}
109+
<td class="cell c2 lastcol" style="text-align:right;">{{{response.text}}}</td>
67110
</tr>
68111
{{/response}}
69112
{{/responses}}
113+
{{#responses.0}}
114+
</tbody>
115+
<tfoot>
116+
{{/responses.0}}
70117
{{#total}}
71118
<tr>
72-
<td colspan="2"><div class="tabledivider"></div></td>
119+
<td colspan={{#tableid}}"3"{{/tableid}}{{^tableid}}"2"{{/tableid}}><div class="tabledivider"></div></td>
73120
</tr>
74121
<tr class="lastrow">
75-
<td class="cell c0" style="text-align:left;">{{#str}} totalresponses, mod_questionnaire{{/str}}</td>
122+
<td class="cell c0" {{#tableid}} colspan="2"{{/tableid}} style="text-align:left;">
123+
{{#str}} totalresponses, mod_questionnaire{{/str}}</td>
76124
<td class="cell c1 lastcol" style="text-align:right;">{{total}}</td>
77125
</tr>
78126
{{/total}}
79127
{{#responses.0}}
80-
</tbody>
128+
</tfoot>
81129
</table>
82130
{{/responses.0}}
83131
{{^responses}}

0 commit comments

Comments
 (0)