diff --git a/classes/feedback_form.php b/classes/feedback_form.php index 25260847..d5f3ebad 100644 --- a/classes/feedback_form.php +++ b/classes/feedback_form.php @@ -36,6 +36,18 @@ class feedback_form extends \moodleform { */ public function definition() { global $questionnaire; + // We need to get the number of current feedbacksections to allow the rose and radar chart types or not. + global $DB; + $sid = $questionnaire->survey->id; + $numfeedbacksections = $DB->count_records('questionnaire_fb_sections', ['surveyid' => $sid]); + + // Get all questions that are valid feedback questions. + $nbvalidquestions = 0; + foreach ($questionnaire->questions as $question) { + if ($question->valid_feedback()) { + $nbvalidquestions++; + } + } $mform =& $this->_form; @@ -44,7 +56,9 @@ public function definition() { $feedbackoptions = []; $feedbackoptions[0] = get_string('feedbacknone', 'questionnaire'); $feedbackoptions[1] = get_string('feedbackglobal', 'questionnaire'); - $feedbackoptions[2] = get_string('feedbacksections', 'questionnaire'); + if ($nbvalidquestions > 1) { + $feedbackoptions[2] = get_string('feedbacksections', 'questionnaire'); + } $mform->addElement('select', 'feedbacksections', get_string('feedbackoptions', 'questionnaire'), $feedbackoptions); $mform->setDefault('feedbacksections', $questionnaire->survey->feedbacksections); @@ -56,6 +70,7 @@ public function definition() { // Is the RGraph library enabled at level site? if (get_config('questionnaire', 'usergraph')) { + /* First set up a Chart type group for the Global Feedback option. */ $chartgroup = []; $charttypes = [null => get_string('none'), 'bipolar' => get_string('chart:bipolar', 'questionnaire'), @@ -63,42 +78,45 @@ public function definition() { $chartgroup[] = $mform->createElement('select', 'chart_type_global', get_string('chart:type', 'questionnaire') . ' (' . get_string('feedbackglobal', 'questionnaire') . ')', $charttypes); + + // If we are editing an already saved questionnaire, and 'Feedback options' + // is set to 'Global Feedback', set default to its charttype. if ($questionnaire->survey->feedbacksections == 1) { $mform->setDefault('chart_type_global', $questionnaire->survey->chart_type); } - $mform->disabledIf('chart_type_global', 'feedbacksections', 'eq', 0); - $mform->disabledIf('chart_type_global', 'feedbacksections', 'neq', 1); - $charttypes = [null => get_string('none'), - 'bipolar' => get_string('chart:bipolar', 'questionnaire'), - 'hbar' => get_string('chart:hbar', 'questionnaire'), - 'rose' => get_string('chart:rose', 'questionnaire')]; - $chartgroup[] = $mform->createElement('select', 'chart_type_two_sections', - get_string('chart:type', 'questionnaire') . ' (' . - get_string('feedbackbysection', 'questionnaire') . ')', $charttypes); - if ($questionnaire->survey->feedbacksections > 1) { - $mform->setDefault('chart_type_two_sections', $questionnaire->survey->chart_type); - } - $mform->disabledIf('chart_type_two_sections', 'feedbacksections', 'neq', 2); + /* If 'Feedback options' is set to 'No Feedback messages' then hide the Global Feedback Chart type group. */ + $mform->hideIf('chart_type_global', 'feedbacksections', 'eq', 0); + /* Now set up a Chart type group for the Feedback sections option. */ $charttypes = [null => get_string('none'), 'bipolar' => get_string('chart:bipolar', 'questionnaire'), - 'hbar' => get_string('chart:hbar', 'questionnaire'), - 'radar' => get_string('chart:radar', 'questionnaire'), - 'rose' => get_string('chart:rose', 'questionnaire')]; + 'hbar' => get_string('chart:hbar', 'questionnaire')]; + /* If there are at least 3 feedback sections, make the rose and radar charttypes available. */ + if ($numfeedbacksections > 2) { + $charttypes['rose'] = get_string('chart:rose', 'questionnaire'); + $charttypes['radar'] = get_string('chart:radar', 'questionnaire'); + } $chartgroup[] = $mform->createElement('select', 'chart_type_sections', get_string('chart:type', 'questionnaire') . ' (' . get_string('feedbackbysection', 'questionnaire') . ')', $charttypes); + + // If we are editing an already saved questionnaire, and 'Feedback options' + // is set to 'Feedback sections', set default to its charttype. if ($questionnaire->survey->feedbacksections > 1) { $mform->setDefault('chart_type_sections', $questionnaire->survey->chart_type); } - $mform->disabledIf('chart_type_sections', 'feedbacksections', 'eq', 0); - $mform->disabledIf('chart_type_sections', 'feedbacksections', 'eq', 1); - $mform->disabledIf('chart_type_sections', 'feedbacksections', 'eq', 2); + + /* If 'Feedback options' is NOT set to 'Feedback sections' then hide the 'Feedback sections' Chart type group. */ + $mform->hideIf('chart_type_sections', 'feedbacksections', 'neq', 2); + /* If 'Feedback options' is set to 'Feedback sections' then hide the Global Feedback Chart type group. */ + $mform->hideIf('chart_type_global', 'feedbacksections', 'eq', 2); $mform->addGroup($chartgroup, 'chartgroup', get_string('chart:type', 'questionnaire'), null, false); $mform->addHelpButton('chartgroup', 'chart:type', 'questionnaire'); + /* If 'Feedback options' is set to 'No Feedback messages' then completely hide the chartgroup group. */ + $mform->hideIf('chartgroup', 'feedbacksections', 'eq', 0); } $editoroptions = ['maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext' => true]; $mform->addElement('editor', 'feedbacknotes', get_string('feedbacknotes', 'questionnaire'), null, $editoroptions); @@ -114,8 +132,9 @@ public function definition() { $mform->setType('courseid', PARAM_RAW); // Can't seem to disable or hide one button in the group, so create two different button sets and hide one. + $submitlabel = get_string('savechangesanddisplay'); $buttongroup = []; - $buttongroup[] = $mform->createElement('submit', 'feedbacksettingsbutton1', get_string('savesettings', 'questionnaire')); + $buttongroup[] = $mform->createElement('submit', 'submitbutton', $submitlabel); $buttongroup[] = $mform->createElement('submit', 'feedbackeditbutton', get_string('feedbackeditsections', 'questionnaire')); $mform->addGroup($buttongroup, 'buttongroup'); if (moodle_major_version() == '3.3') { diff --git a/classes/feedback_section_form.php b/classes/feedback_section_form.php index 740d5604..e052fadf 100644 --- a/classes/feedback_section_form.php +++ b/classes/feedback_section_form.php @@ -170,10 +170,13 @@ public function definition() { $mform->setType('sectionid', PARAM_INT); // Buttons. + $submitlabelsaveanddisplay = get_string('savechangesanddisplay'); + $submitlabelreturntofeedbackoptions = get_string('feedbackoptions', 'questionnaire'); $buttonarray = []; - $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges')); $buttonarray[] = $mform->createElement('submit', 'confirmdeletesection', get_string('deletesection', 'questionnaire')); - $buttonarray[] = $mform->createElement('cancel'); + $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges')); + $buttonarray[] = $mform->createElement('submit', 'submitbutton1', $submitlabelreturntofeedbackoptions); + $buttonarray[] = $mform->createElement('submit', 'submitbutton2', $submitlabelsaveanddisplay); $mform->addGroup($buttonarray, 'buttonar', '', ' ', false); $mform->closeHeaderBefore('buttonar'); } diff --git a/drawchart.php b/drawchart.php index ab50fe83..5e6fcf7c 100644 --- a/drawchart.php +++ b/drawchart.php @@ -104,25 +104,27 @@ function draw_chart($feedbacktype, $labels, $groupname, } } foreach ($labels as $key => $label) { - $lb = explode("|", $label); - // Just in case there is no pipe separator in label. - if (count($lb) > 1) { - $left = $lb[0]; - $right = $lb[1]; - // Lib core_text and diff needed for non-ascii characters. - $lenleft = core_text::strlen($left); - $diffleft = strlen($left) - $lenleft; - $lenright = core_text::strlen($right); - $diffright = strlen($right) - $lenright; - if ($lenleft < $lenright) { - $padlength = $lenright + $diffleft; - $left = str_pad($left, $padlength, ' ', STR_PAD_LEFT); - } - if ($lenleft > $lenright) { - $padlength = $lenleft + $diffright; - $right = str_pad($right, $padlength, ' ', STR_PAD_RIGHT); + if ($label !== null) { + $lb = explode("|", $label); + // Just in case there is no pipe separator in label. + if (count($lb) > 1) { + $left = $lb[0]; + $right = $lb[1]; + // Lib core_text and diff needed for non-ascii characters. + $lenleft = core_text::strlen($left); + $diffleft = strlen($left) - $lenleft; + $lenright = core_text::strlen($right); + $diffright = strlen($right) - $lenright; + if ($lenleft < $lenright) { + $padlength = $lenright + $diffleft; + $left = str_pad($left, $padlength, ' ', STR_PAD_LEFT); + } + if ($lenleft > $lenright) { + $padlength = $lenleft + $diffright; + $right = str_pad($right, $padlength, ' ', STR_PAD_RIGHT); + } + $labels[$key] = $left .' '.$right; } - $labels[$key] = $left .' '.$right; } } // Find length of longest label. diff --git a/fbsections.php b/fbsections.php index 13f3a8e7..c1146b6c 100644 --- a/fbsections.php +++ b/fbsections.php @@ -131,10 +131,6 @@ $feedbackform->set_data($sdata); -if ($feedbackform->is_cancelled()) { - redirect(new moodle_url('/mod/questionnaire/feedback.php', ['id' => $cm->id])); -} - if ($settings = $feedbackform->get_data()) { // Because formslib doesn't support 'numeric' or 'image' inputs, the results won't show up in the $feedbackform object. $fullform = data_submitted(); @@ -177,7 +173,7 @@ // Update the section with question weights. $feedbacksection->set_new_scorecalculation($scorecalculation); - } else if (isset($settings->submitbutton)) { + } else if (isset($settings->submitbutton ) || isset($settings->submitbutton1) || isset($settings->submitbutton2)) { if (isset($fullform->weight)) { $feedbacksection->scorecalculation = $fullform->weight; } else { @@ -238,7 +234,14 @@ // Update all feedback data. $feedbacksection->update(); + if (isset($settings->submitbutton1)) { + redirect(new moodle_url('/mod/questionnaire/feedback.php', ['id' => $cm->id])); + } + if (isset($settings->submitbutton2)) { + redirect(new moodle_url('/mod/questionnaire/view.php', ['id' => $cm->id])); + } } + $feedbackform = new \mod_questionnaire\feedback_section_form('fbsections.php', $customdata); } diff --git a/feedback.php b/feedback.php index cf6c2f50..96516efa 100644 --- a/feedback.php +++ b/feedback.php @@ -115,8 +115,6 @@ if ($settings->feedbacksections == 1) { $sdata->chart_type = $settings->chart_type_global; } else if ($settings->feedbacksections == 2) { - $sdata->chart_type = $settings->chart_type_two_sections; - } else if ($settings->feedbacksections > 2) { $sdata->chart_type = $settings->chart_type_sections; } } @@ -144,6 +142,8 @@ $feedbacksection = mod_questionnaire\feedback\section::new_section($questionnaire->sid, $sectionlabel); } redirect(new moodle_url('/mod/questionnaire/fbsections.php', ['id' => $cm->id, 'section' => $firstsection])); + } else { + redirect('view.php?id='.$cm->id); } }