function toHtml() { global $CFG, $COURSE, $USER, $PAGE, $OUTPUT; $id = $this->_attributes['id']; $elname = $this->_attributes['name']; if ($this->_flagFrozen) { return $this->getFrozenHtml(); } if (!$draftitemid = (int)$this->getValue()) { // no existing area info provided - let's use fresh new draft area $draftitemid = file_get_unused_draft_itemid(); $this->setValue($draftitemid); } if ($COURSE->id == SITEID) { $context = get_context_instance(CONTEXT_SYSTEM); } else { $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); } $client_id = uniqid(); $args = new stdClass(); // need these three to filter repositories list $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*'; $args->return_types = FILE_INTERNAL; $args->itemid = $draftitemid; $args->maxbytes = $this->_options['maxbytes']; $args->context = $PAGE->context; $args->buttonname = $elname.'choose'; $html = $this->_getTabs(); $fp = new file_picker($args); $options = $fp->options; $options->context = $PAGE->context; $html .= $OUTPUT->render($fp); $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>'; $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker')); $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module); $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array( 'env'=>'filepicker', 'action'=>'browse', 'itemid'=>$draftitemid, 'subdirs'=>0, 'maxbytes'=>$options->maxbytes, 'maxfiles'=>1, 'ctx_id'=>$PAGE->context->id, 'course'=>$PAGE->course->id, 'sesskey'=>sesskey(), )); // non js file picker $html .= '<noscript>'; $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>"; $html .= '</noscript>'; return $html; }
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); }
public function create_instance($record = null, array $options = null) { global $CFG, $USER; require_once $CFG->dirroot . '/mod/scorm/lib.php'; require_once $CFG->dirroot . '/mod/scorm/locallib.php'; $cfgscorm = get_config('scorm'); // Add default values for scorm. $record = (array) $record + array('scormtype' => SCORM_TYPE_LOCAL, 'packagefile' => '', 'packagefilepath' => $CFG->dirroot . '/mod/scorm/tests/packages/singlescobasic.zip', 'packageurl' => '', 'updatefreq' => SCORM_UPDATE_NEVER, 'popup' => 0, 'width' => $cfgscorm->framewidth, 'height' => $cfgscorm->frameheight, 'skipview' => $cfgscorm->skipview, 'hidebrowse' => $cfgscorm->hidebrowse, 'displaycoursestructure' => $cfgscorm->displaycoursestructure, 'hidetoc' => $cfgscorm->hidetoc, 'nav' => $cfgscorm->nav, 'navpositionleft' => $cfgscorm->navpositionleft, 'navpositiontop' => $cfgscorm->navpositiontop, 'displayattemptstatus' => $cfgscorm->displayattemptstatus, 'timeopen' => 0, 'timeclose' => 0, 'grademethod' => GRADESCOES, 'maxgrade' => $cfgscorm->maxgrade, 'maxattempt' => $cfgscorm->maxattempt, 'whatgrade' => $cfgscorm->whatgrade, 'forcenewattempt' => $cfgscorm->forcenewattempt, 'lastattemptlock' => $cfgscorm->lastattemptlock, 'forcecompleted' => $cfgscorm->forcecompleted, 'auto' => $cfgscorm->auto); // The 'packagefile' value corresponds to the draft file area ID. If not specified, create from packagefilepath. if (empty($record['packagefile']) && $record['scormtype'] === SCORM_TYPE_LOCAL) { if (!isloggedin() || isguestuser()) { throw new coding_exception('Scorm generator requires a current user'); } if (!file_exists($record['packagefilepath'])) { throw new coding_exception("File {$record['packagefilepath']} does not exist"); } $usercontext = context_user::instance($USER->id); // Pick a random context id for specified user. $record['packagefile'] = file_get_unused_draft_itemid(); // Add actual file there. $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $record['packagefile'], 'filename' => basename($record['packagefilepath']), 'filepath' => '/'); $fs = get_file_storage(); $fs->create_file_from_pathname($filerecord, $record['packagefilepath']); } return parent::create_instance($record, (array) $options); }
public function test_assessable_uploaded() { $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $params['course'] = $course->id; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); $context = context_module::instance($cm->id); $assign = new testable_assign($context, $cm, $course); $this->setUser($user->id); $submission = $assign->get_user_submission($user->id, true); $data = new stdClass(); $data->onlinetext_editor = array('itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', 'format' => FORMAT_PLAIN); $plugin = $assign->get_submission_plugin_by_type('onlinetext'); $sink = $this->redirectEvents(); $plugin->save($submission, $data); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); $this->assertInstanceOf('\\assignsubmission_onlinetext\\event\\assessable_uploaded', $event); $this->assertEquals($context->id, $event->contextid); $this->assertEquals($submission->id, $event->objectid); $this->assertEquals(array(), $event->other['pathnamehashes']); $this->assertEquals('Submission text', $event->other['content']); $expected = new stdClass(); $expected->modulename = 'assign'; $expected->cmid = $cm->id; $expected->itemid = $submission->id; $expected->courseid = $course->id; $expected->userid = $user->id; $expected->content = 'Submission text'; $this->assertEventLegacyData($expected, $event); }
public function create_instance($record = null, array $options = null) { global $CFG; require_once $CFG->libdir . '/filelib.php'; $workshopconfig = get_config('workshop'); // Add default values for workshop. $record = (array) $record + array('strategy' => $workshopconfig->strategy, 'grade' => $workshopconfig->grade, 'gradinggrade' => $workshopconfig->gradinggrade, 'gradedecimals' => $workshopconfig->gradedecimals, 'nattachments' => 1, 'maxbytes' => $workshopconfig->maxbytes, 'latesubmissions' => 0, 'useselfassessment' => 0, 'overallfeedbackmode' => 1, 'overallfeedbackfiles' => 0, 'overallfeedbackmaxbytes' => $workshopconfig->maxbytes, 'useexamples' => 0, 'examplesmode' => $workshopconfig->examplesmode, 'submissionstart' => 0, 'submissionend' => 0, 'phaseswitchassessment' => 0, 'assessmentstart' => 0, 'assessmentend' => 0); if (!isset($record['gradecategory']) || !isset($record['gradinggradecategory'])) { require_once $CFG->libdir . '/gradelib.php'; $courseid = is_object($record['course']) ? $record['course']->id : $record['course']; $gradecategories = grade_get_categories_menu($courseid); reset($gradecategories); $defaultcategory = key($gradecategories); $record += array('gradecategory' => $defaultcategory, 'gradinggradecategory' => $defaultcategory); } if (!isset($record['instructauthorseditor'])) { $record['instructauthorseditor'] = array('text' => 'Instructions for submission ' . ($this->instancecount + 1), 'format' => FORMAT_MOODLE, 'itemid' => file_get_unused_draft_itemid()); } if (!isset($record['instructreviewerseditor'])) { $record['instructreviewerseditor'] = array('text' => 'Instructions for assessment ' . ($this->instancecount + 1), 'format' => FORMAT_MOODLE, 'itemid' => file_get_unused_draft_itemid()); } if (!isset($record['conclusioneditor'])) { $record['conclusioneditor'] = array('text' => 'Conclusion ' . ($this->instancecount + 1), 'format' => FORMAT_MOODLE, 'itemid' => file_get_unused_draft_itemid()); } return parent::create_instance($record, (array) $options); }
function display_add_field($recordid = 0) { global $CFG, $DB, $OUTPUT, $PAGE, $USER; $file = false; $content = false; $displayname = ''; $fs = get_file_storage(); $context = $PAGE->context; $itemid = null; // editing an existing database entry if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); if (!empty($content->content)) { if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { $usercontext = context_user::instance($USER->id); if (!($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false))) { return false; } if (empty($content->content1)) { // Print icon if file already exists $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename()); $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')) . '<a href="' . $src . '" >' . s($file->get_filename()) . '</a>'; } else { $displayname = 'no file added'; } } } } } else { $itemid = file_get_unused_draft_itemid(); } $html = ''; // database entry label $html .= '<div title="' . s($this->field->description) . '">'; $html .= '<fieldset><legend><span class="accesshide">' . $this->field->name . '</span></legend>'; // itemid element $html .= '<input type="hidden" name="field_' . $this->field->id . '_file" value="' . $itemid . '" />'; $options = new stdClass(); $options->maxbytes = $this->field->param3; $options->maxfiles = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future. $options->itemid = $itemid; $options->accepted_types = '*'; $options->return_types = FILE_INTERNAL; $options->context = $PAGE->context; $fm = new form_filemanager($options); // Print out file manager. $output = $PAGE->get_renderer('core', 'files'); $html .= $output->render($fm); $html .= '</fieldset>'; $html .= '</div>'; $module = array('name' => 'form_filemanager', 'fullpath' => '/lib/form/filemanager.js', 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'), 'strings' => array(array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'), array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'), array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'), array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'), array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'), array('confirmrenamefile', 'repository'), array('edit', 'moodle'))); $PAGE->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module); return $html; }
public function toHtml() { global $CFG, $COURSE, $USER, $PAGE, $OUTPUT; $id = $this->_attributes['id']; $elname = $this->_attributes['name']; if ($this->_flagFrozen) { return $this->getFrozenHtml(); } if (!($draftitemid = (int) $this->getValue())) { // no existing area info provided - let's use fresh new draft area $draftitemid = file_get_unused_draft_itemid(); $this->setValue($draftitemid); } if ($COURSE->id == SITEID) { $context = context_system::instance(); } else { $context = context_course::instance($COURSE->id); } $client_id = uniqid(); $args = new stdClass(); // Need these three to filter repositories list. $args->accepted_types = $this->_options['accepted_types'] ? $this->_options['accepted_types'] : '*'; $args->return_types = $this->_options['return_types']; $args->itemid = $draftitemid; $args->maxbytes = 0; $args->context = $PAGE->context; $args->buttonname = $elname . 'choose'; $args->elementname = $elname; // We can only tell the filepicker that we want FILE_REFERENCE repos // and which specific repo types we don't want. So here we build a list // of all FILE_REFERENCE supplying repos that aren't thebox to force // it to only display that one. $refrepos = repository::get_instances(array('currentcontext' => $PAGE->context, 'return_types' => FILE_REFERENCE)); $disabled = array(); foreach ($refrepos as $repo) { if (($name = $repo->get_typename()) != $this->repo) { $disabled[] = $name; } } $args->disable_types = $disabled; $html = $this->_getTabs(); $fp = new file_picker($args); $options = $fp->options; $options->context = $PAGE->context; $html .= $OUTPUT->render($fp); $html .= '<input type="hidden" name="' . $elname . '" id="' . $id . '" value="' . $draftitemid . '" class="filepickerhidden"/>'; $module = array('name' => 'form_filepicker', 'fullpath' => '/lib/form/filepicker.js', 'requires' => array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload')); $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module); $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array('env' => 'filepicker', 'action' => 'browse', 'itemid' => $draftitemid, 'subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1, 'ctx_id' => $PAGE->context->id, 'course' => $PAGE->course->id, 'sesskey' => sesskey())); // non js file picker $html .= '<noscript>'; $html .= "<div><object type='text/html' data='{$nonjsfilepicker}' height='160' width='600' style='border:1px solid #000'></object></div>"; $html .= '</noscript>'; return $html; }
/** * Returns an XHTML string for the editor * * @param string $data * @param string $query * @return string XHTML string for the editor */ public function output_html($data, $query = '') { global $USER; $default = $this->get_defaultsetting(); $defaultinfo = $default; if (!is_null($default) and $default !== '') { $defaultinfo = "\n" . $default; } $ctx = context_user::instance($USER->id); $editor = editors_get_preferred_editor(FORMAT_HTML); $options = $this->get_options(); $draftitemid = file_get_unused_draft_itemid(); $component = is_null($this->plugin) ? 'core' : $this->plugin; $data = file_prepare_draft_area($draftitemid, $options['context']->id, $component, $this->get_full_name() . '_draftitemid', $draftitemid, $options, $data); $fpoptions = array(); $args = new stdClass(); // need these three to filter repositories list $args->accepted_types = array('web_image'); $args->return_types = $options['return_types']; $args->context = $ctx; $args->env = 'filepicker'; // advimage plugin $image_options = initialise_filepicker($args); $image_options->context = $ctx; $image_options->client_id = uniqid(); $image_options->maxbytes = $options['maxbytes']; $image_options->areamaxbytes = $options['areamaxbytes']; $image_options->env = 'editor'; $image_options->itemid = $draftitemid; // moodlemedia plugin $args->accepted_types = array('video', 'audio'); $media_options = initialise_filepicker($args); $media_options->context = $ctx; $media_options->client_id = uniqid(); $media_options->maxbytes = $options['maxbytes']; $media_options->areamaxbytes = $options['areamaxbytes']; $media_options->env = 'editor'; $media_options->itemid = $draftitemid; // advlink plugin $args->accepted_types = '*'; $link_options = initialise_filepicker($args); $link_options->context = $ctx; $link_options->client_id = uniqid(); $link_options->maxbytes = $options['maxbytes']; $link_options->areamaxbytes = $options['areamaxbytes']; $link_options->env = 'editor'; $link_options->itemid = $draftitemid; $fpoptions['image'] = $image_options; $fpoptions['media'] = $media_options; $fpoptions['link'] = $link_options; $editor->use_editor($this->get_id(), $options, $fpoptions); return format_admin_setting($this, $this->visiblename, '<div class="form-textarea"><textarea rows="' . $this->rows . '" cols="' . $this->cols . '" id="' . $this->get_id() . '" name="' . $this->get_full_name() . '" spellcheck="true">' . s($data) . '</textarea></div> <input value="' . $draftitemid . '" name="' . $this->get_full_name() . '_draftitemid" type="hidden" />', $this->description, true, '', $defaultinfo, $query); }
public function create_instance($record = null, array $options = null) { global $CFG; // Add default values for lesson. $record = (array) $record + array('progressbar' => 0, 'ongoing' => 0, 'displayleft' => 0, 'displayleftif' => 0, 'slideshow' => 0, 'maxanswers' => $CFG->lesson_maxanswers, 'feedback' => 0, 'activitylink' => 0, 'available' => 0, 'deadline' => 0, 'usepassword' => 0, 'password' => '', 'dependency' => 0, 'timespent' => 0, 'completed' => 0, 'gradebetterthan' => 0, 'modattempts' => 0, 'review' => 0, 'maxattempts' => 1, 'nextpagedefault' => $CFG->lesson_defaultnextpage, 'maxpages' => 0, 'practice' => 0, 'custom' => 1, 'retake' => 0, 'usemaxgrade' => 0, 'minquestions' => 0, 'grade' => 100); if (!isset($record['mediafile'])) { require_once $CFG->libdir . '/filelib.php'; $record['mediafile'] = file_get_unused_draft_itemid(); } return parent::create_instance($record, (array) $options); }
function display_add_field($recordid = 0) { global $CFG, $DB, $OUTPUT, $PAGE, $USER; $file = false; $content = false; $displayname = ''; $fs = get_file_storage(); $context = $PAGE->context; $itemid = null; // editing an existing database entry if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); if (!empty($content->content)) { if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { $usercontext = context_user::instance($USER->id); if (!($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false))) { return false; } if (empty($content->content1)) { // Print icon if file already exists $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename()); $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')) . '<a href="' . $src . '" >' . s($file->get_filename()) . '</a>'; } else { $displayname = 'no file added'; } } } } } else { $itemid = file_get_unused_draft_itemid(); } $html = ''; // database entry label $html .= '<div title="' . s($this->field->description) . '">'; $html .= '<fieldset><legend><span class="accesshide">' . $this->field->name . '</span></legend>'; // itemid element $html .= '<input type="hidden" name="field_' . $this->field->id . '_file" value="' . $itemid . '" />'; $options = new stdClass(); $options->maxbytes = $this->field->param3; $options->maxfiles = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future. $options->itemid = $itemid; $options->accepted_types = '*'; $options->return_types = FILE_INTERNAL; $options->context = $PAGE->context; $fm = new form_filemanager($options); // Print out file manager. $output = $PAGE->get_renderer('core', 'files'); $html .= $output->render($fm); $html .= '</fieldset>'; $html .= '</div>'; return $html; }
public function create_instance($record = null, array $options = null) { // Add default values for folder. $record = (array) $record + array('display' => 0); if (!isset($record['showexpanded'])) { $record['showexpanded'] = get_config('folder', 'showexpanded'); } if (!isset($record['files'])) { $record['files'] = file_get_unused_draft_itemid(); } return parent::create_instance($record, (array) $options); }
function display_add_field($recordid = 0) { global $CFG, $DB, $OUTPUT, $PAGE, $USER; $file = false; $content = false; $displayname = ''; $fs = get_file_storage(); $context = $PAGE->context; $itemid = null; // editing an existing database entry if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); if (!empty($content->content)) { if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { $usercontext = get_context_instance(CONTEXT_USER, $USER->id); if (!($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false))) { return false; } if (empty($content->content1)) { // Print icon if file already exists $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename()); $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')) . '<a href="' . $src . '" >' . s($file->get_filename()) . '</a>'; } else { $displayname = 'no file added'; } } } } } else { $itemid = file_get_unused_draft_itemid(); } $html = ''; // database entry label $html .= '<div title="' . s($this->field->description) . '">'; $html .= '<fieldset><legend><span class="accesshide">' . $this->field->name . '</span></legend>'; // itemid element $html .= '<input type="hidden" name="field_' . $this->field->id . '_file" value="' . $itemid . '" />'; $options = new stdClass(); $options->maxbytes = $this->field->param3; $options->itemid = $itemid; $options->accepted_types = '*'; $options->return_types = FILE_INTERNAL; $options->context = $PAGE->context; $fp = new file_picker($options); // print out file picker $html .= $OUTPUT->render($fp); $html .= '</fieldset>'; $html .= '</div>'; $module = array('name' => 'data_filepicker', 'fullpath' => '/mod/data/data.js', 'requires' => array('core_filepicker')); $PAGE->requires->js_init_call('M.data_filepicker.init', array($fp->options), true, $module); return $html; }
public function create_instance($record = null, array $options = null) { global $CFG; // Add default values for lesson. $lessonconfig = get_config('mod_lesson'); $record = (array) $record + array('progressbar' => $lessonconfig->progressbar, 'ongoing' => $lessonconfig->ongoing, 'displayleft' => $lessonconfig->displayleftmenu, 'displayleftif' => $lessonconfig->displayleftif, 'slideshow' => $lessonconfig->slideshow, 'maxanswers' => $lessonconfig->maxanswers, 'feedback' => $lessonconfig->defaultfeedback, 'activitylink' => 0, 'available' => 0, 'deadline' => 0, 'usepassword' => 0, 'password' => '', 'dependency' => 0, 'timespent' => 0, 'completed' => 0, 'gradebetterthan' => 0, 'modattempts' => $lessonconfig->modattempts, 'review' => $lessonconfig->displayreview, 'maxattempts' => $lessonconfig->maximumnumberofattempts, 'nextpagedefault' => $lessonconfig->defaultnextpage, 'maxpages' => $lessonconfig->numberofpagestoshow, 'practice' => $lessonconfig->practice, 'custom' => $lessonconfig->customscoring, 'retake' => $lessonconfig->retakesallowed, 'usemaxgrade' => $lessonconfig->handlingofretakes, 'minquestions' => $lessonconfig->minimumnumberofquestions, 'grade' => 100); if (!isset($record['mediafile'])) { require_once $CFG->libdir . '/filelib.php'; $record['mediafile'] = file_get_unused_draft_itemid(); } return parent::create_instance($record, (array) $options); }
/** * Create an assign object and submit an online text submission. */ protected function create_assign_and_submit_text() { $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1, 'assignfeedback_comments_enabled' => 1)); $user = $this->students[0]; $this->setUser($user); // Create an online text submission. $submission = $assign->get_user_submission($user->id, true); $data = new stdClass(); $data->onlinetext_editor = array('text' => '<p>This is some text.</p>', 'format' => 1, 'itemid' => file_get_unused_draft_itemid()); $plugin = $assign->get_submission_plugin_by_type('onlinetext'); $plugin->save($submission, $data); return $assign; }
/** * Test for folder file attachments. * * @return void */ public function test_attach_files() { global $USER; $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $fs = get_file_storage(); $usercontext = context_user::instance($USER->id); $record = new stdClass(); $record->course = $course->id; $record->files = file_get_unused_draft_itemid(); // Attach the main file. We put them in the draft area, create_module will move them. $filerecord = array('contextid' => $usercontext->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $record->files, 'filepath' => '/'); // Attach 4 files. for ($i = 1; $i <= 4; $i++) { $filerecord['filename'] = 'myfile' . $i; $fs->create_file_from_string($filerecord, 'Test folder file ' . $i); } // And a fifth in a sub-folder. $filerecord['filename'] = 'myfile5'; $filerecord['filepath'] = '/subfolder/'; $fs->create_file_from_string($filerecord, 'Test folder file 5'); $this->getDataGenerator()->create_module('folder', $record); // Returns the instance as long as the area is supported. $searcharea = \core_search\manager::get_search_area($this->folderareaid); $this->assertInstanceOf('\\mod_folder\\search\\activity', $searcharea); $recordset = $searcharea->get_recordset_by_timestamp(0); $nrecords = 0; foreach ($recordset as $record) { $doc = $searcharea->get_document($record); $searcharea->attach_files($doc); $files = $doc->get_files(); // Folder should return all files attached. $this->assertCount(5, $files); // We don't know the order, so get all the names, then sort, then check. $filenames = array(); foreach ($files as $file) { $filenames[] = $file->get_filename(); } sort($filenames); for ($i = 1; $i <= 5; $i++) { $this->assertEquals('myfile' . $i, $filenames[$i - 1]); } $nrecords++; } // If there would be an error/failure in the foreach above the recordset would be closed on shutdown. $recordset->close(); $this->assertEquals(1, $nrecords); }
/** * Setup all the various parts of an assignment activity including creating an onlinetext submission. */ protected function setUp() { $this->user = $this->getDataGenerator()->create_user(); $this->course = $this->getDataGenerator()->create_course(); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $params['course'] = $this->course->id; $instance = $generator->create_instance($params); $this->cm = get_coursemodule_from_instance('assign', $instance->id); $this->context = context_module::instance($this->cm->id); $this->assign = new testable_assign($this->context, $this->cm, $this->course); $this->setUser($this->user->id); $this->submission = $this->assign->get_user_submission($this->user->id, true); $this->data = new stdClass(); $this->data->onlinetext_editor = array('itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', 'format' => FORMAT_PLAIN); }
protected function setUp() { parent::setUp(); // Add additional default data (some real attempts and stuff). $this->setUser($this->editingteachers[0]); $this->create_instance(); $setask = $this->create_instance(array('duedate' => time(), 'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL, 'maxattempts' => 3, 'submissiondrafts' => 1, 'setasksubmission_onlinetext_enabled' => 1)); // Add a submission. $this->setUser($this->students[0]); $submission = $setask->get_user_submission($this->students[0]->id, true); $data = new stdClass(); $data->onlinetext_editor = array('itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text', 'format' => FORMAT_HTML); $plugin = $setask->get_submission_plugin_by_type('onlinetext'); $plugin->save($submission, $data); // And now submit it for marking. $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; $setask->testable_update_submission($submission, $this->students[0]->id, true, false); // Mark the submission. $this->setUser($this->teachers[0]); $data = new stdClass(); $data->grade = '50.0'; $setask->testable_apply_grade_to_user($data, $this->students[0]->id, 0); // This is required so that the submissions timemodified > the grade timemodified. sleep(2); // Edit the submission again. $this->setUser($this->students[0]); $submission = $setask->get_user_submission($this->students[0]->id, true); $setask->testable_update_submission($submission, $this->students[0]->id, true, false); // This is required so that the submissions timemodified > the grade timemodified. sleep(2); // Allow the student another attempt. $this->teachers[0]->ignoresesskey = true; $this->setUser($this->teachers[0]); $result = $setask->testable_process_add_attempt($this->students[0]->id); // Add another submission. $this->setUser($this->students[0]); $submission = $setask->get_user_submission($this->students[0]->id, true); $data = new stdClass(); $data->onlinetext_editor = array('itemid' => file_get_unused_draft_itemid(), 'text' => 'Submission text 2', 'format' => FORMAT_HTML); $plugin = $setask->get_submission_plugin_by_type('onlinetext'); $plugin->save($submission, $data); // And now submit it for marking (again). $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; $setask->testable_update_submission($submission, $this->students[0]->id, true, false); }
function display_add_field($recordid = 0) { global $CFG, $DB, $OUTPUT, $PAGE, $USER; $text = ''; $format = 0; $str = '<div title="' . $this->field->description . '">'; editors_head_setup(); $options = $this->get_options(); $itemid = $this->field->id; $field = 'field_' . $itemid; if ($recordid && ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid)))) { $format = $content->content1; $text = clean_text($content->content, $format); $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text); } else { $draftitemid = file_get_unused_draft_itemid(); } $updatecontrol = $field; $idcontrol = $field . '_itemid'; $str .= '<input type="hidden" id="' . $updatecontrol . '" name="' . $updatecontrol . '" value="empty" />'; $str .= '<input type="hidden" name="' . $idcontrol . '" value="' . $draftitemid . '" />'; // $type = DBP_AUDIOMP3; $usercontextid = context_user::instance($USER->id)->id; switch ($this->field->param4) { case DBP_AUDIO: $str .= fetchAudioRecorderForSubmission('auto', 'ignore', $updatecontrol, $usercontextid, "user", "draft", $draftitemid); break; case DBP_VIDEO: $str .= fetchVideoRecorderForSubmission('auto', 'ignore', $updatecontrol, $usercontextid, "user", "draft", $draftitemid); break; case DBP_AUDIOMP3: $str .= fetchMP3RecorderForSubmission($updatecontrol, $usercontextid, "user", "draft", $draftitemid); break; case DBP_WHITEBOARDSIMPLE: case DBP_WHITEBOARDFULL: $str .= fetchWhiteboardForSubmission($updatecontrol, $usercontextid, "user", "draft", $draftitemid); break; case DBP_SNAPSHOT: $str .= fetchSnapshotCameraForSubmission($updatecontrol, 'apic.jpg', 350, 400, $usercontextid, "user", "draft", $draftitemid); break; } return $str; }
function flashcard_filepicker($elname, $value, $contextid, $filearea, $cardid, $maxbytes, $accepted_types = '*') { global $COURSE, $PAGE, $OUTPUT, $USER; $html = ''; $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); // no existing area info provided - let's use fresh new draft area if ($value) { $draftitemid = file_get_submitted_draft_itemid($filearea); $maxbytes = 100000; file_prepare_draft_area($draftitemid, $contextid, 'mod_flashcard', $filearea, $cardid, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 1)); } else { $draftitemid = file_get_unused_draft_itemid(); } if ($COURSE->id == SITEID) { $context = get_context_instance(CONTEXT_SYSTEM); } else { $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); } $client_id = uniqid(); $args = new stdClass(); // need these three to filter repositories list $args->accepted_types = $accepted_types; $args->return_types = FILE_INTERNAL; $args->itemid = $draftitemid; $args->maxbytes = $maxbytes; $args->buttonname = $elname . 'choose'; $args->elementname = $elname; $id = $elname; $fp = new file_picker($args); $options = $fp->options; $options->context = $PAGE->context; $html .= $OUTPUT->render($fp); $html .= '<input type="hidden" name="' . $elname . '" id="' . $id . '" value="' . $draftitemid . '" class="filepickerhidden"/>'; $module = array('name' => 'form_filepicker', 'fullpath' => '/lib/form/filepicker.js', 'requires' => array('core_filepicker', 'node', 'node-event-simulate')); $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module); $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array('env' => 'filepicker', 'action' => 'browse', 'itemid' => $draftitemid, 'subdirs' => 0, 'maxbytes' => $options->maxbytes, 'maxfiles' => 1, 'ctx_id' => $PAGE->context->id, 'course' => $PAGE->course->id, 'sesskey' => sesskey())); // non js file picker $html .= '<noscript>'; $html .= "<div><object type='text/html' data='{$nonjsfilepicker}' height='160' width='600' style='border:1px solid #000'></object></div>"; $html .= '</noscript>'; return $html; }
public function create_instance($record = null, array $options = null) { global $CFG, $USER; // Add default values for imscp. $record = (array) $record + array('package' => '', 'packagepath' => $CFG->dirroot . '/mod/imscp/tests/packages/singlescobasic.zip', 'keepold' => -1); // The 'package' value corresponds to the draft file area ID. If not specified, create from packagepath. if (empty($record['package'])) { if (!isloggedin() || isguestuser()) { throw new coding_exception('IMSCP generator requires a current user'); } if (!file_exists($record['packagepath'])) { throw new coding_exception("File {$record['packagepath']} does not exist"); } $usercontext = context_user::instance($USER->id); // Pick a random context id for specified user. $record['package'] = file_get_unused_draft_itemid(); // Add actual file there. $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $record['package'], 'filename' => basename($record['packagepath']), 'filepath' => '/'); $fs = get_file_storage(); $fs->create_file_from_pathname($filerecord, $record['packagepath']); } return parent::create_instance($record, (array) $options); }
public function test_create_instance() { global $DB, $USER; $this->resetAfterTest(); $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $this->assertFalse($DB->record_exists('folder', array('course' => $course->id))); $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course)); $records = $DB->get_records('folder', array('course' => $course->id), 'id'); $this->assertEquals(1, count($records)); $this->assertTrue(array_key_exists($folder->id, $records)); $params = array('course' => $course->id, 'name' => 'Another folder'); $folder = $this->getDataGenerator()->create_module('folder', $params); $records = $DB->get_records('folder', array('course' => $course->id), 'id'); $this->assertEquals(2, count($records)); $this->assertEquals('Another folder', $records[$folder->id]->name); // Examples of adding a folder with files (do not validate anything, just check for exceptions). $params = array('course' => $course->id, 'files' => file_get_unused_draft_itemid()); $usercontext = context_user::instance($USER->id); $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $params['files'], 'filename' => 'file1.txt', 'filepath' => '/'); $fs = get_file_storage(); $fs->create_file_from_string($filerecord, 'Test file contents'); $folder = $this->getDataGenerator()->create_module('folder', $params); }
/** * Creates new resource module instance. By default it contains a short * text file. * * @param array|stdClass $record data for module being generated. Requires 'course' key * (an id or the full object). Also can have any fields from add module form. * @param null|array $options general options for course module. Since 2.6 it is * possible to omit this argument by merging options into $record * @return stdClass record from module-defined table with additional field * cmid (corresponding id in course_modules table) */ public function create_instance($record = null, array $options = null) { global $CFG, $USER; require_once $CFG->dirroot . '/lib/resourcelib.php'; // Ensure the record can be modified without affecting calling code. $record = (object) (array) $record; // Fill in optional values if not specified. if (!isset($record->display)) { $record->display = RESOURCELIB_DISPLAY_AUTO; } 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' . ($this->instancecount + 1) . '.txt', 'filepath' => '/'); $fs = get_file_storage(); $fs->create_file_from_string($filerecord, 'Test resource ' . ($this->instancecount + 1) . ' file'); } // Do work to actually add the instance. return parent::create_instance($record, (array) $options); }
/** * Test for resource file attachments. * * @return void */ public function test_attach_files() { global $USER; $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $fs = get_file_storage(); $usercontext = context_user::instance($USER->id); $record = new stdClass(); $record->course = $course->id; $record->files = file_get_unused_draft_itemid(); // Attach the main file. We put them in the draft area, create_module will move them. $filerecord = array('contextid' => $usercontext->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $record->files, 'filepath' => '/', 'filename' => 'mainfile', 'sortorder' => 1); $fs->create_file_from_string($filerecord, 'Test resource file'); // Attach a second file that shouldn't be returned with the search doc. $filerecord['filename'] = 'extrafile'; $filerecord['sortorder'] = 0; $fs->create_file_from_string($filerecord, 'Test resource file 2'); $resource = $this->getDataGenerator()->create_module('resource', $record); $searcharea = \core_search\manager::get_search_area($this->resourceareaid); $this->assertInstanceOf('\\mod_resource\\search\\activity', $searcharea); $recordset = $searcharea->get_recordset_by_timestamp(0); $nrecords = 0; foreach ($recordset as $record) { $doc = $searcharea->get_document($record); $searcharea->attach_files($doc); $files = $doc->get_files(); // Resources should only return their main file. $this->assertCount(1, $files); $file = reset($files); $this->assertEquals('mainfile', $file->get_filename()); $nrecords++; } $recordset->close(); $this->assertEquals(1, $nrecords); }
function display_add_field($recordid=0) { global $CFG, $DB, $OUTPUT, $PAGE; $text = ''; $format = 0; $str = '<div title="'.$this->field->description.'">'; editors_head_setup(); $options = $this->get_options(); $itemid = $this->field->id; $field = 'field_'.$itemid; if ($recordid && $content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))){ $format = $content->content1; $text = clean_text($content->content, $format); $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text); } else { $draftitemid = file_get_unused_draft_itemid(); if (can_use_html_editor()) { $format = FORMAT_HTML; } else { $format = FORMAT_PLAIN; } } // get filepicker info // $fpoptions = array(); if ($options['maxfiles'] != 0 ) { $args = new stdClass(); // need these three to filter repositories list $args->accepted_types = array('web_image'); $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL); $args->context = $this->context; $args->env = 'filepicker'; // advimage plugin $image_options = initialise_filepicker($args); $image_options->context = $this->context; $image_options->client_id = uniqid(); $image_options->maxbytes = $options['maxbytes']; $image_options->env = 'editor'; $image_options->itemid = $draftitemid; // moodlemedia plugin $args->accepted_types = array('video', 'audio'); $media_options = initialise_filepicker($args); $media_options->context = $this->context; $media_options->client_id = uniqid(); $media_options->maxbytes = $options['maxbytes']; $media_options->env = 'editor'; $media_options->itemid = $draftitemid; // advlink plugin $args->accepted_types = '*'; $link_options = initialise_filepicker($args); $link_options->context = $this->context; $link_options->client_id = uniqid(); $link_options->maxbytes = $options['maxbytes']; $link_options->env = 'editor'; $link_options->itemid = $draftitemid; $fpoptions['image'] = $image_options; $fpoptions['media'] = $media_options; $fpoptions['link'] = $link_options; } $editor = editors_get_preferred_editor($format); $strformats = format_text_menu(); $formats = $editor->get_supported_formats(); foreach ($formats as $fid) { $formats[$fid] = $strformats[$fid]; } $editor->use_editor($field, $options, $fpoptions); $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.$draftitemid.'" />'; $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'">'.s($text).'</textarea></div>'; $str .= '<div><select name="'.$field.'_content1">'; foreach ($formats as $key=>$desc) { $selected = ($format == $key) ? 'selected="selected"' : ''; $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>'; } $str .= '</select>'; $str .= '</div>'; $str .= '</div>'; return $str; }
case 'rename': repository::update_draftfile($itemid, $draftpath, $filename, array('filename' => $newfilename)); $home_url->param('action', 'browse'); $home_url->param('draftpath', $draftpath); redirect($home_url); break; case 'downloaddir': $zipper = new zip_packer(); $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if ($draftpath === '/') { $filename = get_string('files') . '.zip'; } else { $filename = explode('/', trim($draftpath, '/')); $filename = array_pop($filename) . '.zip'; } $newdraftitemid = file_get_unused_draft_itemid(); if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) { $fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out(); header('Location: ' . $fileurl); } else { print_error('cannotdownloaddir', 'repository'); } break; case 'zip': $zipper = new zip_packer(); $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if (!$file->get_parent_directory()) { $parent_path = '/'; $filepath = '/'; $filename = get_string('files') . '.zip'; } else {
/** * import parts of question common to all types * @param $question array question question array from xml tree * @return object question object */ public function import_headers($question) { global $CFG, $USER; // get some error strings $error_noname = get_string('xmlimportnoname', 'qformat_xml'); $error_noquestion = get_string('xmlimportnoquestion', 'qformat_xml'); // this routine initialises the question object $qo = $this->defaultquestion(); // Question name $qo->name = $this->clean_question_name($this->getpath($question, array('#', 'name', 0, '#', 'text', 0, '#'), '', true, get_string('xmlimportnoname', 'qformat_xml'))); $questiontext = $this->import_text_with_files($question, array('#', 'questiontext', 0)); $qo->questiontext = $questiontext['text']; $qo->questiontextformat = $questiontext['format']; if (!empty($questiontext['itemid'])) { $qo->questiontextitemid = $questiontext['itemid']; } // Backwards compatibility, deal with the old image tag. $filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false); $filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false); if ($filedata && $filename) { $fs = get_file_storage(); if (empty($qo->questiontextitemid)) { $qo->questiontextitemid = file_get_unused_draft_itemid(); } $filename = clean_param(str_replace('/', '_', $filename), PARAM_FILE); $filerecord = array('contextid' => context_user::instance($USER->id)->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $qo->questiontextitemid, 'filepath' => '/', 'filename' => $filename); $fs->create_file_from_string($filerecord, base64_decode($filedata)); $qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />'; } // restore files in generalfeedback $generalfeedback = $this->import_text_with_files($question, array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat)); $qo->generalfeedback = $generalfeedback['text']; $qo->generalfeedbackformat = $generalfeedback['format']; if (!empty($generalfeedback['itemid'])) { $qo->generalfeedbackitemid = $generalfeedback['itemid']; } $qo->defaultmark = $this->getpath($question, array('#', 'defaultgrade', 0, '#'), $qo->defaultmark); $qo->penalty = $this->getpath($question, array('#', 'penalty', 0, '#'), $qo->penalty); // Fix problematic rounding from old files: if (abs($qo->penalty - 0.3333333) < 0.005) { $qo->penalty = 0.3333333; } // Read the question tags. if (!empty($CFG->usetags) && array_key_exists('tags', $question['#']) && !empty($question['#']['tags'][0]['#']['tag'])) { require_once $CFG->dirroot . '/tag/lib.php'; $qo->tags = array(); foreach ($question['#']['tags'][0]['#']['tag'] as $tagdata) { $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true); } } return $qo; }
function display_add_field($recordid = 0) { global $CFG, $DB, $OUTPUT, $USER, $PAGE; $file = false; $content = false; $displayname = ''; $alttext = ''; $itemid = null; $fs = get_file_storage(); if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); if (!empty($content->content)) { if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { $usercontext = get_context_instance(CONTEXT_USER, $USER->id); if (!($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false))) { return false; } if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_' . $content->content)) { $thumbfile->delete(); } if (empty($content->content1)) { // Print icon if file already exists $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename()); $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')) . '<a href="' . $src . '" >' . s($file->get_filename()) . '</a>'; } else { $displayname = get_string('nofilesattached', 'repository'); } } } $alttext = $content->content1; } } else { $itemid = file_get_unused_draft_itemid(); } $str = '<div title="' . s($this->field->description) . '">'; $str .= '<fieldset><legend><span class="accesshide">' . $this->field->name . '</span></legend>'; if ($file) { $src = file_encode_url($CFG->wwwroot . '/pluginfile.php/', $this->context->id . '/mod_data/content/' . $content->id . '/' . $file->get_filename()); $str .= '<img width="' . s($this->previewwidth) . '" height="' . s($this->previewheight) . '" src="' . $src . '" alt="" />'; } $options = new stdClass(); $options->maxbytes = $this->field->param3; $options->itemid = $itemid; $options->accepted_types = array('web_image'); $options->return_types = FILE_INTERNAL; $options->context = $PAGE->context; if (!empty($file)) { $options->filename = $file->get_filename(); $options->filepath = '/'; } $fp = new file_picker($options); $str .= $OUTPUT->render($fp); $str .= '<div class="mdl-left">'; $str .= '<input type="hidden" name="field_' . $this->field->id . '_file" value="' . $itemid . '" />'; $str .= '<label for="field_' . $this->field->id . '_alttext">' . get_string('alttext', 'data') . '</label> <input type="text" name="field_' . $this->field->id . '_alttext" id="field_' . $this->field->id . '_alttext" value="' . s($alttext) . '" />'; $str .= '</div>'; $str .= '</fieldset>'; $str .= '</div>'; $module = array('name' => 'data_imagepicker', 'fullpath' => '/mod/data/data.js', 'requires' => array('core_filepicker')); $PAGE->requires->js_init_call('M.data_imagepicker.init', array($fp->options), true, $module); return $str; }
/** * Store an image file in a draft filearea * @param array $text, if itemid element don't exists it will be created * @param string tempdir path to root of image tree * @param string filepathinsidetempdir path to image in the tree * @param string filename image's name * @return string new name of the image as it was stored */ protected function store_file_for_text_field(&$text, $tempdir, $filepathinsidetempdir, $filename) { global $USER; $fs = get_file_storage(); if (empty($text['itemid'])) { $text['itemid'] = file_get_unused_draft_itemid(); } // As question file areas don't support subdirs, // convert path to filename. // So that images with same name can be imported. $newfilename = clean_param(str_replace('/', '__', $filepathinsidetempdir . '__' . $filename), PARAM_FILE); $filerecord = array('contextid' => context_user::instance($USER->id)->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $text['itemid'], 'filepath' => '/', 'filename' => $newfilename); $fs->create_file_from_pathname($filerecord, $tempdir . '/' . $filepathinsidetempdir . '/' . $filename); return $newfilename; }
/** * Initialise a draft file area from a real one by copying the files. A draft * area will be created if one does not already exist. Normally you should * get $draftitemid by calling file_get_submitted_draft_itemid('elementname'); * * @category files * @global stdClass $CFG * @global stdClass $USER * @param int $draftitemid the id of the draft area to use, or 0 to create a new one, in which case this parameter is updated. * @param int $contextid This parameter and the next two identify the file area to copy files from. * @param string $component * @param string $filearea helps indentify the file area. * @param int $itemid helps identify the file area. Can be null if there are no files yet. * @param array $options text and file options ('subdirs'=>false, 'forcehttps'=>false) * @param string $text some html content that needs to have embedded links rewritten to point to the draft area. * @return string|null returns string if $text was passed in, the rewritten $text is returned. Otherwise NULL. */ function file_prepare_draft_area(&$draftitemid, $contextid, $component, $filearea, $itemid, array $options = null, $text = null) { global $CFG, $USER, $CFG; $options = (array) $options; if (!isset($options['subdirs'])) { $options['subdirs'] = false; } if (!isset($options['forcehttps'])) { $options['forcehttps'] = false; } $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); if (empty($draftitemid)) { // create a new area and copy existing files into $draftitemid = file_get_unused_draft_itemid(); $file_record = array('contextid' => $usercontext->id, 'component' => 'user', 'filearea' => 'draft', 'itemid' => $draftitemid); if (!is_null($itemid) and $files = $fs->get_area_files($contextid, $component, $filearea, $itemid)) { foreach ($files as $file) { if ($file->is_directory() and $file->get_filepath() === '/') { // we need a way to mark the age of each draft area, // by not copying the root dir we force it to be created automatically with current timestamp continue; } if (!$options['subdirs'] and ($file->is_directory() or $file->get_filepath() !== '/')) { continue; } $draftfile = $fs->create_file_from_storedfile($file_record, $file); // XXX: This is a hack for file manager (MDL-28666) // File manager needs to know the original file information before copying // to draft area, so we append these information in mdl_files.source field // {@link file_storage::search_references()} // {@link file_storage::search_references_count()} $sourcefield = $file->get_source(); $newsourcefield = new stdClass(); $newsourcefield->source = $sourcefield; $original = new stdClass(); $original->contextid = $contextid; $original->component = $component; $original->filearea = $filearea; $original->itemid = $itemid; $original->filename = $file->get_filename(); $original->filepath = $file->get_filepath(); $newsourcefield->original = file_storage::pack_reference($original); $draftfile->set_source(serialize($newsourcefield)); // End of file manager hack } } if (!is_null($text)) { // at this point there should not be any draftfile links yet, // because this is a new text from database that should still contain the @@pluginfile@@ links // this happens when developers forget to post process the text $text = str_replace("\"{$CFG->httpswwwroot}/draftfile.php", "\"{$CFG->httpswwwroot}/brokenfile.php#", $text); } } else { // nothing to do } if (is_null($text)) { return null; } // relink embedded files - editor can not handle @@PLUGINFILE@@ ! return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user', 'draft', $draftitemid, $options); }
function display_add_field($recordid = 0, $formdata = null) { global $CFG, $DB, $OUTPUT, $PAGE; $text = ''; $format = 0; $str = '<div title="' . s($this->field->description) . '">'; $str .= '<label for="field_' . $this->field->id . '" class="accesshide">'; $str .= html_writer::span($this->field->name); if ($this->field->required) { $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'), array('class' => 'req', 'title' => get_string('requiredelement', 'form'))); $str .= html_writer::div($image, 'inline-req'); } $str .= '</label>'; editors_head_setup(); $options = $this->get_options(); $itemid = $this->field->id; $field = 'field_'.$itemid; if ($formdata) { $fieldname = 'field_' . $this->field->id . '_content1'; if (isset($formdata->$fieldname)) { $format = $formdata->$fieldname; } else { $format = file_get_unused_draft_itemid(); } $fieldname = 'field_' . $this->field->id . '_itemid'; if (isset($formdata->$fieldname)) { $draftitemid = clean_param($formdata->$fieldname, PARAM_INT); } else { $draftitemid = file_get_unused_draft_itemid(); } $fieldname = 'field_' . $this->field->id; if (isset($formdata->$fieldname)) { $text = $formdata->$fieldname; } } else if ($recordid && $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { $format = $content->content1; $text = clean_text($content->content, $format); $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text); } else { $draftitemid = file_get_unused_draft_itemid(); $format = FORMAT_HTML; } // get filepicker info // $fpoptions = array(); if ($options['maxfiles'] != 0 ) { $args = new stdClass(); // need these three to filter repositories list $args->accepted_types = array('web_image'); $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL); $args->context = $this->context; $args->env = 'filepicker'; // advimage plugin $image_options = initialise_filepicker($args); $image_options->context = $this->context; $image_options->client_id = uniqid(); $image_options->maxbytes = $options['maxbytes']; $image_options->env = 'editor'; $image_options->itemid = $draftitemid; // moodlemedia plugin $args->accepted_types = array('video', 'audio'); $media_options = initialise_filepicker($args); $media_options->context = $this->context; $media_options->client_id = uniqid(); $media_options->maxbytes = $options['maxbytes']; $media_options->env = 'editor'; $media_options->itemid = $draftitemid; // advlink plugin $args->accepted_types = '*'; $link_options = initialise_filepicker($args); $link_options->context = $this->context; $link_options->client_id = uniqid(); $link_options->maxbytes = $options['maxbytes']; $link_options->env = 'editor'; $link_options->itemid = $draftitemid; $fpoptions['image'] = $image_options; $fpoptions['media'] = $media_options; $fpoptions['link'] = $link_options; } $editor = editors_get_preferred_editor($format); $strformats = format_text_menu(); $formats = $editor->get_supported_formats(); foreach ($formats as $fid) { $formats[$fid] = $strformats[$fid]; } $editor->set_text($text); $editor->use_editor($field, $options, $fpoptions); $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />'; $str .= '<div class="mod-data-input">'; $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>'; $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>'; $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">'; foreach ($formats as $key=>$desc) { $selected = ($format == $key) ? 'selected="selected"' : ''; $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>'; } $str .= '</select>'; $str .= '</div>'; $str .= '</div>'; $str .= '</div>'; return $str; }