public function add_file($section, $name, $path, $display = 0) { global $DB, $CFG, $USER; $section = (int) $section; $display = (int) $display; require_once $CFG->dirroot . '/course/lib.php'; require_once $CFG->dirroot . '/mod/resource/lib.php'; require_once $CFG->dirroot . '/mod/resource/locallib.php'; $params = self::validate_parameters(self::add_file_parameters(), array('section' => $section, 'name' => $name, 'path' => $path, 'display' => $display)); $section = $DB->get_record('course_sections', array('id' => $section), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST); self::require_access($course->id); // finally create the file item // first add course_module record because we need the context $newcm = new stdClass(); $newcm->course = $course->id; $newcm->module = $DB->get_field('modules', 'id', array('name' => 'resource')); // not known yet, will be updated later (this is similar to restore code) $newcm->instance = 0; $newcm->section = $params['section']; $newcm->visible = 1; $newcm->groupmode = 0; $newcm->groupingid = 0; $newcm->groupmembersonly = 0; if (!($coursemodule = add_course_module($newcm))) { throw new invalid_parameter_exception('Error creating course module'); } $config = get_config('resource'); $module = new stdClass(); $module->course = $course->id; $module->name = format_string($params['name']); $module->intro = '<p></p>'; $module->introformat = 1; $module->coursemodule = $coursemodule; if (!$display) { $module->display = $config->display; } else { $module->display = $display; } $module->popupwidth = $config->popupwidth; $module->popupheight = $config->popupheight; $module->printheading = $config->printheading; $module->printintro = $config->printintro; // 'Show size' support only from Moodle 2.3 / OU moodle April 2012 if (isset($config->showsize)) { $module->showsize = $config->showsize; $module->showtype = $config->showtype; } $module->filterfiles = $config->filterfiles; $module->section = $section->section; //check $params['path'] and create files based on that and attach to $module->files //now check $path and obtain $filename and $filepath $contextuser = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); $module->files = 0; file_prepare_draft_area($module->files, null, null, null, null); $fileinfo = array('contextid' => $contextuser->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $module->files, 'filepath' => '/', 'filename' => basename($params['path']), 'timecreated' => time(), 'timemodified' => time(), 'mimetype' => mimeinfo('type', $path), 'userid' => $USER->id); if (strpos($params['path'], '://') === false) { //this is a path if (!file_exists($params['path'])) { throw new invalid_parameter_exception('Error accessing filepath - file may not exist.'); } $fs->create_file_from_pathname($fileinfo, $params['path']); } else { //this is a URL - download the file first. $content = download_file_content($params['path']); if ($content === false) { throw new invalid_parameter_exception('Error accessing file - url may not exist.'); } $fs->create_file_from_string($fileinfo, $content); } $module->instance = resource_add_instance($module, array()); $DB->set_field('course_modules', 'instance', $module->instance, array('id' => $coursemodule)); add_mod_to_section($module); rebuild_course_cache($course->id, true); return array('id' => $coursemodule); }
/** * Handle a file that has been uploaded * @param object $uploadinfo details of the file / content that has been uploaded * @return int instance id of the newly created mod */ function resource_dndupload_handle($uploadinfo) { // Gather the required info. $data = new stdClass(); $data->course = $uploadinfo->course->id; $data->name = $uploadinfo->displayname; $data->intro = ''; $data->introformat = FORMAT_HTML; $data->coursemodule = $uploadinfo->coursemodule; $data->files = $uploadinfo->draftitemid; // Set the display options to the site defaults. $config = get_config('resource'); $data->display = $config->display; $data->popupheight = $config->popupheight; $data->popupwidth = $config->popupwidth; $data->printintro = $config->printintro; $data->showsize = (isset($config->showsize)) ? $config->showsize : 0; $data->showtype = (isset($config->showtype)) ? $config->showtype : 0; $data->showdate = (isset($config->showdate)) ? $config->showdate : 0; $data->filterfiles = $config->filterfiles; return resource_add_instance($data, null); }
/** * Creates new resource module instance. By default it contains a short * text file. * * @param array|stdClass $record Resource module record, as from form * @param array $options Standard options about how to create it * @return stdClass Activity record, with extra cmid field */ public function create_instance($record = null, array $options = null) { global $CFG, $USER; require_once($CFG->dirroot . '/mod/resource/locallib.php'); // Count generated modules. $this->instancecount++; $i = $this->instancecount; // Ensure the record can be modified without affecting calling code. $record = (object)(array)$record; $options = (array)$options; // Course is required. if (empty($record->course)) { throw new coding_exception('module generator requires $record->course'); } // Fill in optional values if not specified. if (!isset($record->name)) { $record->name = get_string('pluginname', 'resource') . ' ' . $i; } if (!isset($record->intro)) { $record->intro = 'Test resource ' . $i; } if (!isset($record->introformat)) { $record->introformat = FORMAT_MOODLE; } if (!isset($record->display)) { $record->display = RESOURCELIB_DISPLAY_AUTO; } if (isset($options['idnumber'])) { $record->cmidnumber = $options['idnumber']; } else { $record->cmidnumber = ''; } if (!isset($record->printheading)) { $record->printheading = 1; } if (!isset($record->printintro)) { $record->printintro = 0; } if (!isset($record->showsize)) { $record->showsize = 0; } if (!isset($record->showtype)) { $record->showtype = 0; } // The 'files' value corresponds to the draft file area ID. If not // specified, create a default file. if (!isset($record->files)) { if (empty($USER->username) || $USER->username === 'guest') { throw new coding_exception('resource generator requires a current user'); } $usercontext = context_user::instance($USER->id); // Pick a random context id for specified user. $record->files = file_get_unused_draft_itemid(); // Add actual file there. $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $record->files, 'filename' => 'resource' . $i . '.txt', 'filepath' => '/'); $fs = get_file_storage(); $fs->create_file_from_string($filerecord, 'Test resource ' . $i . ' file'); } // Do work to actually add the instance. $record->coursemodule = $this->precreate_course_module($record->course, $options); $id = resource_add_instance($record, null); return $this->post_add_instance($id, $record->coursemodule); }
$fromform->name = $_FILES['Filedata']['name']; $fromform->summary = ''; $fromform->reference = $dirname . clean_filename($_FILES['Filedata']['name']); $fromform->windowpopup = 0; $fromform->visible = 1; $fromform->course = $course->id; $fromform->coursemodule = ''; $fromform->section = $section->section; $fromform->module = $module->id; $fromform->modulename = $module->name; $fromform->instance = ''; $fromform->add = $module->name; $fromform->update = 0; $fromform->return = 0; $fromform->submitbutton = 'save'; $returnfromfunc = resource_add_instance($fromform); if (!$returnfromfunc) { echo 'ng'; } if (!isset($fromform->groupmode)) { // to deal with pre-1.5 modules $fromform->groupmode = $course->groupmode; /// Default groupmode the same as course } $fromform->instance = $returnfromfunc; // course_modules and course_sections each contain a reference // to each other, so we have to update one of them twice. if (!($fromform->coursemodule = add_course_module($fromform))) { echo 'ng'; } if (!($sectionid = add_mod_to_section($fromform))) {
$data->popupheight = 450; // Create the course module $data->coursemodule = add_course_module($data); unset($data->id); $data->display = $display; if ($data->display === false) { $data->display = RESOURCELIB_DISPLAY_AUTO; } if ($type == 'Files') { // Create the relevant file $fs = get_file_storage(); $cmcontext = get_context_instance(CONTEXT_MODULE, $data->coursemodule); $fileinfo = array('contextid' => $cmcontext->id, 'component' => 'mod_resource', 'filearea' => 'content', 'itemid' => 0, 'filepath' => '/', 'filename' => $filename, 'userid' => $USER->id, 'license' => $CFG->sitedefaultlicense, 'author' => fullname($USER)); $fs->create_file_from_pathname($fileinfo, $filesrc); // Create the resouce database entry $data->instance = resource_add_instance($data, $data); } else { if ($type == 'url') { // Create the url database entry unset($data->id); $data->externalurl = $contents; $data->instance = url_add_instance($data, $data); } else { // Create the page database entry unset($data->id); $data->page = array('text' => $contents, 'itemid' => false); if ($type == 'text') { $data->page['format'] = FORMAT_PLAIN; } else { $data->page['format'] = FORMAT_HTML; }
/** * Handle a file that has been uploaded * @param object $uploadinfo details of the file / content that has been uploaded * @return int instance id of the newly created mod */ function mod_resource_dndupload_handle($uploadinfo) { // Gather the required info. $data = new stdClass(); $data->course = $uploadinfo->course->id; $data->name = $uploadinfo->displayname; $data->intro = '<p>' . $uploadinfo->displayname . '</p>'; $data->introformat = FORMAT_HTML; $data->coursemodule = $uploadinfo->coursemodule; $data->files = $uploadinfo->draftitemid; // Set the display options to the site defaults. $config = get_config('resource'); $data->display = $config->display; $data->popupheight = $config->popupheight; $data->popupwidth = $config->popupwidth; $data->printheading = $config->printheading; $data->printintro = $config->printintro; return resource_add_instance($data, null); }