Skip to content

Commit fc6defd

Browse files
mchurchwardtai.letan
andauthored
Questionnaire: make Summary page display more intuitive (#621)
* Questionnaire: make Summary page display more intuitive * Updating to Moodle 4.5. --------- Co-authored-by: tai.letan <[email protected]>
1 parent 36f22d4 commit fc6defd

File tree

12 files changed

+96
-47
lines changed

12 files changed

+96
-47
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77

88
services:
99
postgres:
10-
image: postgres:13
10+
image: postgres:14
1111
env:
1212
POSTGRES_USER: 'postgres'
1313
POSTGRES_HOST_AUTH_METHOD: 'trust'
@@ -31,10 +31,10 @@ jobs:
3131
matrix:
3232
include:
3333
- php: '8.3'
34-
moodle-branch: 'main'
35-
database: 'pgsql'
34+
moodle-branch: 'MOODLE_405_STABLE'
35+
database: 'mariadb'
3636
- php: '8.2'
37-
moodle-branch: 'MOODLE_404_STABLE'
37+
moodle-branch: 'MOODLE_405_STABLE'
3838
database: 'pgsql'
3939
- php: '8.1'
4040
moodle-branch: 'MOODLE_404_STABLE'

classes/output/renderer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,23 @@ public function download_dataformat_selector($label, $base, $name = 'dataformat'
564564

565565
return $this->render_from_template('mod_questionnaire/dataformat_selector', $data);
566566
}
567+
568+
/**
569+
* @param $urlroot
570+
* @param $options
571+
* @param $userview
572+
* @return string
573+
* @throws \coding_exception
574+
* @throws \moodle_exception
575+
*/
576+
public function viewresponse_print_menu($urlroot, $options, $userview) {
577+
if (!$urlroot instanceof \moodle_url) {
578+
$urlroot = new \moodle_url($urlroot);
579+
}
580+
$select = new \single_select($urlroot, 'responsestats', $options, $userview, null);
581+
$select->label = get_string('view');
582+
$select->class = 'm-1';
583+
$output = $this->render($select);
584+
return $output;
585+
}
567586
}

classes/responsetype/date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function get_results_tags($weights, $participants, $respondents, $showtot
205205

206206
if ($showtotals == 1) {
207207
$pagetags->total = new \stdClass();
208-
$pagetags->total->total = "$numresps/$participants";
208+
$pagetags->total->total = "($respondents)";
209209
}
210210
}
211211

classes/responsetype/rank.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ private function mkrescount($rids, $rows, $sort) {
888888
$nbna = $this->counts[$content]->nbna;
889889
// TOTAL number of responses for this possible answer.
890890
$total = $this->counts[$content]->num;
891-
$nbresp = '<strong>'.$total.'</strong>';
891+
$nbresp = '<strong>('.$total.')</strong>';
892892
if ($osgood) {
893893
// Ensure there are two bits of content.
894894
list($content, $contentright) = array_merge(preg_split('/[|]/', $content), array(' '));

classes/responsetype/responsetype.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function get_results_tags($weights, $participants, $respondents, $showtot
220220
$pagetags->total->width2 = $percent * 1.4;
221221
$pagetags->total->image2 = $imageurl . 'thbar.gif';
222222
$pagetags->total->percent = sprintf('&nbsp;%.'.$precision.'f%%', $percent);
223-
$pagetags->total->total = "$respondents/$participants";
223+
$pagetags->total->total = "($respondents)";
224224
$pagetags->total->evencolor = $evencolor;
225225
}
226226
}

classes/responsetype/single.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,18 @@ public function display_results($rids=false, $sort='', $anonymous=false) {
237237
}
238238
$numresps = count($rids);
239239

240+
$rsql = '';
241+
if (!empty($rids)) {
242+
list($rsql, $params) = $DB->get_in_or_equal($rids);
243+
$rsql = ' AND response_id ' . $rsql;
244+
}
245+
240246
$responsecountsql = 'SELECT COUNT(DISTINCT r.response_id) ' .
241-
'FROM {' . $this->response_table() . '} r ' .
242-
'WHERE r.question_id = ? ';
243-
$numrespondents = $DB->count_records_sql($responsecountsql, [$this->question->id]);
247+
'FROM {' . $this->response_table() . '} r, ' .
248+
'{questionnaire_response} qr ' .
249+
'WHERE question_id = ' . $this->question->id . $rsql .
250+
' AND r.response_id = qr.id ';
251+
$numrespondents = $DB->count_records_sql($responsecountsql, $params);
244252

245253
if ($rows) {
246254
$counts = [];

classes/responsetype/text.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function get_results_tags($weights, $participants, $respondents, $showtot
210210

211211
if ($showtotals == 1) {
212212
$pagetags->total = new \stdClass();
213-
$pagetags->total->total = "$respondents/$participants";
213+
$pagetags->total->total = "($respondents)";
214214
}
215215
} else {
216216
$nbresponses = 0;
@@ -250,7 +250,7 @@ public function get_results_tags($weights, $participants, $respondents, $showtot
250250

251251
if ($showtotals == 1) {
252252
$pagetags->total = new \stdClass();
253-
$pagetags->total->total = "$respondents/$participants";
253+
$pagetags->total->total = "($respondents)";
254254
$pagetags->total->evencolor = $evencolor;
255255
}
256256
}

lang/en/questionnaire.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
$string['allnameddegrees'] = 'Named degrees';
4242
$string['allnameddegrees_help'] = 'Specify text to display for rate values instead of the number. Leave a value blank to not use.';
4343
$string['alreadyfilled'] = 'You have already filled out this questionnaire for us{$a}. Thank you.';
44+
$string['allresponses'] = 'All responses';
4445
$string['andaveragevalues'] = 'and average values';
4546
$string['anonymous'] = 'Anonymous';
4647
$string['answergiven'] = 'This answer given';
@@ -250,6 +251,7 @@
250251
$string['firstrespondent'] = 'First Respondent';
251252
$string['formateditor'] = 'HTML editor';
252253
$string['formatplain'] = 'Plain text';
254+
$string['fullsubmissions'] = 'Full submissions';
253255
$string['grade'] = 'Submission grade';
254256
$string['gradesdeleted'] = 'Questionnaire grades deleted';
255257
$string['headingtext'] = 'Heading text';
@@ -528,6 +530,7 @@
528530
$string['remove'] = 'Delete';
529531
$string['removenotinuse'] = 'This questionnaire used to depend on a Public questionnaire which has been deleted.
530532
It can no longer be used and should be deleted.';
533+
$string['responsesnotsubmitted'] = 'Responses not submitted';
531534
$string['required'] = 'Response is required';
532535
$string['required_help'] = 'If you select ***Yes***, response to this question will be required, i.e.
533536
the respondent will not be able to submit the questionnaire
@@ -609,6 +612,7 @@
609612
$string['submitpreview'] = 'Submit preview';
610613
$string['submitpreviewcorrect'] = 'This submission would be accepted as correctly filled in.';
611614
$string['submitsurvey'] = 'Submit questionnaire';
615+
$string['submissions'] = 'Submissions';
612616
$string['submitted'] = 'Submitted on:';
613617
$string['subtitle'] = 'Subtitle';
614618
$string['subtitle_help'] = 'Subtitle of this questionnaire. Appears below the title on the first page only.';

questionnaire.class.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,12 @@ public function can_view_response($rid) {
709709

710710
/**
711711
* True if the user can view the responses to this questionnaire, and there are valid responses.
712+
*
712713
* @param null|int $usernumresp
714+
* @param bool $isviewreport
713715
* @return bool
714716
*/
715-
public function can_view_all_responses($usernumresp = null) {
717+
public function can_view_all_responses($usernumresp = null, $isviewreport = false) {
716718
global $USER, $SESSION;
717719

718720
$owner = $this->is_survey_owner();
@@ -738,7 +740,7 @@ public function can_view_all_responses($usernumresp = null) {
738740
}
739741

740742
$grouplogic = $canviewgroups || $canviewallgroups;
741-
$respslogic = ($numresp > 0) && ($numselectedresps > 0);
743+
$respslogic = ($numresp > 0) && ($numselectedresps > 0) || $isviewreport;
742744
return $this->can_view_all_responses_anytime($grouplogic, $respslogic) ||
743745
$this->can_view_all_responses_with_restrictions($usernumresp, $grouplogic, $respslogic);
744746
}
@@ -851,15 +853,20 @@ public function get_responses($userid=false, $groupid=0) {
851853
$sql = 'SELECT r.* ' .
852854
'FROM {questionnaire_response} r ' .
853855
$groupsql .
854-
'WHERE r.questionnaireid = :questionnaireid AND r.complete = :status' . $groupcnd;
856+
'WHERE r.questionnaireid = :questionnaireid' . $groupcnd;
855857
$params['questionnaireid'] = $this->id;
856-
$params['status'] = 'y';
857858
}
858859
if ($userid) {
859860
$sql .= ' AND r.userid = :userid';
860861
$params['userid'] = $userid;
861862
}
862863

864+
$status = optional_param('responsestats', '0', PARAM_ALPHANUM);
865+
if ($status) {
866+
$status = ($status === 'n') ? 'n' : 'y';
867+
$sql .= ' AND r.complete = :status';
868+
$params['status'] = $status;
869+
}
863870
$sql .= ' ORDER BY r.id';
864871
return $DB->get_records_sql($sql, $params) ?? [];
865872
}
@@ -2859,6 +2866,7 @@ public function survey_results($rid = '', $uid=false, $pdf = false, $currentgrou
28592866
$numresps = 1;
28602867
} else {
28612868
$navbar = false;
2869+
$userview = optional_param('responsestats', 0, PARAM_ALPHA);
28622870
if ($uid !== false) { // One participant only.
28632871
$rows = $this->get_responses($uid);
28642872
// All participants or all members of a group.
@@ -2875,8 +2883,12 @@ public function survey_results($rid = '', $uid=false, $pdf = false, $currentgrou
28752883
return;
28762884
}
28772885
$numresps = count($rows);
2886+
$respondentstring = get_string('responses', 'questionnaire');
2887+
if ($userview === 'y') {
2888+
$respondentstring = get_string('submissions', 'questionnaire');
2889+
}
28782890
$this->page->add_to_page('respondentinfo',
2879-
' '.get_string('responses', 'questionnaire').': <strong>'.$numresps.'</strong>');
2891+
' ' . $respondentstring . ': <strong>' . $numresps . '</strong>');
28802892
if (empty($rows)) {
28812893
$errmsg = get_string('erroropening', 'questionnaire') .' '. get_string('noresponsedata', 'questionnaire');
28822894
return($errmsg);

report.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
$currentgroupid = optional_param('group', 0, PARAM_INT); // Groupid.
3737
$user = optional_param('user', '', PARAM_INT);
3838
$outputtarget = optional_param('target', 'html', PARAM_ALPHA); // Default 'html'. Could be 'pdf'.
39+
$userview = optional_param('responsestats', '0', PARAM_ALPHANUM);
3940

4041
$userid = $USER->id;
4142
switch ($action) {
@@ -87,7 +88,7 @@
8788

8889
// If you can't view the questionnaire, or can't view a specified response, error out.
8990
$context = context_module::instance($cm->id);
90-
if (!$questionnaire->can_view_all_responses() && !$individualresponse) {
91+
if (!$questionnaire->can_view_all_responses(null, true) && !$individualresponse) {
9192
// Should never happen, unless called directly by a snoop...
9293
throw new \moodle_exception('nopermissions', 'mod_questionnaire');
9394
}
@@ -205,6 +206,14 @@
205206
}
206207
}
207208

209+
$responsestatus = [
210+
'y' => get_string('fullsubmissions', 'questionnaire'),
211+
'0' => get_string('allresponses', 'questionnaire'),
212+
'n' => get_string('responsesnotsubmitted', 'questionnaire'),
213+
];
214+
// Set default userview to all responses.
215+
$userview = array_key_exists($userview, $responsestatus) ? $userview : '0';
216+
208217
switch ($action) {
209218

210219
case 'dresp': // Delete individual response? Ask for confirmation.
@@ -582,7 +591,7 @@
582591
if ($currentgroupid > 0) {
583592
$groupname = get_string('group').': <strong>'.groups_get_group_name($currentgroupid).'</strong>';
584593
} else {
585-
$groupname = '<strong>'.get_string('allparticipants').'</strong>';
594+
$groupname = '<strong>'.$responsestatus[$userview].'</strong>';
586595
}
587596

588597
// Available group modes (0 = no groups; 1 = separate groups; 2 = visible groups).
@@ -627,9 +636,9 @@
627636
if ($currentgroupid > 0) {
628637
$groupname = get_string('group') . ': <strong>' . groups_get_group_name($currentgroupid) . '</strong>';
629638
} else {
630-
$groupname = '<strong>' . get_string('allparticipants') . '</strong>';
639+
$groupname = '<strong>' . $responsestatus[$userview] . '</strong>';
631640
}
632-
$respinfo = get_string('viewallresponses', 'questionnaire') . '. ' . $groupname . '. ';
641+
$respinfo = get_string('view') . ' ' . $groupname;
633642
$strsort = get_string('order_' . $sort, 'questionnaire');
634643
$respinfo .= $strsort;
635644
$questionnaire->page->add_to_page('respondentinfo', $respinfo);
@@ -650,13 +659,15 @@
650659
if ($outputtarget != 'print') {
651660
$linkname = get_string('downloadpdf', 'mod_questionnaire');
652661
$link = new moodle_url('/mod/questionnaire/report.php',
653-
['action' => 'vall', 'instance' => $instance, 'group' => $currentgroupid, 'target' => 'pdf']);
662+
['action' => 'vall', 'instance' => $instance, 'group' => $currentgroupid, 'target' => 'pdf',
663+
'responsestats' => $userview]);
654664
$downpdficon = new pix_icon('f/pdf', $linkname);
655665
$respinfo .= $questionnaire->renderer->action_link($link, null, null, null, $downpdficon);
656666

657667
$linkname = get_string('print', 'mod_questionnaire');
658668
$link = new \moodle_url('/mod/questionnaire/report.php',
659-
['action' => 'vall', 'instance' => $instance, 'group' => $currentgroupid, 'target' => 'print']);
669+
['action' => 'vall', 'instance' => $instance, 'group' => $currentgroupid, 'target' => 'print',
670+
'responsestats' => $userview]);
660671
$htmlicon = new pix_icon('t/print', $linkname);
661672
$options = ['menubar' => true, 'location' => false, 'scrollbars' => true, 'resizable' => true,
662673
'height' => 600, 'width' => 800, 'title' => $linkname];
@@ -666,7 +677,7 @@
666677
$respinfo .= $questionnaire->renderer->action_link($link, null, $action,
667678
['class' => $class, 'title' => $linkname], $htmlicon) . '&nbsp;';
668679

669-
$respinfo .= get_string('viewallresponses', 'questionnaire') . '. ' . $groupname . '. ';
680+
$respinfo .= $questionnaire->renderer->viewresponse_print_menu($url->out(), $responsestatus, $userview);
670681
$strsort = get_string('order_' . $sort, 'questionnaire');
671682
$respinfo .= $strsort;
672683
$respinfo .= $questionnaire->renderer->help_icon('orderresponses', 'questionnaire');
@@ -763,7 +774,7 @@
763774
if ($currentgroupid > 0) {
764775
$groupname = get_string('group') . ': <strong>' . groups_get_group_name($currentgroupid) . '</strong>';
765776
} else {
766-
$groupname = '<strong>' . get_string('allparticipants') . '</strong>';
777+
$groupname = '<strong>' . $responsestatus[$userview] . '</strong>';
767778
}
768779
if (!$byresponse) { // Show respondents individual responses.
769780
$questionnaire->view_response($rid, '', $resps, true, true, false, $currentgroupid, $outputtarget);
@@ -797,7 +808,7 @@
797808

798809
$groupname = get_string('group').': <strong>'.groups_get_group_name($currentgroupid).'</strong>';
799810
if ($currentgroupid == 0 ) {
800-
$groupname = get_string('allparticipants');
811+
$groupname = $responsestatus[$userview];
801812
}
802813
if ($byresponse) {
803814
$respinfo = '';

0 commit comments

Comments
 (0)