From 74003bf0bbd961e097efd3d1b3a270613478d9ab Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 12 Mar 2015 23:58:35 -0700 Subject: [PATCH] Moved file manager to the image element --- edit.php | 3 --- edit_element.php | 3 ++- edit_form.php | 18 ------------------ element/image/lib.php | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/edit.php b/edit.php index dd3109f..44faf19 100644 --- a/edit.php +++ b/edit.php @@ -158,9 +158,6 @@ $mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->i 'course' => $course)); if ($data = $mform->get_data()) { - // Handle file uploads. - customcert_upload_imagefiles($data->customcertimage, context_course::instance($course->id)->id); - // Save any page data. customcert_save_page_data($data); diff --git a/edit_element.php b/edit_element.php index cffb1e5..e50ff42 100644 --- a/edit_element.php +++ b/edit_element.php @@ -62,7 +62,8 @@ $PAGE->set_heading($course->fullname); $PAGE->set_title(get_string('editcustomcert', 'customcert', format_string($customcert->name))); $PAGE->set_url($pageurl); -$mform = new mod_customcert_edit_element_form($pageurl, array('element' => $element, 'cmid' => $cmid, 'action' => $action)); +$mform = new mod_customcert_edit_element_form($pageurl, array('element' => $element, 'course' => $course, + 'cmid' => $cmid, 'action' => $action)); // Check if they cancelled. if ($mform->is_cancelled()) { diff --git a/edit_form.php b/edit_form.php index 22af0d1..f5baae3 100644 --- a/edit_form.php +++ b/edit_form.php @@ -41,11 +41,6 @@ class mod_customcert_edit_form extends moodleform { */ private $numpages = 1; - /** - * The filemanager options. - */ - private $filemanageroptions = array(); - /** * Form definition. */ @@ -53,9 +48,6 @@ class mod_customcert_edit_form extends moodleform { global $DB; $this->id = $this->_customdata['customcertid']; - $this->filemanageroptions = array('maxbytes' => $this->_customdata['course']->maxbytes, - 'subdirs' => 1, - 'accepted_types' => 'image'); $mform =& $this->_form; @@ -71,10 +63,6 @@ class mod_customcert_edit_form extends moodleform { $mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert')); - $mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert')); - - $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions); - $mform->closeHeaderBefore('submitbtn'); // Add the submit buttons. @@ -99,12 +87,6 @@ class mod_customcert_edit_form extends moodleform { $mform = $this->_form; - // Editing existing instance - copy existing files into draft area. - $draftitemid = file_get_submitted_draft_itemid('customcertimage'); - file_prepare_draft_area($draftitemid, context_course::instance($this->_customdata['course']->id)->id, 'mod_customcert', 'image', 0, $this->filemanageroptions); - $element = $mform->getElement('customcertimage'); - $element->setValue($draftitemid); - // Check that we are updating a current customcert. if ($this->id) { // Get the pages for this customcert. diff --git a/element/image/lib.php b/element/image/lib.php index 1967484..61721b2 100644 --- a/element/image/lib.php +++ b/element/image/lib.php @@ -33,6 +33,8 @@ class customcert_element_image extends customcert_element_base { * @param mod_customcert_edit_element_form $mform the edit_form instance */ public function render_form_elements($mform) { + global $COURSE; + $mform->addElement('select', 'image', get_string('image', 'customcertelement_image'), self::get_images()); $mform->addElement('text', 'width', get_string('width', 'customcertelement_image'), array('size' => 10)); @@ -46,6 +48,12 @@ class customcert_element_image extends customcert_element_base { $mform->addHelpButton('height', 'height', 'customcertelement_image'); parent::render_form_element_position($mform); + + $filemanageroptions = array('maxbytes' => $COURSE->maxbytes, + 'subdirs' => 1, + 'accepted_types' => 'image'); + + $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $filemanageroptions); } /** @@ -75,6 +83,21 @@ class customcert_element_image extends customcert_element_base { return $errors; } + /** + * Handles saving the form elements created by this element. + * Can be overridden if more functionality is needed. + * + * @param stdClass $data the form data + */ + public function save_form_elements($data) { + global $COURSE; + + // Handle file uploads. + customcert_upload_imagefiles($data->customcertimage, context_course::instance($COURSE->id)->id); + + parent::save_form_elements($data); + } + /** * This will handle how form data will be saved into the data column in the * customcert_elements table. @@ -126,12 +149,24 @@ class customcert_element_image extends customcert_element_base { * @param mod_customcert_edit_element_form $mform the edit_form instance */ public function definition_after_data($mform) { + global $COURSE; + // Set the image, width and height for this element. $imageinfo = json_decode($this->element->data); $this->element->image = $imageinfo->pathnamehash; $this->element->width = $imageinfo->width; $this->element->height = $imageinfo->height; + // Editing existing instance - copy existing files into draft area. + $draftitemid = file_get_submitted_draft_itemid('customcertimage'); + $filemanageroptions = array('maxbytes' => $COURSE->maxbytes, + 'subdirs' => 1, + 'accepted_types' => 'image'); + file_prepare_draft_area($draftitemid, context_course::instance($COURSE->id)->id, 'mod_customcert', 'image', 0, + $filemanageroptions); + $element = $mform->getElement('customcertimage'); + $element->setValue($draftitemid); + parent::definition_after_data($mform); }