From 2b568f1c935f3167210b4290bc5e3d6bf9818d7e Mon Sep 17 00:00:00 2001
From: Julien Boulen <julien.boulen@univ-rennes2.fr>
Date: Fri, 10 Feb 2023 11:07:45 +0100
Subject: [PATCH] MDL-76314 forum: add form check to prevent combining wrong
 parameters

Form validation prevents combining the forum type "single discussion" and the group mode "separate groups".
---
 mod/forum/lang/en/forum.php             |  2 ++
 mod/forum/mod_form.php                  |  5 +++++
 mod/forum/tests/behat/add_forum.feature | 15 +++++++++++++++
 3 files changed, 22 insertions(+)

diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php
index 248d3090708..388758cb59b 100644
--- a/mod/forum/lang/en/forum.php
+++ b/mod/forum/lang/en/forum.php
@@ -95,6 +95,8 @@ $string['cannotfavourite'] = 'Sorry, but you do not have the permission to star
 $string['cannottrack'] = 'Could not stop tracking that forum';
 $string['cannotunsubscribe'] = 'Could not unsubscribe you from that forum';
 $string['cannotupdatepost'] = 'You can not update this post';
+$string['cannotuseseperategroupsandsingletopic'] = 'Separate groups cannot be used with single discussion topic';
+$string['cannotusesingletopicandseperategroups'] = 'Single discussion topic cannot be used with separate groups';
 $string['cannotviewpostyet'] = 'You can\'t read other students\' posts in this discussion yet because you haven\'t posted.';
 $string['cannotviewusersposts'] = 'There are no posts made by this user that you are able to view.';
 $string['cleanreadtime'] = 'Mark old posts as read hour';
diff --git a/mod/forum/mod_form.php b/mod/forum/mod_form.php
index 65cd77a75ea..37174841848 100644
--- a/mod/forum/mod_form.php
+++ b/mod/forum/mod_form.php
@@ -341,6 +341,11 @@ class mod_forum_mod_form extends moodleform_mod {
     public function validation($data, $files) {
         $errors = parent::validation($data, $files);
 
+        if ($data['type'] === 'single' && $data['groupmode'] == SEPARATEGROUPS) {
+            $errors['type'] = get_string('cannotusesingletopicandseperategroups', 'forum');
+            $errors['groupmode'] = get_string('cannotuseseperategroupsandsingletopic', 'forum');
+        }
+
         if ($data['duedate'] && $data['cutoffdate']) {
             if ($data['duedate'] > $data['cutoffdate']) {
                 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'forum');
diff --git a/mod/forum/tests/behat/add_forum.feature b/mod/forum/tests/behat/add_forum.feature
index f3cfed05696..885a29335ca 100644
--- a/mod/forum/tests/behat/add_forum.feature
+++ b/mod/forum/tests/behat/add_forum.feature
@@ -44,3 +44,18 @@ Feature: Add forum activities and discussions
     And I should see "empty.txt"
     And I follow "Edit"
     And the field "Attachment" matches value "empty.txt"
+
+  @javascript
+  Scenario: Test forum settings validation
+    Given the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1        | 0        |
+    And I log in as "admin"
+    And I am on "Course 1" course homepage with editing mode on
+    And I add a "Forum" to section "0" and I fill the form with:
+      | Forum name | Test forum |
+      | Forum type | single     |
+      | Group mode | 1          |
+    When I press "Save and display"
+    Then I should see "Separate groups cannot be used with single discussion topic"
+    And I should see "Single discussion topic cannot be used with separate groups"