MDL-78940 course: create social forum using event observers

This commit also convert manual steps to create discussions on the
Social forum to use data generators.
This commit is contained in:
Simey Lameze 2023-08-02 10:05:25 +08:00
parent 513f3b02c7
commit af453412e2
4 changed files with 97 additions and 133 deletions

View File

@ -14,97 +14,18 @@ Feature: Change number of discussions displayed
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I turn editing mode on
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 10 |
| Message | This is forum post ten |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 9 |
| Message | This is forum post nine |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 8 |
| Message | This is forum post eight |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 7 |
| Message | This is forum post seven |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 6 |
| Message | This is forum post six |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 5 |
| Message | This is forum post five |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 4 |
| Message | This is forum post four |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 3 |
| Message | This is forum post three |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 2 |
| Message | This is forum post two |
And I press "Post to forum"
And I wait to be redirected
And I am on "Course 1" course homepage
And I wait "1" seconds
And I click on "Social forum" "link"
And I follow "Add discussion topic"
And I set the following fields to these values:
| Subject | Forum Post 1 |
| Message | This is forum post one |
And I press "Post to forum"
And I wait to be redirected
And the following "mod_forum > discussions" exist:
| user | forum | subject | message | created | timemodified |
| teacher1 | Social forum | Forum Post 10 | This is forum post ten | ##now +1 second## | ##now +1 second## |
| teacher1 | Social forum | Forum Post 9 | This is forum post nine | ##now +2 seconds## | ##now +2 seconds## |
| teacher1 | Social forum | Forum Post 8 | This is forum post eight | ##now +3 seconds## | ##now +3 seconds## |
| teacher1 | Social forum | Forum Post 7 | This is forum post seven | ##now +4 seconds## | ##now +4 seconds## |
| teacher1 | Social forum | Forum Post 6 | This is forum post six | ##now +5 seconds## | ##now +5 seconds## |
| teacher1 | Social forum | Forum Post 5 | This is forum post five | ##now +6 seconds## | ##now +6 seconds## |
| teacher1 | Social forum | Forum Post 4 | This is forum post four | ##now +7 seconds## | ##now +7 seconds## |
| teacher1 | Social forum | Forum Post 3 | This is forum post three | ##now +8 seconds## | ##now +8 seconds## |
| teacher1 | Social forum | Forum Post 2 | This is forum post two | ##now +9 seconds## | ##now +9 seconds## |
| teacher1 | Social forum | Forum Post 1 | This is forum post one | ##now +10 seconds## | ##now +10 seconds## |
And I am on the "C1" "course editing" page logged in as teacher1
Scenario: When number of discussions is decreased fewer discussions appear

View File

@ -108,14 +108,29 @@ class get_state_test extends \externallib_advanced_testcase {
$this->setUser($user);
}
// Add some activities to the course.
$this->create_activity($course->id, 'page', 1, true, $canedit);
$this->create_activity($course->id, 'forum', 1, true, $canedit);
$this->create_activity($course->id, 'book', 1, false, $canedit);
$this->create_activity($course->id, 'assign', 2, false, $canedit);
$this->create_activity($course->id, 'glossary', 4, true, $canedit);
$this->create_activity($course->id, 'label', 5, false, $canedit);
$this->create_activity($course->id, 'feedback', 5, true, $canedit);
// Social course format automatically creates a forum activity.
if (course_get_format($course)->get_format() === 'social') {
$cms = get_fast_modinfo($course)->get_cms();
// Let's add this assertion just to ensure course format has only one activity.
$this->assertCount(1, $cms);
$activitycm = reset($cms);
// And that activity is a forum.
$this->assertEquals('forum', $activitycm->modname);
// Assign the activity cm to the activities array.
$this->activities[$activitycm->id] = $activitycm;
} else {
// Add some activities to the course.
$this->create_activity($course->id, 'page', 1, true, $canedit);
$this->create_activity($course->id, 'forum', 1, true, $canedit);
$this->create_activity($course->id, 'book', 1, false, $canedit);
$this->create_activity($course->id, 'assign', 2, false, $canedit);
$this->create_activity($course->id, 'glossary', 4, true, $canedit);
$this->create_activity($course->id, 'label', 5, false, $canedit);
$this->create_activity($course->id, 'feedback', 5, true, $canedit);
}
if ($expectedexception) {
$this->expectException($expectedexception);

View File

@ -60,23 +60,27 @@ class state_test extends \advanced_testcase {
// Create and enrol user.
$this->setAdminUser();
// Add some activities to the course.
$this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
'visible' => 1]);
$this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
'visible' => 0]);
$this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
'visible' => 1]);
$this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
'visible' => 0]);
$this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
'visible' => 1]);
$courseformat = course_get_format($course->id);
$modinfo = $courseformat->get_modinfo();
$issocialformat = $courseformat->get_format() === 'social';
// Only create activities if the course format is not social.
// There's no course home page (and sections) for social course format.
if (!$issocialformat || $format == 'theunittest') {
// Add some activities to the course.
$this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
'visible' => 1]);
$this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
'visible' => 0]);
$this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
'visible' => 1]);
$this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
'visible' => 0]);
$this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
'visible' => 1]);
}
$courseclass = $courseformat->get_output_classname('state\\course');
$sectionclass = $courseformat->get_output_classname('state\\section');
@ -106,23 +110,27 @@ class state_test extends \advanced_testcase {
foreach ($sections as $key => $section) {
$this->assertEquals($section->id, $result->course->sectionlist[$key]);
if (!$issocialformat || $format == 'theunittest') {
if (!empty($section->uservisible)) {
$sectionstate = new $sectionclass($courseformat, $section);
$result->section[$key] = $sectionstate->export_for_template($renderer);
$this->assertEquals($section->id, $result->section[$key]->id);
$this->assertEquals($section->section, $result->section[$key]->section);
$this->assertTrue($section->visible == $result->section[$key]->visible);
if (!empty($section->uservisible)) {
$sectionstate = new $sectionclass($courseformat, $section);
$result->section[$key] = $sectionstate->export_for_template($renderer);
$this->assertEquals($section->id, $result->section[$key]->id);
$this->assertEquals($section->section, $result->section[$key]->section);
$this->assertTrue($section->visible == $result->section[$key]->visible);
if ($key === 0 || $key === 3 || $key === 6) {
$this->assertEmpty($result->section[$key]->cmlist);
} else if ($key === 1) {
$this->assertEquals(2, count($result->section[$key]->cmlist));
} else if ($key === 2 || $key === 4) {
$this->assertEquals(1, count($result->section[$key]->cmlist));
} else if ($key === 5) {
$this->assertEquals(2, count($result->section[$key]->cmlist));
if ($key === 0 || $key === 3 || $key === 6) {
$this->assertEmpty($result->section[$key]->cmlist);
} else if ($key === 1) {
$this->assertEquals(2, count($result->section[$key]->cmlist));
} else if ($key === 2 || $key === 4) {
$this->assertEquals(1, count($result->section[$key]->cmlist));
} else if ($key === 5) {
$this->assertEquals(2, count($result->section[$key]->cmlist));
}
}
} else {
// Social course format doesn't have sections.
$this->assertEmpty($result->section);
}
}

View File

@ -125,10 +125,20 @@ class mod_forum_observer {
$course = $event->get_record_snapshot('course', $event->objectid);
$format = course_get_format($course);
$courseformat = $format->get_format();
$forumformat = '';
if ($format->supports_news() && !empty($course->newsitems)) {
$forumformat = 'news';
} else if ($courseformat === 'social') {
$forumformat = 'social';
}
if ($forumformat) {
require_once($CFG->dirroot . '/mod/forum/lib.php');
// Auto create the announcements forum.
forum_get_course_forum($event->objectid, 'news');
// Auto-create the course forum if necessary.
forum_get_course_forum($event->objectid, $forumformat);
}
}
@ -143,10 +153,20 @@ class mod_forum_observer {
$course = $event->get_record_snapshot('course', $event->objectid);
$format = course_get_format($course);
$courseformat = $format->get_format();
$forumformat = '';
if ($format->supports_news() && !empty($course->newsitems)) {
$forumformat = 'news';
} else if ($courseformat === 'social') {
$forumformat = 'social';
}
if ($forumformat) {
require_once($CFG->dirroot . '/mod/forum/lib.php');
// Auto create the announcements forum.
forum_get_course_forum($event->objectid, 'news');
// Auto-create the course forum if necessary.
forum_get_course_forum($event->objectid, $forumformat);
}
}
}