Skip to content

Commit 9dec750

Browse files
Update sectiontext questions to display on mobile #482 (#517)
* Update sectiontext questions to display on mobile. Also refactored the code a bit for clarity: The question class now has an is_numbered() method (default true) that can be overridden for question types that should not have numbers. Replaced some checks for specific question types with checks for is_numbered(). * test: Fix PHPDoc for mobile_question_display
1 parent 1326e31 commit 9dec750

File tree

6 files changed

+78
-14
lines changed

6 files changed

+78
-14
lines changed

classes/output/mobile.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ public static function mobile_view_activity($args) {
162162
if ($question->supports_mobile()) {
163163
$pagequestions[] = $question->mobile_question_display($qnum, $questionnaire->autonum);
164164
$responses = array_merge($responses, $question->get_mobile_response_data($response));
165-
$qnum++;
165+
if ($question->is_numbered()) {
166+
$qnum++;
167+
}
166168
}
167169
}
168170
$data['prevpage'] = 0;
@@ -265,8 +267,10 @@ protected static function add_pagequestion_data($questionnaire, $pagenum, $respo
265267
if (($response !== null) && isset($response->answers[$questionid])) {
266268
$responses = array_merge($responses, $question->get_mobile_response_data($response));
267269
}
270+
if ($question->is_numbered()) {
271+
$qnum++;
272+
}
268273
}
269-
$qnum++;
270274
}
271275

272276
return ['pagequestions' => $pagequestions, 'responses' => $responses];

classes/question/pagebreak.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ public function supports_mobile() {
102102
public function mobile_question_display($qnum, $autonum = false) {
103103
return false;
104104
}
105+
106+
/**
107+
* Override and return false if a number should not be rendered for this question in any context.
108+
* @return bool
109+
*/
110+
public function is_numbered() {
111+
return false;
112+
}
105113
}

classes/question/question.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ public function supports_feedback_scores() {
497497
return $this->supports_feedback();
498498
}
499499

500+
/**
501+
* Override and return false if a number should not be rendered for this question in any context.
502+
* @return bool
503+
*/
504+
public function is_numbered() {
505+
return true;
506+
}
507+
500508
/**
501509
* True if the question supports feedback and has valid settings for feedback. Override if the default logic is not enough.
502510
* @return bool

classes/question/sectiontext.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,44 @@ public function supports_feedback() {
5959
return false;
6060
}
6161

62+
/**
63+
* True if question provides mobile support.
64+
* @return bool
65+
*/
66+
public function supports_mobile() {
67+
return true;
68+
}
69+
70+
/**
71+
* Display on mobile.
72+
*
73+
* @param int $qnum
74+
* @param bool $autonum
75+
*/
76+
public function mobile_question_display($qnum, $autonum = false) {
77+
$options = ['noclean' => true, 'para' => false, 'filter' => true,
78+
'context' => $this->context, 'overflowdiv' => true];
79+
$mobiledata = (object)[
80+
'id' => $this->id,
81+
'name' => $this->name,
82+
'type_id' => $this->type_id,
83+
'length' => $this->length,
84+
'content' => format_text(file_rewrite_pluginfile_urls($this->content, 'pluginfile.php', $this->context->id,
85+
'mod_questionnaire', 'question', $this->id), FORMAT_HTML, $options),
86+
'content_stripped' => strip_tags($this->content),
87+
'required' => false,
88+
'deleted' => $this->deleted,
89+
'response_table' => $this->responsetable,
90+
'fieldkey' => $this->mobile_fieldkey(),
91+
'precise' => $this->precise,
92+
'qnum' => '',
93+
'errormessage' => get_string('required') . ': ' . $this->name
94+
];
95+
96+
$mobiledata->issectiontext = true;
97+
return $mobiledata;
98+
}
99+
62100
/**
63101
* True if question type supports feedback scores and weights. Same as supports_feedback() by default.
64102
*/
@@ -81,6 +119,14 @@ public function question_template() {
81119
return 'mod_questionnaire/question_sectionfb';
82120
}
83121

122+
/**
123+
* Override and return false if a number should not be rendered for this question in any context.
124+
* @return bool
125+
*/
126+
public function is_numbered() {
127+
return false;
128+
}
129+
84130
/**
85131
* Return the context tags for the check question template.
86132
* @param \mod_questionnaire\responsetype\response\response $response

classes/questions_form.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function definition() {
146146
redirect($CFG->wwwroot.'/mod/questionnaire/questions.php?id='.$questionnaire->cm->id);
147147
}
148148

149-
if ($tid != QUESPAGEBREAK && $tid != QUESSECTIONTEXT) {
149+
if ($question->is_numbered()) {
150150
$qnum++;
151151
}
152152

@@ -330,12 +330,10 @@ public function definition() {
330330
$mform->addElement('static', 'qdepend_' . $question->id, '', $dependencies);
331331
}
332332

333-
if ($tid != QUESPAGEBREAK) {
334-
if ($tid != QUESSECTIONTEXT) {
333+
if ($question->is_numbered()) {
335334
$qnumber = '<div class="qn-info"><h2 class="qn-number">'.$qnum.'</h2></div>';
336-
} else {
335+
} else {
337336
$qnumber = '';
338-
}
339337
}
340338

341339
if ($this->moveq && $pos < $moveqposition) {

questionnaire.class.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ private function survey_render(&$formdata, $section = 1, $message = '') {
13231323
$this->renderer->render_progress_bar($section, $this->questionsbysec));
13241324
}
13251325
foreach ($this->questionsbysec[$section] as $questionid) {
1326-
if ($this->questions[$questionid]->type_id != QUESSECTIONTEXT) {
1326+
if ($this->questions[$questionid]->is_numbered()) {
13271327
$i++;
13281328
}
13291329
// Need questionnaire id to get the questionnaire object in sectiontext (Label) question class.
@@ -1615,7 +1615,7 @@ public function survey_print_render($courseid, $message = '', $referer='', $rid=
16151615
$page++;
16161616
}
16171617
foreach ($section as $questionid) {
1618-
if ($this->questions[$questionid]->type_id == QUESSECTIONTEXT) {
1618+
if (!$this->questions[$questionid]->is_numbered()) {
16191619
$i--;
16201620
}
16211621
if (isset($allqdependants[$questionid])) {
@@ -1848,8 +1848,8 @@ private function response_check_format($section, $formdata, $checkmissing = true
18481848

18491849
if (key_exists($section, $this->questionsbysec)) {
18501850
foreach ($this->questionsbysec[$section] as $questionid) {
1851-
$tid = $this->questions[$questionid]->type_id;
1852-
if ($tid != QUESSECTIONTEXT) {
1851+
1852+
if ($this->questions[$questionid]->is_numbered()) {
18531853
$qnum++;
18541854
}
18551855
if (!$this->questions[$questionid]->response_complete($formdata)) {
@@ -2910,13 +2910,13 @@ public function survey_results($rid = '', $uid=false, $pdf = false, $currentgrou
29102910
if ($question->type_id == QUESPAGEBREAK) {
29112911
continue;
29122912
}
2913-
if ($question->type_id != QUESSECTIONTEXT) {
2913+
if ($question->is_numbered()) {
29142914
$qnum++;
29152915
}
29162916
if (!$pdf) {
29172917
$this->page->add_to_page('responses', $this->renderer->container_start('qn-container'));
29182918
$this->page->add_to_page('responses', $this->renderer->container_start('qn-info'));
2919-
if ($question->type_id != QUESSECTIONTEXT) {
2919+
if ($question->is_numbered()) {
29202920
$this->page->add_to_page('responses', $this->renderer->heading($qnum, 2, 'qn-number'));
29212921
}
29222922
$this->page->add_to_page('responses', $this->renderer->container_end()); // End qn-info.
@@ -2928,7 +2928,7 @@ public function survey_results($rid = '', $uid=false, $pdf = false, $currentgrou
29282928
}
29292929
if ($pdf) {
29302930
$response = new stdClass();
2931-
if ($question->type_id != QUESSECTIONTEXT) {
2931+
if ($question->is_numbered()) {
29322932
$response->qnum = $qnum;
29332933
}
29342934
$response->qcontent = format_text(file_rewrite_pluginfile_urls($question->content, 'pluginfile.php',

0 commit comments

Comments
 (0)