Commit 96dd6c9c authored by Peter Dias's avatar Peter Dias
Browse files

MDL-72904 core_badges: Introduce tertiary navigation for badges

parent 58a729f0
......@@ -42,11 +42,14 @@ $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
if ($badge->type == BADGE_TYPE_COURSE) {
require_login($badge->courseid);
$course = get_course($badge->courseid);
$heading = format_string($course->fullname, true, ['context' => $context]);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
$PAGE->set_pagelayout('standard');
navigation_node::override_active_url($navurl);
} else {
$PAGE->set_pagelayout('admin');
$heading = get_string('administrationsite');
navigation_node::override_active_url($navurl, true);
}
......@@ -107,7 +110,7 @@ if ($activate) {
$strheading = get_string('reviewbadge', 'badges');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$PAGE->set_heading($badge->name);
$PAGE->set_heading($heading);
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
......
......@@ -45,18 +45,21 @@ if ($badge->type == BADGE_TYPE_COURSE) {
print_error('coursebadgesdisabled', 'badges');
}
require_login($badge->courseid);
$course = get_course($badge->courseid);
$heading = format_string($course->fullname, true, ['context' => $context]);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
$PAGE->set_pagelayout('standard');
navigation_node::override_active_url($navurl);
} else {
$PAGE->set_pagelayout('admin');
$heading = get_string('administrationsite');
navigation_node::override_active_url($navurl, true);
}
$currenturl = new moodle_url('/badges/alignment.php', array('id' => $badge->id));
$PAGE->set_context($context);
$PAGE->set_url($currenturl);
$PAGE->set_heading($badge->name);
$PAGE->set_heading($heading);
$PAGE->set_title($badge->name);
$PAGE->navbar->add($badge->name);
......@@ -80,9 +83,10 @@ if ($mform->is_cancelled()) {
}
echo $OUTPUT->header();
$actionbar = new \core_badges\output\manage_badge_action_bar($badge, $PAGE);
echo $output->render_tertiary_navigation($actionbar);
echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
echo $output->print_badge_status_box($badge);
$output->print_badge_tabs($badgeid, $context, 'alignment');
if ($emsg !== '') {
echo $OUTPUT->notification($emsg);
} else if ($msg !== '') {
......
......@@ -50,11 +50,14 @@ if ($badge->type == BADGE_TYPE_COURSE) {
print_error('coursebadgesdisabled', 'badges');
}
require_login($badge->courseid);
$course = get_course($badge->courseid);
$heading = format_string($course->fullname, true, ['context' => $context]);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
$PAGE->set_pagelayout('standard');
navigation_node::override_active_url($navurl);
} else {
$PAGE->set_pagelayout('admin');
$heading = get_string('administrationsite');
navigation_node::override_active_url($navurl, true);
}
......@@ -68,7 +71,7 @@ $PAGE->set_context($context);
$strrecipients = get_string('recipients', 'badges');
$PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))->add($strrecipients);
$PAGE->set_title($strrecipients);
$PAGE->set_heading($badge->name);
$PAGE->set_heading($heading);
if (!$badge->is_active()) {
echo $OUTPUT->header();
......@@ -77,15 +80,18 @@ if (!$badge->is_active()) {
die();
}
$returnurl = new moodle_url('recipients.php', array('id' => $badge->id));
$returnlink = html_writer::link($returnurl, $strrecipients);
$actionbar = new \core_badges\output\standard_action_bar($PAGE, $badge->type, false, false, $returnurl);
$output = $PAGE->get_renderer('core', 'badges');
$tertiarynav = $output->render_tertiary_navigation($actionbar);
// Roles that can award this badge.
$acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
if (empty($acceptedroles)) {
echo $OUTPUT->header();
$return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
echo $OUTPUT->footer();
die();
}
......@@ -126,6 +132,7 @@ if (count($acceptedroles) > 1) {
if (!$role) {
$pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
echo $OUTPUT->header();
echo $tertiarynav;
echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
null, array('label' => get_string('selectaward', 'badges'))));
echo $OUTPUT->footer();
......@@ -139,8 +146,7 @@ if (count($acceptedroles) > 1) {
}
} else {
echo $OUTPUT->header();
$return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
echo $OUTPUT->footer();
die();
}
......@@ -150,8 +156,7 @@ if (count($acceptedroles) > 1) {
$usersids = array_keys($users);
if (!$isadmin && !in_array($USER->id, $usersids)) {
echo $OUTPUT->header();
$return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
echo $OUTPUT->footer();
die();
} else {
......@@ -206,6 +211,7 @@ if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $co
}
echo $OUTPUT->header();
echo $tertiarynav;
echo $OUTPUT->heading($strrecipients);
// Print group selector/dropdown menu (find out current groups mode).
......
<?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 core_badges\output;
use renderable;
use templatable;
use moodle_page;
/**
* Abstract class for the badges tertiary navigation. The class initialises the page and type class variables.
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_action_bar implements renderable, templatable {
/** @var moodle_page $page The context we are operating within. */
protected $page;
/** @var int $type The badge type. */
protected $type;
/**
* standard_action_bar constructor.
*
* @param moodle_page $page
* @param int $type
*/
public function __construct(moodle_page $page, int $type) {
$this->type = $type;
$this->page = $page;
}
/**
* The template that this tertiary nav should use.
*
* @return string
*/
abstract public function get_template(): string;
}
<?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 core_badges\output;
use core_badges\badge;
use moodle_url;
use renderer_base;
use single_button;
use moodle_page;
use url_select;
/**
* Class manage_badge_action_bar - Display the action bar
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manage_badge_action_bar extends base_action_bar {
/** @var badge $badge The badge we are managing. */
protected $badge;
/**
* manage_badge_action_bar constructor
*
* @param badge $badge The badge we are viewing
* @param moodle_page $page The page object
*/
public function __construct(badge $badge, moodle_page $page) {
parent::__construct($page, $badge->type);
$this->badge = $badge;
}
/**
* The template that this tertiary nav should use.
*
* @return string
*/
public function get_template(): string {
return 'core_badges/manage_badge';
}
/**
* Export the action bar
*
* @param renderer_base $output
* @return array
*/
public function export_for_template(renderer_base $output): array {
$elements = [];
$params = ['type' => $this->type];
if ($this->page->context->contextlevel == CONTEXT_COURSE) {
$params['id'] = $this->page->context->instanceid;
}
$elements['button'] = new single_button(new moodle_url('/badges/index.php', $params), get_string('back'), 'get');
$elements['urlselect'] = new url_select($this->generate_badge_navigation(), $this->page->url->out(false), null);
foreach ($elements as $key => $element) {
$elements[$key] = $element->export_for_template($output);
}
return $elements;
}
/**
* Returns a multi dimensional array of the links that should be displayed when creating a badge.
* The keys of the array feed into the text shown to the user and content of each element contain the following:
* - url URL for the option
* - additionalparams Additional params to feed into the url
* - capability The capabilities to check that governs visibility
* @return array
*/
protected function get_badge_administration_mapping_construct(): array {
return [
'boverview' => [
'url' => '/badges/overview.php',
'capability' => ''
],
'bdetails' => [
'url' => '/badges/edit.php',
'additionalparams' => ['action' => 'badge'],
'capability' => 'moodle/badges:configuredetails'
],
'bcriteria' => [
'url' => '/badges/criteria.php',
'capability' => 'moodle/badges:configurecriteria'
],
'bmessage' => [
'url' => '/badges/edit.php',
'additionalparams' => ['action' => 'message'],
'capability' => 'moodle/badges:configuremessages'
],
'bawards' => [
'url' => '/badges/recipients.php',
'additionalparams' => ['sort' => 'dateissued', 'dir' => 'DESC'],
'capability' => 'moodle/badges:viewawarded'
],
'bendorsement' => [
'url' => '/badges/endorsement.php',
'capability' => 'moodle/badges:configuredetails'
],
'brelated' => [
'url' => '/badges/related.php',
'capability' => 'moodle/badges:configuredetails'
],
'balignment' => [
'url' => '/badges/alignment.php',
'capability' => 'moodle/badges:configuredetails'
],
];
}
/**
* Generate the options to be displayed when editing a badge. This feeds into a URL select which will be displayed
* in the tertiary navigation.
*
* @return array
*/
protected function generate_badge_navigation(): array {
global $DB;
$params = ['id' => $this->badge->id];
$options = [];
$construct = $this->get_badge_administration_mapping_construct();
foreach ($construct as $stringidentifier => $checks) {
if ($checks['capability'] && !has_capability($checks['capability'], $this->page->context)) {
continue;
}
$sql = '';
switch ($stringidentifier) {
case 'bawards':
$sql = "SELECT COUNT(b.userid)
FROM {badge_issued} b
INNER JOIN {user} u ON b.userid = u.id
WHERE b.badgeid = :badgeid AND u.deleted = 0";
break;
case 'brelated':
$sql = "SELECT COUNT(br.badgeid)
FROM {badge_related} br
WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
break;
case 'balignment':
$sql = "SELECT COUNT(bc.id)
FROM {badge_alignment} bc
WHERE bc.badgeid = :badgeid";
break;
}
$content = null;
if ($sql) {
$content = $DB->count_records_sql($sql, ['badgeid' => $this->badge->id, 'badgeid2' => $this->badge->id]);
}
$url = new moodle_url($checks['url'], $params + ($checks['additionalparams'] ?? []));
$options[get_string($stringidentifier, 'core_badges', $content)] = $url->out(false);
}
return array_flip($options);
}
}
<?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 core_badges\output;
use moodle_url;
use renderer_base;
use single_button;
/**
* Class recipients_action_bar - Display the action bar
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class recipients_action_bar extends manage_badge_action_bar {
/**
* The template that this tertiary nav should use.
*
* @return string
*/
public function get_template(): string {
return 'core_badges/award_badge';
}
/**
* Export the action bar
*
* @param renderer_base $output
* @return array
*/
public function export_for_template(renderer_base $output): array {
$elements = parent::export_for_template($output);
// Add button for badge manual award.
if ($this->badge->has_manual_award_criteria()
&& has_capability('moodle/badges:awardbadge', $this->page->context) && $this->badge->is_active()) {
$url = new moodle_url('/badges/award.php', ['id' => $this->badge->id]);
$button = new single_button($url, get_string('award', 'badges'), 'post', true);
$elements['awardbutton'] = $button->export_for_template($output);
}
return $elements;
}
}
<?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 core_badges\output;
use moodle_page;
use moodle_url;
use renderer_base;
use single_button;
/**
* Class standard_action_bar - Display the action bar
*
* @package core_badges
* @copyright 2021 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class standard_action_bar extends base_action_bar {
/** @var bool $showmanage Whether or not to show the manage badges button. */
protected $showmanage;
/** @var bool $showaddbadge Whether or not to show the add badges button. */
protected $showaddbadge;
/** @var moodle_url $backurl BackURL to be used when the back button is required. */
protected $backurl;
/**
* standard_action_bar constructor
*
* @param moodle_page $page The page object
* @param int $type The type of badge we are operating with
* @param bool $showmanage Whether or not to show the manage badges button
* @param bool $showaddbadge Whether or not to show the add badges button
* @param moodle_url|null $backurl The backurl to be used
*/
public function __construct(moodle_page $page, int $type, bool $showmanage = true,
$showaddbadge = true, ?moodle_url $backurl = null) {
parent::__construct($page, $type);
$this->showmanage = $showmanage;
$this->showaddbadge = $showaddbadge;
$this->backurl = $backurl;
}
/**
* The template that this tertiary nav should use.
*
* @return string
*/
public function get_template(): string {
return 'core_badges/manage_badges';
}
/**
* Export the action bar
*
* @param renderer_base $output
* @return array The buttons to be rendered
*/
public function export_for_template(renderer_base $output): array {
$buttons = [];
if ($this->backurl) {
$buttons[] = new single_button($this->backurl, get_string('back'), 'get');
}
$params = ['type' => $this->type];
if ($this->page->context->contextlevel == CONTEXT_COURSE) {
$params['id'] = $this->page->context->instanceid;
}
if ($this->showmanage) {
$buttons[] = new single_button(new moodle_url('/badges/index.php', $params),
get_string('managebadges', 'core_badges'), 'get');
}
if ($this->showaddbadge && has_capability('moodle/badges:createbadge', $this->page->context)) {
$buttons[] = new single_button(new moodle_url('/badges/newbadge.php', $params),
get_string('newbadge', 'core_badges'), 'post', true);
}
foreach ($buttons as $key => $button) {
$buttons[$key] = $button->export_for_template($output);
}
return ['buttons' => $buttons];
}
}
......@@ -47,11 +47,14 @@ if ($badge->type == BADGE_TYPE_COURSE) {
print_error('coursebadgesdisabled', 'badges');
}
require_login($badge->courseid);
$course = get_course($badge->courseid);
$heading = format_string($course->fullname, true, ['context' => $context]);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
$PAGE->set_pagelayout('standard');
navigation_node::override_active_url($navurl);
} else {
$PAGE->set_pagelayout('admin');
$heading = get_string('administrationsite');
navigation_node::override_active_url($navurl, true);
}
......@@ -59,7 +62,7 @@ $currenturl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
$PAGE->set_context($context);
$PAGE->set_url($currenturl);
$PAGE->set_heading($badge->name);
$PAGE->set_heading($heading);
$PAGE->set_title($badge->name);
$PAGE->navbar->add($badge->name, $currenturl);
......@@ -80,6 +83,8 @@ if ((($update == BADGE_CRITERIA_AGGREGATION_ALL) || ($update == BADGE_CRITERIA_A
}
echo $OUTPUT->header();
$actionbar = new \core_badges\output\manage_badge_action_bar($badge, $PAGE);
echo $output->render_tertiary_navigation($actionbar);
echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
if ($emsg !== '') {
......@@ -87,9 +92,7 @@ if ($emsg !== '') {
} else if ($msg !== '') {
echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
}
echo $output->print_badge_status_box($badge);
$output->print_badge_tabs($badgeid, $context, 'criteria');
if (!$badge->is_locked() && !$badge->is_active()) {
echo $output->print_criteria_actions($badge);
......
......@@ -57,18 +57,21 @@ if ($badge->is_active() || $badge->is_locked()) {
if ($badge->type == BADGE_TYPE_COURSE) {
require_login($badge->courseid);
$course = get_course($badge->courseid);
$heading = format_string($course->fullname, true, ['context' => $context]);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
$PAGE->set_pagelayout('standard');
navigation_node::override_active_url($navurl);
} else {
$PAGE->set_pagelayout('admin');
$heading = get_string('administrationsite');
navigation_node::override_active_url($navurl, true);
}
$urlparams = array('badgeid' => $badgeid, 'edit' => $edit, 'type' => $type, 'crit' => $crit);
$PAGE->set_context($context);
$PAGE->set_url('/badges/criteria_settings.php', $urlparams);
$PAGE->set_heading($badge->name);
$PAGE->set_heading($heading);
$PAGE->set_title($badge->name);
$PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))->add(get_string('criteria_' . $type, 'badges'));
......
......@@ -52,11 +52,14 @@ if ($badge->type == BADGE_TYPE_COURSE) {
print_error('coursebadgesdisabled', 'badges');
}
require_login($badge->courseid);
$course = get_course($badge->courseid);
$heading = format_string($course->fullname, true, ['context' => $context]);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
$PAGE->set_pagelayout('incourse');
navigation_node::override_active_url($navurl);
} else {
$PAGE->set_pagelayout('admin');
$heading = get_string('administrationsite');
navigation_node::override_active_url($navurl, true);
}
......@@ -64,7 +67,7 @@ $currenturl = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'acti