Images can now be saved into either the course or system context

This commit is contained in:
Mark Nelson 2013-09-09 16:50:48 +08:00
parent b7890cbb4f
commit 4b2692d377
10 changed files with 212 additions and 6 deletions

44
adminsetting.class.php Normal file
View file

@ -0,0 +1,44 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Creates an upload form on the settings page.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @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);
}
}

View file

@ -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);

View file

@ -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);

View file

@ -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;
}

View file

@ -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';

View file

@ -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.';

View file

@ -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);
}
/**

30
settings.php Normal file
View file

@ -0,0 +1,30 @@
<?php
// This file is part of the Certificate module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Creates a link to the upload form on the settings page.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @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'), ''));

57
upload_image.php Normal file
View file

@ -0,0 +1,57 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Handles uploading files
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @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();

61
upload_image_form.php Normal file
View file

@ -0,0 +1,61 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <markn@moodle.com>
* @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);
}
}