Hack to add links to primary menu (see README.md)

This commit is contained in:
PMKuipers 2023-06-03 00:00:19 +02:00
parent e7b3185d41
commit 292445f360
3 changed files with 103 additions and 9 deletions

View File

@ -1,2 +1,23 @@
# moodle-local_treestudyplan
## Adding links to the primary navigation menu
The studyplan plugin will automatically add links to the flat navigation bat used in Moodle 3.11 and below
However, Moodle 4.0-4.2 use a new "primary navigation" bar, and does not yet support customizing the primary navigation bar (it is a planned feature though)
**Use the following workaround:**
You can add custom primary menu items in **Site administration** -> **Appearance** -> **Theme settings**
That page contains an item called "Custom menu items"
Add the following into that area to custimize it
// [your name for my studyplan]|/local/treestudyplan/myreport.php
// [your name for studyplan viewing]|/local/treestudyplan/view-plan.php
// [your name for studyplan managing]|/local/treestudyplan/edit-plan.php
// For example:
```
Mijn studieplan|/local/treestudyplan/myreport.php||nl
Studieplannen|/local/treestudyplan/view-plan.php||nl
Studieplannen beheren|/local/treestudyplan/edit-plan.php||nl
My study plan|/local/treestudyplan/myreport.php||en
Study plans|/local/treestudyplan/view-plan.php||en
Manage Study plans|/local/treestudyplan/edit-plan.php||en
```

View File

@ -0,0 +1,40 @@
/*eslint no-var: "error" */
/*eslint no-unused-vars: "off" */
/*eslint linebreak-style: "off" */
/*eslint no-trailing-spaces: "off" */
/*eslint-env es6*/
// Put this file in path/to/plugin/amd/src
// You can call it anything you like
import Debugger from './debugger';
let debug = new Debugger("primary-nav-tools");
debug.enable();
/**
* Hide a primary navigation item by href
* @param {string|Array} hrefs The link that should be hidden
*/
export function hide_primary(hrefs) {
let element = document.createElement('style');
document.head.appendChild(element);
let sheet = element.sheet;
if(typeof hrefs === 'string' || hrefs instanceof String){
hrefs = [hrefs];
}
if(typeof hrefs === 'object' && Array.isArray(hrefs)){
for(const ix in hrefs){
const href = hrefs[ix];
debug.info("Hiding",href);
let style = `
.primary-navigation li.nav-item > a[href*="${href}"] {
display: none;
}
`;
debug.info("Adding style",style);
sheet.insertRule(style, sheet.cssRules.length);
}
}
debug.info("stylesheet",sheet);
}

47
lib.php
View File

@ -14,8 +14,21 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) {
$systemcontext = context_system::instance();
// Moodle 4.0-4.2 do not yet support customizing the primary navigation bar (it is a planned feature though)
// For now, go to theme settings and add the following into "Custom menu items"
// [your name for my studyplan]|/local/treestudyplan/myreport.php
// [your name for studyplan viewing]|/local/treestudyplan/view.php
// For example:
// Mijn studieplan|/local/treestudyplan/myreport.php
// Studieplannen|/local/treestudyplan/view.php
// Using some javascript magic we'll hide the links that are not accessible
// we will add all the hrefs that should be hidden to this variable below
$hide_primary_hrefs = [];
if($USER->id > 1) // Don't show if user is not logged in (id == 0) or is guest user (id == 1)
{
$userstudyplans = studyplan::find_for_user($USER->id);
if(!empty($userstudyplans))
{
@ -24,18 +37,19 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) {
$node = navigation_node::create(
get_string("link_myreport","local_treestudyplan"),
new moodle_url($CFG->wwwroot . "/local/treestudyplan/myreport.php", array()),
global_navigation::TYPE_USER ,
global_navigation::TYPE_SYSTEM,
null,
"local_treestudyplan_myreport",
new pix_icon("myreport", '', 'local_treestudyplan')
);
$node->showinflatnavigation = true;
$node->showinsecondarynavigation=true;
// create invitenode node
$invitenode = navigation_node::create(
get_string("manage_invites","local_treestudyplan"),
new moodle_url($CFG->wwwroot . "/local/treestudyplan/invitations.php", array()),
global_navigation::TYPE_USER ,
global_navigation::TYPE_CUSTOM ,
null,
"local_treestudyplan_invitemgmt",
new pix_icon("invitemgmt", '', 'local_treestudyplan')
@ -46,32 +60,48 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) {
$navigation->add_node($node,'mycourses');
}
else {
$hide_primary_hrefs[] = "/local/treestudyplan/myreport.php";
}
if(has_capability('local/treestudyplan:viewuserreports',context_system::instance()))
{
$node = navigation_node::create(
get_string("link_viewplan","local_treestudyplan"),
new moodle_url($CFG->wwwroot . "/local/treestudyplan/view-plan.php", array()),
global_navigation::TYPE_USER ,
global_navigation::TYPE_SYSTEM ,
null,
"local_treestudyplan_viewplan",
new pix_icon("viewplans", '', 'local_treestudyplan')
);
$node->showinflatnavigation = true;
$node->showinsecondarynavigation=true;
$navigation->add_node($node,'mycourses');
}
else {
$hide_primary_hrefs[] = "/local/treestudyplan/view-plan.php";
}
if(has_capability('local/treestudyplan:editstudyplan',context_system::instance()))
{
$node = navigation_node::create(
get_string("cfg_plans","local_treestudyplan"),
new moodle_url($CFG->wwwroot . "/local/treestudyplan/edit-plan.php", array()),
global_navigation::TYPE_USER ,
global_navigation::TYPE_SYSTEM ,
null,
"local_treestudyplan_editplan",
new pix_icon("viewplans", '', 'local_treestudyplan')
);
$node->showinflatnavigation = true;
$node->showinsecondarynavigation=true;
$navigation->add_node($node,'mycourses');
}
else {
$hide_primary_hrefs[] = "/local/treestudyplan/edit-plan.php";
}
}
else {
$hide_primary_hrefs[] = "/local/treestudyplan/myreport.php";
$hide_primary_hrefs[] = "/local/treestudyplan/edit-plan.php";
$hide_primary_hrefs[] = "/local/treestudyplan/view-plan.php";
}
// create invitenode node
$invitenode = navigation_node::create(
@ -86,8 +116,11 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) {
$navigation->add_node($invitenode,'mycourses');
// Add navigation node to course category pages
$categoryid = optional_param('categoryid', 0, PARAM_INT); // Category id
// Now using some javascript magic, we'll hide the links that are not accessible
$PAGE->requires->js_call_amd('local_treestudyplan/primary-nav-tools', 'hide_primary', [$hide_primary_hrefs]);
}
function local_treestudyplan_extend_navigation_category_settings($navigation, context_coursecat $coursecategorycontext) {