MDL-65082 format_social: Update social format to use new API

This commit is contained in:
Andrew Nicols
2019-03-22 15:01:48 +08:00
parent c661a129d1
commit 1bfaeeb2eb
4 changed files with 209 additions and 156 deletions
+67 -31
View File
@@ -1,42 +1,78 @@
<?php
// format.php - course format featuring social forum
// included from view.php
// 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 <http://www.gnu.org/licenses/>.
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 .= '<div class="editinglink"><a title="'.$streditsummary.'" '.
' href="modedit.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
$OUTPUT->pix_icon('t/edit', $streditsummary) . '</a></div>';
}
echo $OUTPUT->box($introcontent, 'generalbox', 'intro');
}
$coursemodule = get_coursemodule_from_instance('forum', $forum->id);
$modcontext = context_module::instance($coursemodule->id);
echo '<div class="subscribelink">', forum_get_subscribe_link($forum, $modcontext), '</div>';
$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);
@@ -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"
+13 -1
View File
@@ -469,10 +469,22 @@ class 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 single type discussion list renderer.
*
+10 -3
View File
@@ -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)) {