How can i get all activies for a course?

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

by Black Dream -
Number of replies: 8

Hello,

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

 

Thank you smile

Average of ratings: Useful (1)
In reply to Black Dream

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

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
get_fast_modinfo()
Average of ratings: Useful (1)
In reply to Davo Smith

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

by Black Dream -

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.

 

 

 

 

In reply to Black Dream

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

by Drew Smith -

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?

In reply to Drew Smith

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

by Darko Miletić -

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; 
  }
}
In reply to Drew Smith

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

by Darko Miletić -

Also a more simpler version:

$list = get_array_of_activities($courseid);
// to see the structure
var_dump($list);
Average of ratings: Useful (1)
In reply to Darko Miletić

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

by Davo Smith -
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)
In reply to Black Dream

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

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@"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.wink