From f34ef6d0246e8c07c28d232d6f7cd1ede8623273 Mon Sep 17 00:00:00 2001 From: Dasu Gunathunga Date: Mon, 27 May 2019 13:47:04 +1000 Subject: [PATCH 1/2] Added new field type file upload --- fieldtype/fileupload/classes/define.php | 47 ++++++++++ fieldtype/fileupload/classes/metadata.php | 88 +++++++++++++++++++ .../lang/en/metadatafieldtype_fileupload.php | 31 +++++++ fieldtype/fileupload/lib.php | 60 +++++++++++++ fieldtype/fileupload/version.php | 33 +++++++ 5 files changed, 259 insertions(+) create mode 100755 fieldtype/fileupload/classes/define.php create mode 100755 fieldtype/fileupload/classes/metadata.php create mode 100755 fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php create mode 100755 fieldtype/fileupload/lib.php create mode 100755 fieldtype/fileupload/version.php diff --git a/fieldtype/fileupload/classes/define.php b/fieldtype/fileupload/classes/define.php new file mode 100755 index 0000000..c97cfc3 --- /dev/null +++ b/fieldtype/fileupload/classes/define.php @@ -0,0 +1,47 @@ +. + +/** + * 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'); + + } +} + + diff --git a/fieldtype/fileupload/classes/metadata.php b/fieldtype/fileupload/classes/metadata.php new file mode 100755 index 0000000..d5363ea --- /dev/null +++ b/fieldtype/fileupload/classes/metadata.php @@ -0,0 +1,88 @@ +. + +/** + * 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 '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; + } +} + + diff --git a/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php b/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php new file mode 100755 index 0000000..c4ff9ee --- /dev/null +++ b/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php @@ -0,0 +1,31 @@ +. + +/** + * 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.'; \ No newline at end of file diff --git a/fieldtype/fileupload/lib.php b/fieldtype/fileupload/lib.php new file mode 100755 index 0000000..4eebaf5 --- /dev/null +++ b/fieldtype/fileupload/lib.php @@ -0,0 +1,60 @@ +contextlevel != CONTEXT_MODULE) { + return false; + } + + require_course_login($course, true, $cm); + + $areas = forum_get_file_areas($course, $cm, $context); + + // filearea must contain a real area + 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! +} \ No newline at end of file diff --git a/fieldtype/fileupload/version.php b/fieldtype/fileupload/version.php new file mode 100755 index 0000000..23bbfe7 --- /dev/null +++ b/fieldtype/fileupload/version.php @@ -0,0 +1,33 @@ +. + +/** + * Metadata fileupload fieldtype plugin version info. + * + * @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; + +$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). \ No newline at end of file From dca86cb3a5007230608b154ab18859ce225ee970 Mon Sep 17 00:00:00 2001 From: Dasu Gunathunga Date: Tue, 28 May 2019 11:07:47 +1000 Subject: [PATCH 2/2] Codechecker changes --- fieldtype/fileupload/classes/define.php | 94 +++++----- fieldtype/fileupload/classes/metadata.php | 175 +++++++++--------- .../lang/en/metadatafieldtype_fileupload.php | 60 +++--- fieldtype/fileupload/lib.php | 138 ++++++++------ fieldtype/fileupload/version.php | 66 +++---- 5 files changed, 276 insertions(+), 257 deletions(-) diff --git a/fieldtype/fileupload/classes/define.php b/fieldtype/fileupload/classes/define.php index c97cfc3..eb300ad 100755 --- a/fieldtype/fileupload/classes/define.php +++ b/fieldtype/fileupload/classes/define.php @@ -1,47 +1,47 @@ -. - -/** - * 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'); - - } -} - - +. + +/** + * 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'); + + } +} + + diff --git a/fieldtype/fileupload/classes/metadata.php b/fieldtype/fileupload/classes/metadata.php index d5363ea..790b8c6 100755 --- a/fieldtype/fileupload/classes/metadata.php +++ b/fieldtype/fileupload/classes/metadata.php @@ -1,88 +1,87 @@ -. - -/** - * 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 '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; - } -} - - +. + +/** + * 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 '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; + } +} + + diff --git a/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php b/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php index c4ff9ee..34a1ff3 100755 --- a/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php +++ b/fieldtype/fileupload/lang/en/metadatafieldtype_fileupload.php @@ -1,31 +1,31 @@ -. - -/** - * 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'; +. + +/** + * 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.'; \ No newline at end of file diff --git a/fieldtype/fileupload/lib.php b/fieldtype/fileupload/lib.php index 4eebaf5..8843a87 100755 --- a/fieldtype/fileupload/lib.php +++ b/fieldtype/fileupload/lib.php @@ -1,60 +1,80 @@ -contextlevel != CONTEXT_MODULE) { - return false; - } - - require_course_login($course, true, $cm); - - $areas = forum_get_file_areas($course, $cm, $context); - - // filearea must contain a real area - 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! +. + +/** + * 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! } \ No newline at end of file diff --git a/fieldtype/fileupload/version.php b/fieldtype/fileupload/version.php index 23bbfe7..5426854 100755 --- a/fieldtype/fileupload/version.php +++ b/fieldtype/fileupload/version.php @@ -1,33 +1,33 @@ -. - -/** - * Metadata fileupload fieldtype plugin version info. - * - * @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; - -$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). \ No newline at end of file +. + +/** + * Metadata fileupload fieldtype plugin version info. + * + * @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; + +$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).