Skip to content

Commit

Permalink
MDL-72904 core_badges: Introduce tertiary navigation for badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Dias committed Dec 23, 2021
1 parent 58a729f commit 96dd6c9
Show file tree
Hide file tree
Showing 22 changed files with 761 additions and 83 deletions.
5 changes: 4 additions & 1 deletion badges/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@

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);
}

Expand Down Expand Up @@ -107,7 +110,7 @@
$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);

Expand Down
8 changes: 6 additions & 2 deletions badges/alignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@
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);

Expand All @@ -80,9 +83,10 @@
}

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 !== '') {
Expand Down
20 changes: 13 additions & 7 deletions badges/award.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@
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);
}

Expand All @@ -68,7 +71,7 @@
$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();
Expand All @@ -77,15 +80,18 @@
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();
}
Expand Down Expand Up @@ -126,6 +132,7 @@
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();
Expand All @@ -139,8 +146,7 @@
}
} 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();
}
Expand All @@ -150,8 +156,7 @@
$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 {
Expand Down Expand Up @@ -206,6 +211,7 @@
}

echo $OUTPUT->header();
echo $tertiarynav;
echo $OUTPUT->heading($strrecipients);

// Print group selector/dropdown menu (find out current groups mode).
Expand Down
53 changes: 53 additions & 0 deletions badges/classes/output/base_action_bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;
}
172 changes: 172 additions & 0 deletions badges/classes/output/manage_badge_action_bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?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);
}
}
Loading

0 comments on commit 96dd6c9

Please sign in to comment.