Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-25552 mod_lesson: changing filepicker element to filemanager to a…

…llow the deletion of a file
  • Loading branch information...
commit 684c3be721cd2fb8d259315189f7401f1f249e26 1 parent 8ccaa29
@markn86 markn86 authored
Showing with 46 additions and 23 deletions.
  1. +36 −16 mod/lesson/lib.php
  2. +10 −7 mod/lesson/mod_form.php
View
52 mod/lesson/lib.php
@@ -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
17 mod/lesson/mod_form.php
@@ -259,11 +259,14 @@ function definition() {
//-------------------------------------------------------------------------------
$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 @@ function data_preprocessing(&$default_values) {
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;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.