Exemplo n.º 1
0
/**
 * Returns a list of information about file types based on extensions.
 *
 * The following elements expected in value array for each extension:
 * 'type' - mimetype
 * 'icon' - location of the icon file. If value is FILENAME, then either pix/f/FILENAME.gif
 *     or pix/f/FILENAME.png must be present in moodle and contain 16x16 filetype icon;
 *     also files with bigger sizes under names
 *     FILENAME-24, FILENAME-32, FILENAME-64, FILENAME-128, FILENAME-256 are recommended.
 * 'groups' (optional) - array of filetype groups this filetype extension is part of;
 *     commonly used in moodle the following groups:
 *       - web_image - image that can be included as <img> in HTML
 *       - image - image that we can parse using GD to find it's dimensions, also used for portfolio format
 *       - video - file that can be imported as video in text editor
 *       - audio - file that can be imported as audio in text editor
 *       - archive - we can extract files from this archive
 *       - spreadsheet - used for portfolio format
 *       - document - used for portfolio format
 *       - presentation - used for portfolio format
 * 'string' (optional) - the name of the string from lang/en/mimetypes.php that displays
 *     human-readable description for this filetype;
 *     Function {@link get_mimetype_description()} first looks at the presence of string for
 *     particular mimetype (value of 'type'), if not found looks for string specified in 'string'
 *     attribute, if not found returns the value of 'type';
 * 'defaulticon' (boolean, optional) - used by function {@link file_mimetype_icon()} to find
 *     an icon for mimetype. If an entry with 'defaulticon' is not found for a particular mimetype,
 *     this function will return first found icon; Especially usefull for types such as 'text/plain'
 *
 * @category files
 * @return array List of information about file types based on extensions.
 *   Associative array of extension (lower-case) to associative array
 *   from 'element name' to data. Current element names are 'type' and 'icon'.
 *   Unknown types should use the 'xxx' entry which includes defaults.
 */
function &get_mimetypes_array()
{
    // Get types from the core_filetypes function, which includes caching.
    return core_filetypes::get_types();
}
Exemplo n.º 2
0
 /**
  * Validates the form input
  *
  * @param array $data submitted data
  * @param array $files submitted files
  * @return array eventual errors indexed by the field name
  */
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     // Validate lists of allowed extensions.
     foreach (array('submissionfiletypes', 'overallfeedbackfiletypes') as $fieldname) {
         if (isset($data[$fieldname])) {
             $invalidextensions = workshop::invalid_file_extensions($data[$fieldname], array_keys(core_filetypes::get_types()));
             if ($invalidextensions) {
                 $errors[$fieldname] = get_string('err_unknownfileextension', 'mod_workshop', workshop::clean_file_extensions($invalidextensions));
             }
         }
     }
     // check the phases borders are valid
     if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
         $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
     }
     if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
         $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
     }
     // check the phases do not overlap
     if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
         $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
         $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
         if ($phaseassessmentstart == 0) {
             $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
         }
         if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
             foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
                 if ($data[$f] > 0) {
                     $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
                     break;
                 }
             }
         }
     }
     // Check that the submission grade pass is a valid number.
     if (!empty($data['submissiongradepass'])) {
         $submissiongradefloat = unformat_float($data['submissiongradepass'], true);
         if ($submissiongradefloat === false) {
             $errors['submissiongradepass'] = get_string('err_numeric', 'form');
         } else {
             if ($submissiongradefloat > $data['grade']) {
                 $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
             }
         }
     }
     // Check that the grade pass is a valid number.
     if (!empty($data['gradinggradepass'])) {
         $gradepassfloat = unformat_float($data['gradinggradepass'], true);
         if ($gradepassfloat === false) {
             $errors['gradinggradepass'] = get_string('err_numeric', 'form');
         } else {
             if ($gradepassfloat > $data['gradinggrade']) {
                 $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
             }
         }
     }
     return $errors;
 }
Exemplo n.º 3
0
 /**
  * Tests the get_mimetype_description function.
  */
 public function test_get_mimetype_description()
 {
     $this->resetAfterTest();
     // Test example type (.doc).
     $this->assertEquals(get_string('application/msword', 'mimetypes'), get_mimetype_description(array('filename' => 'test.doc')));
     // Test an unknown file type.
     $this->assertEquals(get_string('document/unknown', 'mimetypes'), get_mimetype_description(array('filename' => 'test.frog')));
     // Test a custom filetype with no lang string specified.
     core_filetypes::add_type('frog', 'application/x-frog', 'document');
     $this->assertEquals('application/x-frog', get_mimetype_description(array('filename' => 'test.frog')));
     // Test custom description.
     core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document', array(), '', 'Froggy file');
     $this->assertEquals('Froggy file', get_mimetype_description(array('filename' => 'test.frog')));
     // Test custom description using multilang filter.
     filter_set_global_state('multilang', TEXTFILTER_ON);
     filter_set_applies_to_strings('multilang', true);
     core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document', array(), '', '<span lang="en" class="multilang">Green amphibian</span>' . '<span lang="fr" class="multilang">Amphibian vert</span>');
     $this->assertEquals('Green amphibian', get_mimetype_description(array('filename' => 'test.frog')));
 }
Exemplo n.º 4
0
 /**
  * Reset contents of all database tables to initial values, reset caches, etc.
  *
  * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
  *
  * @static
  * @param bool $detectchanges
  *      true  - changes in global state and database are reported as errors
  *      false - no errors reported
  *      null  - only critical problems are reported as errors
  * @return void
  */
 public static function reset_all_data($detectchanges = false)
 {
     global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
     // Stop any message redirection.
     phpunit_util::stop_message_redirection();
     // Stop any message redirection.
     phpunit_util::stop_event_redirection();
     // Start a new email redirection.
     // This will clear any existing phpmailer redirection.
     // We redirect all phpmailer output to this message sink which is
     // called instead of phpmailer actually sending the message.
     phpunit_util::start_phpmailer_redirection();
     // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
     // This accounted for 25% of the total time running phpunit - so we removed it.
     // Show any unhandled debugging messages, the runbare() could already reset it.
     self::display_debugging_messages();
     self::reset_debugging();
     // reset global $DB in case somebody mocked it
     $DB = self::get_global_backup('DB');
     if ($DB->is_transaction_started()) {
         // we can not reset inside transaction
         $DB->force_transaction_rollback();
     }
     $resetdb = self::reset_database();
     $warnings = array();
     if ($detectchanges === true) {
         if ($resetdb) {
             $warnings[] = 'Warning: unexpected database modification, resetting DB state';
         }
         $oldcfg = self::get_global_backup('CFG');
         $oldsite = self::get_global_backup('SITE');
         foreach ($CFG as $k => $v) {
             if (!property_exists($oldcfg, $k)) {
                 $warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
             } else {
                 if ($oldcfg->{$k} !== $CFG->{$k}) {
                     $warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
                 }
             }
             unset($oldcfg->{$k});
         }
         if ($oldcfg) {
             foreach ($oldcfg as $k => $v) {
                 $warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
             }
         }
         if ($USER->id != 0) {
             $warnings[] = 'Warning: unexpected change of $USER';
         }
         if ($COURSE->id != $oldsite->id) {
             $warnings[] = 'Warning: unexpected change of $COURSE';
         }
         if ($CFG->ostype === 'WINDOWS') {
             if (setlocale(LC_TIME, 0) !== 'English_Australia.1252') {
                 $warnings[] = 'Warning: unexpected change of locale';
             }
         } else {
             if (setlocale(LC_TIME, 0) !== 'en_AU.UTF-8') {
                 $warnings[] = 'Warning: unexpected change of locale';
             }
         }
     }
     if (ini_get('max_execution_time') != 0) {
         // This is special warning for all resets because we do not want any
         // libraries to mess with timeouts unintentionally.
         // Our PHPUnit integration is not supposed to change it either.
         if ($detectchanges !== false) {
             $warnings[] = 'Warning: max_execution_time was changed to ' . ini_get('max_execution_time');
         }
         set_time_limit(0);
     }
     // restore original globals
     $_SERVER = self::get_global_backup('_SERVER');
     $CFG = self::get_global_backup('CFG');
     $SITE = self::get_global_backup('SITE');
     $_GET = array();
     $_POST = array();
     $_FILES = array();
     $_REQUEST = array();
     $COURSE = $SITE;
     // reinitialise following globals
     $OUTPUT = new bootstrap_renderer();
     $PAGE = new moodle_page();
     $FULLME = null;
     $ME = null;
     $SCRIPT = null;
     // Empty sessison and set fresh new not-logged-in user.
     \core\session\manager::init_empty_session();
     // reset all static caches
     \core\event\manager::phpunit_reset();
     accesslib_clear_all_caches(true);
     get_string_manager()->reset_caches(true);
     reset_text_filters_cache(true);
     events_get_handlers('reset');
     core_text::reset_caches();
     get_message_processors(false, true);
     filter_manager::reset_caches();
     core_filetypes::reset_caches();
     // Reset static unit test options.
     if (class_exists('\\availability_date\\condition', false)) {
         \availability_date\condition::set_current_time_for_test(0);
     }
     // Reset internal users.
     core_user::reset_internal_users();
     //TODO MDL-25290: add more resets here and probably refactor them to new core function
     // Reset course and module caches.
     if (class_exists('format_base')) {
         // If file containing class is not loaded, there is no cache there anyway.
         format_base::reset_course_cache(0);
     }
     get_fast_modinfo(0, 0, true);
     // Reset other singletons.
     if (class_exists('core_plugin_manager')) {
         core_plugin_manager::reset_caches(true);
     }
     if (class_exists('\\core\\update\\checker')) {
         \core\update\checker::reset_caches(true);
     }
     if (class_exists('\\core\\update\\deployer')) {
         \core\update\deployer::reset_caches(true);
     }
     // Clear static cache within restore.
     if (class_exists('restore_section_structure_step')) {
         restore_section_structure_step::reset_caches();
     }
     // purge dataroot directory
     self::reset_dataroot();
     // restore original config once more in case resetting of caches changed CFG
     $CFG = self::get_global_backup('CFG');
     // inform data generator
     self::get_data_generator()->reset();
     // fix PHP settings
     error_reporting($CFG->debug);
     // Reset the date/time class.
     core_date::phpunit_reset();
     // Make sure the time locale is consistent - that is Australian English.
     if ($CFG->ostype === 'WINDOWS') {
         setlocale(LC_TIME, 'English_Australia.1252');
     } else {
         setlocale(LC_TIME, 'en_AU.UTF-8');
     }
     // verify db writes just in case something goes wrong in reset
     if (self::$lastdbwrites != $DB->perf_get_writes()) {
         error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
         self::$lastdbwrites = $DB->perf_get_writes();
     }
     if ($warnings) {
         $warnings = implode("\n", $warnings);
         trigger_error($warnings, E_USER_WARNING);
     }
 }
Exemplo n.º 5
0
 /**
  * Clears the type cache. This is not needed in normal use as the
  * set_custom_types function automatically clears the cache. Intended for
  * use in unit tests.
  */
 public static function reset_caches()
 {
     self::$cachedtypes = null;
 }
Exemplo n.º 6
0
/**
 * Delete a file type with a confirmation box.
 *
 * @package tool_filetypes
 * @copyright 2014 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require __DIR__ . '/../../../config.php';
require_once $CFG->libdir . '/adminlib.php';
admin_externalpage_setup('tool_filetypes');
$extension = required_param('extension', PARAM_ALPHANUMEXT);
$redirecturl = new \moodle_url('/admin/tool/filetypes/index.php');
if (optional_param('delete', 0, PARAM_INT)) {
    require_sesskey();
    // Delete the file type from the config.
    core_filetypes::delete_type($extension);
    redirect($redirecturl);
}
// Page settings.
$title = get_string('deletefiletypes', 'tool_filetypes');
$context = context_system::instance();
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/delete.php', array('extension' => $extension)));
$PAGE->navbar->add($title);
$PAGE->set_context($context);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($SITE->fullname . ': ' . $title);
// Display the page.
echo $OUTPUT->header();
$message = get_string('delete_confirmation', 'tool_filetypes', $extension);
$deleteurl = new \moodle_url('delete.php', array('extension' => $extension, 'delete' => 1));
$yesbutton = new single_button($deleteurl, get_string('yes'));
Exemplo n.º 7
0
 /**
  * Check that the logic cleans up the variable by deleting parts that are
  * no longer needed.
  */
 public function test_cleanup()
 {
     global $CFG;
     $this->resetAfterTest();
     // The custom filetypes setting is empty to start with.
     $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
     // Add a custom filetype, then delete it.
     core_filetypes::add_type('frog', 'application/x-frog', 'document');
     $this->assertObjectHasAttribute('customfiletypes', $CFG);
     core_filetypes::delete_type('frog');
     $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
     // Change a standard filetype, then change it back.
     core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
     $this->assertObjectHasAttribute('customfiletypes', $CFG);
     core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode');
     $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
     // Delete a standard filetype, then add it back (the same).
     core_filetypes::delete_type('asm');
     $this->assertObjectHasAttribute('customfiletypes', $CFG);
     core_filetypes::add_type('asm', 'text/plain', 'sourcecode');
     $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
     // Revert a changed type.
     core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
     $this->assertObjectHasAttribute('customfiletypes', $CFG);
     core_filetypes::revert_type_to_default('asm');
     $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
     // Revert a deleted type.
     core_filetypes::delete_type('asm');
     $this->assertObjectHasAttribute('customfiletypes', $CFG);
     core_filetypes::revert_type_to_default('asm');
     $this->assertObjectNotHasAttribute('customfiletypes', $CFG);
 }
Exemplo n.º 8
0
        }
        if (empty($data->defaulticon)) {
            $data->defaulticon = 0;
        }
        if (empty($data->corestring)) {
            $data->corestring = '';
        }
        if (empty($data->description)) {
            $data->description = '';
        }
        if ($data->oldextension) {
            // Update an existing file type.
            core_filetypes::update_type($data->oldextension, $data->extension, $data->mimetype, $data->icon, $data->groups, $data->corestring, $data->description, (bool) $data->defaulticon);
        } else {
            // Add a new file type entry.
            core_filetypes::add_type($data->extension, $data->mimetype, $data->icon, $data->groups, $data->corestring, $data->description, (bool) $data->defaulticon);
        }
        redirect($backurl);
    }
}
// Page settings.
$context = context_system::instance();
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/edit.php', array('oldextension' => $oldextension)));
$PAGE->navbar->add($oldextension ? s($oldextension) : $title);
$PAGE->set_context($context);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($SITE->fullname . ': ' . $title);
// Display the page.
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();
Exemplo n.º 9
0
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * Display the custom file type settings page.
 *
 * @package tool_filetypes
 * @copyright 2014 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require __DIR__ . '/../../../config.php';
require_once $CFG->libdir . '/adminlib.php';
admin_externalpage_setup('tool_filetypes');
// Page settings.
$title = get_string('pluginname', 'tool_filetypes');
$context = context_system::instance();
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/index.php'));
$PAGE->set_context($context);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($SITE->fullname . ': ' . $title);
$renderer = $PAGE->get_renderer('tool_filetypes');
// Is it restricted because set in config.php?
$restricted = array_key_exists('customfiletypes', $CFG->config_php_settings);
// Display the page.
echo $renderer->header();
echo $renderer->edit_table(get_mimetypes_array(), core_filetypes::get_deleted_types(), $restricted);
echo $renderer->footer();
Exemplo n.º 10
0
/**
 * Resets a file type to the default Moodle values.
 *
 * @package tool_filetypes
 * @copyright 2014 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require __DIR__ . '/../../../config.php';
require_once $CFG->libdir . '/adminlib.php';
admin_externalpage_setup('tool_filetypes');
$extension = required_param('extension', PARAM_RAW);
$redirecturl = new \moodle_url('/admin/tool/filetypes/index.php');
if (optional_param('revert', 0, PARAM_INT)) {
    require_sesskey();
    // Reset the file type in config.
    core_filetypes::revert_type_to_default($extension);
    redirect($redirecturl);
}
// Page settings.
$title = get_string('revertfiletype', 'tool_filetypes');
$context = context_system::instance();
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/revert.php', array('extension' => $extension)));
$PAGE->navbar->add($title);
$PAGE->set_context($context);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($SITE->fullname . ': ' . $title);
// Display the page.
echo $OUTPUT->header();
$message = get_string('revert_confirmation', 'tool_filetypes', $extension);
$reverturl = new \moodle_url('revert.php', array('extension' => $extension, 'revert' => 1));
$yesbutton = new single_button($reverturl, get_string('yes'));