publish an assignment file

publish an assignment file

by good news -
Number of replies: 6
Hello,
I want to extend the assign plugin to publish selected assignment files to the course page in the same section as the assignment.
Could you please tell me how?
Average of ratings: -
In reply to good news

Re: publish an assignment file

by Peter Diedrichs -
Picture of Particularly helpful Moodlers
The Student folder plugin should be good for this, https://moodle.org/plugins/mod_publication
In reply to Peter Diedrichs

Re: publish an assignment file

by good news -
I want to add the assignment file in the course section not in the student folder. Also, the student folder files do not have tags which are necessary for me.

In order to publish an assignment file in the coresponding course section, I tried to change the table mdl_context and mdl_files so that mdl_files.component=mod_resource, mdl_files.filearea=content, mdl_context.contextlevel=50, mdl_context.instanceid=courseid. But I got an error that the (50, courseid) is duplicated. So what should I do? should I also change mdl_context.path, and mdl_course_modules.* ??
In reply to good news

Re: publish an assignment file

by Mary Cooch -
Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators
Could you explain more what you need? You say you want to display various assignment files in the same course section as the assignment? Are these files students have submitted that you want to show as examples? Or files they need to help them do their assignment? Is it not useful to add them to the assignment itself in the 'Additional files" section? Or does it not work to upload them as file resources?
In reply to Mary Cooch

Re: publish an assignment file

by good news -
I want to convert some assignment files (such that good answers of the assignment) to file resources.
I wrote the following code but it does not work:

<?php
require_once('../../config.php');
require_once($CFG->dirroot . '/course/lib.php');

// Get file ID from POST data.
$file_id = required_param('fileid', PARAM_INT);

// Load file information.
$myfile = $DB->get_record('files', array('id' => $file_id), '*', MUST_EXIST);

// Determine the course and section to add the file to.
$context = $DB->get_record('context', array('id' => $myfile->contextid), '*', MUST_EXIST);

$assignid = $context->instanceid;

$course_module = $DB->get_record('course_modules', array('id' => $assignid), '*', MUST_EXIST);

$course_id = $course_module->course; // Replace YOUR_SOURCE_COURSE_ID with the actual source course ID
$section_id = $course_module->section; // Replace YOUR_SOURCE_SECTION_ID with the actual source section ID

// Load source section
$source_section = $DB->get_record('course_sections', array('id' => $section_id), '*', MUST_EXIST);

// Define the assignment file ID and the course section ID
$course_section_id = $section_id; // Replace with the actual course section ID

// Retrieve details of the assignment file
$assignment_file = $DB->get_record('files', array('id' => $file_id), '*', MUST_EXIST);

// Create a new file resource
$new_resource = new stdClass();
$new_resource->course = $course_id;
$new_resource->section = $course_section_id;
$new_resource->module = $DB->get_field('modules', 'id', array('name' => 'resource')); // Get the ID of the resource module
$new_resource->modulename = 'resource';
$new_resource->instance = 0; // For a new resource, set instance to 0
$new_resource_id = $DB->insert_record('course_modules', $new_resource);

// Upload the assignment file to the new file resource
$file = new stdClass();
$file->contextid = context_module::instance($new_resource_id)->id;
$file->component = 'mod_resource';
$file->filearea = 'content';
$file->itemid = 0;
$file->filepath = '/';
$file->filename = $assignment_file->filename;
$file->userid = $USER->id;
$file->filesize = $assignment_file->filesize;
$file->mimetype = $assignment_file->mimetype;
$file->status = 0; // File status
$file->source = 'file';

$fs = get_file_storage();
$new_file = $fs->create_file_from_storedfile($file, $file_id);

// Associate the file resource with the course section
$DB->insert_record('course_sections', $new_resource);

// Output the new resource ID if needed
echo "New resource ID: " . $new_resource_id;

In reply to good news

Re: publish an assignment file

by Mary Cooch -
Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators
I'm going to move your discussion to the General developer forum - no need to do anything - but go there to find it again.