Skip to content

Commit

Permalink
Update sectiontext questions to display on mobile #482 (#517)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sayoder-aprende authored Feb 23, 2024
1 parent 1326e31 commit 9dec750
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
8 changes: 6 additions & 2 deletions classes/output/mobile.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public static function mobile_view_activity($args) {
if ($question->supports_mobile()) {
$pagequestions[] = $question->mobile_question_display($qnum, $questionnaire->autonum);
$responses = array_merge($responses, $question->get_mobile_response_data($response));
$qnum++;
if ($question->is_numbered()) {
$qnum++;
}
}
}
$data['prevpage'] = 0;
Expand Down Expand Up @@ -265,8 +267,10 @@ protected static function add_pagequestion_data($questionnaire, $pagenum, $respo
if (($response !== null) && isset($response->answers[$questionid])) {
$responses = array_merge($responses, $question->get_mobile_response_data($response));
}
if ($question->is_numbered()) {
$qnum++;
}
}
$qnum++;
}

return ['pagequestions' => $pagequestions, 'responses' => $responses];
Expand Down
8 changes: 8 additions & 0 deletions classes/question/pagebreak.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public function supports_mobile() {
public function mobile_question_display($qnum, $autonum = false) {
return false;
}

/**
* Override and return false if a number should not be rendered for this question in any context.
* @return bool
*/
public function is_numbered() {
return false;
}
}
8 changes: 8 additions & 0 deletions classes/question/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ public function supports_feedback_scores() {
return $this->supports_feedback();
}

/**
* Override and return false if a number should not be rendered for this question in any context.
* @return bool
*/
public function is_numbered() {
return true;
}

/**
* True if the question supports feedback and has valid settings for feedback. Override if the default logic is not enough.
* @return bool
Expand Down
46 changes: 46 additions & 0 deletions classes/question/sectiontext.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,44 @@ public function supports_feedback() {
return false;
}

/**
* True if question provides mobile support.
* @return bool
*/
public function supports_mobile() {
return true;
}

/**
* Display on mobile.
*
* @param int $qnum
* @param bool $autonum
*/
public function mobile_question_display($qnum, $autonum = false) {
$options = ['noclean' => true, 'para' => false, 'filter' => true,
'context' => $this->context, 'overflowdiv' => true];
$mobiledata = (object)[
'id' => $this->id,
'name' => $this->name,
'type_id' => $this->type_id,
'length' => $this->length,
'content' => format_text(file_rewrite_pluginfile_urls($this->content, 'pluginfile.php', $this->context->id,
'mod_questionnaire', 'question', $this->id), FORMAT_HTML, $options),
'content_stripped' => strip_tags($this->content),
'required' => false,
'deleted' => $this->deleted,
'response_table' => $this->responsetable,
'fieldkey' => $this->mobile_fieldkey(),
'precise' => $this->precise,
'qnum' => '',
'errormessage' => get_string('required') . ': ' . $this->name
];

$mobiledata->issectiontext = true;
return $mobiledata;
}

/**
* True if question type supports feedback scores and weights. Same as supports_feedback() by default.
*/
Expand All @@ -81,6 +119,14 @@ public function question_template() {
return 'mod_questionnaire/question_sectionfb';
}

/**
* Override and return false if a number should not be rendered for this question in any context.
* @return bool
*/
public function is_numbered() {
return false;
}

/**
* Return the context tags for the check question template.
* @param \mod_questionnaire\responsetype\response\response $response
Expand Down
8 changes: 3 additions & 5 deletions classes/questions_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function definition() {
redirect($CFG->wwwroot.'/mod/questionnaire/questions.php?id='.$questionnaire->cm->id);
}

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

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

if ($tid != QUESPAGEBREAK) {
if ($tid != QUESSECTIONTEXT) {
if ($question->is_numbered()) {
$qnumber = '<div class="qn-info"><h2 class="qn-number">'.$qnum.'</h2></div>';
} else {
} else {
$qnumber = '';
}
}

if ($this->moveq && $pos < $moveqposition) {
Expand Down
14 changes: 7 additions & 7 deletions questionnaire.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ private function survey_render(&$formdata, $section = 1, $message = '') {
$this->renderer->render_progress_bar($section, $this->questionsbysec));
}
foreach ($this->questionsbysec[$section] as $questionid) {
if ($this->questions[$questionid]->type_id != QUESSECTIONTEXT) {
if ($this->questions[$questionid]->is_numbered()) {
$i++;
}
// Need questionnaire id to get the questionnaire object in sectiontext (Label) question class.
Expand Down Expand Up @@ -1615,7 +1615,7 @@ public function survey_print_render($courseid, $message = '', $referer='', $rid=
$page++;
}
foreach ($section as $questionid) {
if ($this->questions[$questionid]->type_id == QUESSECTIONTEXT) {
if (!$this->questions[$questionid]->is_numbered()) {
$i--;
}
if (isset($allqdependants[$questionid])) {
Expand Down Expand Up @@ -1848,8 +1848,8 @@ private function response_check_format($section, $formdata, $checkmissing = true

if (key_exists($section, $this->questionsbysec)) {
foreach ($this->questionsbysec[$section] as $questionid) {
$tid = $this->questions[$questionid]->type_id;
if ($tid != QUESSECTIONTEXT) {

if ($this->questions[$questionid]->is_numbered()) {
$qnum++;
}
if (!$this->questions[$questionid]->response_complete($formdata)) {
Expand Down Expand Up @@ -2910,13 +2910,13 @@ public function survey_results($rid = '', $uid=false, $pdf = false, $currentgrou
if ($question->type_id == QUESPAGEBREAK) {
continue;
}
if ($question->type_id != QUESSECTIONTEXT) {
if ($question->is_numbered()) {
$qnum++;
}
if (!$pdf) {
$this->page->add_to_page('responses', $this->renderer->container_start('qn-container'));
$this->page->add_to_page('responses', $this->renderer->container_start('qn-info'));
if ($question->type_id != QUESSECTIONTEXT) {
if ($question->is_numbered()) {
$this->page->add_to_page('responses', $this->renderer->heading($qnum, 2, 'qn-number'));
}
$this->page->add_to_page('responses', $this->renderer->container_end()); // End qn-info.
Expand All @@ -2928,7 +2928,7 @@ public function survey_results($rid = '', $uid=false, $pdf = false, $currentgrou
}
if ($pdf) {
$response = new stdClass();
if ($question->type_id != QUESSECTIONTEXT) {
if ($question->is_numbered()) {
$response->qnum = $qnum;
}
$response->qcontent = format_text(file_rewrite_pluginfile_urls($question->content, 'pluginfile.php',
Expand Down

0 comments on commit 9dec750

Please sign in to comment.