## General developer forum

### How can i get all activies for a course?

How can i get all activies for a course?

Hello,

I want to make a block to get all the activities for a course. When i am saying activities i need these http://docs.moodle.org/24/en/Activities.

For example a simple output would be:

* COURSE NAME
- Assignments
+ Assignment 1
+ Assignment 2
- Quiz
+ Quiz 1
+ Quiz 2

- Wiki
+ ....
+ ....
- Lesson
+ ....

ETC ETC. I am sure that there are already some functions out there that doing these jobs but seems that i can't find them.

Thank you

Average of ratings: -
Re: How can i get all activies for a course?

Average of ratings: -
Re: How can i get all activies for a course?

Hello,

I want the functions in PHP. I don't know what is this you showed me above.

Thank you

Average of ratings: Useful (1)
Re: How can i get all activies for a course?
get_fast_modinfo()

Average of ratings: -
Re: How can i get all activies for a course?

Hello,

Yes it seems that this is the function i want. I used it like this:

$info = get_fast_modinfo($theCourse);

And the output was

 course_modinfo Object( [course:course_modinfo:private] => stdClass Object ( [id] => 2 [category] => 1.........................

The question is how i can get inside the object course:course_modinfo:private ???

I tried $info->{'course:course_modinfo'} but doesnt work. Average of ratings: - Re: How can i get all activies for a course? You could always try looking at some of the functions included in the class course_modinfo - https://github.com/moodle/moodle/blob/master/lib/modinfolib.php Or, possibly, search through the Moodle core code for examples of where get_fast_modinfo is already in use ... Average of ratings: - Re: How can i get all activies for a course? I'm encountering the same issue. I'm at a loss at how to burrow down into course_modinfo. Were you able to come up with a solution? Average of ratings: - Re: How can i get all activies for a course? Use get_course_mods API. require ("/path/to/config.php"); global$USER, $DB;$mods = get_course_mods($courseid); foreach($mods as $cm) { if (coursemodule_visible_for_user($cm, $USER->id)) {$modrec = $DB->get_record($cm->modname, array('id' => $cm->instance)); echo$cm->modname, " : ", $modrec->fullname, PHP_EOL; } }  Average of ratings: - Re: How can i get all activies for a course? Also a more simpler version: $list = get_array_of_activities($courseid); // to see the structure var_dump($list);


Average of ratings: Useful (1)
Re: How can i get all activies for a course?

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: -
Re: How can i get all activies for a course?

@"Black Dream", What I showed you is one of the standard "blocks" that you can add to any course in Moodle. It seems that you are wanting to re-invent the wheel.

Average of ratings: -