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

Library: API to get cm_info more easily

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.8
    • Fix Version/s: 2.8
    • Component/s: Libraries
    • Testing Instructions:
      Hide

      Please run unit tests, specifically:

      vendor/bin/phpunit lib/tests/modinfolib_test.php

      There is no actual code using these functions yet, they are intended for new development, so you can't test in the interface.

      Show
      Please run unit tests, specifically: vendor/bin/phpunit lib/tests/modinfolib_test.php There is no actual code using these functions yet, they are intended for new development, so you can't test in the interface.
    • Affected Branches:
      MOODLE_28_STABLE
    • Fixed Branches:
      MOODLE_28_STABLE
    • Pull Master Branch:
      MDL-46706-master

      Description

      In Moodle, $cm objects are sometimes stdClass and sometimes cm_info. The cm_info objects are required in certain places, and have performance advantages.

      The main way to get a cm_info object is like this:

      $modinfo = get_fast_modinfo($course);
      $cm = $modinfo->get_cm($cmid);
      

      As you can see, a course is required as well as a cmid. In the common situation where you only have a cmid, it requires some SQL-query gymnastics to do this without making unnecessary queries.

      This compares to the nice simple function for getting stdclass-type $cm:

      $cm = get_coursemodule_from_id('assign', $id, 0, false, MUST_EXIST);
      

      I would like to implement two new utility functions:

      1) An efficient function that returns $cm and $course, given a $cmid (and optionally checking the module type also).

      2) A convenience function cm_info::from_mixed($cm) that returns a cm_info based on an existing $cm object that might be any type. (This will not be as efficient as ensuring that the $cm object was right first time round, but realistically, is needed in some places.)

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                quen Sam Marshall
                Reporter:
                quen Sam Marshall
                Peer reviewer:
                Marina Glancy
                Integrator:
                Damyon Wiese
                Tester:
                Frédéric Massart
                Participants:
                Component watchers:
                Amaia Anabitarte, Bas Brands, Carlos Escobedo, Sara Arjona (@sarjona), Víctor Déniz Falcón
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  10/Nov/14