Hello Davo,
I am experiencing the same problems as Anna is facing. Basically, I am almost done with the activity plugin that I'm developing. The challenge that I'm facing is on saving the files and then fetching it back to be displayed in the ***/mod/pluginname/view.php page. I have no clue on how I can handle this. I've read the documentation on the File API and Forms, as well as studying other moodle plugin code e.g. Video plugin, but somehow I'm coming incredibly short. I'm seeing entries in the DB, both plugin table as well as the mdl_files table. But I cant seem to be able to get the file uploaded. Please see my code and show me where I'm going wrong:
MOD_FORM.PHP
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once("$CFG->libdir/formslib.php");
require_once($CFG->libdir . '/filelib.php');
require_once(dirname(__FILE__) . '/lib.php');
class mod_audio_mod_form extends moodleform_mod {
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('header', 'general', get_string('general', 'audio'));
$mform->addElement('text', 'name', get_string('audioname', 'audio'), array('size' => '64'));
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'audioname', 'audio');
$mform->setDefault('name', 'Audio file name');
// Adding the standard "intro" and "introformat" fields.
if ($CFG->branch >= 29) {
$this->standard_intro_elements();
} else {
$this->add_intro_editor();
}
$mform->addElement('filepicker', 'audiofile', get_string('audiofile', 'audio'), null, array('maxbytes' => $CFG->maxbytes, 'accepted_types' => 'audio'));
$this->standard_grading_coursemodule_elements();
// Add standard elements, common to all modules.
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
AND THEN IN THE LIB.PHP, I HAVE THIS:
function audio_add_instance(stdClass $audio, mod_audio_mod_form $mform = null) {
global $DB;
$audio->timecreated = time();
$context = context_module::instance($data->coursemodule);
// I may have to add extra stuff in here.
$audio->id = $DB->insert_record('audio', $audio);
audio_grade_item_update($audio);
return $audio->id;
}
function audio_save_draft_area_files($data) {
$context = context_module::instance($data->coursemodule);
if (isset($data->attachments)) {
file_save_draft_area_files($data->attachments, $context->id, 'mod_audio', 'attachments', 0, array('subdirs' => 0, 'maxbytes' => $CFG->maxbytes, 'maxfiles' => 50));
}
}