File not found after adding recource programmatically

File not found after adding recource programmatically

Sven Milde-mit -
Antal besvarelser: 0

Hey,

I'm trying to create a file from string and add this file to a course as a resource. After the task has run, the filename is shown in the course, but when I click it, it says 'file not found'. Following my code for this. 

During execution there are no other errors or warning. Any hints why this is not working are very welcome.


$course = $DB->get_record('course', array('id'=> 9));

$csv = 'This is my string in csv format';

 $modulename = 'resource';

        

  require_once($CFG->dirroot . '/course/modlib.php');

   list($module, $context, $cw, $cm, $data2) = prepare_new_moduleinfo_data($course, $modulename, 1);

   $context = \context_module::instance(add_course_module($data2));

   $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' => $draftitemid,

            'filepath' => '/',

            'filename' => $filename.'.csv',

            'source' => 'disea_consent'

   );

        

   $f = $fs->create_file_from_string($filerecord, $csv);

   $file = $fs->get_area_files($context->id, $component, $filearea, 0, $itemid, false);

   $file = reset($file);

        

   $uploadinfo = new \stdClass();

   $uploadinfo->type = 'Files';

   $uploadinfo->course = $course;

   $uploadinfo->section = 1;

   $uploadinfo->module = $module->id;

   $uploadinfo->modulename= $module->name;

   $uploadinfo->files=$draftitemid;

   $uploadinfo->displayname = 'Log-'.$filename.'.csv';

       

   $data3 = new \stdClass();

   $data3->course = $uploadinfo->course->id;

   $data3->name = $uploadinfo->displayname;

   $data3->intro = '';

   $data3->introformat = FORMAT_HTML;

   $data3->section = $uploadinfo->section;

   $data3->module =$uploadinfo->module;

   $data3->modulename =$uploadinfo->modulename;

   $data3->add ='resource';

   $data3->return = 0;

   $data3->sr = 0;

   $data3->files = $uploadinfo->files;

   $data3->visible=1;

   

   add_moduleinfo($data3, $uploadinfo->course);

Gennemsnitsbedømmelse: -