bmbrands / moodle-local_analytics

A Local Moodle Plugin adding Site Analytics
10 stars 23 forks source link

JavaScript error on course section page when edit mode enabled #40

Open leonstr opened 2 months ago

leonstr commented 2 months ago

On Moodle 4.4 if you:

  1. create a course and enrol and editing teacher;
  2. log in as the editing teacher;
  3. go to the course page;
  4. click on a section title to go to a course section page (course/section.php?id=<ID>);
  5. enable editing mode,

then the page is broken, for example, the left-hand navigation drawer does not load and the user menu in the top right does not open when clicked. The browser console shows '' string literal contains an unescaped line break.

Moodle 4.4.2 (Build: 20240812) with local_analytics 2017032702.

leonstr commented 2 months ago

For Google Analytics local_analytics populates the 'title' property with format_string($PAGE->heading). With edit mode enabled for a course section page $PAGE->heading is:

<div data-for="section_title"><span class="inplaceeditable inplaceeditable-text" data-inplaceeditable="1" data-component="format_topics" data-itemtype="sectionnamenl" data-itemid="3"
    data-value="" data-editlabel="New name for section New section" data-type="text" data-options="">

        <a href="#" class="quickeditlink aalink" data-inplaceeditablelink="1" title="Edit section name">
            New section
            <span class="quickediticon visibleifjs icon-size-3">
                <i class="icon fa fa-pencil fa-fw "  title="Edit section name" role="img" aria-label="Edit section name"></i>
            </span>
        </a>
</span></div>

format_string() strips the tags but leaves the line breaks making this:


            New section

Obviously wrapping the above in single quotes is not a valid JavaScript string value, thus the syntax error.

leonstr commented 2 months ago

PR with proposed fix added. An alternative approach would be to use trim() to remove $PAGE->heading's newlines by changing classes/api/guniversal.php line 55 to:

                'title' : '".addslashes(trim(format_string($PAGE->heading)))."'