I've been working on a mod plugin that is a UI for students. The teacher can upload a text file (it's presently going to a cloud service), and the plugin uses it to generate the interface. The interface is intended to be used by several different courses.
Can I use the File API to store and retrieve the text file "locally?"
I've gone through https://docs.moodle.org/dev/File_API#Create_file and being relatively new to Moodle (and development), it's left me with some questions about the API that were unclear to me.
Block 1: Create a file.
- From my limited understanding of moodle, the contextid would provide a unique element for any given file as the context would be specific to that particular interface and that particular course. Is this understanding correct?
- If a teacher was using the interface on 3 separate courses, their upload creating that interface would be unique to each course?
- As well, since I only want one text file for a specific course, can I hardcode in the itemid, so that the file gets replaced every time the teacher uploads a new one for that course?
- Finally, since there might be several files uploaded for several different courses, can I use a variable that takes in the user's original file name (or code that creates a unique one) to populate the filename?
$fs = get_file_storage(); // Prepare file record object $fileinfo = array( 'contextid' => $context->id, // ID of context 'component' => 'mod_mymodule', // usually = table name 'filearea' => 'myarea', // usually = table name 'itemid' => 0, // usually = ID of row in table 'filepath' => '/', // any path beginning and ending in / 'filename' => 'myfile.txt'); // any filename // Create file containing text 'hello world' $fs->create_file_from_string($fileinfo, 'hello world');
The filearea is also somewhat confusing to me. Looking at the snippet below, the filearea in this case would be "post."
For my purposes, could I make a folder mod/mymodule/interface_files to store the txt files (in filepath) and then call the 'filearea' value 'interface_files'?
$url = $CFG->wwwroot/pluginfile.php/$forumcontextid/mod_forum/post/$postid/image.jpg
The plugin needs to read the file every time the screen refreshes.
Can I use the Read File code, provided it's inside my mod/mymodule folder somewhere?
The moodle docs state "Please note you are allowed to do this ONLY from mod/mymodule/* code, it is not acceptable to do this anywhere else."
I'm assuming they mean the code must be inside the folder (say in my index.php file). Is this correct?
Block 2: Read a file:
$fs = get_file_storage(); // Prepare file record object $fileinfo = array( 'component' => 'mod_mymodule', // usually = table name 'filearea' => 'myarea', // usually = table name 'itemid' => 0, // usually = ID of row in table 'contextid' => $context->id, // ID of context 'filepath' => '/', // any path beginning and ending in / 'filename' => 'myfile.txt'); // any filename // Get file $file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); // Read contents if ($file) { $contents = $file->get_content(); } else { // file doesn't exist - do something }