General developer forum

 
 
Picture of Joseph Rézeau
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful MoodlersGroup TestersGroup Translators

 
Average of ratings: -
Picture of Black Dream
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 smile

 
Average of ratings:Useful (1)
Davo
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful Moodlers
get_fast_modinfo()
 
Average of ratings: -
Picture of Black Dream
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: -
Davo
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful Moodlers

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: -
Picture of Drew Smith
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: -
Picture of Darko Miletić
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful Moodlers

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: -
Picture of Darko Miletić
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful Moodlers

Also a more simpler version:

$list = get_array_of_activities($courseid);
// to see the structure
var_dump($list);
 
Average of ratings: -
Davo
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful Moodlers

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: -
Picture of Joseph Rézeau
Re: How can i get all activies for a course?
Group DevelopersGroup Particularly helpful MoodlersGroup TestersGroup 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

 
Average of ratings: -