/** * Serves assignment feedback and other files. * * @param mixed $course course or id of the course * @param mixed $cm course module or id of the course module * @param context $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function assignfeedback_editpdf_pluginfile($course, $cm, context $context, $filearea, $args, $forcedownload) { global $USER, $DB, $CFG; if ($context->contextlevel == CONTEXT_MODULE) { require_login($course, false, $cm); $itemid = (int) array_shift($args); if (!($assign = $DB->get_record('assign', array('id' => $cm->instance)))) { return false; } $record = $DB->get_record('assign_grades', array('id' => $itemid), 'userid,assignment', MUST_EXIST); $userid = $record->userid; if ($assign->id != $record->assignment) { return false; } // Check is users feedback or has grading permission. if ($USER->id != $userid and !has_capability('mod/assign:grade', $context)) { return false; } $relativepath = implode('/', $args); $fullpath = "/{$context->id}/assignfeedback_editpdf/{$filearea}/{$itemid}/{$relativepath}"; $fs = get_file_storage(); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { return false; } // Download MUST be forced - security! send_stored_file($file, 0, 0, true); // Check if we want to retrieve the stamps. } }
/** * Constructs a file picker object. * * The following are possible options for the filepicker: * - accepted_types (*) * - return_types (FILE_INTERNAL) * - env (filepicker) * - client_id (uniqid) * - itemid (0) * - maxbytes (-1) * - maxfiles (1) * - buttonname (false) * * @param stdClass $options An object containing options for the file picker. */ public function __construct(stdClass $options) { global $CFG, $USER, $PAGE; require_once $CFG->dirroot . '/repository/lib.php'; $defaults = array('accepted_types' => '*', 'return_types' => FILE_INTERNAL, 'env' => 'filepicker', 'client_id' => uniqid(), 'itemid' => 0, 'maxbytes' => -1, 'maxfiles' => 1, 'buttonname' => false); foreach ($defaults as $key => $value) { if (empty($options->{$key})) { $options->{$key} = $value; } } $options->currentfile = ''; if (!empty($options->itemid)) { $fs = get_file_storage(); $usercontext = context_user::instance($USER->id); if (empty($options->filename)) { if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) { $file = reset($files); } } else { $file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename); } if (!empty($file)) { $options->currentfile = html_writer::link(moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()), $file->get_filename()); } } // initilise options, getting files in root path $this->options = initialise_filepicker($options); // copying other options foreach ($options as $name => $value) { if (!isset($this->options->{$name})) { $this->options->{$name} = $value; } } }
/** * File serving. * * @param stdClass $course The course object. * @param stdClass $bi Block instance record. * @param context $context The context object. * @param string $filearea The file area. * @param array $args List of arguments. * @param bool $forcedownload Whether or not to force the download of the file. * @param array $options Array of options. * @return void|false */ function block_xp_pluginfile($course, $bi, $context, $filearea, $args, $forcedownload, array $options = array()) { global $CFG; if ($CFG->block_xp_context == CONTEXT_SYSTEM && $context->contextlevel !== CONTEXT_SYSTEM) { return false; } else { if ($CFG->block_xp_context != CONTEXT_SYSTEM && $context->contextlevel !== CONTEXT_COURSE) { return false; } } $fs = get_file_storage(); $file = null; if ($filearea == 'badges') { // For performance reason, and very low risk, we do not restrict the access to the level badges // to the participant of the course, nor do we check if they have the required level, etc... $itemid = array_shift($args); $filename = array_shift($args); $filepath = '/'; $file = $fs->get_file($context->id, 'block_xp', $filearea, $itemid, $filepath, $filename . '.png'); if (!$file) { $file = $fs->get_file($context->id, 'block_xp', $filearea, $itemid, $filepath, $filename . '.jpg'); } } if (!$file) { return false; } send_stored_file($file); }
/** * Serves the dataformview_tabular template files. * * @param object $course * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ function dataformview_tabular_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { if (!in_array($filearea, dataformview_tabular_tabular::get_file_areas())) { return false; } if ($context->contextlevel == CONTEXT_MODULE) { require_course_login($course, true, $cm); $viewid = (int) array_shift($args); $dataformid = $cm->instance; // Confirm user access. $params = array('dataformid' => $dataformid, 'viewid' => $viewid); if (!mod_dataform\access\view_access::validate($params)) { return false; } $relativepath = implode('/', $args); $fullpath = "/{$context->id}/dataformview_tabular/{$filearea}/{$viewid}/{$relativepath}"; $fs = get_file_storage(); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { return false; } // Finally send the file. send_stored_file($file, 0, 0, true); // Download MUST be forced - security! } return false; }
/** * Serves assignment submissions and other files. * * @param mixed $course course or id of the course * @param mixed $cm course module or id of the course module * @param context $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function assignsubmission_onlinepoodll_pluginfile($course, $cm, context $context, $filearea, $args, $forcedownload) { global $USER, $DB; if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_login($course, false, $cm); $itemid = (int) array_shift($args); //back image is a special case if (!($itemid == 0 && ($filearea = "onlinepoodll_backimage"))) { $record = $DB->get_record('assign_submission', array('id' => $itemid), 'userid, assignment', MUST_EXIST); $userid = $record->userid; if (!($assign = $DB->get_record('assign', array('id' => $cm->instance)))) { return false; } if ($assign->id != $record->assignment) { return false; } // check is users submission or has grading permission if ($USER->id != $userid and !has_capability('mod/assign:grade', $context)) { return false; } } $relativepath = implode('/', $args); $fullpath = "/{$context->id}/assignsubmission_onlinepoodll/{$filearea}/{$itemid}/{$relativepath}"; $fs = get_file_storage(); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { return false; } send_stored_file($file, 0, 0, true); // download MUST be forced - security! }
private function printFile($arg) { global $CFG; $id = trim($arg); if (strlen($id) == 40) { //this is file hash $hash = $id; } else { $fs = get_file_storage(); $file = $fs->get_file_by_id($id); if (!$file) { echo "File '{$id}' not found\n"; return; } $hash = $file->get_contenthash(); } if (isset($CFG->filedir)) { $filedir = $CFG->filedir; } else { $filedir = $CFG->dataroot . '/filedir'; } $l1 = $hash[0] . $hash[1]; $l2 = $hash[2] . $hash[3]; if ($this->expandedOptions['relative']) { echo "filedir/{$l1}/{$l2}/{$hash}\n"; } else { echo "{$filedir}/{$l1}/{$l2}/{$hash}\n"; } }
function copyFromBlock(GcrMdlBlockCourseProfile $parent_block, GcrMdlCourse $course) { $obj = $parent_block->getObject(); // If the instructor set on the parent block also has a teaching role // on this course, copy that instructorid. Otherwise, set it to the // default instructor of this course. $instructorid = 0; $courseid = $course->getObject()->id; $course_context_id = $course->getContext()->id; $instructor = $parent_block->getApp()->getUserById($obj->instructorid); if ($instructor && $course->isInstructor($instructor)) { $instructorid = $obj->instructorid; } else { $instructor = $course->getInstructor(); if ($instructor) { $instructorid = $instructor->getObject()->id; } else { return false; } } $fs = get_file_storage(); $existing_file = $fs->get_file($course_context_id, 'block_course_profile', 'courseicon', 0, '/', $courseid); if ($existing_file) { $existing_file->delete(); } $file = $fs->get_file($parent_block->getContext()->id, 'block_course_profile', 'courseicon', 0, '/', $obj->courseid); $file_record = array('contextid' => $course_context_id, 'component' => 'block_course_profile', 'filearea' => 'courseicon', 'itemid' => 0, 'filepath' => '/', 'filename' => $courseid, 'userid' => $instructorid); $fs->create_file_from_storedfile($file_record, $file); $data = array('courseid' => $courseid, 'instructorid' => $instructorid, 'courseicon' => $obj->courseicon); $course->getApp()->upsertIntoMdlTable('block_course_profile', $data, array('courseid' => $courseid)); }
public function test_import_chapters_events() { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $context = context_module::instance($book->cmid); $record = new stdClass(); $record->contextid = $context->id; $record->component = 'phpunit'; $record->filearea = 'test'; $record->itemid = 0; $record->filepath = '/'; $record->filename = 'chapters.zip'; $fs = get_file_storage(); $file = $fs->create_file_from_pathname($record, __DIR__ . '/fixtures/chapters.zip'); // Importing the chapters. $sink = $this->redirectEvents(); toolbook_importhtml_import_chapters($file, 2, $book, $context, false); $events = $sink->get_events(); // Checking the results. $this->assertCount(5, $events); foreach ($events as $event) { $this->assertInstanceOf('\\mod_book\\event\\chapter_created', $event); $this->assertEquals($context, $event->get_context()); $chapter = $event->get_record_snapshot('book_chapters', $event->objectid); $this->assertNotEmpty($chapter); $this->assertEventContextNotUsed($event); } }
/** * Form for editing HTML block instances. * * @copyright 2010 Petr Skoda (http://skodak.org) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @package block_html * @category files * @param stdClass $course course object * @param stdClass $birecord_or_cm block instance record * @param stdClass $context context object * @param string $filearea file area * @param array $args extra arguments * @param bool $forcedownload whether or not force download * @param array $options additional options affecting the file serving * @return bool */ function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options = array()) { global $SCRIPT; if ($context->contextlevel != CONTEXT_BLOCK) { send_file_not_found(); } require_course_login($course); if ($filearea !== 'content') { send_file_not_found(); } $fs = get_file_storage(); $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } if ($parentcontext = get_context_instance_by_id($birecord_or_cm->parentcontextid)) { if ($parentcontext->contextlevel == CONTEXT_USER) { // force download on all personal pages including /my/ //because we do not have reliable way to find out from where this is used $forcedownload = true; } } else { // weird, there should be parent context, better force dowload then $forcedownload = true; } session_get_instance()->write_close(); send_stored_file($file, 60 * 60, 0, $forcedownload, $options); }
/** * Updates the provided users profile picture based upon the expected fields * returned from the edit or edit_advanced forms. * * @global moodle_database $DB * @param stdClass $usernew An object that contains some information about the user being updated * @param moodleform $userform The form that was submitted to edit the form * @return bool True if the user was updated, false if it stayed the same. */ function useredit_update_picture(stdClass $usernew, moodleform $userform) { global $CFG, $DB; require_once("$CFG->libdir/gdlib.php"); $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST); // This will hold the value to set to the user's picture field at the end of // this function $picturetouse = null; if (!empty($usernew->deletepicture)) { // The user has chosen to delete the selected users picture $fs = get_file_storage(); $fs->delete_area_files($context->id, 'user', 'icon'); // drop all areas $picturetouse = 0; } else if ($iconfile = $userform->save_temp_file('imagefile')) { // There is a new image that has been uploaded // Process the new image and set the user to make use of it. // NOTE: This may be overridden by Gravatar if (process_new_icon($context, 'user', 'icon', 0, $iconfile)) { $picturetouse = 1; } // Delete the file that has now been processed @unlink($iconfile); } // If we have a picture to set we can now do so. Note this will still be NULL // unless the user has changed their picture or caused a change by selecting // to delete their picture or use gravatar if (!is_null($picturetouse)) { $DB->set_field('user', 'picture', $picturetouse, array('id' => $usernew->id)); return true; } return false; }
/** * Serves assignment submissions and other files. * * @param mixed $course course or id of the course * @param mixed $cm course module or id of the course module * @param context $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function assignsubmission_onenote_pluginfile($course, $cm, context $context, $filearea, $args, $forcedownload) { global $DB, $CFG; if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_login($course, false, $cm); $itemid = (int) array_shift($args); $record = $DB->get_record('assign_submission', array('id' => $itemid), 'userid, assignment, groupid', MUST_EXIST); $userid = $record->userid; $groupid = $record->groupid; require_once $CFG->dirroot . '/mod/assign/locallib.php'; $assign = new assign($context, $cm, $course); if ($assign->get_instance()->id != $record->assignment) { return false; } if ($assign->get_instance()->teamsubmission && !$assign->can_view_group_submission($groupid)) { return false; } if (!$assign->get_instance()->teamsubmission && !$assign->can_view_submission($userid)) { return false; } $relativepath = implode('/', $args); $fullpath = "/{$context->id}/assignsubmission_onenote/{$filearea}/{$itemid}/{$relativepath}"; $fs = get_file_storage(); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) { return false; } // Download MUST be forced - security! send_stored_file($file, 0, 0, true); }
function local_filemanager_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) { global $DB; if ($context->contextlevel != CONTEXT_SYSTEM) { return false; } require_login(); if ($filearea != 'attachment') { return false; } $itemid = (int) array_shift($args); if ($itemid != 0) { return false; } $fs = get_file_storage(); $filename = array_pop($args); if (empty($args)) { $filepath = '/'; } else { $filepath = '/' . implode('/', $args) . '/'; } $file = $fs->get_file($context->id, 'local_filemanager', $filearea, $itemid, $filepath, $filename); if (!$file) { return false; } // finally send the file send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security! }
/** * Copy one file from moodle storage to backup storage */ public static function copy_file_moodle2backup($backupid, $filerecorid) { global $DB; // Normalise param if (!is_object($filerecorid)) { $filerecorid = $DB->get_record('files', array('id' => $filerecorid)); } // Directory, nothing to do if ($filerecorid->filename === '.') { return; } $fs = get_file_storage(); $file = $fs->get_file_instance($filerecorid); // If the file is external file, skip copying. if ($file->is_external_file()) { return; } // Calculate source and target paths (use same subdirs strategy for both) $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' . self::get_backup_content_file_location($filerecorid->contenthash); // Create target dir if necessary if (!file_exists(dirname($targetfilepath))) { if (!check_dir_exists(dirname($targetfilepath), true, true)) { throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath)); } } // And copy the file (if doesn't exist already) if (!file_exists($targetfilepath)) { $file->copy_content_to($targetfilepath); } }
public function __construct() { global $USER; $this->context = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0); }
public function __construct($context) { global $USER; $this->context = $context; $fs = get_file_storage(); $this->dir = $fs->get_area_tree($this->context->id, 'user', 'csvenrol', 0); }
/** * Serves the message attachments. Implements needed access control ;-) * * @param object $course * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ function block_jmail_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $SCRIPT; if ($context->contextlevel != CONTEXT_BLOCK) { //send_file_not_found(); } require_course_login($course); $coursecontext = block_jmail_get_context(CONTEXT_COURSE, $course->id, MUST_EXIST); // The mailbox constructor does the permission validation if (!($mailbox = new block_jmail_mailbox($course, $coursecontext, $context))) { return; } $messageid = (int) array_shift($args); $message = block_jmail_message::get_from_id($messageid); // We check if we are the senders or the receivers if (!$message) { send_file_not_found(); } $pendingaprobal = !$message->approved and has_capability('block/jmail:approvemessages', $context); if (!$message->is_mine() and !$pendingaprobal) { send_file_not_found(); } $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/{$context->id}/block_jmail/{$filearea}/{$messageid}/{$relativepath}"; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } $forcedownload = true; send_stored_file($file, 60 * 60, 0, $forcedownload); }
/** * Deletes an assignment instance * * @param $id */ function assignment_delete_instance($id) { global $CFG, $DB; if (!($assignment = $DB->get_record('assignment', array('id' => $id)))) { return false; } $result = true; // Now get rid of all files $fs = get_file_storage(); if ($cm = get_coursemodule_from_instance('assignment', $assignment->id)) { $context = context_module::instance($cm->id); $fs->delete_area_files($context->id); } if (!$DB->delete_records('assignment_submissions', array('assignment' => $assignment->id))) { $result = false; } if (!$DB->delete_records('event', array('modulename' => 'assignment', 'instance' => $assignment->id))) { $result = false; } if (!$DB->delete_records('assignment', array('id' => $assignment->id))) { $result = false; } grade_update('mod/assignment', $assignment->course, 'mod', 'assignment', $assignment->id, 0, NULL, array('deleted' => 1)); return $result; }
/** * Serves seplment feedback and other files. * * @param mixed $course course or id of the course * @param mixed $cm course module or id of the course module * @param context $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - just send the file */ function seplfeedback_file_pluginfile($course, $cm, context $context, $filearea, $args, $forcedownload) { global $USER, $DB; if ($context->contextlevel != CONTEXT_MODULE) { return false; } require_login($course, false, $cm); $itemid = (int) array_shift($args); $record = $DB->get_record('sepl_grades', array('id' => $itemid), 'userid,seplment', MUST_EXIST); $userid = $record->userid; if (!($sepl = $DB->get_record('sepl', array('id' => $cm->instance)))) { return false; } if ($sepl->id != $record->seplment) { return false; } // Check is users feedback or has grading permission. if ($USER->id != $userid and !has_capability('mod/sepl:grade', $context)) { return false; } $relativepath = implode('/', $args); $fullpath = "/{$context->id}/seplfeedback_file/{$filearea}/{$itemid}/{$relativepath}"; $fs = get_file_storage(); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { return false; } // Download MUST be forced - security! send_stored_file($file, 0, 0, true); }
/** * Open the file * * @param int $mode One of RLIP_FILE_READ or RLIP_FILE_WRITE, specifying * the mode in which the file should be opened */ function open($mode) { global $CFG; $fs = get_file_storage(); if ($mode == RLIP_FILE_WRITE) { if ($this->sendtobrowser) { // must buffer output in case of errors $this->tempfile = tempnam(sys_get_temp_dir(), basename($this->filename)); $this->filepointer = fopen($this->tempfile, 'w'); } else { //we are only writing to files on the file-system $this->filepointer = fopen($this->filename, 'w'); } } else { if ($this->filename != '') { //read from the file system $this->filepointer = fopen($this->filename, 'r'); } else { //read from a Moodle file $file = $fs->get_file_by_id($this->fileid); $this->filepointer = $file->get_content_file_handle(); } } $this->first = true; $this->header = NULL; }
public function save_question_options($question) { global $DB; $result = new stdClass(); $context = $question->context; // Fetch old answer ids so that we can reuse them. $oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'); // Save the true answer - update an existing answer if possible. $answer = array_shift($oldanswers); if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; $answer->answer = ''; $answer->feedback = ''; $answer->id = $DB->insert_record('question_answers', $answer); } $answer->answer = get_string('true', 'qtype_truefalse'); $answer->fraction = $question->correctanswer; $answer->feedback = $this->import_or_save_files($question->feedbacktrue, $context, 'question', 'answerfeedback', $answer->id); $answer->feedbackformat = $question->feedbacktrue['format']; $DB->update_record('question_answers', $answer); $trueid = $answer->id; // Save the false answer - update an existing answer if possible. $answer = array_shift($oldanswers); if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; $answer->answer = ''; $answer->feedback = ''; $answer->id = $DB->insert_record('question_answers', $answer); } $answer->answer = get_string('false', 'qtype_truefalse'); $answer->fraction = 1 - (int) $question->correctanswer; $answer->feedback = $this->import_or_save_files($question->feedbackfalse, $context, 'question', 'answerfeedback', $answer->id); $answer->feedbackformat = $question->feedbackfalse['format']; $DB->update_record('question_answers', $answer); $falseid = $answer->id; // Delete any left over old answer records. $fs = get_file_storage(); foreach ($oldanswers as $oldanswer) { $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); $DB->delete_records('question_answers', array('id' => $oldanswer->id)); } // Save question options in question_truefalse table. if ($options = $DB->get_record('question_truefalse', array('question' => $question->id))) { // No need to do anything, since the answer IDs won't have changed // But we'll do it anyway, just for robustness. $options->trueanswer = $trueid; $options->falseanswer = $falseid; $DB->update_record('question_truefalse', $options); } else { $options = new stdClass(); $options->question = $question->id; $options->trueanswer = $trueid; $options->falseanswer = $falseid; $DB->insert_record('question_truefalse', $options); } $this->save_hints($question); return true; }
/** * EditPDF upgrade code * @param int $oldversion * @return bool */ function xmldb_assignfeedback_editpdf_upgrade($oldversion) { global $CFG; if ($oldversion < 2013110800) { // Check that no stamps where uploaded. $fs = get_file_storage(); $stamps = $fs->get_area_files(context_system::instance()->id, 'assignfeedback_editpdf', 'stamps', 0, "filename", false); // Add default stamps. if (empty($stamps)) { // List of default stamps. $defaultstamps = array('smile.png', 'sad.png', 'tick.png', 'cross.png'); // Stamp file object. $filerecord = new stdClass(); $filerecord->component = 'assignfeedback_editpdf'; $filerecord->contextid = context_system::instance()->id; $filerecord->userid = get_admin()->id; $filerecord->filearea = 'stamps'; $filerecord->filepath = '/'; $filerecord->itemid = 0; // Add all default stamps. foreach ($defaultstamps as $stamp) { $filerecord->filename = $stamp; $fs->create_file_from_pathname($filerecord, $CFG->dirroot . '/mod/assign/feedback/editpdf/pix/' . $filerecord->filename); } } upgrade_plugin_savepoint(true, 2013110800, 'assignfeedback', 'editpdf'); } // Moodle v2.6.0 release upgrade line. // Put any upgrade step following this. // Moodle v2.7.0 release upgrade line. // Put any upgrade step following this. // Moodle v2.8.0 release upgrade line. // Put any upgrade step following this. return true; }
/** * Retrieve a file for a user of the Moodle client calling this function * The file is encoded in base64 * @global <type> $DB * @global <type> $USER * @global <type> $MNET_REMOTE_CLIENT * @param <type> $username * @param <type> $source * @return <type> */ public function retrieveFile($username, $source) { global $DB, $USER, $MNET_REMOTE_CLIENT; ///check the the user is known ///he has to be previously connected to the server site in order to be in the database //TODO: this seems weird - is it executed from cron or what? Please review $USER = $DB->get_record('user', array('username' => $username, 'mnethostid' => $MNET_REMOTE_CLIENT->id)); if (empty($USER)) { exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle', $username))); } $file = unserialize(base64_decode($source)); $contextid = $file[0]; $filearea = $file[1]; $itemid = $file[2]; $filepath = $file[3]; $filename = $file[4]; ///check that the user has read permission on this file $browser = get_file_browser(); $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename); if (empty($fileinfo)) { exit(mnet_server_fault(9016, get_string('usercannotaccess', 'repository_remotemoodle', $file))); } ///retrieve the file with file API functions and return it encoded in base64 $fs = get_file_storage(); $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename); $contents = base64_encode($sf->get_content()); return array($contents, $sf->get_filename()); }
function validation($data, $files) { global $USER; if ($errors = parent::validation($data, $files)) { return $errors; } $usercontext = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['package'], 'id', false)) { if (!$this->current->instance) { $errors['package'] = get_string('required'); return $errors; } } else { $file = reset($files); if ($file->get_mimetype() != 'application/zip') { $errors['package'] = get_string('invalidfiletype', 'error', '', $file); // better delete current file, it is not usable anyway $fs->delete_area_files($usercontext->id, 'user', 'draft', $data['package']); } } return $errors; }
/** * Gets a stored file for the backup course filearea directory. * * @param int $itemid item ID * @param string $filepath file path * @param string $filename file name * @return file_info|null file_info instance or null if not found or access not allowed */ protected function get_area_backup_course($itemid, $filepath, $filename) { global $CFG; if (!isloggedin()) { return null; } if (!has_any_capability(array('moodle/backup:backupcourse', 'moodle/restore:restorecourse'), $this->context)) { return null; } if (is_null($itemid)) { return $this; } $fs = get_file_storage(); $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; if (!($storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename))) { if ($filepath === '/' && $filename === '.') { $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0); } else { // Not found. return null; } } $downloadable = has_capability('moodle/backup:downloadfile', $this->context); $uploadable = has_capability('moodle/restore:uploadfile', $this->context); $urlbase = $CFG->wwwroot . '/pluginfile.php'; return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false); }
function validation($data, $files) { global $USER; if ($errors = parent::validation($data, $files)) { return $errors; } $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); if (!($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['importfile'], 'id', false))) { $errors['importfile'] = get_string('required'); return $errors; } else { $file = reset($files); if ($file->get_mimetype() != 'application/zip') { $errors['importfile'] = get_string('invalidfiletype', 'error', $file->get_filename()); // better delete current file, it is not usable anyway $fs->delete_area_files($usercontext->id, 'user', 'draft', $data['importfile']); } else { if (!($chpterfiles = toolbook_importhtml_get_chapter_files($file, $data['type']))) { $errors['importfile'] = get_string('errornochapters', 'booktool_importhtml'); } } } return $errors; }
/** * Updates the provided users profile picture based upon the expected fields * returned from the edit or edit_advanced forms. * * @global moodle_database $DB * @param stdClass $usernew An object that contains some information about the user being updated * @param moodleform $userform The form that was submitted to edit the form * @return bool True if the user was updated, false if it stayed the same. */ function useredit_update_picture(stdClass $usernew, moodleform $userform) { global $CFG, $DB; require_once "{$CFG->libdir}/gdlib.php"; $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST); $user = $DB->get_record('user', array('id' => $usernew->id), 'id, picture', MUST_EXIST); $newpicture = $user->picture; if (!empty($usernew->deletepicture)) { // The user has chosen to delete the selected users picture $fs = get_file_storage(); $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area $newpicture = 0; } else { if ($iconfile = $userform->save_temp_file('imagefile')) { // There is a new image that has been uploaded // Process the new image and set the user to make use of it. // NOTE: Uploaded images always take over Gravatar $newpicture = (int) process_new_icon($context, 'user', 'icon', 0, $iconfile); // Delete the file that has now been processed @unlink($iconfile); } } if ($newpicture != $user->picture) { $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id)); return true; } else { return false; } }
public function test_create_instance() { global $DB, $CFG, $USER; $this->resetAfterTest(); $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $this->assertFalse($DB->record_exists('imscp', array('course' => $course->id))); $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course)); $records = $DB->get_records('imscp', array('course' => $course->id), 'id'); $this->assertEquals(1, count($records)); $this->assertTrue(array_key_exists($imscp->id, $records)); $params = array('course' => $course->id, 'name' => 'Another imscp'); $imscp = $this->getDataGenerator()->create_module('imscp', $params); $records = $DB->get_records('imscp', array('course' => $course->id), 'id'); $this->assertEquals(2, count($records)); $this->assertEquals('Another imscp', $records[$imscp->id]->name); // Examples of specifying the package file (do not validate anything, just check for exceptions). // 1. As path to the file in filesystem: $params = array('course' => $course->id, 'packagepath' => $CFG->dirroot . '/mod/imscp/tests/packages/singlescobasic.zip'); $imscp = $this->getDataGenerator()->create_module('imscp', $params); // 2. As file draft area id: $fs = get_file_storage(); $params = array('course' => $course->id, 'package' => file_get_unused_draft_itemid()); $usercontext = context_user::instance($USER->id); $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $params['package'], 'filename' => 'singlescobasic.zip', 'filepath' => '/'); $fs->create_file_from_pathname($filerecord, $CFG->dirroot . '/mod/imscp/tests/packages/singlescobasic.zip'); $imscp = $this->getDataGenerator()->create_module('imscp', $params); }
/** * Convenience to take a fixture test file and create a stored_file. * * @param string $filepath * @return stored_file */ protected function create_stored_file_from_path($filepath) { $syscontext = context_system::instance(); $filerecord = array('contextid' => $syscontext->id, 'component' => 'mod_scorm', 'filearea' => 'unittest', 'itemid' => 0, 'filepath' => '/', 'filename' => basename($filepath)); $fs = get_file_storage(); return $fs->create_file_from_pathname($filerecord, $filepath); }
function atto_ejsapp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) { // Make sure the user is logged in and has access to the module (plugins that are not course modules should leave out the 'cm' part). require_login($course, true, $cm); // Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead). $itemid = array_shift($args); // The first item in the $args array. // Use the itemid to retrieve any relevant data records and perform any security checks to see if the // user really does have access to the file in question. // Extract the filename / filepath from the $args array. $filename = array_pop($args); // The last item in the $args array. if (!$args) { $filepath = '/'; // $args is empty => the path is '/' } else { $filepath = '/' . implode('/', $args) . '/'; // $args contains elements of the filepath } // Retrieve the file from the Files API. $fs = get_file_storage(); $file = $fs->get_file($context->id, 'atto_ejsapp', $filearea, $itemid, $filepath, $filename); if (!$file) { return false; // The file does not exist. } // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering. // From Moodle 2.3, use send_stored_file instead. send_stored_file($file, 86400, 0, $forcedownload, $options); }
function instance_delete() { global $DB; $fs = get_file_storage(); $fs->delete_area_files($this->context->id, 'block_html'); return true; }