Skip to content

Commit

Permalink
MDL-66770 mod_forum: put nested v2 display mode behind user preference
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie committed Nov 1, 2019
1 parent 2602c7b commit 667e5fd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 18 deletions.
7 changes: 4 additions & 3 deletions mod/forum/classes/local/renderers/discussion.php
Expand Up @@ -208,7 +208,7 @@ public function render(
'html' => [
'hasanyactions' => $hasanyactions,
'posts' => $this->postsrenderer->render($user, [$this->forum], [$this->discussion], $posts),
'modeselectorform' => $this->get_display_mode_selector_html($displaymode),
'modeselectorform' => $this->get_display_mode_selector_html($displaymode, $user),
'subscribe' => null,
'movediscussion' => null,
'pindiscussion' => null,
Expand Down Expand Up @@ -274,14 +274,15 @@ private function get_exported_discussion(stdClass $user) : array {
* Get the HTML for the display mode selector.
*
* @param int $displaymode The current display mode
* @param stdClass $user The current user
* @return string
*/
private function get_display_mode_selector_html(int $displaymode) : string {
private function get_display_mode_selector_html(int $displaymode, stdClass $user) : string {
$baseurl = $this->baseurl;
$select = new single_select(
$baseurl,
'mode',
forum_get_layout_modes(),
forum_get_layout_modes(get_user_preferences('forum_useexperimentalui', false, $user)),
$displaymode,
null,
'mode'
Expand Down
21 changes: 18 additions & 3 deletions mod/forum/discuss.php
Expand Up @@ -241,11 +241,27 @@

unset($SESSION->fromdiscussion);

$saveddisplaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);

if ($mode) {
set_user_preference('forum_displaymode', $mode);
$displaymode = $mode;
} else {
$displaymode = $saveddisplaymode;
}

$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
if (get_user_preferences('forum_useexperimentalui', false)) {
if ($displaymode == FORUM_MODE_NESTED) {
$displaymode = FORUM_MODE_NESTED_V2;
}
} else {
if ($displaymode == FORUM_MODE_NESTED_V2) {
$displaymode = FORUM_MODE_NESTED;
}
}

if ($displaymode != $saveddisplaymode) {
set_user_preference('forum_displaymode', $displaymode);
}

if ($parent) {
// If flat AND parent, then force nested display this time
Expand Down Expand Up @@ -296,7 +312,6 @@
$PAGE->set_heading($course->fullname);
if ($isnestedv2displaymode) {
$PAGE->add_body_class('nested-v2-display-mode reset-style');
$PAGE->set_include_region_main_settings_in_header_actions(true);
$settingstrigger = $OUTPUT->render_from_template('mod_forum/settings_drawer_trigger', null);
$PAGE->add_header_action($settingstrigger);
} else {
Expand Down
1 change: 1 addition & 0 deletions mod/forum/lang/en/forum.php
Expand Up @@ -702,6 +702,7 @@
$string['unsubscribeallempty'] = 'You are not subscribed to any forums. To disable all notifications from this server go to Messaging in My Profile Settings.';
$string['unsubscribed'] = 'Unsubscribed';
$string['unsubscribeshort'] = 'Unsubscribe';
$string['useexperimentalui'] = 'Use experimental nested discussion view';
$string['usermarksread'] = 'Manual message read marking';
$string['unpindiscussion'] = 'Unpin this discussion';
$string['viewalldiscussions'] = 'View all discussions';
Expand Down
47 changes: 39 additions & 8 deletions mod/forum/lib.php
Expand Up @@ -2541,12 +2541,29 @@ function forum_get_discussion_subscription_icon_preloaders() {
*/
function forum_print_mode_form($id, $mode, $forumtype='') {
global $OUTPUT;
$useexperimentalui = get_user_preferences('forum_useexperimentalui', false);
if ($forumtype == 'single') {
$select = new single_select(new moodle_url("/mod/forum/view.php", array('f'=>$id)), 'mode', forum_get_layout_modes(), $mode, null, "mode");
$select = new single_select(
new moodle_url("/mod/forum/view.php",
array('f' => $id)),
'mode',
forum_get_layout_modes($useexperimentalui),
$mode,
null,
"mode"
);
$select->set_label(get_string('displaymode', 'forum'), array('class' => 'accesshide'));
$select->class = "forummode";
} else {
$select = new single_select(new moodle_url("/mod/forum/discuss.php", array('d'=>$id)), 'mode', forum_get_layout_modes(), $mode, null, "mode");
$select = new single_select(
new moodle_url("/mod/forum/discuss.php",
array('d' => $id)),
'mode',
forum_get_layout_modes($useexperimentalui),
$mode,
null,
"mode"
);
$select->set_label(get_string('displaymode', 'forum'), array('class' => 'accesshide'));
}
echo $OUTPUT->render($select);
Expand Down Expand Up @@ -5301,14 +5318,23 @@ function forum_reset_course_form_defaults($course) {
/**
* Returns array of forum layout modes
*
* @param bool $useexperimentalui use experimental layout modes or not
* @return array
*/
function forum_get_layout_modes() {
return array (FORUM_MODE_FLATOLDEST => get_string('modeflatoldestfirst', 'forum'),
FORUM_MODE_FLATNEWEST => get_string('modeflatnewestfirst', 'forum'),
FORUM_MODE_THREADED => get_string('modethreaded', 'forum'),
FORUM_MODE_NESTED => get_string('modenested', 'forum'),
FORUM_MODE_NESTED_V2 => get_string('modenestedv2', 'forum'));
function forum_get_layout_modes(bool $useexperimentalui = false) {
$modes = [
FORUM_MODE_FLATOLDEST => get_string('modeflatoldestfirst', 'forum'),
FORUM_MODE_FLATNEWEST => get_string('modeflatnewestfirst', 'forum'),
FORUM_MODE_THREADED => get_string('modethreaded', 'forum')
];

if ($useexperimentalui) {
$modes[FORUM_MODE_NESTED_V2] = get_string('modenestedv2', 'forum');
} else {
$modes[FORUM_MODE_NESTED] = get_string('modenested', 'forum');
}

return $modes;
}

/**
Expand Down Expand Up @@ -6842,6 +6868,11 @@ function mod_forum_user_preferences() {
$discussionlistvault::SORTORDER_REPLIES_ASC
)
);
$preferences['forum_useexperimentalui'] = [
'null' => NULL_NOT_ALLOWED,
'default' => false,
'type' => PARAM_BOOL
];

return $preferences;
}
Expand Down
22 changes: 22 additions & 0 deletions mod/forum/tests/lib_test.php
Expand Up @@ -3696,4 +3696,26 @@ public function test_mod_forum_core_calendar_event_timestart_updated_due_event()
$this->assertEquals($newduedate, $forum->duedate);
$this->assertEquals($cutoffdate, $forum->cutoffdate);
}

/**
* Test forum_get_layout_modes function.
*/
public function test_forum_get_layout_modes() {
$expectednormal = [
FORUM_MODE_FLATOLDEST => get_string('modeflatoldestfirst', 'forum'),
FORUM_MODE_FLATNEWEST => get_string('modeflatnewestfirst', 'forum'),
FORUM_MODE_THREADED => get_string('modethreaded', 'forum'),
FORUM_MODE_NESTED => get_string('modenested', 'forum')
];
$expectedexperimental = [
FORUM_MODE_FLATOLDEST => get_string('modeflatoldestfirst', 'forum'),
FORUM_MODE_FLATNEWEST => get_string('modeflatnewestfirst', 'forum'),
FORUM_MODE_THREADED => get_string('modethreaded', 'forum'),
FORUM_MODE_NESTED_V2 => get_string('modenestedv2', 'forum')
];

$this->assertEquals($expectednormal, forum_get_layout_modes());
$this->assertEquals($expectednormal, forum_get_layout_modes(false));
$this->assertEquals($expectedexperimental, forum_get_layout_modes(true));
}
}
25 changes: 21 additions & 4 deletions mod/forum/view.php
Expand Up @@ -78,13 +78,28 @@

require_course_login($course, true, $cm);

$istypesingle = 'single' === $forum->get_type();
$istypesingle = $forum->get_type() === 'single';
$saveddisplaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);

if ($mode) {
set_user_preference('forum_displaymode', $mode);
$displaymode = $mode;
} else {
$displaymode = $saveddisplaymode;
}

$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
if (get_user_preferences('forum_useexperimentalui', false)) {
if ($displaymode == FORUM_MODE_NESTED) {
$displaymode = FORUM_MODE_NESTED_V2;
}
} else {
if ($displaymode == FORUM_MODE_NESTED_V2) {
$displaymode = FORUM_MODE_NESTED;
}
}

if ($displaymode != $saveddisplaymode) {
set_user_preference('forum_displaymode', $displaymode);
}

$PAGE->set_context($forum->get_context());
$PAGE->set_title($forum->get_name());
Expand All @@ -111,7 +126,9 @@
$PAGE->set_button(implode('', $buttons));

if ($istypesingle && $displaymode == FORUM_MODE_NESTED_V2) {
$PAGE->add_body_class('nested-v2-display-mode reset-style');
$PAGE->add_body_class('reset-style');
$settingstrigger = $OUTPUT->render_from_template('mod_forum/settings_drawer_trigger', null);
$PAGE->add_header_action($settingstrigger);
}

if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) {
Expand Down
2 changes: 2 additions & 0 deletions user/forum.php
Expand Up @@ -39,6 +39,7 @@
$forumform = new user_edit_forum_form(null, array('userid' => $user->id));

$user->markasreadonnotification = get_user_preferences('forum_markasreadonnotification', 1, $user->id);
$user->useexperimentalui = get_user_preferences('forum_useexperimentalui', 0, $user->id);
$forumform->set_data($user);

$redirect = new moodle_url("/user/preferences.php", array('userid' => $user->id));
Expand All @@ -48,6 +49,7 @@

$user->maildigest = $data->maildigest;
$user->autosubscribe = $data->autosubscribe;
$user->preference_forum_useexperimentalui = $data->useexperimentalui;
if (!empty($CFG->forum_trackreadposts)) {
$user->trackforums = $data->trackforums;
if (property_exists($data, 'markasreadonnotification')) {
Expand Down
6 changes: 6 additions & 0 deletions user/forum_form.php
Expand Up @@ -61,6 +61,12 @@ public function definition () {
$mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
$mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));

$choices = array();
$choices['1'] = get_string('yes');
$choices['0'] = get_string('no');
$mform->addElement('select', 'useexperimentalui', get_string('useexperimentalui', 'mod_forum'), $choices);
$mform->setDefault('useexperimentalui', '0');

if (!empty($CFG->forum_trackreadposts)) {
$mform->addElement('header', 'trackreadposts', get_string('trackreadposts_header', 'mod_forum'));
$choices = array();
Expand Down

0 comments on commit 667e5fd

Please sign in to comment.