Skip to content

Commit

Permalink
MDL-71915 mod_forum: Update the tertiary nav
Browse files Browse the repository at this point in the history
Update the tertiary navigation for this
activity.
  • Loading branch information
sharidas committed Dec 6, 2021
1 parent 621d0a8 commit 9283354
Show file tree
Hide file tree
Showing 21 changed files with 647 additions and 91 deletions.
2 changes: 1 addition & 1 deletion mod/forum/classes/local/renderers/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private function get_notifications($user) : array {
if ($this->capabilitymanager->must_post_before_viewing_discussion($user, $discussion)) {
$notifications[] = (new notification(
get_string('qandanotify', 'forum')
))->set_show_closebutton(true);
))->set_show_closebutton(true)->set_extra_classes(['mt-3']);
}
}

Expand Down
33 changes: 29 additions & 4 deletions mod/forum/classes/local/renderers/discussion_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use renderer_base;
use stdClass;
use core\output\notification;
use mod_forum\local\data_mappers\legacy\forum;
use mod_forum\local\factories\builder as builder_factory;

require_once($CFG->dirroot . '/mod/forum/lib.php');
Expand Down Expand Up @@ -145,6 +146,7 @@ public function __construct(
* @param int $pageno The zero-indexed page number to use
* @param int $pagesize The number of discussions to show on the page
* @param int $displaymode The discussion display mode
* @param bool $enablediscussioncreation To show the discussion button.
* @return string The rendered content for display
*/
public function render(
Expand All @@ -154,7 +156,8 @@ public function render(
?int $sortorder,
?int $pageno,
?int $pagesize,
int $displaymode = null
int $displaymode = null,
bool $enablediscussioncreation = true
) : string {
global $PAGE;

Expand Down Expand Up @@ -209,7 +212,8 @@ public function render(
],
'totaldiscussioncount' => $alldiscussionscount,
'userid' => $user->id,
'visiblediscussioncount' => count($discussions)
'visiblediscussioncount' => count($discussions),
'enablediscussioncreation' => $enablediscussioncreation,
];

if ($forumview['forum']['capabilities']['create']) {
Expand Down Expand Up @@ -241,6 +245,27 @@ public function render(
return $this->renderer->render_from_template($this->template, $forumview);
}

/**
* Add new discussion button to the action bar for tertiary nav.
*
* @param stdClass $user The user object.
* @param int|null $groupid The group id.
* @return string rendered HTML string
*/
public function render_new_discussion(stdClass $user, ?int $groupid): string {
$forumexporter = $this->exporterfactory->get_forum_exporter(
$user,
$this->forum,
$groupid
);

$forumview = [
'forum' => (array) $forumexporter->export($this->renderer),
];

return $this->renderer->render_from_template('mod_forum/forum_new_discussion_actionbar', $forumview);
}

/**
* Get the mod_forum_post_form. This is the default boiler plate from mod_forum/post_form.php with the inpage flag caveat
*
Expand Down Expand Up @@ -335,7 +360,7 @@ private function get_page_number(?int $pageno) : int {
* @param int|null $groupid The forum's group id
* @return array
*/
private function get_notifications(stdClass $user, ?int $groupid) : array {
private function get_notifications(stdClass $user, ?int $groupid): array {
$notifications = $this->notifications;
$forum = $this->forum;
$capabilitymanager = $this->capabilitymanager;
Expand Down Expand Up @@ -381,7 +406,7 @@ private function get_notifications(stdClass $user, ?int $groupid) : array {
$notifications[] = (new notification(
get_string('qandanotify', 'forum'),
notification::NOTIFY_INFO
))->set_show_closebutton();
))->set_show_closebutton()->set_extra_classes(['mt-3']);
}

if ('eachuser' === $forum->get_type()) {
Expand Down
135 changes: 135 additions & 0 deletions mod/forum/classes/output/forum_actionbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?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/>.

namespace mod_forum\output;

use renderable;
use renderer_base;
use templatable;
use moodle_url;
use help_icon;
use mod_forum\local\entities\forum as forum_entity;

/**
* Render activity page for tertiary nav
*
* Render elements search forum, add new discussion button and subscribe all
* to the page action.
*
* @package mod_forum
* @copyright 2021 Sujith Haridasan <sujith@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class forum_actionbar implements renderable, templatable {
/**
* @var forum_entity $forum
*/
private $forum;

/**
* @var \stdClass $course
*/
private $course;

/**
* @var mixed $groupid
*/
private $groupid;

/**
* @var string $search
*/
private $search;

/**
* forum_actionbar constructor.
*
* @param forum_entity $forum The forum object.
* @param \stdClass $course The course object.
* @param int|null $groupid The group id.
* @param string $search The search string.
*/
public function __construct(forum_entity $forum, \stdClass $course, ?int $groupid, string $search) {
$this->forum = $forum;
$this->course = $course;
$this->groupid = $groupid;
$this->search = $search;
}

/**
* Render the new discussion button.
*
* @return string HTML button
*/
private function get_new_discussion_topic_button(): string {
global $USER;
$renderfactory = \mod_forum\local\container::get_renderer_factory();
$discussionrenderer = $renderfactory->get_discussion_list_renderer($this->forum);
return $discussionrenderer->render_new_discussion($USER, $this->groupid);
}

/**
* Data for the template.
*
* @param renderer_base $output The render_base object.
* @return array data for the template
*/
public function export_for_template(renderer_base $output): array {
global $USER;
$actionurl = (new moodle_url('/mod/forum/search.php'))->out(false);
$helpicon = new help_icon('search', 'core');
$hiddenfields = [
(object) ['name' => 'id', 'value' => $this->course->id],
];
$shownewdiscussionbtn = '';
if ($this->forum->get_type() !== 'single') {
$shownewdiscussionbtn = $this->get_new_discussion_topic_button();
}
$data = [
'action' => $actionurl,
'hiddenfields' => $hiddenfields,
'query' => $this->search,
'helpicon' => $helpicon->export_for_template($output),
'inputname' => 'search',
'searchstring' => get_string('searchforums', 'mod_forum'),
'newdiscussionbtn' => $shownewdiscussionbtn,
];

$legacydatamapperfactory = \mod_forum\local\container::get_legacy_data_mapper_factory();
$forumobject = $legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($this->forum);
$context = $this->forum->get_context();
$activeenrolled = is_enrolled($context, $USER, '', true);
$canmanage = has_capability('mod/forum:managesubscriptions', $context);
$cansubscribe = $activeenrolled && !($this->forum->get_subscription_mode() === FORUM_FORCESUBSCRIBE) &&
(!($this->forum->get_subscription_mode() === FORUM_DISALLOWSUBSCRIBE) || $canmanage);
if ($cansubscribe) {
$returnurl =
(new moodle_url('/mod/forum/view.php', ['id' => $this->forum->get_course_module_record()->id]))->out(false);
if (!\mod_forum\subscriptions::is_subscribed($USER->id, $forumobject, null, $this->forum->get_course_module_record())) {
$data['subscribetoforum'] = (new moodle_url(
'/mod/forum/subscribe.php',
['id' => $forumobject->id, 'sesskey' => sesskey(), 'returnurl' => $returnurl]
))->out(false);
} else {
$data['unsubscribefromforum'] = (new moodle_url(
'/mod/forum/subscribe.php',
['id' => $forumobject->id, 'sesskey' => sesskey(), 'returnurl' => $returnurl]
))->out(false);
}
}
return $data;
}
}
143 changes: 143 additions & 0 deletions mod/forum/classes/output/subscription_actionbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?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/>.

namespace mod_forum\output;

use moodle_url;
use renderer_base;
use url_select;
use renderable;
use templatable;

/**
* Renders the subscribers page for this activity.
*
* @package mod_forum
* @copyright 2021 Sujith Haridasan <sujith@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class subscription_actionbar implements renderable, templatable {
/** @var int course id */
private $id;

/** @var moodle_url */
private $currenturl;

/** @var \stdClass */
private $forum;

/**
* subscription_actionbar constructor.
*
* @param int $id The forum id.
* @param moodle_url $currenturl Current URL.
* @param \stdClass $forum The forum object.
*/
public function __construct(int $id, moodle_url $currenturl, \stdClass $forum) {
$this->id = $id;
$this->currenturl = $currenturl;
$this->forum = $forum;
}

/**
* Create url select menu for subscription option
*
* @return url_select the url_select object
*/
private function create_subscription_menu(): url_select {
$sesskey = sesskey();
$modeset = \mod_forum\subscriptions::get_subscription_mode($this->forum);
$optionallink = new moodle_url('/mod/forum/subscribe.php', ['id' => $this->id, 'mode' => 0, 'sesskey' => $sesskey]);
$forcedlink = new moodle_url('/mod/forum/subscribe.php', ['id' => $this->id, 'mode' => 1, 'sesskey' => $sesskey]);
$autolink = new moodle_url('/mod/forum/subscribe.php', ['id' => $this->id, 'mode' => 2, 'sesskey' => $sesskey]);
$disabledlink = new moodle_url('/mod/forum/subscribe.php', ['id' => $this->id, 'mode' => 3, 'sesskey' => $sesskey]);

$menu = [
$optionallink->out(false) => get_string('subscriptionoptional', 'forum'),
$forcedlink->out(false) => get_string('subscriptionforced', 'forum'),
$autolink->out(false) => get_string('subscriptionauto', 'forum'),
$disabledlink->out(false) => get_string('subscriptiondisabled', 'forum'),
];

switch ($modeset) {
case FORUM_CHOOSESUBSCRIBE:
$set = get_string('subscriptionoptional', 'forum');
break;
case FORUM_FORCESUBSCRIBE:
$set = get_string('subscriptionforced', 'forum');
break;
case FORUM_INITIALSUBSCRIBE:
$set = get_string('subscriptionauto', 'forum');
break;
case FORUM_DISALLOWSUBSCRIBE:
$set = get_string('subscriptiondisabled', 'forum');
break;
default:
throw new \moodle_exception(get_string('invalidforcesubscribe', 'forum'));
}

$menu = array_filter($menu, function($key) use ($set) {
if ($key !== $set) {
return true;
}
});
$urlselect = new url_select($menu, $this->currenturl, ['' => $set], 'selectsubscriptionoptions');
$urlselect->set_label(get_string('subscriptions', 'mod_forum'), ['class' => 'accesshide']);
$urlselect->class .= ' float-right';
return $urlselect;
}

/**
* Create view and manage subscribers select menu.
*
* @return url_select get url_select object.
*/
private function create_view_manage_menu(): url_select {
$viewlink = new moodle_url('/mod/forum/subscribers.php', ['id' => $this->id, 'edit' => 'off']);
$managelink = new moodle_url('/mod/forum/subscribers.php', ['id' => $this->id, 'edit' => 'on']);

$menu = [
$viewlink->out(false) => get_string('forum:viewsubscribers', 'forum'),
$managelink->out(false) => get_string('managesubscriptionson', 'forum'),
];

if ($this->currenturl->get_param('edit') === 'off') {
$this->currenturl = $viewlink;
} else {
$this->currenturl = $managelink;
}

$urlselect = new url_select($menu, $this->currenturl->out(false), null, 'selectviewandmanagesubscribers');
$urlselect->set_label(get_string('subscribers', 'forum'), ['class' => 'accesshide']);
return $urlselect;
}

/**
* Data for the template.
*
* @param renderer_base $output The render_base object.
* @return array data for template
*/
public function export_for_template(renderer_base $output): array {
$subscribeoptionselect = $this->create_subscription_menu();
$viewmanageselect = $this->create_view_manage_menu();
$data = [
'subscriptionoptions' => $subscribeoptionselect->export_for_template($output),
'viewandmanageselect' => $viewmanageselect->export_for_template($output),
];
return $data;
}
}
4 changes: 3 additions & 1 deletion mod/forum/discuss.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@

echo $OUTPUT->header();
if (!$isnestedv2displaymode) {
echo $OUTPUT->heading(format_string($forum->get_name()), 2);
if (!$PAGE->has_secondary_navigation()) {
echo $OUTPUT->heading(format_string($forum->get_name()), 2);
}
echo $OUTPUT->heading(format_string($discussion->get_name()), 3, 'discussionname');
}

Expand Down
4 changes: 3 additions & 1 deletion mod/forum/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ function($exportdata) use ($fields, $striphtml, $humandates, $canviewfullname, $
$PAGE->set_heading($pagetitle);

echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle);
if (!$PAGE->has_secondary_navigation()) {
echo $OUTPUT->heading($pagetitle);
}

// It is possible that the following fields have been provided in the URL.
$form->set_data(['useridsselected' => $userids, 'discussionids' => $discussionids, 'from' => $from, 'to' => $to]);
Expand Down
Loading

0 comments on commit 9283354

Please sign in to comment.