Hi
Im trying to make a local plugin that can control some of the layout of the course.
I want to be able to upload images but i cant get it to work.
My code is WIP so sorry for the messy code examples
My layout_form.php file that contains the filemanager
<?phpThe view.php file that calls the form and sets the draft area:
require_once('../../config.php');
require_login();
//moodleform is defined in formslib.php
require_once('classes/curl.php');
require_once($CFG->libdir.'/formslib.php');
class course_layout_form extends moodleform
{
public function definition() {
global $CFG, $USER;
$mform = $this->_form; // Don't forget the underscore!
$maxfiles = 0;
$subdirs = 0;
if ($CFG->legacyfilesaddallowed) {
$maxfiles = -1;
$subdirs = 1;
}
$maxbytes = 500000;
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'moodle'), null,
array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50,
'accepted_types' => array('document', '.jpg', '.jpeg'), 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL));
$mform->setType('filemanager', PARAM_RAW);
$mform->addElement('hidden', 'id', '737');
$mform->setType('id', PARAM_RAW);
$buttonarray=array();
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
$buttonarray[] = &$mform->createElement('reset', 'resetbutton', get_string('revert'));
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
<?php
require_once('../../config.php');
//require_once(dirname(__FILE__).'/lib.php');
require_once($CFG->dirroot.'/local/course_layout/classes/xml2array.php');
require_login();
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->dirroot . '/lib/filelib.php');
global $CFG, $PAGE, $COURSE, $DB, $USER;
$PAGE->requires->js('/local/course_layout/js/bootstrap-multiselect.js');
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('browser-title', 'praxis_course_layout'));
$PAGE->set_heading('Form name');
$PAGE->set_url($CFG->wwwroot.'/local/course_layout/view.php');
$id = null;
//set the id from url
if(isset( $_GET['courseid'])){
$id = $_GET['courseid'];
}
if(!$id){
$id = $_POST['id'];
}
$table = 'course';
$select = "id = ".$id;
$course_info = $DB->get_record_select($table, $select, NULL);
// Output starts here
echo $OUTPUT->header();
// Show the heading
echo $OUTPUT->heading($course_info->fullname);
echo '<div class="meta">';
//includeform.php
require_once('layout_form.php');
//Set settings for the form values
$settings = new stdClass();
$settings->email = "hsp@praxis.dk";
$settings->id = $id;
//Instantiate praxis_course_layout_form
echo '<div class="form-metaform">';
//set the id and the access to get the right course and access
$mform = new course_layout_form('view.php?id='.$id);
//Set default data (if any)
$mform->set_data($settings);
//displays the form
$mform->display();
print_r($_POST); //
$coursecontext = context_course::instance(737); // build the right context from hardcoded course id - course 737 is the one im working on for test
$maxbytes = 500000;
if (empty($entry->id)) {
$entry = new stdClass;
$entry->id = 0;
}
$coursecontext = context_course::instance(737);
$draftitemid = file_get_submitted_draft_itemid('attachments');
echo $coursecontext->id;
echo '!' . $entry->id . '!';
file_prepare_draft_area($draftitemid, $coursecontext->id, 'praxis_course_layout', 'attachments', $entry->id,
array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));
$entry->attachments = $draftitemid;
$mform->set_data($entry);
echo "<hr />";
if ($data = $mform->get_data()) {
print_r($data);
// ... store or update $entry
file_save_draft_area_files($data->attachments, $coursecontext->id, 'praxis_course_layout', 'attachments',
$entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));
}
$contextid = $coursecontext->id;
$component = 'praxis_course_layout';
$filearea = 'attachments';
echo $url = $CFG->wwwroot.'/pluginfile.php/'.$contextid.'/'.$component.'/'.$filearea.'/600x378_verden.jpg'; // hardcoded image for test - is the same image i try to upload
echo '<img src="'.$url.'" />';
echo "</div>";
// Finish the page.
echo "</div>";
echo $OUTPUT->footer();
It seems the file is uploaded - an entry is set in the database, the moodledata folder is growing in size, but no image can be retrived with
$CFG->wwwroot.'/pluginfile.php/'.$contextid.'/'.$component.'/'.$filearea.'/600x378_verden.jpg';
And if i leave the page and come back later, the file area in the manager is empty. Not showing the files uploaded.
I am not sure what im doing wrong and i have tried to follow the Documentation, but it seems that im missing something.
Please help
Kind regards
Henrik Sune Pedersen