Access uploaded file from Moodle activity

Access uploaded file from Moodle activity

by Jamie McGowan -
Number of replies: 0

I am developing an activity module plugin for Moodle that allows the user to upload a plain text file. The contents of the file will be visible (but marked up/manipulated) when the activity is viewed. I am having trouble finding the location of the file and getting its contents. I have read through the APIs and searched here but can't find anything.

In my mod_form.php I have a file_manager that handles the choosing and uploading of the file (a single file per activity). Then in lib.php I have the following:

function myactivity_add_instance(stdClass $myactivity, mod_myactivity_mod_form $mform = null) {
    global $CFG, $DB, $USER;
    require_once("$CFG->dirroot/mod/myactivity/locallib.php");
    $myactivity->timecreated = time();
    $myactivity->id = $DB->insert_record('myactivity', $myactivity);

    myactivity_grade_item_update($myactivity);

    $fs = get_file_storage();
    $cmid = $data->coursemodule;
    $draftitemid = $data->files;
    $context = context_module::instance($cmid);
    if ($draftitemid) {
        file_save_draft_area_files($draftitemid, $context->id, 'mod_myactivity', 'content', 0, array('subdirs'=>true));
    }
    $files = $fs->get_area_files($context->id, 'mod_myactivity', 'content', 0, 'sortorder', false);
    if (count($files) == 1) {
        $file = reset($files);
        file_set_sortorder($context->id, 'mod_myactivity', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
    }
    return $myactivity->id;
}

[Some of this code is taken directly from \mod\resource\locallib.php]

From looking through the database and browsing the file system I can see that the files do get saved. But I don't know how to programmatically find out the location of the file. Specifically I want to save the file name and file location in a database and render the contents of the file in view.php.

Thanks for any help!

Average of ratings: -