General developer forum

file not found

 
Picture of A. Obeid
file not found
Particularly helpful Moodlers

Hi

I am trying to add resource from another System to moodle. I get the file contents from $url/Api.

Scripts are in a local plugin.

$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$type = 'Files';
$modulename = 'resource';
 list($module, $context, $cw, $cm, $cdata) = prepare_new_moduleinfo_data($course, $modulename, $section);
$context = context_module::instance(add_course_module($cdata));
 $component = 'mod_resource'; $filearea = 'content'; $itemid=0; $draftitemid = file_get_submitted_draft_itemid($filearea); file_prepare_draft_area($draftitemid, $context->id, $component, $filearea, $itemid); $fs = get_file_storage(); $filerecord = array( 'contextid' => $context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/', 'filename'=>$filename); $urlparams = array( 'calctimeout' => false, 'timeout' => 5, 'skipcertverify' => true, 'connecttimeout' => 5 ); try { $fs->create_file_from_url($filerecord, $url,$urlparams, array('Content-disposition'=> 'attachment', 'filename'=>$filename), true ); } catch (\file_exception $e) { return get_string($e->errorcode, $e->module, $e->a); } $file =$fs->get_area_files($context->id, $component, $filearea, 0, 'itemid', false); $file = reset($file );// file is there. $uploadinfo = new stdClass(); $uploadinfo->type = 'Files'; $uploadinfo->course = $course; $uploadinfo->section = $section; $uploadinfo->module = $module->id; $uploadinfo->modulename= $module->name; $uploadinfo->files=$draftitemid; $uploadinfo->displayname = $name; add_importedmaterial($uploadinfo, $context);
function add_importedmaterial($uploadinfo, $context){
    $data = new stdClass();
    $data->course = $uploadinfo->course->id;
    $data->name = $uploadinfo->displayname;
    $data->intro = '';
    $data->introformat = FORMAT_HTML;
    $data->section = $uploadinfo->section;
    $data->module =$uploadinfo->module;
    $data->modulename =$uploadinfo->modulename;
    $data->add ='resource';
    $data->return = 0;
    $data->sr = 0;    
    $data->files = $uploadinfo->files; 
$data->visible=1;
// Set the display options to the site defaults.
add_moduleinfo($data, $uploadinfo->course);

 // $fs = get_file_storage(); // $fs->delete_area_files($context->id, 'mod_resource', 'content'); }
Resource Instance is added but i get after clicking on it "File not found, sorry.".  In the files DB there is a 2 new entries

Any idea what I am doing wrong?
 
Average of ratings: -
Picture of A. Obeid
Re: file not found
Particularly helpful Moodlers

Solved

    $itemid=0;
    $fs = get_file_storage();   
    $draftcontext = context_user::instance($USER->id);
    $draftitemid = file_get_submitted_draft_itemid($draftcontext->id);   
    file_prepare_draft_area($draftitemid, $draftcontext->id, 'user', 'draft', $itemid);        
    $filerecord = array(
        'contextid' => $draftcontext->id, 
        'component' => 'user', 
        'filearea' => 'draft',
        'itemid' => $draftitemid,
        'filepath' => '/',
        'filename'=>$filename);
    $urlparams = array(
            'calctimeout' => false,
            'timeout' => 5,
            'skipcertverify' => true,
            'connecttimeout' => 5
        );
    try....
 
Average of ratings: -