MDL-25552 mod_lesson: changing filepicker element to filemanager to allow the deletion of a file

This commit is contained in:
Mark Nelson 2012-11-20 17:43:50 +08:00
parent 8ccaa296fa
commit 684c3be721
2 changed files with 46 additions and 23 deletions

View File

@ -43,6 +43,8 @@ function lesson_add_instance($data, $mform) {
global $DB;
$cmid = $data->coursemodule;
$draftitemid = $data->mediafile;
$context = context_module::instance($cmid);
lesson_process_pre_save($data);
@ -50,14 +52,9 @@ function lesson_add_instance($data, $mform) {
$lessonid = $DB->insert_record("lesson", $data);
$data->id = $lessonid;
$context = context_module::instance($cmid);
$lesson = $DB->get_record('lesson', array('id'=>$lessonid), '*', MUST_EXIST);
if ($filename = $mform->get_new_filename('mediafilepicker')) {
if ($file = $mform->save_stored_file('mediafilepicker', $context->id, 'mod_lesson', 'mediafile', 0, '/', $filename)) {
$DB->set_field('lesson', 'mediafile', '/'.$file->get_filename(), array('id'=>$lesson->id));
}
}
lesson_update_media_file($lessonid, $context, $draftitemid);
lesson_process_post_save($data);
@ -80,22 +77,15 @@ function lesson_update_instance($data, $mform) {
$data->id = $data->instance;
$cmid = $data->coursemodule;
$draftitemid = $data->mediafile;
$context = context_module::instance($cmid);
lesson_process_pre_save($data);
unset($data->mediafile);
$DB->update_record("lesson", $data);
$context = context_module::instance($cmid);
if ($filename = $mform->get_new_filename('mediafilepicker')) {
if ($file = $mform->save_stored_file('mediafilepicker', $context->id, 'mod_lesson', 'mediafile', 0, '/', $filename, true)) {
$DB->set_field('lesson', 'mediafile', '/'.$file->get_filename(), array('id'=>$data->id));
} else {
$DB->set_field('lesson', 'mediafile', '', array('id'=>$data->id));
}
} else {
$DB->set_field('lesson', 'mediafile', '', array('id'=>$data->id));
}
lesson_update_media_file($data->id, $context, $draftitemid);
lesson_process_post_save($data);
@ -959,3 +949,33 @@ function lesson_page_type_list($pagetype, $parentcontext, $currentcontext) {
'mod-lesson-edit'=>get_string('page-mod-lesson-edit', 'lesson'));
return $module_pagetype;
}
/**
* Update the lesson activity to include any file
* that was uploaded, or if there is none, set the
* mediafile field to blank.
*
* @param int $lessonid the lesson id
* @param stdClass $context the context
* @param int $draftitemid the draft item
*/
function lesson_update_media_file($lessonid, $context, $draftitemid) {
global $DB;
// Set the filestorage object.
$fs = get_file_storage();
// Save the file if it exists that is currently in the draft area.
file_save_draft_area_files($draftitemid, $context->id, 'mod_lesson', 'mediafile', 0);
// Get the file if it exists.
$files = $fs->get_area_files($context->id, 'mod_lesson', 'mediafile', 0, 'itemid, filepath, filename', false);
// Check that there is a file to process.
if (count($files) == 1) {
// Get the first (and only) file.
$file = reset($files);
// Set the mediafile column in the lessons table.
$DB->set_field('lesson', 'mediafile', '/' . $file->get_filename(), array('id' => $lessonid));
} else {
// Set the mediafile column in the lessons table.
$DB->set_field('lesson', 'mediafile', '', array('id' => $lessonid));
}
}

View File

@ -259,11 +259,14 @@ class mod_lesson_mod_form extends moodleform_mod {
//-------------------------------------------------------------------------------
$mform->addElement('header', 'mediafileheader', get_string('mediafile', 'lesson'));
$filepickeroptions = array();
$filepickeroptions['filetypes'] = '*';
$filepickeroptions['maxbytes'] = $this->course->maxbytes;
$mform->addElement('filepicker', 'mediafilepicker', get_string('mediafile', 'lesson'), null, $filepickeroptions);
$mform->addHelpButton('mediafilepicker', 'mediafile', 'lesson');
$filemanageroptions = array();
$filemanageroptions['filetypes'] = '*';
$filemanageroptions['maxbytes'] = $this->course->maxbytes;
$filemanageroptions['subdirs'] = 0;
$filemanageroptions['maxfiles'] = 1;
$mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions);
$mform->addHelpButton('mediafile', 'mediafile', 'lesson');
//-------------------------------------------------------------------------------
$mform->addElement('header', 'dependencyon', get_string('dependencyon', 'lesson'));
@ -321,9 +324,9 @@ class mod_lesson_mod_form extends moodleform_mod {
if ($this->current->instance) {
// editing existing instance - copy existing files into draft area
$draftitemid = file_get_submitted_draft_itemid('mediafilepicker');
$draftitemid = file_get_submitted_draft_itemid('mediafile');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
$default_values['mediafilepicker'] = $draftitemid;
$default_values['mediafile'] = $draftitemid;
}
}