Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-68233

Code in renderers should not refer to global $PAGE or global $OUTPUT

    XMLWordPrintable

    Details

    • Testing Instructions:
      Hide

      I don't think there is useful manual testing to do here. The only thing is to ensure this does not introduce regressions, and that is what the automated tests are for.

      Show
      I don't think there is useful manual testing to do here. The only thing is to ensure this does not introduce regressions, and that is what the automated tests are for.
    • Affected Branches:
      MOODLE_37_STABLE, MOODLE_38_STABLE
    • Fixed Branches:
      MOODLE_39_STABLE
    • Pull Master Branch:

      Description

      Code in a renderer should never refer to global $PAGE. The page this renderer is rendering should be accessed as $this->page.

      Similarly, never refer to global $OUTPUT. If you need to access the core renderer from a plugin renderer, use $this->output.

      99% of the time this does not matter, because each script is only rendering its own output. However, in theory it should be possible to write a script like to archive all quiz results:

      foreach ($quizattempts as $attempt) {
          $reviewpage = new moodle_page();
          // Setup $reviewpage;
          $output = $reviewpage->get_renderer('mod_quiz');
          $html = $output->render_review_page($attempt);
          // Store $html in a zip file.
      }
      

      If renderers refer to the global variables, this breaks .

      We really ought to have a CodeChecker rule to catch people making this mistake, since it is clearly very easy to do.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              timhunt Tim Hunt
              Reporter:
              timhunt Tim Hunt
              Peer reviewer:
              Eloy Lafuente (stronk7)
              Integrator:
              Sara Arjona (@sarjona)
              Tester:
              CiBoT
              Participants:
              Component watchers:
              Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Sara Arjona (@sarjona), Víctor Déniz Falcón, Bas Brands
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Fix Release Date:
                15/Jun/20

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 3 hours, 5 minutes
                  3h 5m