From 87799e9fba5738eb9a3f45875e25008b4cef679b Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Wed, 16 Nov 2016 14:10:19 +0800 Subject: [PATCH] MDL-56994 course: New method to check if format supports news forum --- course/format/lib.php | 20 ++++++++++ course/format/topics/lib.php | 9 +++++ course/format/upgrade.txt | 3 ++ course/format/weeks/lib.php | 9 +++++ course/tests/courseformat_test.php | 62 ++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) diff --git a/course/format/lib.php b/course/format/lib.php index 35d371db0bc..3bb98305c96 100644 --- a/course/format/lib.php +++ b/course/format/lib.php @@ -1139,6 +1139,26 @@ abstract class format_base { return $startdate + $courseduration; } + /** + * Indicates whether the course format supports the creation of the Announcements forum. + * + * For course format plugin developers, please override this to return true if you want the Announcements forum + * to be created upon course creation. + * + * @return bool + */ + public function supports_news() { + // For backwards compatibility, check if default blocks include the news_items block. + $defaultblocks = $this->get_default_blocks(); + foreach ($defaultblocks as $blocks) { + if (in_array('news_items', $blocks)) { + return true; + } + } + // Return false by default. + return false; + } + /** * Get the start date value from the course settings page form. * diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php index 5acb246d95a..f5e0488084c 100644 --- a/course/format/topics/lib.php +++ b/course/format/topics/lib.php @@ -401,6 +401,15 @@ class format_topics extends format_base { } return parent::inplace_editable_render_section_name($section, $linkifneeded, $editable, $edithint, $editlabel); } + + /** + * Indicates whether the course format supports the creation of a news forum. + * + * @return bool + */ + public function supports_news() { + return true; + } } /** diff --git a/course/format/upgrade.txt b/course/format/upgrade.txt index 8b80ae6bd89..ccc38b6b440 100644 --- a/course/format/upgrade.txt +++ b/course/format/upgrade.txt @@ -7,6 +7,9 @@ Overview of this plugin type at http://docs.moodle.org/dev/Course_formats * Course formats can overwrite get_default_course_enddate function to set the default course end date for new courses. format_base::get_default_course_enddate uses the new "Course duration" site setting to calculate the default course end date from the default course start date. +* New method format_base::supports_news() which is used to determine whether an Announcements forum will be automatically created on + course creation. For course format plugin developers, please override format_base::supports_news() to return true if you want the + Announcements forum to be created upon course creation and remove the block names defined in format_base::get_default_blocks(). === 3.1 === * Course format may use the inplace_editable template to allow quick editing of section names, see diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index e9237c60f40..af0fba40424 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -488,6 +488,15 @@ class format_weeks extends format_base { $dates = $this->get_section_dates(intval($numsections), $startdate); return $dates->end; } + + /** + * Indicates whether the course format supports the creation of a news forum. + * + * @return bool + */ + public function supports_news() { + return true; + } } /** diff --git a/course/tests/courseformat_test.php b/course/tests/courseformat_test.php index c7529cb9c24..47d7dabdef8 100644 --- a/course/tests/courseformat_test.php +++ b/course/tests/courseformat_test.php @@ -101,4 +101,66 @@ class core_course_courseformat_testcase extends advanced_testcase { $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->available); $this->assertTrue($modinfoteacher->get_cm($assign1->cmid)->uservisible); } + + /** + * Test for supports_news() with a course format plugin that doesn't define 'news_items' in default blocks. + */ + public function test_supports_news() { + $this->resetAfterTest(); + $format = course_get_format((object)['format' => 'testformat']); + $this->assertFalse($format->supports_news()); + } + + /** + * Test for supports_news() for old course format plugins that defines 'news_items' in default blocks. + */ + public function test_supports_news_legacy() { + $this->resetAfterTest(); + $format = course_get_format((object)['format' => 'testlegacy']); + $this->assertTrue($format->supports_news()); + } +} + +/** + * Class format_testformat. + * + * A test class that simulates a course format that doesn't define 'news_items' in default blocks. + * + * @copyright 2016 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class format_testformat extends format_base { + /** + * Returns the list of blocks to be automatically added for the newly created course. + * + * @return array + */ + public function get_default_blocks() { + return [ + BLOCK_POS_RIGHT => [], + BLOCK_POS_LEFT => [] + ]; + } +} + +/** + * Class format_testlegacy. + * + * A test class that simulates old course formats that define 'news_items' in default blocks. + * + * @copyright 2016 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class format_testlegacy extends format_base { + /** + * Returns the list of blocks to be automatically added for the newly created course. + * + * @return array + */ + public function get_default_blocks() { + return [ + BLOCK_POS_RIGHT => ['news_items'], + BLOCK_POS_LEFT => [] + ]; + } }