How can i get all activies for a course?

Re: How can i get all activies for a course?

by Davo Smith -
Number of replies: 0
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Both get_array_of_activities and get_course_mods are to be avoided, if possible, as both require extra database queries in order to work (and only return limited data from particular database tables), whereas get_fast_modinfo generally accesses the data already in memory, or via the cache, before starting on any database queries (and contains lots of extra data about completion, availability, groupings, user visibility etc.).

Once you've called '$modinfo = get_fast_modinfo($courseobj)' you have a number of options (all documented in lib/modinfolib.php).

In your case, you can call $modinfo->get_used_module_names() to get a list of all the module types (in the form of an array [pluginname] => [human readable name] - e.g. assign => Assignment).

You can then loop through the 'pluginname' in the list and call $modinfo->get_instances_of($pluginname)

This will return an array containing cm_info objects representing each instance of that activity in the course.

cm_info objects are very helpful as they tell you everything you need to know about an activity, including:

  • the name/title of the instance, ready to output (->get_formatted_name() )
  • whether or not it is visible (->visible)
  • whether or not the current user can see it (->uservisible) - useful as admin can see activities which are not 'visible', information
  • information about availability, completion, groups, groupings etc.
  • a link to view the activity, for activities other than labels (->get_url() )
  • the content of the activity, for labels (->get_content() )
  • the icon to display (->get_icon_url() ) - very helpful as some activities, e.g. file resources, can display different icons depending on the embedded file type
  • lots of other stuff that you are less likely to use on a regular basis, but which it is worth reading about
Average of ratings: Useful (1)