diff --git a/adminsetting.class.php b/adminsetting.class.php new file mode 100644 index 0000000..3b9438c --- /dev/null +++ b/adminsetting.class.php @@ -0,0 +1,44 @@ +. + +/** + * Creates an upload form on the settings page. + * + * @package mod_customcert + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require_once($CFG->libdir.'/adminlib.php'); +require_once($CFG->dirroot.'/mod/customcert/upload_image_form.php'); + +/** + * Class extends admin setting class to allow/process an uploaded file + */ +class mod_customcert_admin_setting_upload extends admin_setting_configtext { + + /** + * Output the link to the upload image page. + * + * @param mixed $data + * @param string $query + * @return string + */ + public function output_html($data, $query = '') { + return format_admin_setting($this, $this->visiblename, + html_writer::link(new moodle_url('/mod/customcert/upload_image.php'), get_string('upload')), + $this->description, true, '', null, $query); + } +} diff --git a/edit.php b/edit.php index 859b9ac..e082d8b 100644 --- a/edit.php +++ b/edit.php @@ -159,7 +159,7 @@ $mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->i if ($data = $mform->get_data()) { // Handle file uploads. - customcert_upload_imagefiles($data->customcertimage); + customcert_upload_imagefiles($data->customcertimage, context_course::instance($course->id)->id); // Save any page data. customcert_save_page_data($data); diff --git a/edit_form.php b/edit_form.php index e1cedbe..7a79765 100644 --- a/edit_form.php +++ b/edit_form.php @@ -101,7 +101,7 @@ class mod_customcert_edit_form extends moodleform { // Editing existing instance - copy existing files into draft area. $draftitemid = file_get_submitted_draft_itemid('customcertimage'); - file_prepare_draft_area($draftitemid, context_system::instance()->id, 'mod_customcert', 'image', 0, $this->filemanageroptions); + 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); diff --git a/element/image/lib.php b/element/image/lib.php index e1251d6..f966c7e 100644 --- a/element/image/lib.php +++ b/element/image/lib.php @@ -141,17 +141,28 @@ class customcert_element_image extends customcert_element_base { * @return array the list of images that can be used */ public static function get_images() { + global $COURSE; + // Create file storage object. $fs = get_file_storage(); // The array used to store the images. $arrfiles = array(); - $arrfiles[0] = get_string('noimage', 'customcert'); + // Loop through the files uploaded in the system context. if ($files = $fs->get_area_files(context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) { foreach ($files as $hash => $file) { $arrfiles[$hash] = $file->get_filename(); } } + // Loop through the files uploaded in the course context. + if ($files = $fs->get_area_files(context_course::instance($COURSE->id)->id, 'mod_customcert', 'image', false, 'filename', false)) { + foreach ($files as $hash => $file) { + $arrfiles[$hash] = $file->get_filename(); + } + } + + customcert_perform_asort($arrfiles); + $arrfiles = array_merge(array('0' => get_string('noimage', 'customcert')), $arrfiles); return $arrfiles; } diff --git a/element/image/version.php b/element/image/version.php index 6879c2a..408388c 100644 --- a/element/image/version.php +++ b/element/image/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013062800; +$plugin->version = 2013090900; $plugin->requires = 2012120300; // Requires this Moodle version. $plugin->component = 'customcertelement_image'; diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 44bf48b..be5de66 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -95,6 +95,8 @@ $string['templatename'] = 'Template name'; $string['templatenameexists'] = 'That template name is currently in use, please choose another.'; $string['type'] = 'Type'; $string['uploadimage'] = 'Upload image'; +$string['uploadimagedesc'] = 'This link will take you to a new screen where you will be able to upload images. Images uploaded using +this method will be available throughout your site to all users who are able to create a custom certificate.'; $string['viewcustomcertissues'] = 'View {$a} issued custom certificates'; $string['width'] = 'Width'; $string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.'; diff --git a/lib.php b/lib.php index 4f291f9..ce45d18 100644 --- a/lib.php +++ b/lib.php @@ -362,10 +362,11 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na * Handles uploading an image for the customcert module. * * @param int $draftitemid the draft area containing the files + * @param int $contextid the context we are storing this image in */ -function customcert_upload_imagefiles($draftitemid) { +function customcert_upload_imagefiles($draftitemid, $contextid) { // Save the file if it exists that is currently in the draft area. - file_save_draft_area_files($draftitemid, context_system::instance()->id, 'mod_customcert', 'image', 0); + file_save_draft_area_files($draftitemid, $contextid, 'mod_customcert', 'image', 0); } /** diff --git a/settings.php b/settings.php new file mode 100644 index 0000000..6e35a3a --- /dev/null +++ b/settings.php @@ -0,0 +1,30 @@ +. + +/** + * Creates a link to the upload form on the settings page. + * + * @package mod_customcert + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +require_once($CFG->dirroot.'/mod/customcert/adminsetting.class.php'); + +$settings->add(new mod_customcert_admin_setting_upload('customcert/uploadimage', + get_string('uploadimage', 'customcert'), get_string('uploadimagedesc', 'customcert'), '')); diff --git a/upload_image.php b/upload_image.php new file mode 100644 index 0000000..0a20c59 --- /dev/null +++ b/upload_image.php @@ -0,0 +1,57 @@ +. + +/** + * Handles uploading files + * + * @package mod_customcert + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../../config.php'); +require_once($CFG->dirroot.'/mod/customcert/lib.php'); +require_once($CFG->dirroot.'/mod/customcert/upload_image_form.php'); + +require_login(); + +$context = context_system::instance(); +require_capability('moodle/site:config', $context); + +$struploadimage = get_string('uploadimage', 'customcert'); + +$PAGE->set_url('/admin/settings.php', array('section' => 'modsettingcustomcert')); +$PAGE->set_pagetype('admin-setting-modsettingcustomcert'); +$PAGE->set_pagelayout('admin'); +$PAGE->set_context($context); +$PAGE->set_title($struploadimage); +$PAGE->set_heading($SITE->fullname); +$PAGE->navbar->add($struploadimage); + +$uploadform = new mod_customcert_upload_image_form(); + +if ($uploadform->is_cancelled()) { + redirect(new moodle_url('/admin/settings.php?section=modsettingcustomcert')); +} else if ($data = $uploadform->get_data()) { + // Handle file uploads. + customcert_upload_imagefiles($data->customcertimage, $context->id); + + redirect(new moodle_url('/mod/customcert/upload_image.php'), get_string('changessaved')); +} + +echo $OUTPUT->header(); +$uploadform->display(); +echo $OUTPUT->footer(); diff --git a/upload_image_form.php b/upload_image_form.php new file mode 100644 index 0000000..75afa77 --- /dev/null +++ b/upload_image_form.php @@ -0,0 +1,61 @@ +. + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +require_once($CFG->libdir.'/formslib.php'); + +/** + * Handles uploading files + * + * @package mod_customcert + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_customcert_upload_image_form extends moodleform { + + /** @var array the filemanager options */ + private $filemanageroptions = array(); + + /** + * Form definition. + */ + public function definition() { + global $CFG; + + $mform = $this->_form; + $this->filemanageroptions = array( + 'maxbytes' => $CFG->maxbytes, + 'subdirs' => 1, + 'accepted_types' => 'image'); + $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions); + + $this->add_action_buttons(); + } + + /** + * Fill in the current page data for this customcert. + */ + public function definition_after_data() { + $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_system::instance()->id, 'mod_customcert', 'image', 0, $this->filemanageroptions); + $element = $mform->getElement('customcertimage'); + $element->setValue($draftitemid); + } +}