How to hide topic-0 in Moodle 4

How to hide topic-0 in Moodle 4

by Bob Gilmore -
Number of replies: 19
Picture of Particularly helpful Moodlers

I'm trying to migrate some courses over to Moodle 4 from 3.11 and older. A lot of my longer courses use the Show one section per page setting to keep the scroll down.

If I have anything in the general / section-0 section, it's repeated on every topic page which we don't want. If we clear it out, it shows up in the course menu as a blank bar or "General" and if the General heading is shown, each topic has the general heading with nothing in it at the top of the topic.

Some examples below:

Course page with Section-0 custom title of a single space:



Course page with the empty General section and non-custom title:



Single topic open with the General section repeated here:

In the past I would have used some themes and course formats (Grid) to avoid this problem, but with Moodle 4, those themes are defunct and the Grid format currently is unable to merge topic-0 into the grid as it was previously able.

I was thinking about using CSS to hide it, but I cant seem to target selectors for the Topics course format that differentiates from Show All Topics / Show One Topic per page.

Does anyone have any suggestions for hiding the General section. I can live with it on the course page as I can put the course summary information (the welcome and about in the screenies) there, but I desperately need it to not show on the topics pages.
Average of ratings: -
In reply to Bob Gilmore

Re: How to hide topic-0 in Moodle 4

by Jared Wall -
I have a different issue. We utilize Section 0 (General) in our academic course templates for all syllabus and policy verification materials. Usually, topic contains all of the important information for the course including the announcements forum, syllabus, Panopto block, etc. We have this set to one topic per page, but the content for section 0 shows at the top of every topic. In essence, one must scroll through all of the section 0 material to access the current topic content. Is there a way to make this so section 0 only appear when section 0 is selected? Any help would be appreciated. A CSS fix is fine if anyone know what is needed.

I have found this in a previous post, but it hides section 0 all the time. I still need access to section 0 when that topic is selected.

#section-0 {
display: none;
In reply to Jared Wall

Re: How to hide topic-0 in Moodle 4

by Bob Gilmore -
Picture of Particularly helpful Moodlers
That's the same issue for us.  Apologies if i didn't explain it correctly. I thought that, if I move all of the section 0 stuff into section 1 and tried to hide section 0 it might work, but then we have the issue of the blank or pointless heading with no content on every page.
In reply to Bob Gilmore

Re: How to hide topic-0 in Moodle 4

by Daniel Thies -
Picture of Core developers Picture of Plugin developers Picture of Testers
You might consider using another course format that does this and is designed optimized for multiple pages. A good candidate might be Grid format. Section 0 is displayed on main page with links and images for the other sections below. Section 0 is not displayed on those pages.
Average of ratings: Useful (1)
In reply to Bob Gilmore

Re: How to hide topic-0 in Moodle 4

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Bob and Jared,

You can also just add a small script as follows, without the need to install a plugin.

#section-0 {display: none;} is fine, but you also need to distinguish between the main course page where you want the General section to appear and other section pages where you don't want it to appear.

Add the code below to the HTML of the General section (by editing the General section) and you should get what you want, that is, as I understand it, showing the General section on the main course page and hiding it on the other section pages, when the Course layout is set to "Show one section per page":

<!-- Use no-nonsense jQuery :-) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script>

// Wait for the document to be loaded.
$(document).ready(function() {

    // Check if the URL does not:
    // contain "course" and
    // contain "section=0" or does not contain "section",
    // i.e. check if the page is not the main course page.
    // If so, hide the General section.

    var dc1 = window.location.href.indexOf("course") > -1;
    var dc2 = window.location.href.indexOf("section=0") > -1;
    var dc3 = window.location.href.indexOf("section") <= -1;
    var dc4 = dc2 || dc3;
    var dc5 = dc1 && dc4;
    if (!dc5) {
        $("#section-0").hide();
    }
});
</script>
In reply to Dominique Bauer

Re: How to hide topic-0 in Moodle 4

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Dear Dominique,

With respect, that's a horrible and dangerous solution as you're almost certainly loading jQuery twice on the page - the one served by Moodle and the one in your script.  This is not a good thing, Google it.  In addition its possible that '$' will be attempted to be used before the browser loads the jQuery you've requested and even though therefore logically would fail, then I suspect Moodle's jQuery could then be used instead, but again, that might not also be loaded and the script would fail.

Therefore the most ideal solution is in the PHP code itself that controls the logic of the output, i.e. 'export_for_template' method in '/course/format/classes/output/local/content.php':

Differences in content.php.
 
Tested on Topics format, should work (after code inspection) on Weeks format, and indeed any other format that uses that 'content.php' and does not override that method, i.e. not Collapsed Topics or Grid.

G
Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: How to hide topic-0 in Moodle 4

by Bob Gilmore -
Picture of Particularly helpful Moodlers
I actually did something similar with pure JS. For a variety of reasons, I'm allergic to jQuery, so I just did a regex on window.location for similar strings. Then i discovered the sections course format which did what I wanted and more, so I've started using that. I should have come back here and posted my solution.
In reply to Dominique Bauer

Re: How to hide topic-0 in Moodle 4

by Joshua Moore -
Could you add this code to the site HTML instead of editing the general section?
In reply to Bob Gilmore

Re: How to hide topic-0 in Moodle 4

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Bob and Jared,

Make no mistake about it, as surprising as it may seem these days, it very well seems like some programmers' motivation is still to denigrate any front-end solution, which can be used by any teacher.

JavaScript solutions have the advantage of being immediately and directly implementable by teachers without the need to access the site administration. Also, unlike PHP solutions, they don't automatically apply to all courses on a site, which is another big advantage in large organizations like universities where professors may want to customize their own courses.

[

Content deleted. Participants are reminded to show respect and consideration to others, as mentioned in our site policy. Any flaming or personal attacks will be deleted.

]

jQuery is used on more than three quarters of all websites. By the way it is just a JavaScript library. When browsing the web, the browser does not load jQuery with each new site. For efficiency, it uses the cached version instead. You can load jQuery a million times, and absolutely nothing will happen.

You can google "load jQuery twice" and you'll only find a few worthless old conspiratorial posts.[
Content deleted. Participants are reminded to show respect and consideration to others, as mentioned in our site policy. Any flaming or personal attacks will be deleted.
]

If there was a problem, jQuery would notify its millions of users. But no, there is no problem.

If it meets your needs, you can safely use my solution. If you have access to the site administration, you can of course also use the "Sections format" plugin or even make changes to the PHP code.

(Edited by Aurélie Soulier - original submission Friday, 30 December 2022, 6:52 PM)

In reply to Dominique Bauer

Re: How to hide topic-0 in Moodle 4

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Dear Dominique,

I have no intention of getting into or appearing to have a 'flame attack' etc, which is against the site policy, see 'Respect others' on https://moodle.org/mod/page/view.php?id=7080. That was not the intent of my reply. My reply was a pure professional criticism of the code presented and my understanding of the issues it could cause. Solutions can be anything, back or front end. Ok, I am not disseminating false information as your code does not use the 'noConflict' method (https://api.jquery.com/jQuery.noConflict/) which is there to avoid issues when jQuery is used twice on the same page, their words "If for some reason two versions of jQuery are loaded (which is not recommended), calling $.noConflict( true ) from the second version will return the globally scoped jQuery variables to those of the first version.", and as such a possible conflict of versions. Yes, M4.0 does use 3.6.0, however the unsuspecting person not knowing this information and then employing the script on M4.1, which has jQuery 3.6.1 and the same Course Format API requiring such a solution.

The statement "When browsing the web, the browser does not load jQuery with each new site. For efficiency, it uses the cached version instead." is not completely true, caching by the browser depends on the URI's used on the page used to specify where resources are located.  Thus one site can specify a locally hosted jQuery file, as Moodle does (stored in '/lib/jquery/') and another hosted on 'googleapis.com', both will be cached separately and so the same version can be stored twice as the URI's will be different.

G
Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: How to hide topic-0 in Moodle 4

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Gareth,

Your reference to the code of conduct is welcome. 
[
Content deleted. Participants are reminded to show respect and consideration to others, as mentioned in our site policy. Any flaming or personal attacks will be deleted.
]

Contrary to what you say, Moodle does not load jQuery on the pages discussed here. This is easily verifiable, on a fresh version of Moodle, as if you don't include the first line, the rest of the code won't work. If the code works without the first line, just don't include it. A possible conflict between jQuery versions simply does not exist, because the browser only uses the latest loaded version on a given page (unless you intentionally want to use two versions, which is not recommended and in which case you would need to use the noConflict menthod, but this has nothing to do with our case). Also, it matters very little if jq3.6.0, jq.3.6.1 or another recent version is used.

[
Content deleted. Participants are reminded to show respect and consideration to others, as mentioned in our site policy. Any flaming or personal attacks will be deleted.
]

Once again, front-end solutions using JavaScript and the jQuery library are sound and safe.

(Edited by Aurélie Soulier - original submission Saturday, 31 December 2022, 1:59 AM)

(Edited by Aurélie Soulier - original submission Saturday, 31 December 2022, 1:59 AM)

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: How to hide topic-0 in Moodle 4

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Dominique,

To clarify things:

  • I'm not criticising jQuery but your code.  To be additionally clear, I'm not personally criticising you but your code.  I'm also not perfect, I've written horrible code at times, kicked myself over it when its been peer reviewed by somebody else or spotted it myself at a later time, and then gone on to learn from the mistake and correct it.
  • Your code is course format related and thus independent of the theme and other plugins.  Themes and other plugins can load jQuery therefore it is possible for Moodle to load jQuery on any page.
  • "because the browser only uses the latest loaded version on a given page" - not true, it does not use the 'latest' version, but the version its been told to load, i.e. 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'.
  • I've absolutely no idea where you've got the idea that I dislike jQuery?  I actually love it and have done for over a decade.  Indeed I disagree with "The use of jQuery in new code is strongly discouraged" on https://moodledev.io/docs/guides/javascript/jquery - but that's my opinion.  jQuery has a syntax that is in my opinion easier to understand than pure JavaScript.  I've used jQuery in my plugins for ages, and still continue to do so.  If the shoe fits, wear it.  Therefore I consider that jQuery's use in Moodle can have merit.
  • "Once again, front-end solutions using JavaScript and the jQuery library are sound and safe." - Not true.  Code is (for the most part) created by humans.  Humans make mistakes.  Code fails with different levels of severity and impact.  QED (Quod Erat Demonstrandum).
  • From a moderation perspective, you are in my opinion getting personal about this with me, e.g. "Your interpretation of using jQuery seems much more emotional than rational.".  Please keep this on topic and related to the discussion of the problem.  This is not a personal attack on you, but rather me reminding you of the intent of "Participants are reminded to show respect and consideration to others, as mentioned in our site policy" as stated on https://docs.moodle.org/dev/Moderator_guidelines#Flaming_.2F_personal_attacks.

G

Average of ratings: Useful (3)
In reply to Gareth J Barnard

Re: How to hide topic-0 in Moodle 4

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Gareth,

Saying that you dislike jQuery is the first thing that came to mind, because several people do. I was not getting personal with you, I was just trying to figure out why you say my code is horrible and dangerous.

I'll try again, no offense. If I understood correctly, you are the author of themes and many other plugins. You are certainly an excellent programmer. According to you, my code is horrible and dangerous because it could lead to version conflicts and other problems. I must admit that from your point of view as a programmer, you are absolutely right. A good programmer wants to avoid these kinds of problems.

Your turn to try to understand why I proposed my solution. I am a user, not a programmer, just a teacher in a school where there are currently 260 teachers. All Moodle courses are on a site administered by the IT department. If I want the General section not to appear on the course pages, I have to make the request to the IT department who will certainly refuse to modify the PHP code because this change would affect all the courses and other teachers could certainly not agree to this change. In the case that concerns us, there is a plugin that meets the request perfectly, but this is far from always the case. My request to the IT department to install the plugin could be accepted or not. Chances are it wouldn't, because the IT department would be too busy or they wouldn't consider my request justified.

Again nothing personal, but it seems that many programmers don't fully understand user needs. It looks like they think their software is perfect and it stops there. But I, who am still not an idiot (I have a doctorate in engineering from a well-recognized university and am a professor in a major engineering school), I would still like the General section not to appear on all the main course pages.

So I use a simple little JavaScript that does the job. That's all. And from experience, with the Boost theme, this kind of script, with the call to jQuery, works 100% of the time and does not cause any problems. It could be that in some cases, for example with another theme, this kind of script causes a problem. However, in reality, the risk is surely very low since no problem has ever been reported.

So, for you my code is horrible and dangerous. I agree. But for me it is great, very useful and effective. It works perfectly and causes absolutely no problems. And I hope it can help other teachers in the same situation as me.

Front-end solutions using JavaScript and the jQuery library are great workarounds for users when better solutions simply don't exist or aren't accessible. Obviously, they are not perfect and have a very significant risk of about 0% of causing problems.

In reply to Dominique Bauer

Re: How to hide topic-0 in Moodle 4

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Dominique,

I was a teacher in a school and have worked for large organisations that have very restrictive practices, with tight controls over the operational software that orchestrates the business.  Thus I do understand your situation.  However, the problems you face are human ones and technical solutions shouldn't be used to solve those problems as a means of getting around them.  The fundamental issue has not been resolved, that of the client, being the educator, having their requirements fulfilled in order to undertake the job that they need to perform.  By taking this path, in my opinion, you'll only succeed in a negative way, leading to management decisions to curtail your ability to do this, for example, please see https://moodle.org/mod/forum/discuss.php?d=442154. The positive solution will be to engage with all concerned and work out what human process changes can be enacted so that you're not forced down this technical bypass route.

No code works 100% of the time, (we'll possibly TCP (or is it IP, I forget), as its a simple finite state machine, but even then if there is a hardware issue...), and no code has a 0% risk of causing problems.  I don't think my software is perfect and I do listen and understand users needs on their terms.

G

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: How to hide topic-0 in Moodle 4

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
[
Post deleted. Participants are reminded to show respect and consideration to others, as mentioned in our site policy. Any flaming or personal attacks will be deleted.
]

(Edited by Aurélie Soulier - original submission Saturday, 31 December 2022, 11:01 PM)

In reply to Bob Gilmore

Re: How to hide topic-0 in Moodle 4

by Finlay Wright -
I have had some success with this css-only solution. Not sure if the data attributes have been added since previous replies:

#section-0:not(#section-0[data-sectionreturnid='0']) {

  display: none;

}

Average of ratings: Useful (1)
In reply to Finlay Wright

Re: How to hide topic-0 in Moodle 4

by Bob Gilmore -
Picture of Particularly helpful Moodlers
Hi Findlay,

That's good! I dunno of the data attribute was their either, but me likey.

It's amusing however, that this entire issue seems to have been fully addressed for the 4.4 release. It's already working nicely on the qa site. However this will keep me going until it's live.
In reply to Bob Gilmore

Re: How to hide topic-0 in Moodle 4

by Melanie Scott -
Picture of Particularly helpful Moodlers
I will second the post about OneTopic. It is great. You could name the General tab Course Information or Syllabus or whatever. You can even have subtabs inside tabs, which is a little complicated but works if that's what you need.

I really liked Grid in 3.x but it wasn't functional in 4 when we upgraded (this may no longer be true) so I installed Tile format. I like it a lot, except for the fact that every time the page loads it helpfully asks if I want it to remember my preferences. I think it does this to everyone. It may be a beta version. If I say yes, it doesn't. If I say no, it doesn't and nothing happens. Other that that, it is just as terrific as Grid. Now that I look at one of my Tiles courses, it hasn't done that today. We did some updating on plugins recently...maybe it's fixed.