Private files plugin

Private files plugin

by Fuchur Kool -
Number of replies: 12

Hi,

I am trying to implement a plugin which shows users' private files within the app. For this, I want to make use of the MM.moodleDownloadFile function. So far, I have identified that the page with all the files can be reached the following way:

MM.config.current_site.siteurl + "/user/files.php"

But is there any way to get a list of the included element? And how is the link created for every object?

I also had a look at the "core_files_get_files " webservice, but it also needs a filepath.

Average of ratings: -
In reply to Fuchur Kool

Re: Private files plugin

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

For downloading files you need to use allways an url like:

MM.config.current_site.siteurl + "webservice/pluginfile.php/path/to/the/file?token=" + MM.config.current_token

But you need a webservice for listing all the files a user has in his private file area

In reply to Juan Leyva

Re: Private files plugin

by Fuchur Kool -

Hi Juan,

Thanks for the response, I already figured that out and started to implement a webservice like that. I wonder if there is any documentation on how to query the moodle DB? I haven't found anything about that so far..

In reply to Fuchur Kool

Re: Private files plugin

by Corey Wallis -

Hi,

This page in the Moodle Developer Documentation website covers all of the ways to query the database

http://docs.moodle.org/dev/Data_manipulation_API

Hope this helps. 

-Corey

 

In reply to Corey Wallis

Re: Private files plugin

by Fuchur Kool -

Hi,

Thanks for the link!

 

I'm trying to execute the following code, but unfortunatelly it doesn't work. 

 

global $USER, $DB;

$result = $DB->get_records_sql("SELECT * FROM {files} WHERE userid=:userId", array('userId' = 3));

 

Any idea why?

 

I also tried $DB->set_debug(true); but I don't see any error messages..

In reply to Fuchur Kool

Re: Private files plugin

by Corey Wallis -

Hi,

Without details of any error message, it's had to see what is going wrong.  

if it were me, I'd have written the something like this:

global $USER, $DB;
$records = $DB->get_records('files', array('userid' => $USER->id));

foreach ($records as $record) {
// do something with the records
}

Or, if I expected may results to be returned I'd use the result set version:

global $USER, $DB;
$rs = $DB->get_recordset('files', array('userid' => $USER->id));

if ($rs->is_valid()) {
foreach ($rs as $record) {
// do something with the records
}
}

Hope this helps. 

-Corey

In reply to Corey Wallis

Re: Private files plugin

by Fuchur Kool -

Hi,

Thanks for your post.

 

This worked for me:

 

		$results = $DB->get_records_sql("SELECT id, contextid, component, component, filearea, itemid, filepath, filename, filesize, mimetype FROM {files} WHERE userid=" . $USER->id . " GROUP BY filename");
		
		$myfiles = array();
		
		foreach ($results as $result){
			$file = new stdClass();
			$file->id = (int) $result->id;
			$file->contextid = (int) $result->contextid;
			$file->component = $result->component;
			$file->filearea = $result->filearea;
			$file->itemid = (int) $result->itemid;
			$file->filepath = $result->filepath;
			$file->filename = $result->filename;
			$file->filesize = (int) $result->filesize;
			$file->mimetype = $result->mimetype;
			$myfiles[] = (array) $file;
		}

But, how can I see error msgs? I have set $DB->debug(true); but I didn't see any errors...
In reply to Fuchur Kool

Re: Private files plugin

by Fuchur Kool -

Is it possible to generate a token for my webservice "on the fly"?

 

Because now I have to generate an own token for every single user, so he/she can use it. This is not a good process..

 

And when I create a token for a basic user i get the following notification of missing permissions:

 

moodle/user:viewdetails, moodle/user:viewhiddendetails, moodle/course:useremail, moodle/user:update, moodle/site:accessallgroups, moodle/site:viewparticipants, moodle/course:viewparticipants, moodle/role:review, moodle/course:enrolreview, moodle/course:update, moodle/course:viewhiddencourses, moodle/site:sendmessage, moodle/notes:manage, moodle/calendar:manageentries

In reply to Fuchur Kool

Re: Private files plugin

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi,

see login/token.php, but the user needs to authenticate to generate a token

Regards

In reply to Juan Leyva

Re: Private files plugin

by Fuchur Kool -

Hi,

MM.config.current_site.siteurl + "webservice/pluginfile.php/path/to/the/file?token=" + MM.config.current_token doesn't work for files which have filearea=draft. 

1) How can I change the filearea?

2) Is there any way to download / show files with filearea = draft?

3) How can I show the file within the app? <a href="..." target=_blank> ? 


Thanks


In reply to Fuchur Kool

Re: Private files plugin

by Dave Perry -
Picture of Testers

I might be missing something here, but isn't the point of draft that the user has to confirm saving the files is ok before they're accessible beyond them being on the upload files form? That might be why it doesn't show them (by default by the sounds of it, I've not dealt that deep into the programming side).

In reply to Dave Perry

Re: Private files plugin

by Fuchur Kool -

So how can I change the state from "draft" to "private" ?

In reply to Fuchur Kool

Re: Private files plugin

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi,

what I think is that you shouldn't display "draft" files in the Mobile app, since as their name indicates are "draft" files that for some reason are in that state in the file system.

Regards