Context Id in mform

Context Id in mform

by Pedro Remedios -
Number of replies: 3
Hi,

When I store a file during record saving I would use $mform->get_context()->id to pass the contextid to save_stored_file(). When I want to delete that stored file, I don't have access to $mform. Which context level was being used when I was passing it to save_stored_file()?


Thanks,

Pedro

Average of ratings: -
In reply to Pedro Remedios

Re: Context Id in mform

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Pedro,

The moodleform base class doesn't have a get_context() method of its own, am I correct in thinking you added this method to your form's class?  If you can share that code, we can see which context you used.

In reply to Mark Johnson

Re: Context Id in mform

by Pedro Remedios -

Here is the entire function:

function telemedia_add_instance(stdClass $telemedia, mod_telemedia_mod_form $mform = null) {
    global $DB;
    require_once(__DIR__ . '/locallib.php');

    $telemedia->timecreated = time();
    $telemedia->timemodified = time();

    $formdata = $mform->get_submitted_data();

    $config = get_config('telemedia');

    $videofilename = $mform->get_new_filename('videofile') != '' ? $mform->get_new_filename('videofile') : '';
    $audiofilename = $mform->get_new_filename('audiofile') != '' ? $mform->get_new_filename('audiofile') : '';

    $telemedia->videofile = '';
    $telemedia->audiofile = '';

    if ($telemedia->contentdelivery == DELIVERY_CONTENT_PROVIDER_FILE) {
        // Video file
        $telemedia->videofile = $videofilename;
        //$draftfileid = file_get_submitted_draft_itemid('videofile');

        //file_prepare_draft_area($draftfileid, $mform->get_context()->id, 'telemedia, ', 'video', $telemedia->id);
        //file_save_draft_area_files($draftfileid, $mform->get_context()->id,'telemedia','video', $telemedia->id);

        // Audio file
        $telemedia->audiofile = $audiofilename;
        //$draftfileid = $telemedia->audiofile;
        //file_prepare_draft_area($draftfileid, $mform->get_context()->id,'telemedia','audio',$telemedia->id);
        //file_save_draft_area_files($draftfileid, $mform->get_context()->id, 'telemedia', 'audio', $telemedia->id);
    } else {
        $telemedia->url = $config->baseurl.'/musica?tituloaula='.urlencode($formdata->contentviaurl_classname).'&nomeprof='.urlencode($formdata->contentviaurl_teachername);
    }
    // You may have to add extra stuff in here.
    $telemedia->id = $DB->insert_record( 'telemedia', $telemedia );

    if ($telemedia->contentdelivery == DELIVERY_CONTENT_PROVIDER_FILE) {
        if ($videofilename) {
            $mform->save_stored_file('videofile', $mform->get_context()->id, 'telemedia', 'video', $telemedia->id);
        }

        if ($audiofilename) {
            $mform->save_stored_file('audiofile', $mform->get_context()->id, 'telemedia', 'audio', $telemedia->id);;
        }
    }

    //telemedia_grade_item_update( $telemedia );
    return $telemedia->id;
}

In reply to Pedro Remedios

Re: Context Id in mform

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Thanks Pedro,

I was looking at the wrong base class. moodleform_mod does have its own get_context() method.

According to the comments, the context it returns is either the course or the module instance, depending on whether the course instance already exists.  Taking a quick look at the way mod_assign handles files, you might want to try doing context_module::instance($telemedia->coursemodule) instead, this will definitely give you the context of the module you just created.

When you're getting the file for deletion, you can get the context the same way, using the cmid.

Average of ratings: Useful (1)