function validation($data, $files) { global $CFG, $USER, $DB; $errors = parent::validation($data, $files); if (empty($data['id']) and empty($data['example'])) { // make sure there is no submission saved meanwhile from another browser window $sql = "SELECT COUNT(s.id)\n FROM {workshop_submissions} s\n JOIN {workshop} w ON (s.workshopid = w.id)\n JOIN {course_modules} cm ON (w.id = cm.instance)\n JOIN {modules} m ON (m.name = 'workshop' AND m.id = cm.module)\n WHERE cm.id = ? AND s.authorid = ? AND s.example = 0"; if ($DB->count_records_sql($sql, array($data['cmid'], $USER->id))) { $errors['title'] = get_string('err_multiplesubmissions', 'mod_workshop'); } } if (isset($data['attachment_filemanager']) and isset($this->_customdata['workshop']->submissionfiletypes)) { $whitelist = workshop::normalize_file_extensions($this->_customdata['workshop']->submissionfiletypes); if ($whitelist) { $draftfiles = file_get_drafarea_files($data['attachment_filemanager']); if ($draftfiles) { $wrongfiles = array(); foreach ($draftfiles->list as $file) { if (!workshop::is_allowed_file_type($file->filename, $whitelist)) { $wrongfiles[] = $file->filename; } } if ($wrongfiles) { $a = array('whitelist' => workshop::clean_file_extensions($whitelist), 'wrongfiles' => implode(', ', $wrongfiles)); $errors['attachment_filemanager'] = get_string('err_wrongfileextension', 'mod_workshop', $a); } } } } return $errors; }
public function test_import_files_as_draft() { $this->resetAfterTest(); $this->setAdminUser(); $xml = <<<END <questiontext format="html"> <text><![CDATA[<p><a href="@@PLUGINFILE@@/moodle.txt">This text file</a> contains the word 'Moodle'.</p>]]></text> <file name="moodle.txt" encoding="base64">TW9vZGxl</file> </questiontext> END; $textxml = xmlize($xml); $qo = new stdClass(); $importer = new qformat_xml(); $draftitemid = $importer->import_files_as_draft($textxml['questiontext']['#']['file']); $files = file_get_drafarea_files($draftitemid); $this->assertEquals(1, count($files->list)); $file = $files->list[0]; $this->assertEquals('moodle.txt', $file->filename); $this->assertEquals('/', $file->filepath); $this->assertEquals(6, $file->size); }
public static function file_uploaded($draftitemid) { $draftareafiles = file_get_drafarea_files($draftitemid); do { $draftareafile = array_shift($draftareafiles->list); } while ($draftareafile !== null && $draftareafile->filename == '.'); if ($draftareafile === null) { return false; } return true; }
case 'mkdir': $newfolderpath = $draftpath . trim($newdirname, '/') . '/'; $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath); $home_url->param('action', 'browse'); if (!empty($newdirname)) { $home_url->param('draftpath', $newfolderpath); $str = get_string('createfoldersuccess', 'repository'); } else { $home_url->param('draftpath', $draftpath); $str = get_string('createfolderfail', 'repository'); } redirect($home_url, $str); break; case 'browse': default: $files = file_get_drafarea_files($itemid, $draftpath); $info = file_get_draft_area_info($itemid); $filecount = $info['filecount']; echo $OUTPUT->header(); if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') { echo '<div class="fm-breadcrumb">'; $home_url->param('action', 'browse'); $home_url->param('draftpath', '/'); echo '<a href="' . $home_url->out() . '">' . get_string('files') . '</a> ▶'; $trail = ''; if ($draftpath !== '/') { $path = '/' . trim($draftpath, '/') . '/'; $parts = explode('/', $path); foreach ($parts as $part) { if ($part != '') { $trail .= '/' . $part . '/';
/** * Constructor * * @param stdClass $options options for filemanager * default options are: * maxbytes=>-1, * maxfiles=>-1, * itemid=>0, * subdirs=>false, * client_id=>uniqid(), * acepted_types=>'*', * return_types=>FILE_INTERNAL, * context=>$PAGE->context, * author=>fullname($USER), * licenses=>array build from $CFG->licenses, * defaultlicense=>$CFG->sitedefaultlicense */ public function __construct(stdClass $options) { global $CFG, $USER, $PAGE; require_once $CFG->dirroot . '/repository/lib.php'; $defaults = array('maxbytes' => -1, 'maxfiles' => -1, 'itemid' => 0, 'subdirs' => 0, 'client_id' => uniqid(), 'accepted_types' => '*', 'return_types' => FILE_INTERNAL, 'context' => $PAGE->context, 'author' => fullname($USER), 'licenses' => array()); if (!empty($CFG->licenses)) { $array = explode(',', $CFG->licenses); foreach ($array as $license) { $l = new stdClass(); $l->shortname = $license; $l->fullname = get_string($license, 'license'); $defaults['licenses'][] = $l; } } if (!empty($CFG->sitedefaultlicense)) { $defaults['defaultlicense'] = $CFG->sitedefaultlicense; } foreach ($defaults as $key => $value) { if (empty($options->{$key})) { $options->{$key} = $value; } } $fs = get_file_storage(); // initilise options, getting files in root path $this->options = file_get_drafarea_files($options->itemid, '/'); // calculate file count $usercontext = context_user::instance($USER->id); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $filecount = count($files); $this->options->filecount = $filecount; // copying other options foreach ($options as $name => $value) { $this->options->{$name} = $value; } // calculate the maximum file size as minimum from what is specified in filepicker options, // course options, global configuration and php settings $coursebytes = $maxbytes = 0; list($context, $course, $cm) = get_context_info_array($this->options->context->id); if (is_object($course)) { $coursebytes = $course->maxbytes; } if (!empty($this->options->maxbytes) && $this->options->maxbytes > 0) { $maxbytes = $this->options->maxbytes; } $this->options->maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $coursebytes, $maxbytes); // building file picker options $params = new stdClass(); $params->accepted_types = $options->accepted_types; $params->return_types = $options->return_types; $params->context = $options->context; $params->env = 'filemanager'; $params->disable_types = !empty($options->disable_types) ? $options->disable_types : array(); $filepicker_options = initialise_filepicker($params); $this->options->filepicker = $filepicker_options; }
$filepath = optional_param('filepath', '/', PARAM_PATH); $user_context = get_context_instance(CONTEXT_USER, $USER->id); echo $OUTPUT->header(); // send headers // //NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!! // switch ($action) { case 'dir': $data = new stdClass(); file_get_drafarea_folders($draftid, $filepath, $data); echo json_encode($data); die; case 'list': $filepath = optional_param('filepath', '/', PARAM_PATH); $data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath)); $info = file_get_draft_area_info($draftid); $data->filecount = $info['filecount']; $data->filesize = $info['filesize']; $data->tree = new stdClass(); file_get_drafarea_folders($draftid, '/', $data->tree); echo json_encode($data); die; case 'mkdir': $filepath = required_param('filepath', PARAM_PATH); $newdirname = required_param('newdirname', PARAM_FILE); $fs = get_file_storage(); $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath) . $newdirname)); $return = new stdClass(); $return->filepath = $filepath; echo json_encode($return);
/** * Constructor * * @param stdClass $options options for filemanager */ public function __construct(stdClass $options) { global $CFG, $USER, $PAGE; require_once $CFG->dirroot . '/repository/lib.php'; $defaults = array('maxbytes' => -1, 'maxfiles' => -1, 'itemid' => 0, 'subdirs' => 0, 'client_id' => uniqid(), 'accepted_types' => '*', 'return_types' => FILE_INTERNAL, 'context' => $PAGE->context); foreach ($defaults as $key => $value) { if (empty($options->{$key})) { $options->{$key} = $value; } } $fs = get_file_storage(); // initilise options, getting files in root path $this->options = file_get_drafarea_files($options->itemid, '/'); // calculate file count $usercontext = get_context_instance(CONTEXT_USER, $USER->id); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $filecount = count($files); $this->options->filecount = $filecount; // copying other options foreach ($options as $name => $value) { $this->options->{$name} = $value; } // building file picker options $params = new stdClass(); $params->accepted_types = $options->accepted_types; $params->return_types = $options->return_types; $params->context = $options->context; $params->env = 'filemanager'; $params->disable_types = !empty($options->disable_types) ? $options->disable_types : array(); $filepicker_options = initialise_filepicker($params); $this->options->filepicker = $filepicker_options; }
$cmid = required_param('id', PARAM_INT); $pageparams['id'] = $cmid; $cloneid = optional_param('clone', 0, PARAM_INT); if ($cloneid) { $pageparams['clone'] = $cloneid; } $forum = mod_forumng::get_from_cmid($cmid, $cloneid); $forum->require_view(mod_forumng::NO_GROUPS); // Work out some standard variables $filecontext = $forum->get_context(true); // All files stored in real forum, if this is clone $fileoptions = array('subdirs' => false, 'maxbytes' => $forum->get_max_bytes()); // Set up file area $draftitemid = 0; // We don't accept this form, so it's always a new one $messagedraftitemid = 0; // New reply file_prepare_draft_area($draftitemid, $filecontext->id, 'mod_forumng', 'attachment', null, $fileoptions); file_prepare_draft_area($messagedraftitemid, $filecontext->id, 'mod_forumng', 'message', 0, $fileoptions); // Get the files and count $options = file_get_drafarea_files($draftitemid, '/'); $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $filecount = count($files); $options->filecount = $filecount; $messageoptions = file_get_drafarea_files($messagedraftitemid, '/'); $messageoptions->filecount = 0; $combined = (object) array('options' => $options, 'messageoptions' => $messageoptions); // Print for output print json_encode($combined);
public function test_import_truefalse_wih_files() { $this->resetAfterTest(); $this->setAdminUser(); $xml = '<question type="truefalse"> <name> <text>truefalse</text> </name> <questiontext format="html"> <text><![CDATA[<p><a href="@@PLUGINFILE@@/myfolder/moodle.txt">This text file</a> contains the word Moodle.</p>]]></text> <file name="moodle.txt" path="/myfolder/" encoding="base64">TW9vZGxl</file> </questiontext> <generalfeedback format="html"> <text><![CDATA[<p>For further information, see the documentation about Moodle.</p>]]></text> </generalfeedback> <defaultgrade>1.0000000</defaultgrade> <penalty>1.0000000</penalty> <hidden>0</hidden> <answer fraction="100" format="moodle_auto_format"> <text>true</text> <feedback format="html"> <text></text> </feedback> </answer> <answer fraction="0" format="moodle_auto_format"> <text>false</text> <feedback format="html"> <text></text> </feedback> </answer> </question>'; $xmldata = xmlize($xml); $importer = new qformat_xml(); $q = $importer->import_truefalse($xmldata['question']); $draftitemid = $q->questiontextitemid; $files = file_get_drafarea_files($draftitemid, '/myfolder/'); $this->assertEquals(1, count($files->list)); $file = $files->list[0]; $this->assertEquals('moodle.txt', $file->filename); $this->assertEquals('/myfolder/', $file->filepath); $this->assertEquals(6, $file->size); }
/** * Determine if a submission is empty * * This is distinct from is_empty in that it is intended to be used to * determine if a submission made before saving is empty. * * @param stdClass $data The submission data * @return bool */ public function submission_is_empty(stdClass $data) { $files = file_get_drafarea_files($data->files_filemanager); return count($files->list) == 0; }
$filepath = optional_param('filepath', '/', PARAM_PATH); $user_context = get_context_instance(CONTEXT_USER, $USER->id); echo $OUTPUT->header(); // send headers // //NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!! // switch ($action) { case 'dir': $data = new stdClass(); file_get_drafarea_folders($draftid, $filepath, $data); echo json_encode($data); die; case 'list': $filepath = optional_param('filepath', '/', PARAM_PATH); $data = file_get_drafarea_files($draftid, $filepath); echo json_encode($data); die; case 'mkdir': $filepath = required_param('filepath', PARAM_PATH); $newdirname = required_param('newdirname', PARAM_FILE); $fs = get_file_storage(); $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath) . $newdirname)); $return = new stdClass(); $return->filepath = $filepath; echo json_encode($return); die; case 'delete': $filename = required_param('filename', PARAM_FILE); $filepath = required_param('filepath', PARAM_PATH); $fs = get_file_storage();
/** * Prints the content of this draft as a JavaScript variable (including * surrounding script tag). * @param mod_forumng $forum Forum object * @return string HTML (including JS) code to place in page */ public function prepare_edit_js($forum) { global $USER; // Copy fields $fields = clone $this->draftfields; // Prepare file areas $fileoptions = array('subdirs' => false, 'maxbytes' => $forum->get_max_bytes()); $filecontext = $forum->get_context(true); // Prepare draft area for attachments $draftitemid = 0; file_prepare_draft_area($draftitemid, $filecontext->id, 'mod_forumng', 'draft', $this->get_id(), $fileoptions); // Prepare draft area for message files $messagedraftitemid = 0; $fields->message = file_prepare_draft_area($messagedraftitemid, $filecontext->id, 'mod_forumng', 'draftmessage', $this->get_id(), $fileoptions, $fields->message); // Get list of files for main attachment area $options = file_get_drafarea_files($draftitemid, '/'); $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $options->filecount = count($files); $fields->attachmentoptions = $options; // Get list of files for message area $messageoptions = file_get_drafarea_files($messagedraftitemid, '/'); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $messageoptions->itemid, 'id', false); $messageoptions->filecount = count($files); $fields->messageoptions = $messageoptions; // Unset things we don't need in JS unset($fields->discussionid); unset($fields->discussionsubject); unset($fields->courseid); unset($fields->replytouser); unset($fields->options); unset($fields->attachments); // Add options foreach ((array) $this->get_options() as $key => $value) { $fields->{$key} = $value; } return "<script type='text/javascript'>\n" . "var forumng_draft = " . json_encode($fields) . ";\n</script>\n"; }
/** * Validate assessment form data. * * @param array $data * @param array $files * @return array */ public function validation($data, $files) { $errors = parent::validation($data, $files); if (isset($data['feedbackauthorattachment_filemanager']) and isset($this->workshop->overallfeedbackfiletypes)) { $whitelist = workshop::normalize_file_extensions($this->workshop->overallfeedbackfiletypes); if ($whitelist) { $draftfiles = file_get_drafarea_files($data['feedbackauthorattachment_filemanager']); if ($draftfiles) { $wrongfiles = array(); foreach ($draftfiles->list as $file) { if (!workshop::is_allowed_file_type($file->filename, $whitelist)) { $wrongfiles[] = $file->filename; } } if ($wrongfiles) { $a = array('whitelist' => workshop::clean_file_extensions($whitelist), 'wrongfiles' => implode(', ', $wrongfiles)); $errors['feedbackauthorattachment_filemanager'] = get_string('err_wrongfileextension', 'mod_workshop', $a); } } } } return $errors; }
public static function ajaxListFiles() { $draftIDs = explode(',', $_POST['draftIDs']); $files = array(); foreach ($draftIDs as $id) { $data = repository::prepare_listing(file_get_drafarea_files($id, '/')); if (!empty($data->list)) { foreach ($data->list as $f) { $files[] = $f->filename; } } } $out = array('draftIDs' => $draftIDs, 'files' => $files); header('Content-type: application/json'); echo json_encode($out); die; }
/** * Describes the post fields in JSON format. This is used for the AJAX * edit code. * @return string JSON structure listing key post fields. */ public function prepare_edit_json() { global $USER; $forum = $this->get_forum(); $filecontext = $forum->get_context(true); $fileoptions = array('subdirs' => false, 'maxbytes' => $forum->get_max_bytes()); // Prepare draft area for attachments $draftitemid = 0; file_prepare_draft_area($draftitemid, $filecontext->id, 'mod_forumng', 'attachment', $this->get_id(), $fileoptions); // Prepare draft area for message files $messagedraftitemid = 0; $message = $this->get_raw_message(); $message = file_prepare_draft_area($messagedraftitemid, $filecontext->id, 'mod_forumng', 'message', $this->get_id(), $fileoptions, $message); // Get list of files for main attachment area $options = file_get_drafarea_files($draftitemid, '/'); $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $options->filecount = count($files); // Get list of files for message area $messageoptions = file_get_drafarea_files($messagedraftitemid, '/'); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $messageoptions->itemid, 'id', false); $messageoptions->filecount = count($files); // Put everything together with basic data $basicvalues = (object) array('subject' => $this->get_subject(), 'message' => $message, 'format' => $this->get_format(), 'setimportant' => $this->is_important() ? 1 : 0); $basicvalues->options = $options; $basicvalues->messageoptions = $messageoptions; // Add time limit info $timelimit = $this->can_ignore_edit_time_limit() ? 0 : $this->get_edit_time_limit(); if ($timelimit) { $basicvalues->editlimit = $timelimit - time(); $basicvalues->editlimitmsg = get_string('editlimited', 'forumng', userdate($timelimit - 30, get_string('strftimetime', 'langconfig'))); } else { $basicvalues->editlimit = 0; } // JSON encoding return json_encode($basicvalues); }