## General developer forum

### List of resources

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
List of resources

Hi there,I have currently developed a script which counts the number of resources in each course, i do this by checking for the course id in the URL, resource, quiz labels table.  In the labels table i only check labels that contain embedded video clips. I add the amount found in each table for a course together to get the overall resources number.

What i would like to know is, where can i find images which have been uploaded to a course or images which are within a folder inside a course?  I have seen a database table on there which holds the files which are on Moodle but there is no way of linking what images to a specific course id.

I have been gathering information in the following type format so far but cannot see how i could do it for getting the total amount of images for a course:

$DB->count_records('resource', array('course'=>$allcourse->id));

Many thanks,

Mark

Average of ratings: -
Re: List of resources

You might want to start by investigating the function get_fast_modinfo - as that will give you all the information about the modules in a particular course (without the need for lots of database queries).

var_dump(get_fast_modinfo($courseid)); // This will show what information is included The images (and all other files) are not stored in the 'course' they are stored within each activity in the course. To find the files for each activity you should use the files API ( http://docs.moodle.org/dev/File_API ) or, if you are trying to search for lots of files at once, you could look directly in the mdl_files table - the contextid will be based on the context for each activity/resource in the course (which may be directly in get_fast_modinfo, otherwise you'll need to call context_module::instance($cmid) for each one), after that you'll need to search based on the mimetype of the records, if you want to find all the images.

Average of ratings: -
Re: List of resources

Hi,

I did try that but it just comes up as:

Fatal error: Cannot pass parameter 1 by reference in (filename here) <-deleted the filename as it included a path

Basically i tried var_dump(get_fast_modinfo($courseid)); and that is what it said when i gave it$courseid of 17 which is a course on my server.

This need a specific library to run?

Mark

Average of ratings: -
Re: List of resources

OK then, try:

$modinfo = get_fast_modinfo($courseid);

var_dump($modinfo); Average of ratings: - Re: List of resources Hi there, I tried what you said and cannot see any options to do what i want. I may look more into the resources.php file but looks like a long winded process and so i may just stick to what i have for the moment. The get_fast_modinfo brings up the following results for the course which has a course id of 17: object(course_modinfo)#457 (7) { ["course":"course_modinfo":private]=> int(17) ["courseid"]=> NULL ["userid"]=> string(1) "3" ["sections"]=> array(0) { } ["cms"]=> array(0) { } ["instances"]=> array(0) { } ["groups"]=> NULL } Many thanks for your suggestion, it was a good trick for me to try Mark Average of ratings: - Re: List of resources Actually, for very little effort (and, at most 1 DB query - you could get it down to 0 DB queries if you pass it an existing course object), you now know that course id 17 has no sections or activities in it. Average of ratings: - Re: List of resources Hi there, Yes you are quite correct it does show that it has no sections or activities. My course which has ID of 17 actually has 8 sections and has some quizzes, PowerPoint presentations, PDF's and some images which do not display with the modinfo method. Do you know a quick way to display these other than how i am currently doing it? Many thanks, Mark Average of ratings: - Re: List of resources Something very strange is happening then - get_fast_modinfo is used by the course page to get all the information needed to display the course contents. If it did not return that information then the course would be empty. Have you tried:$course = $DB->get_record('course', array('id' => 17));$modinfo = get_fast_modinfo($course); var_dump($modinfo);

Average of ratings: -
Re: List of resources

Starting to get there now.  Ok so i have done a var_dump on modinfo for the course and it is now working after linking to the database.

What i now wish to ask is the following.  When i do a var_dump i get information about the course such as:

[cms] => Array ( [1457] => cm_info Object ( [modinfo:cm_info:private] => course_modinfo Object *RECURSION* [state:cm_info:private] => 1 [id] => 1457 [instance] => 67 [course] => 17 [idnumber] => [added] => 1343043762 [score] => 0 [visible] => 0 [visibleold] => 1 [groupmode] => 0 [groupingid] => 0 [groupmembersonly] => 0 [indent] => 0 [completion] => 1 [completiongradeitemnumber] => [completionview] => 0 [completionexpected] => 0 [availablefrom] => 0 [availableuntil] => 0 [showavailability] => 1 [extra] => [icon] => f/web [iconcomponent] => [modname] => url [module] => 17 [name] => Moodle tutorial links - Information for staff [sectionnum] => 0 [section] => 48 [conditionscompletion] => Array [conditionsgrade] => Array [modplural] => URLs [available] => 1

As you can see from above i have highlighted the relevant code which i am after looking for, so the code above shows a resource available which is a URL.  the problem is that towards the end of the page it again shows this very same information as though it shows it all twice:

cm_info Object ( [modinfo:cm_info:private] => course_modinfo Object *RECURSION* [state:cm_info:private] => 1 [id] => 1457 [instance] => 67 [course] => 17 [idnumber] => [added] => 1343043762 [score] => 0 [visible] => 0 [visibleold] => 1 [groupmode] => 0 [groupingid] => 0 [groupmembersonly] => 0 [indent] => 0 [completion] => 1 [completiongradeitemnumber] => [completionview] => 0 [completionexpected] => 0 [availablefrom] => 0 [availableuntil] => 0 [showavailability] => 1 [extra] => [icon] => f/web [iconcomponent] => [modname] => url [module] => 17 [name] => Moodle tutorial links - Information for staff [sectionnum] => 0 [section] => 48

As you can see, i have again highlighted the relevent information.  What i was going to do was to search the whole modinfo as a string and find the different modnames and add them all together e.g. total number of url, quiz, file etc.  If the information is being repeated twice then i will need to get around this.  Do you have any idea as to

A. why it displays the information once and then again towards end of the var_dump and

B. how i could potentially search all [modname] to get the value from within it?  At the moment it would show each thing twice if i did a search on the whole data as var_dump seems to show information twice.

Once i know how to solve this i should then be ok.

Mark

Average of ratings: -
Re: List of resources

You may find it clearer to view the output via 'view source' in your browser, as it is easier to see the structure if you do that.

The raw data dump from get_fast_modinfo contains a number of top-level arrays (look in lib/modinfolib.php for more information about the course_modinfo object):

• sections - array of section number => array of cmids
• cms - array of cmid => module details (cm_info)
• instances - array of modulename => array of instanceid => module details (cm_info)

So, depending on how you want to access the activity details, the details of each module are listed twice - once indexed by cmid (i.e. the ID of the table mdl_course_modules) and once indexed by module name and instanceid (e.g. 'forum' => ID of the table mdl_forum, 'choice' => ID of the table mdl_choice).

Note the use of the get_instances, get_instances_of, etc. functions in lib/modinfolib.php (and, as an aside, have you got debugging displayed on your site? I got the code a bit wrong in my first post and that should have output a clear warning message when you ran it)

Average of ratings: -