Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new field type file upload #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions fieldtype/fileupload/classes/define.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
// This file is part of 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/>.

/**
* Fileupload profile field
*
* @package profilefield_fileupload
* @copyright
* @license
*/

namespace metadatafieldtype_fileupload;

defined('MOODLE_INTERNAL') || die;

/**
* Class local_metadata_define_fileupload
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class define extends \local_metadata\fieldtype\define_base {

/**
* Add elements for creating/editing a fileupload profile field.
*
* @param moodleform $form
*/
public function define_form_specific($form) {
$form->addElement('filemanager', 'image', 'Uploadfile');

}
}


87 changes: 87 additions & 0 deletions fieldtype/fileupload/classes/metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
// This file is part of 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/>.

/**
* Strings for component 'profilefield_fileupload', language 'en', branch 'MOODLE_20_STABLE'
*
* @package profilefield_fileupload
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace metadatafieldtype_fileupload;

defined('MOODLE_INTERNAL') || die;

/**
* Class local_metadata_field_fileupload
*
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class metadata extends \local_metadata\fieldtype\metadata {
/**
* Add elements for editing the profile field value.
* @param moodleform $mform
*/
public function edit_field_add($mform) {
// Create the form field.
$fileupload = $mform->addElement('filemanager', $this->inputname, format_string($this->field->name), null,
array('subdirs' => 0, 'maxbytes' => '50', 'areamaxbytes' => 83886080, 'maxfiles' => 1,
'accepted_types' => array('*'), 'return_types' => FILE_INTERNAL | FILE_EXTERNAL));
}

public function edit_save_data($new) {
global $DB;
$array = json_decode(json_encode($new), true);
$keys = array_keys($array);
$itemid = file_get_submitted_draft_itemid($keys[1]);
$this->dradt_item_id = $itemid;
$context = \context_module::instance($new->id);
parent::edit_save_data($new);
if (!empty($itemid)) {
file_save_draft_area_files($itemid, $context->id, 'local_metadata', 'imageupload', $itemid, array());
}
}

/**
* Display the data for this field
*
* @return string HTML.
*/
public function display_data() {
return '<input disabled="disabled" type="filemanager" name="'.$this->inputname.'" />test';
}


/**
* When passing the instance object to the form class for the edit page
* we should load the key for the saved data
*
* Overwrites the base class method.
*
* @param stdClass $instance Instance object.
*/
public function edit_load_instance_data($instance) {
global $DB;
$context = \context_module::instance($instance->id);
$draftitemid = file_get_submitted_draft_itemid('imageupload');
file_prepare_draft_area($draftitemid, $context->id, 'local_metadata', 'imageupload', $this->data, array(), null);
$instance->{$this->inputname} = $draftitemid;
}
}


31 changes: 31 additions & 0 deletions fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
// This file is part of 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/>.

/**
* Metadata text fieldtype plugin language file.
*
* @package local_metadata
* @subpackage metadatafieldtype_fileupload
* @author Dasu Gunathunga
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

$string['pluginname'] = 'Fileupload metadata fieldtype';
$string['displayname'] = 'File Upload';
$string['privacy:metadata'] = 'Fieldtypes do not store data.';
80 changes: 80 additions & 0 deletions fieldtype/fileupload/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// This file is part of 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/>.

/**
* Strings for component 'profilefield_fileupload', language 'en', branch 'MOODLE_20_STABLE'
*
* @package profilefield_fileupload
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;
function fileupload_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {

global $CFG, $DB;

if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}

require_course_login($course, true, $cm);

$areas = forum_get_file_areas($course, $cm, $context);

if (!isset($areas[$filearea])) {
return false;
}

$postid = (int)array_shift($args);

if (!$post = $DB->get_record('forum_posts', array('id' => $postid))) {
return false;
}

if (!$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
return false;
}

if (!$forum = $DB->get_record('forum', array('id' => $cm->instance))) {
return false;
}

$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_forum/$filearea/$postid/$relativepath";
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}

// Make sure groups allow this user to see this file.
if ($discussion->groupid > 0) {
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode == SEPARATEGROUPS) {
if (!groups_is_member($discussion->groupid) and !has_capability('moodle/site:accessallgroups', $context)) {
return false;
}
}
}

// Make sure we're allowed to see it.
if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
return false;
}

// Finally send the file.
send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
}
33 changes: 33 additions & 0 deletions fieldtype/fileupload/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// This file is part of 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/>.

/**
* Metadata fileupload fieldtype plugin version info.
*
* @package local_metadata
* @subpackage metadatafieldtype_fileupload
* @author Dasu Gunathunga <Dasu.Gunathunga@racp.edu.au>
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2019050100;
$plugin->release = 'v1.0.0';
$plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2016052300; // Requires this Moodle version.
$plugin->component = 'metadatafieldtype_fileupload'; // Full name of the plugin (used for diagnostics).