diff --git a/course/format/social/format.php b/course/format/social/format.php index 4fcd31398d4..bde97091021 100644 --- a/course/format/social/format.php +++ b/course/format/social/format.php @@ -1,42 +1,78 @@ . - require_once($CFG->dirroot.'/mod/forum/lib.php'); +/** + * Course format featuring social forum. + * + * @package format_social + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ - $strgroups = get_string('groups'); - $strgroupmy = get_string('groupmy'); - $editing = $PAGE->user_is_editing(); +defined('MOODLE_INTERNAL') || die(); - if ($forum = forum_get_course_forum($course->id, 'social')) { +$pageno = optional_param('p', 0, PARAM_INT); - $cm = get_coursemodule_from_instance('forum', $forum->id); - $modcontext = context_module::instance($cm->id); +require_once($CFG->dirroot.'/mod/forum/lib.php'); - /// Print forum intro above posts MDL-18483 - if (trim($forum->intro) != '') { - $options = new stdClass(); - $options->para = false; - $introcontent = format_module_intro('forum', $forum, $cm->id); +$forum = forum_get_course_forum($course->id, 'social'); +if (empty($forum)) { + echo $OUTPUT->notification('Could not find or create a social forum here'); +} - if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $modcontext)) { - $streditsummary = get_string('editsummary'); - $introcontent .= ''; - } - echo $OUTPUT->box($introcontent, 'generalbox', 'intro'); - } +$coursemodule = get_coursemodule_from_instance('forum', $forum->id); +$modcontext = context_module::instance($coursemodule->id); - echo ''; +$entityfactory = mod_forum\local\container::get_entity_factory(); +$forumentity = $entityfactory->get_forum_from_stdclass($forum, $modcontext, $coursemodule, $course); - $numdiscussions = course_get_format($course)->get_course()->numdiscussions; - if ($numdiscussions < 1) { - // Make sure that the value is at least one. - $numdiscussions = 1; - } - forum_print_latest_discussions($course, $forum, $numdiscussions, 'plain', '', false); +// Print forum intro above posts MDL-18483. +if (trim($forum->intro) != '') { + $options = (object) [ + 'para' => false, + ]; + $introcontent = format_module_intro('forum', $forum, $coursemodule->id); - } else { - echo $OUTPUT->notification('Could not find or create a social forum here'); + if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $modcontext)) { + $streditsummary = get_string('editsummary'); + $introcontent .= html_writer::start_div('editinglink'); + $introcontent .= html_writer::link( + new moodle_url('/modedit.php', [ + 'update' => $coursemodule->id, + 'sesskey' => sesskey(), + ]), + $OUTPUT->pix_icon('t/edit', $streditsummary), + [ + 'title' => $streditsummary, + ] + ); + $introcontent .= html_writer::end_div(); } + echo $OUTPUT->box($introcontent, 'generalbox', 'intro'); +} + +echo html_writer::div(forum_get_subscribe_link($forum, $modcontext), 'subscribelink'); + +$numdiscussions = course_get_format($course)->get_course()->numdiscussions; +if ($numdiscussions < 1) { + // Make sure that the value is at least one. + $numdiscussions = 1; +} + +$rendererfactory = mod_forum\local\container::get_renderer_factory(); +$discussionsrenderer = $rendererfactory->get_social_discussion_list_renderer($forumentity); +$cm = \cm_info::create($coursemodule); +echo $discussionsrenderer->render($USER, $cm, null, null, $pageno, $numdiscussions); diff --git a/course/format/social/tests/behat/social_adjust_discussion_count.feature b/course/format/social/tests/behat/social_adjust_discussion_count.feature index a0a709ed5ab..830ad4ecd2d 100644 --- a/course/format/social/tests/behat/social_adjust_discussion_count.feature +++ b/course/format/social/tests/behat/social_adjust_discussion_count.feature @@ -4,124 +4,122 @@ Feature: Change number of discussions displayed As a teacher I need to edit the course and change the number of sections displayed. -# Uncomment these tests when the forum_print_latest_discussions function is -# deprecated in MDL-65082. -# Background: -# Given the following "users" exist: -# | username | firstname | lastname | email | -# | teacher1 | Teacher | 1 | teacher1@example.com | -# And the following "courses" exist: -# | fullname | shortname | category | format | -# | Course 1 | C1 | 0 | social | -# 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 press "Add a new 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 I am on "Course 1" course homepage -# -# Scenario: When number of discussions is decreased fewer discussions appear -# Given I navigate to "Edit settings" in current page administration -# And I set the following fields to these values: -# | numdiscussions | 5 | -# When I press "Save and display" -# Then I should see "This is forum post one" -# And I should see "This is forum post five" -# And I should not see "This is forum post six" -# -# Scenario: When number of discussions is decreased to less than 1 only 1 discussion should appear -# Given I navigate to "Edit settings" in current page administration -# And I set the following fields to these values: -# | numdiscussions | -1 | -# When I press "Save and display" -# Then I should see "This is forum post one" -# And I should not see "This is forum post two" -# And I should not see "This is forum post ten" -# -# Scenario: When number of discussions is increased more discussions appear -# Given I navigate to "Edit settings" in current page administration -# And I set the following fields to these values: -# | numdiscussions | 9 | -# When I press "Save and display" -# Then I should see "This is forum post one" -# And I should see "This is forum post five" -# And I should see "This is forum post nine" -# And I should not see "This is forum post ten" + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | format | + | Course 1 | C1 | 0 | social | + 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 follow "Add a new 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 I am on "Course 1" course homepage + + Scenario: When number of discussions is decreased fewer discussions appear + Given I navigate to "Edit settings" in current page administration + And I set the following fields to these values: + | numdiscussions | 5 | + When I press "Save and display" + Then I should see "This is forum post one" + And I should see "This is forum post five" + And I should not see "This is forum post six" + + Scenario: When number of discussions is decreased to less than 1 only 1 discussion should appear + Given I navigate to "Edit settings" in current page administration + And I set the following fields to these values: + | numdiscussions | -1 | + When I press "Save and display" + Then I should see "This is forum post one" + And I should not see "This is forum post two" + And I should not see "This is forum post ten" + + Scenario: When number of discussions is increased more discussions appear + Given I navigate to "Edit settings" in current page administration + And I set the following fields to these values: + | numdiscussions | 9 | + When I press "Save and display" + Then I should see "This is forum post one" + And I should see "This is forum post five" + And I should see "This is forum post nine" + And I should not see "This is forum post ten" diff --git a/course/renderer.php b/course/renderer.php index b7a01bd07d6..6b883962289 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -2334,10 +2334,10 @@ class core_course_renderer extends plugin_renderer_base { /** * Output news for the frontpage (extract from site-wide news forum) * - * @param stdClass $newsforum record from db table 'forum' that represents the site news forum + * @param stdClass $forum record from db table 'forum' that represents the site news forum * @return string */ - protected function frontpage_news($newsforum) { + protected function frontpage_news($forum) { global $CFG, $SITE, $SESSION, $USER; require_once($CFG->dirroot .'/mod/forum/lib.php'); @@ -2346,23 +2346,27 @@ class core_course_renderer extends plugin_renderer_base { if (isloggedin()) { $SESSION->fromdiscussion = $CFG->wwwroot; $subtext = ''; - if (\mod_forum\subscriptions::is_subscribed($USER->id, $newsforum)) { - if (!\mod_forum\subscriptions::is_forcesubscribed($newsforum)) { + if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum)) { + if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) { $subtext = get_string('unsubscribe', 'forum'); } } else { $subtext = get_string('subscribe', 'forum'); } - $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey())); + $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id, 'sesskey' => sesskey())); $output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink')); } - ob_start(); - forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC'); - $output .= ob_get_contents(); - ob_end_clean(); + $coursemodule = get_coursemodule_from_instance('forum', $forum->id); + $context = context_module::instance($coursemodule->id); - return $output; + $entityfactory = mod_forum\local\container::get_entity_factory(); + $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE); + + $rendererfactory = mod_forum\local\container::get_renderer_factory(); + $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity); + $cm = \cm_info::create($coursemodule); + return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems); } /** diff --git a/mod/forum/classes/local/exporters/forum.php b/mod/forum/classes/local/exporters/forum.php index f7d246b750e..160b37deed7 100644 --- a/mod/forum/classes/local/exporters/forum.php +++ b/mod/forum/classes/local/exporters/forum.php @@ -82,6 +82,7 @@ class forum extends exporter { 'type' => [ 'create' => ['type' => PARAM_URL], 'markasread' => ['type' => PARAM_URL], + 'view' => ['type' => PARAM_URL], ], ], ]; @@ -115,6 +116,7 @@ class forum extends exporter { 'urls' => [ 'create' => $urlfactory->get_discussion_create_url($this->forum)->out(false), 'markasread' => $urlfactory->get_mark_all_discussions_as_read_url($this->forum)->out(false), + 'view' => $urlfactory->get_forum_view_url_from_forum($this->forum)->out(false), ], ]; } diff --git a/mod/forum/classes/local/factories/renderer.php b/mod/forum/classes/local/factories/renderer.php index e21f9a02f06..2d450a0ed0f 100644 --- a/mod/forum/classes/local/factories/renderer.php +++ b/mod/forum/classes/local/factories/renderer.php @@ -366,6 +366,21 @@ class renderer { $rendererbase = $this->rendererbase; $notifications = []; + switch ($forum->get_type()) { + case 'news': + if (SITEID == $forum->get_course_id()) { + $template = 'mod_forum/frontpage_news_discussion_list'; + } else { + $template = 'mod_forum/news_discussion_list'; + } + break; + case 'qanda': + $template = 'mod_forum/qanda_discussion_list'; + break; + default: + $template = 'mod_forum/discussion_list'; + } + return new discussion_list_renderer( $forum, $rendererbase, @@ -375,6 +390,7 @@ class renderer { $this->builderfactory, $capabilitymanager, $this->urlfactory, + $template, $notifications, function($discussions, $user, $forum) { @@ -389,13 +405,15 @@ class renderer { } /** - * Create a blog type discussion list renderer. + * Create a discussion list renderer which shows more information about the first post. * * @param forum_entity $forum The forum that the discussions belong to + * @param string $template The template to use * @return discussion_list_renderer */ - public function get_blog_discussion_list_renderer( - forum_entity $forum + private function get_detailed_discussion_list_renderer( + forum_entity $forum, + string $template ) : discussion_list_renderer { $capabilitymanager = $this->managerfactory->get_capability_manager($forum); @@ -411,6 +429,7 @@ class renderer { $this->builderfactory, $capabilitymanager, $this->urlfactory, + $template, $notifications, function($discussions, $user, $forum) { $exportedpostsbuilder = $this->builderfactory->get_exported_posts_builder(); @@ -445,6 +464,42 @@ class renderer { ); } + /** + * Create a blog type discussion list renderer. + * + * @param forum_entity $forum The forum that the discussions belong to + * @return discussion_list_renderer + */ + public function get_blog_discussion_list_renderer( + forum_entity $forum + ) : discussion_list_renderer { + return $this->get_detailed_discussion_list_renderer($forum, 'mod_forum/blog_discussion_list'); + } + + /** + * Create a discussion list renderer for the social course format. + * + * @param forum_entity $forum The forum that the discussions belong to + * @return discussion_list_renderer + */ + public function get_social_discussion_list_renderer( + forum_entity $forum + ) : discussion_list_renderer { + return $this->get_detailed_discussion_list_renderer($forum, 'mod_forum/social_discussion_list'); + } + + /** + * Create a discussion list renderer for the social course format. + * + * @param forum_entity $forum The forum that the discussions belong to + * @return discussion_list_renderer + */ + public function get_frontpage_news_discussion_list_renderer( + forum_entity $forum + ) : discussion_list_renderer { + return $this->get_detailed_discussion_list_renderer($forum, 'mod_forum/frontpage_social_discussion_list'); + } + /** * Create a single type discussion list renderer. * diff --git a/mod/forum/classes/local/renderers/discussion_list.php b/mod/forum/classes/local/renderers/discussion_list.php index 319c452ed76..7b7f0e02bae 100644 --- a/mod/forum/classes/local/renderers/discussion_list.php +++ b/mod/forum/classes/local/renderers/discussion_list.php @@ -81,6 +81,9 @@ class discussion_list { /** @var callable $postprocessfortemplate Function to process exported posts before template rendering */ private $postprocessfortemplate; + /** @var string $template The template to use when displaying */ + private $template; + /** * Constructor for a new discussion list renderer. * @@ -92,6 +95,7 @@ class discussion_list { * @param builder_factory $builderfactory The factory used to fetch the builder instances * @param capability_manager $capabilitymanager The managed used to check capabilities on the forum * @param url_factory $urlfactory The factory used to create URLs in the forum + * @param string $template * @param notification[] $notifications A list of any notifications to be displayed within the page * @param callable|null $postprocessfortemplate Callback function to process discussion lists for templates */ @@ -104,6 +108,7 @@ class discussion_list { builder_factory $builderfactory, capability_manager $capabilitymanager, url_factory $urlfactory, + string $template, array $notifications = [], callable $postprocessfortemplate = null ) { @@ -114,9 +119,11 @@ class discussion_list { $this->vaultfactory = $vaultfactory; $this->builderfactory = $builderfactory; $this->capabilitymanager = $capabilitymanager; + $this->urlfactory = $urlfactory; $this->notifications = $notifications; $this->postprocessfortemplate = $postprocessfortemplate; + $this->template = $template; $forumdatamapper = $this->legacydatamapperfactory->get_forum_data_mapper(); $this->forumrecord = $forumdatamapper->to_legacy_object($forum); @@ -161,11 +168,12 @@ class discussion_list { $this->urlfactory->get_forum_view_url_from_forum($forum), true ), - 'notifications' => $this->get_notifications($user, $groupid) + 'hasmore' => ($alldiscussionscount > $pagesize), + 'notifications' => $this->get_notifications($user, $groupid), ]; if (!$discussions) { - return $this->renderer->render_from_template($this->get_template(), $forumview); + return $this->renderer->render_from_template($this->template, $forumview); } if ($this->postprocessfortemplate !== null) { @@ -181,7 +189,7 @@ class discussion_list { $exportedposts ); - return $this->renderer->render_from_template($this->get_template(), $forumview); + return $this->renderer->render_from_template($this->template, $forumview); } /** @@ -306,27 +314,6 @@ class discussion_list { return $pageno; } - /** - * Fetch the name of the template to use for the current forum and view modes. - * - * @return string - */ - private function get_template() : string { - switch ($this->forum->get_type()) { - case 'news': - return 'mod_forum/news_discussion_list'; - break; - case 'blog': - return 'mod_forum/blog_discussion_list'; - break; - case 'qanda': - return 'mod_forum/qanda_discussion_list'; - break; - default: - return 'mod_forum/discussion_list'; - } - } - /** * Get the list of notification for display. * diff --git a/mod/forum/deprecatedlib.php b/mod/forum/deprecatedlib.php index f4395149626..8289cbf8c56 100644 --- a/mod/forum/deprecatedlib.php +++ b/mod/forum/deprecatedlib.php @@ -1312,3 +1312,311 @@ function forum_print_posts_nested($course, &$cm, $forum, $discussion, $parent, $ } } } + +/** + * Prints the discussion view screen for a forum. + * + * @param object $course The current course object. + * @param object $forum Forum to be printed. + * @param int $maxdiscussions + * @param string $displayformat The display format to use (optional). + * @param string $sort Sort arguments for database query (optional). + * @param int $currentgroup + * @param int $groupmode Group mode of the forum (optional). + * @param int $page Page mode, page to display (optional). + * @param int $perpage The maximum number of discussions per page(optional) + * @param stdClass $cm + * @deprecated since Moodle 3.7 + */ +function forum_print_latest_discussions($course, $forum, $maxdiscussions = -1, $displayformat = 'plain', $sort = '', + $currentgroup = -1, $groupmode = -1, $page = -1, $perpage = 100, $cm = null) { + debugging('forum_print_latest_discussions has been deprecated.', DEBUG_DEVELOPER); + global $CFG, $USER, $OUTPUT; + + require_once($CFG->dirroot . '/course/lib.php'); + + if (!$cm) { + if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) { + print_error('invalidcoursemodule'); + } + } + $context = context_module::instance($cm->id); + + if (empty($sort)) { + $sort = forum_get_default_sort_order(); + } + + $olddiscussionlink = false; + + // Sort out some defaults. + if ($perpage <= 0) { + $perpage = 0; + $page = -1; + } + + if ($maxdiscussions == 0) { + // All discussions - backwards compatibility. + $page = -1; + $perpage = 0; + if ($displayformat == 'plain') { + $displayformat = 'header'; // Abbreviate display by default. + } + + } else if ($maxdiscussions > 0) { + $page = -1; + $perpage = $maxdiscussions; + } + + $fullpost = false; + if ($displayformat == 'plain') { + $fullpost = true; + } + + // Decide if current user is allowed to see ALL the current discussions or not. + // First check the group stuff. + if ($currentgroup == -1 or $groupmode == -1) { + $groupmode = groups_get_activity_groupmode($cm, $course); + $currentgroup = groups_get_activity_group($cm); + } + + // Cache. + $groups = array(); + + // If the user can post discussions, then this is a good place to put the + // button for it. We do not show the button if we are showing site news + // and the current user is a guest. + + $canstart = forum_user_can_post_discussion($forum, $currentgroup, $groupmode, $cm, $context); + if (!$canstart and $forum->type !== 'news') { + if (isguestuser() or !isloggedin()) { + $canstart = true; + } + if (!is_enrolled($context) and !is_viewing($context)) { + // Allow guests and not-logged-in to see the button - they are prompted to log in after clicking the link + // normal users with temporary guest access see this button too, they are asked to enrol instead + // do not show the button to users with suspended enrolments here. + $canstart = enrol_selfenrol_available($course->id); + } + } + + if ($canstart) { + switch ($forum->type) { + case 'news': + case 'blog': + $buttonadd = get_string('addanewtopic', 'forum'); + break; + case 'qanda': + $buttonadd = get_string('addanewquestion', 'forum'); + break; + default: + $buttonadd = get_string('addanewdiscussion', 'forum'); + break; + } + $button = new single_button(new moodle_url('/mod/forum/post.php', ['forum' => $forum->id]), $buttonadd, 'get'); + $button->class = 'singlebutton forumaddnew'; + $button->formid = 'newdiscussionform'; + echo $OUTPUT->render($button); + + } else if (isguestuser() or !isloggedin() or $forum->type == 'news' or + $forum->type == 'qanda' and !has_capability('mod/forum:addquestion', $context) or + $forum->type != 'qanda' and !has_capability('mod/forum:startdiscussion', $context)) { + // No button and no info. + $ignore = true; + } else if ($groupmode and !has_capability('moodle/site:accessallgroups', $context)) { + // Inform users why they can not post new discussion. + if (!$currentgroup) { + if (!has_capability('mod/forum:canposttomygroups', $context)) { + echo $OUTPUT->notification(get_string('cannotadddiscussiongroup', 'forum')); + } else { + echo $OUTPUT->notification(get_string('cannotadddiscussionall', 'forum')); + } + } else if (!groups_is_member($currentgroup)) { + echo $OUTPUT->notification(get_string('cannotadddiscussion', 'forum')); + } + } + + // Get all the recent discussions we're allowed to see. + + $getuserlastmodified = ($displayformat == 'header'); + + $discussions = forum_get_discussions($cm, $sort, $fullpost, null, $maxdiscussions, $getuserlastmodified, $page, $perpage); + if (!$$discussions) { + echo '
'; + if ($forum->type == 'news') { + echo '('.get_string('nonews', 'forum').')'; + } else if ($forum->type == 'qanda') { + echo '('.get_string('noquestions', 'forum').')'; + } else { + echo '('.get_string('nodiscussions', 'forum').')'; + } + echo "
\n"; + return; + } + + $canseeprivatereplies = has_capability('mod/forum:readprivatereplies', $context); + // If we want paging. + if ($page != -1) { + // Get the number of discussions found. + $numdiscussions = forum_get_discussions_count($cm); + + // Show the paging bar. + echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f=$forum->id"); + if ($numdiscussions > 1000) { + // Saves some memory on sites with very large forums. + $replies = forum_count_discussion_replies($forum->id, $sort, $maxdiscussions, $page, $perpage, $canseeprivatereplies); + } else { + $replies = forum_count_discussion_replies($forum->id, "", -1, -1, 0, $canseeprivatereplies); + } + + } else { + $replies = forum_count_discussion_replies($forum->id, "", -1, -1, 0, $canseeprivatereplies); + + if ($maxdiscussions > 0 and $maxdiscussions <= count($discussions)) { + $olddiscussionlink = true; + } + } + + $canviewparticipants = course_can_view_participants($context); + $canviewhiddentimedposts = has_capability('mod/forum:viewhiddentimedposts', $context); + + $strdatestring = get_string('strftimerecentfull'); + + // Check if the forum is tracked. + if ($cantrack = forum_tp_can_track_forums($forum)) { + $forumtracked = forum_tp_is_tracked($forum); + } else { + $forumtracked = false; + } + + if ($forumtracked) { + $unreads = forum_get_discussions_unread($cm); + } else { + $unreads = array(); + } + + if ($displayformat == 'header') { + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + if ($groupmode > 0) { + echo ''; + } + if (has_capability('mod/forum:viewdiscussion', $context)) { + echo ''; + // If the forum can be tracked, display the unread column. + if ($cantrack) { + echo ''; + } + } + echo ''; + if ((!is_guest($context, $USER) && isloggedin()) && has_capability('mod/forum:viewdiscussion', $context)) { + if (\mod_forum\subscriptions::is_subscribable($forum)) { + echo ''; + } + } + echo ''; + echo ''; + echo ''; + } + + foreach ($discussions as $discussion) { + if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $context) && + !forum_user_has_posted($forum->id, $discussion->discussion, $USER->id)) { + $canviewparticipants = false; + } + + if (!empty($replies[$discussion->discussion])) { + $discussion->replies = $replies[$discussion->discussion]->replies; + $discussion->lastpostid = $replies[$discussion->discussion]->lastpostid; + } else { + $discussion->replies = 0; + } + + // SPECIAL CASE: The front page can display a news item post to non-logged in users. + // All posts are read in this case. + if (!$forumtracked) { + $discussion->unread = '-'; + } else if (empty($USER)) { + $discussion->unread = 0; + } else { + if (empty($unreads[$discussion->discussion])) { + $discussion->unread = 0; + } else { + $discussion->unread = $unreads[$discussion->discussion]; + } + } + + if (isloggedin()) { + $ownpost = ($discussion->userid == $USER->id); + } else { + $ownpost = false; + } + // Use discussion name instead of subject of first post. + $discussion->subject = $discussion->name; + + switch ($displayformat) { + case 'header': + if ($groupmode > 0) { + if (isset($groups[$discussion->groupid])) { + $group = $groups[$discussion->groupid]; + } else { + $group = $groups[$discussion->groupid] = groups_get_group($discussion->groupid); + } + } else { + $group = -1; + } + forum_print_discussion_header($discussion, $forum, $group, $strdatestring, $cantrack, $forumtracked, + $canviewparticipants, $context, $canviewhiddentimedposts); + break; + default: + $link = false; + + if ($discussion->replies) { + $link = true; + } else { + $modcontext = context_module::instance($cm->id); + $link = forum_user_can_see_discussion($forum, $discussion, $modcontext, $USER); + } + + $discussion->forum = $forum->id; + + forum_print_post_start($discussion); + forum_print_post($discussion, $discussion, $forum, $cm, $course, $ownpost, 0, $link, false, + '', null, true, $forumtracked); + forum_print_post_end($discussion); + break; + } + } + + if ($displayformat == "header") { + echo ''; + echo '
'.get_string('discussion', 'forum').''.get_string('startedby', 'forum').''.get_string('group').''.get_string('replies', 'forum').''.get_string('unread', 'forum'); + if ($forumtracked) { + echo ''. + $OUTPUT->pix_icon('t/markasread', get_string('markallread', 'forum')) . ''; + } + echo ''.get_string('lastpost', 'forum').''; + echo forum_get_discussion_subscription_icon_preloaders(); + echo '
'; + } + + if ($olddiscussionlink) { + if ($forum->type == 'news') { + $strolder = get_string('oldertopics', 'forum'); + } else { + $strolder = get_string('olderdiscussions', 'forum'); + } + echo '
'; + echo ''; + echo $strolder.' ...
'; + } + + if ($page != -1) { + // Show the paging bar. + echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f=$forum->id"); + } +} diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 9281187dcec..a21301d98a0 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4022,314 +4022,6 @@ function forum_user_can_see_post($forum, $discussion, $post, $user = null, $cm = return true; } - -/** - * Prints the discussion view screen for a forum. - * - * @global object - * @global object - * @param object $course The current course object. - * @param object $forum Forum to be printed. - * @param int $maxdiscussions . - * @param string $displayformat The display format to use (optional). - * @param string $sort Sort arguments for database query (optional). - * @param int $groupmode Group mode of the forum (optional). - * @param void $unused (originally current group) - * @param int $page Page mode, page to display (optional). - * @param int $perpage The maximum number of discussions per page(optional) - * @param boolean $subscriptionstatus Whether the user is currently subscribed to the discussion in some fashion. - * - */ -function forum_print_latest_discussions($course, $forum, $maxdiscussions = -1, $displayformat = 'plain', $sort = '', - $currentgroup = -1, $groupmode = -1, $page = -1, $perpage = 100, $cm = null) { - global $CFG, $USER, $OUTPUT; - - require_once($CFG->dirroot . '/course/lib.php'); - - if (!$cm) { - if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) { - print_error('invalidcoursemodule'); - } - } - $context = context_module::instance($cm->id); - - if (empty($sort)) { - $sort = forum_get_default_sort_order(); - } - - $olddiscussionlink = false; - - // Sort out some defaults - if ($perpage <= 0) { - $perpage = 0; - $page = -1; - } - - if ($maxdiscussions == 0) { - // all discussions - backwards compatibility - $page = -1; - $perpage = 0; - if ($displayformat == 'plain') { - $displayformat = 'header'; // Abbreviate display by default - } - - } else if ($maxdiscussions > 0) { - $page = -1; - $perpage = $maxdiscussions; - } - - $fullpost = false; - if ($displayformat == 'plain') { - $fullpost = true; - } - - -// Decide if current user is allowed to see ALL the current discussions or not - -// First check the group stuff - if ($currentgroup == -1 or $groupmode == -1) { - $groupmode = groups_get_activity_groupmode($cm, $course); - $currentgroup = groups_get_activity_group($cm); - } - - $groups = array(); //cache - -// If the user can post discussions, then this is a good place to put the -// button for it. We do not show the button if we are showing site news -// and the current user is a guest. - - $canstart = forum_user_can_post_discussion($forum, $currentgroup, $groupmode, $cm, $context); - if (!$canstart and $forum->type !== 'news') { - if (isguestuser() or !isloggedin()) { - $canstart = true; - } - if (!is_enrolled($context) and !is_viewing($context)) { - // allow guests and not-logged-in to see the button - they are prompted to log in after clicking the link - // normal users with temporary guest access see this button too, they are asked to enrol instead - // do not show the button to users with suspended enrolments here - $canstart = enrol_selfenrol_available($course->id); - } - } - - if ($canstart) { - switch ($forum->type) { - case 'news': - case 'blog': - $buttonadd = get_string('addanewtopic', 'forum'); - break; - case 'qanda': - $buttonadd = get_string('addanewquestion', 'forum'); - break; - default: - $buttonadd = get_string('addanewdiscussion', 'forum'); - break; - } - $button = new single_button(new moodle_url('/mod/forum/post.php', ['forum' => $forum->id]), $buttonadd, 'get'); - $button->class = 'singlebutton forumaddnew'; - $button->formid = 'newdiscussionform'; - echo $OUTPUT->render($button); - - } else if (isguestuser() or !isloggedin() or $forum->type == 'news' or - $forum->type == 'qanda' and !has_capability('mod/forum:addquestion', $context) or - $forum->type != 'qanda' and !has_capability('mod/forum:startdiscussion', $context)) { - // no button and no info - - } else if ($groupmode and !has_capability('moodle/site:accessallgroups', $context)) { - // inform users why they can not post new discussion - if (!$currentgroup) { - if (!has_capability('mod/forum:canposttomygroups', $context)) { - echo $OUTPUT->notification(get_string('cannotadddiscussiongroup', 'forum')); - } else { - echo $OUTPUT->notification(get_string('cannotadddiscussionall', 'forum')); - } - } else if (!groups_is_member($currentgroup)) { - echo $OUTPUT->notification(get_string('cannotadddiscussion', 'forum')); - } - } - -// Get all the recent discussions we're allowed to see - - $getuserlastmodified = ($displayformat == 'header'); - - if (! $discussions = forum_get_discussions($cm, $sort, $fullpost, null, $maxdiscussions, $getuserlastmodified, $page, $perpage) ) { - echo '
'; - if ($forum->type == 'news') { - echo '('.get_string('nonews', 'forum').')'; - } else if ($forum->type == 'qanda') { - echo '('.get_string('noquestions','forum').')'; - } else { - echo '('.get_string('nodiscussions', 'forum').')'; - } - echo "
\n"; - return; - } - -// If we want paging - if ($page != -1) { - ///Get the number of discussions found - $numdiscussions = forum_get_discussions_count($cm); - - ///Show the paging bar - echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f=$forum->id"); - if ($numdiscussions > 1000) { - // saves some memory on sites with very large forums - $replies = forum_count_discussion_replies($forum->id, $sort, $maxdiscussions, $page, $perpage); - } else { - $replies = forum_count_discussion_replies($forum->id); - } - - } else { - $replies = forum_count_discussion_replies($forum->id); - - if ($maxdiscussions > 0 and $maxdiscussions <= count($discussions)) { - $olddiscussionlink = true; - } - } - - $canviewparticipants = course_can_view_participants($context); - $canviewhiddentimedposts = has_capability('mod/forum:viewhiddentimedposts', $context); - - $strdatestring = get_string('strftimerecentfull'); - - // Check if the forum is tracked. - if ($cantrack = forum_tp_can_track_forums($forum)) { - $forumtracked = forum_tp_is_tracked($forum); - } else { - $forumtracked = false; - } - - if ($forumtracked) { - $unreads = forum_get_discussions_unread($cm); - } else { - $unreads = array(); - } - - if ($displayformat == 'header') { - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - if ($groupmode > 0) { - echo ''; - } - if (has_capability('mod/forum:viewdiscussion', $context)) { - echo ''; - // If the forum can be tracked, display the unread column. - if ($cantrack) { - echo ''; - } - } - echo ''; - if ((!is_guest($context, $USER) && isloggedin()) && has_capability('mod/forum:viewdiscussion', $context)) { - if (\mod_forum\subscriptions::is_subscribable($forum)) { - echo ''; - } - } - echo ''; - echo ''; - echo ''; - } - - foreach ($discussions as $discussion) { - if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $context) && - !forum_user_has_posted($forum->id, $discussion->discussion, $USER->id)) { - $canviewparticipants = false; - } - - if (!empty($replies[$discussion->discussion])) { - $discussion->replies = $replies[$discussion->discussion]->replies; - $discussion->lastpostid = $replies[$discussion->discussion]->lastpostid; - } else { - $discussion->replies = 0; - } - - // SPECIAL CASE: The front page can display a news item post to non-logged in users. - // All posts are read in this case. - if (!$forumtracked) { - $discussion->unread = '-'; - } else if (empty($USER)) { - $discussion->unread = 0; - } else { - if (empty($unreads[$discussion->discussion])) { - $discussion->unread = 0; - } else { - $discussion->unread = $unreads[$discussion->discussion]; - } - } - - if (isloggedin()) { - $ownpost = ($discussion->userid == $USER->id); - } else { - $ownpost=false; - } - // Use discussion name instead of subject of first post. - $discussion->subject = $discussion->name; - - switch ($displayformat) { - case 'header': - if ($groupmode > 0) { - if (isset($groups[$discussion->groupid])) { - $group = $groups[$discussion->groupid]; - } else { - $group = $groups[$discussion->groupid] = groups_get_group($discussion->groupid); - } - } else { - $group = -1; - } - forum_print_discussion_header($discussion, $forum, $group, $strdatestring, $cantrack, $forumtracked, - $canviewparticipants, $context, $canviewhiddentimedposts); - break; - default: - $link = false; - - if ($discussion->replies) { - $link = true; - } else { - $modcontext = context_module::instance($cm->id); - $link = forum_user_can_see_discussion($forum, $discussion, $modcontext, $USER); - } - - $discussion->forum = $forum->id; - - forum_print_post_start($discussion); - forum_print_post($discussion, $discussion, $forum, $cm, $course, $ownpost, 0, $link, false, - '', null, true, $forumtracked); - forum_print_post_end($discussion); - break; - } - } - - if ($displayformat == "header") { - echo ''; - echo '
'.get_string('discussion', 'forum').''.get_string('startedby', 'forum').''.get_string('group').''.get_string('replies', 'forum').''.get_string('unread', 'forum'); - if ($forumtracked) { - echo ''. - $OUTPUT->pix_icon('t/markasread', get_string('markallread', 'forum')) . ''; - } - echo ''.get_string('lastpost', 'forum').''; - echo forum_get_discussion_subscription_icon_preloaders(); - echo '
'; - } - - if ($olddiscussionlink) { - if ($forum->type == 'news') { - $strolder = get_string('oldertopics', 'forum'); - } else { - $strolder = get_string('olderdiscussions', 'forum'); - } - echo '
'; - echo ''; - echo $strolder.' ...
'; - } - - if ($page != -1) { ///Show the paging bar - echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f=$forum->id"); - } -} - /** * Returns all forum posts since a given time in specified forum. * diff --git a/mod/forum/templates/blog_discussion_list.mustache b/mod/forum/templates/blog_discussion_list.mustache index f5ba2c920ed..abe8313d9a4 100644 --- a/mod/forum/templates/blog_discussion_list.mustache +++ b/mod/forum/templates/blog_discussion_list.mustache @@ -1,4 +1,3 @@ - {{! This file is part of Moodle - http://moodle.org/ @@ -30,20 +29,8 @@ { } }} -{{< mod_forum/discussion_list }} +{{< mod_forum/social_discussion_list }} {{$discussion_create_text}} {{#str}}addanewtopic, forum{{/str}} {{/discussion_create_text}} - {{$discussion_list_output}} - {{#posts}} - {{< mod_forum/forum_discussion_post }} - {{$footer}} - - {{/footer}} - {{$replyoutput}}{{/replyoutput}} - {{/ mod_forum/forum_discussion_post }} - {{/posts}} - {{/discussion_list_output}} -{{/ mod_forum/discussion_list }} +{{/ mod_forum/social_discussion_list }} diff --git a/mod/forum/templates/discussion_list.mustache b/mod/forum/templates/discussion_list.mustache index 1b72546604c..bbc91625379 100644 --- a/mod/forum/templates/discussion_list.mustache +++ b/mod/forum/templates/discussion_list.mustache @@ -50,7 +50,9 @@ {{/forum.capabilities.create}} {{#state.hasdiscussions}} - {{{ pagination }}} + {{$discussion_top_pagination}} + {{{ pagination }}} + {{/discussion_top_pagination}} {{$discussion_list_output}} {{$discussion_list_header}} @@ -217,9 +219,9 @@ {{/discussion_list_body}}
{{/discussion_list_output}} - {{$pagination_output}} + {{$discussion_bottom_pagination}} {{{ pagination }}} - {{/pagination_output}} + {{/discussion_bottom_pagination}} {{#can_create_discussion}}
{{create_discussion_link_text}} diff --git a/mod/forum/templates/frontpage_news_discussion_list.mustache b/mod/forum/templates/frontpage_news_discussion_list.mustache new file mode 100644 index 00000000000..ed1d27b1748 --- /dev/null +++ b/mod/forum/templates/frontpage_news_discussion_list.mustache @@ -0,0 +1,36 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template mod_forum/social_discussion_list + + Template which defines a forum post for sending in a single-post HTML email. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Example context (json): + { + } +}} +{{< mod_forum/news_discussion_list }} + {{$discussion_create_text}} + {{#str}}addanewtopic, forum{{/str}} + {{/discussion_create_text}} +{{/ mod_forum/news_discussion_list }} diff --git a/mod/forum/templates/frontpage_social_discussion_list.mustache b/mod/forum/templates/frontpage_social_discussion_list.mustache new file mode 100644 index 00000000000..5613ee6ce2b --- /dev/null +++ b/mod/forum/templates/frontpage_social_discussion_list.mustache @@ -0,0 +1,42 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template mod_forum/frontpage_social_discussion_list + + Template which defines a forum post for sending in a single-post HTML email. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Example context (json): + { + } +}} +{{< mod_forum/social_discussion_list }} + {{$discussion_create_text}} + {{#str}}addanewtopic, forum{{/str}} + {{/discussion_create_text}} + {{$discussion_top_pagination}}{{/discussion_top_pagination}} + {{$discussion_bottom_pagination}} + {{#hasmore}} + {{#str}}oldertopics, mod_forum{{/str}}... + {{/hasmore}} + {{/discussion_bottom_pagination}} +{{/ mod_forum/social_discussion_list }} diff --git a/mod/forum/templates/social_discussion_list.mustache b/mod/forum/templates/social_discussion_list.mustache new file mode 100644 index 00000000000..fc24f190689 --- /dev/null +++ b/mod/forum/templates/social_discussion_list.mustache @@ -0,0 +1,48 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template mod_forum/social_discussion_list + + Template which defines a forum post for sending in a single-post HTML email. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Example context (json): + { + } +}} +{{< mod_forum/discussion_list }} + {{$discussion_create_text}} + {{#str}}addanewdiscussion, forum{{/str}} + {{/discussion_create_text}} + {{$discussion_list_output}} + {{#posts}} + {{< mod_forum/forum_discussion_post }} + {{$footer}} + + {{/footer}} + {{$replyoutput}}{{/replyoutput}} + {{/ mod_forum/forum_discussion_post }} + {{/posts}} + {{/discussion_list_output}} +{{/ mod_forum/discussion_list }} diff --git a/mod/forum/upgrade.txt b/mod/forum/upgrade.txt index 3381c91e223..3f301ab2713 100644 --- a/mod/forum/upgrade.txt +++ b/mod/forum/upgrade.txt @@ -5,6 +5,7 @@ information provided here is intended especially for developers. * Changed the forum discussion rendering to use templates rather than print functions. * Added new forum entities, factories, exporters, renderers, and vaults in the local namespace to better encapsulate the forum data. * Deprecated all of the forum_print_* functions in lib.php. + * The forum_print_latest_discussions function has been deprecated and will not be replaced. === 3.6 === * forum_print_post should be surrounded with calls to forum_print_post_start and forum_print_post_end to create the proper HTML structure for the post. diff --git a/mod/forum/view.php b/mod/forum/view.php index 4de4e125e7f..7a9a38f9c67 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -45,9 +45,16 @@ if (!$cmid && !$forumid) { print_error('missingparameter'); } -$forum = $forumid ? $forumvault->get_from_id($forumid) : $forumvault->get_from_course_module_id($cmid); -if (!$forum) { - throw new \moodle_exception('Unable to find forum with id ' . $forumid); +if ($cmid) { + $forum = $forumvault->get_from_course_module_id($cmid); + if (empty($forum)) { + throw new \moodle_exception('Unable to find forum with cmid ' . $cmid); + } +} else { + $forum = $forumvault->get_from_id($forumid); + if (empty($forum)) { + throw new \moodle_exception('Unable to find forum with id ' . $forumid); + } } if (!empty($showall)) {