function store_annotation_document($data)
{
    global $DB, $CFG;
    $fs = get_file_storage();
    $cmid = $data->coursemodule;
    $draftitemid = $data->files;
    $context = context_module::instance($cmid);
    if ($draftitemid) {
        $messagetext = file_save_draft_area_files($draftitemid, $context->id, 'mod_annotation', 'content', 0, array('subdirs' => true));
    }
    $files = $fs->get_area_files($context->id, 'mod_annotation', 'content', 0, 'sortorder', false);
    if (count($files) == 1) {
        // Only one file attached, set it as main file automatically.
        $file = reset($files);
        file_set_sortorder($context->id, 'mod_annotation', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
    }
    // Find out the file location by getting the content hash.
    $table = "files";
    $results = $DB->get_records($table, array('itemid' => $draftitemid));
    foreach ($results as $result) {
        $userid = $result->userid;
        $timecreated = $result->timecreated;
        $contenthash = $result->contenthash;
        break;
        // Bad way of doing it, TODO.
    }
    // Insert a reference into mdl_annotation_document.
    $table = 'annotation_document';
    $record = new stdClass();
    $record->id = 0;
    // DB will auto increment it.
    $record->userid = $userid;
    $record->group_id = 0;
    $record->time_created = $timecreated;
    $record->documenttype = $data->type;
    $record->location = $contenthash;
    $record->lang = "";
    // Lang column no longer used, highlightjs automatically detects language.
    $record->cmid = $cmid;
    $record->groupannotation = $data->groupannotation;
    if (isset($record->groupannotationsvisible)) {
        $record->groupannotationsvisible = $data->groupannotationsvisible;
    } else {
        $record->groupannotationsvisible = 0;
    }
    $record->allowfrom = $data->allowfrom;
    $record->allowuntil = $data->allowuntil;
    $insertid = $DB->insert_record('annotation_document', $record, false);
}
/**
 * Saves the file from pdfparts record somewhere on the server.
 * @param object $data
 */
function pdfparts_set_mainfile($data)
{
    global $DB;
    $fs = get_file_storage();
    $cmid = $data->coursemodule;
    $draftitemid = $data->files;
    $context = context_module::instance($cmid);
    if ($draftitemid) {
        file_save_draft_area_files($draftitemid, $context->id, 'mod_pdfparts', 'content', 0, array('subdirs' => true));
    }
    $files = $fs->get_area_files($context->id, 'mod_pdfparts', 'content', 0, 'sortorder', false);
    if (count($files) == 1) {
        // only one file attached, set it as main file automatically
        $file = reset($files);
        file_set_sortorder($context->id, 'mod_pdfparts', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
    }
}
Beispiel #3
0
 function validation($data, $files)
 {
     global $USER;
     $errors = parent::validation($data, $files);
     $usercontext = context_user::instance($USER->id);
     $fs = get_file_storage();
     if (!($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false))) {
         $errors['files'] = get_string('required');
         return $errors;
     }
     if (count($files) == 1) {
         // no need to select main file if only one picked
         return $errors;
     } else {
         if (count($files) > 1) {
             $mainfile = false;
             foreach ($files as $file) {
                 if ($file->get_sortorder() == 1) {
                     $mainfile = true;
                     break;
                 }
             }
             // set a default main file
             if (!$mainfile) {
                 $file = reset($files);
                 file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), 1);
             }
         }
     }
     return $errors;
 }
 /**
  * validation
  *
  * @param xxx $data
  * @return xxx
  */
 function validation($data, $files)
 {
     global $USER;
     // http://docs.moodle.org/en/Development:lib/formslib.php_Validation
     // Note: see "lang/en/error.php" for a list of common messages
     $errors = array();
     // get the $files specified in the form
     $usercontext = hotpot_get_context(CONTEXT_USER, $USER->id);
     $fs = get_file_storage();
     $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['sourceitemid'], 'sortorder, id', 0);
     // files only, no dirs
     // check we have at least one file
     // (and set mainfile marker, if necessary)
     if (empty($files)) {
         $errors['sourceitemid'] = get_string('required');
         // $errors['sourceitemid'] = get_string('nofile', 'error');
     } else {
         $mainfile = false;
         foreach ($files as $file) {
             if ($file->get_sortorder() == 1) {
                 $mainfile = true;
                 break;
             }
         }
         if (!$mainfile) {
             $file = reset($files);
             // first file in the list
             file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), 1);
         }
     }
     // studentfeedbackurl
     if ($data['studentfeedback'] == hotpot::FEEDBACK_WEBPAGE || $data['studentfeedback'] == hotpot::FEEDBACK_FORMMAIL) {
         if (empty($data['studentfeedbackurl']) || !preg_match('/^https?:\\/\\/.+/', $data['studentfeedbackurl'])) {
             // empty or invalid url
             $errors['studentfeedback_elements'] = get_string('invalidurl', 'error');
         }
     }
     return $errors;
 }
Beispiel #5
0
             } else {
                 echo json_encode(false);
             }
         }
     } else {
         echo json_encode(false);
     }
     die;
 case 'setmainfile':
     $filename = required_param('filename', PARAM_FILE);
     $filepath = required_param('filepath', PARAM_PATH);
     $filepath = file_correct_filepath($filepath);
     // reset sort order
     file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
     // set main file
     $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
     echo json_encode($return);
     die;
 case 'updatefile':
     // Allows to Rename file, move it to another directory, change it's license and author information in one request
     $filename = required_param('filename', PARAM_FILE);
     $filepath = required_param('filepath', PARAM_PATH);
     $fs = get_file_storage();
     if (!($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename))) {
         die(json_encode((object) array('error' => get_string('filenotfound', 'error'))));
     }
     $updatedata = array();
     $updatedata['filename'] = $newfilename = optional_param('newfilename', $file->get_filename(), PARAM_FILE);
     $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $file->get_filepath(), PARAM_PATH);
     $updatedata['license'] = optional_param('newlicense', $file->get_license(), PARAM_TEXT);
     $updatedata['author'] = optional_param('newauthor', $file->get_author(), PARAM_TEXT);
function xmldb_resource_upgrade($oldversion)
{
    global $CFG, $DB;
    require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php";
    $dbman = $DB->get_manager();
    //===== 1.9.0 upgrade line ======//
    if ($oldversion < 2009041900) {
        resource_20_prepare_migration();
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009041900, 'resource');
    }
    if ($oldversion < 2009042000) {
        // Rename field summary on table resource to intro
        $table = new xmldb_table('resource');
        $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'reference');
        // Launch rename field summary
        $dbman->rename_field($table, $field, 'intro');
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009042000, 'resource');
    }
    if ($oldversion < 2009042001) {
        // Define field introformat to be added to resource
        $table = new xmldb_table('resource');
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
        // Launch add field introformat
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // conditionally migrate to html format in intro
        if ($CFG->texteditors !== 'textarea') {
            $rs = $DB->get_recordset('resource', array('introformat' => FORMAT_MOODLE), '', 'id,intro,introformat');
            foreach ($rs as $r) {
                $r->intro = text_to_html($r->intro, false, false, true);
                $r->introformat = FORMAT_HTML;
                $DB->update_record('resource', $r);
                upgrade_set_timeout();
            }
            $rs->close();
        }
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009042001, 'resource');
    }
    if ($oldversion < 2009062600) {
        $res_count = $DB->count_records('resource');
        $old_count = $DB->count_records('resource_old', array('migrated' => 0));
        if ($res_count != $old_count) {
            //we can not continue, something is very wrong!!
            upgrade_log(UPGRADE_LOG_ERROR, null, 'Resource migration failed.');
            upgrade_mod_savepoint(false, 2009062600, 'resource');
        }
        // Drop obsoleted fields from resource table
        $table = new xmldb_table('resource');
        $fields = array('type', 'reference', 'alltext', 'popup', 'options');
        foreach ($fields as $fname) {
            $field = new xmldb_field($fname);
            $dbman->drop_field($table, $field);
        }
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009062600, 'resource');
    }
    if ($oldversion < 2009062601) {
        $table = new xmldb_table('resource');
        // Define field tobemigrated to be added to resource
        $field = new xmldb_field('tobemigrated', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'introformat');
        // Conditionally launch add field tobemigrated
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field mainfile to be added to resource
        $field = new xmldb_field('mainfile', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'tobemigrated');
        // Conditionally launch add field mainfile
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field legacyfiles to be added to resource
        $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'mainfile');
        // Conditionally launch add field legacyfiles
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field legacyfileslast to be added to resource
        $field = new xmldb_field('legacyfileslast', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'legacyfiles');
        // Conditionally launch add field legacyfileslast
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field display to be added to resource
        $field = new xmldb_field('display', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'legacyfileslast');
        // Conditionally launch add field display
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field displayoptions to be added to resource
        $field = new xmldb_field('displayoptions', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'display');
        // Conditionally launch add field displayoptions
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field filterfiles to be added to resource
        $field = new xmldb_field('filterfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'displayoptions');
        // Conditionally launch add field filterfiles
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field revision to be added to resource
        $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'filterfiles');
        // Conditionally launch add field revision
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        //mark all records as awaiting migration
        $DB->set_field('resource', 'tobemigrated', 1, array());
        // resource savepoint reached
        upgrade_mod_savepoint(true, 2009062601, 'resource');
    }
    if ($oldversion < 2009062603) {
        resource_20_migrate();
        upgrade_mod_savepoint(true, 2009062603, 'resource');
    }
    if ($oldversion < 2009063000) {
        //migrate and prune old settings - admins need to review and set up all module settings anyway
        if (!empty($CFG->resource_framesize)) {
            set_config('framesize', $CFG->resource_framesize, 'resource');
        }
        if (!empty($CFG->resource_popupheight)) {
            set_config('popupheight', $CFG->resource_popupheight, 'resource');
        }
        if (!empty($CFG->resource_popupwidth)) {
            set_config('popupwidth', $CFG->resource_popupwidth, 'resource');
        }
        $cleanupsettings = array('resource_framesize', 'resource_popupheight', 'resource_popupwidth', 'resource_popupmenubar', 'resource_websearch', 'resource_defaulturl', 'resource_allowlocalfiles', 'resource_popup', 'resource_popupresizable', 'resource_popupscrollbars', 'resource_popupdirectories', 'resource_popuplocation', 'resource_popuptoolbar', 'resource_popupstatus');
        foreach ($cleanupsettings as $setting) {
            unset_config($setting);
        }
        upgrade_mod_savepoint(true, 2009063000, 'resource');
    }
    if ($oldversion < 2009080501) {
        require_once "{$CFG->libdir}/filelib.php";
        $sql = "SELECT r.id,\n                       r.mainfile,\n                       cm.id AS cmid\n                  FROM {resource} r\n                  JOIN {modules} m ON m.name='resource'\n                  JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = r.id)";
        $instances = $DB->get_recordset_sql($sql);
        foreach ($instances as $instance) {
            if (empty($instance->mainfile)) {
                // weird
                continue;
            }
            $context = get_context_instance(CONTEXT_MODULE, $instance->cmid, MUST_EXIST);
            $parts = explode('/', $instance->mainfile);
            $filename = array_pop($parts);
            $filepath = implode('/', $parts);
            file_set_sortorder($context->id, 'mod_resource', 'content', 0, $filepath, $filename, 1);
        }
        $instances->close();
        /// Define field mainfile to be dropped from resource
        $table = new xmldb_table('resource');
        $field = new xmldb_field('mainfile');
        $dbman->drop_field($table, $field);
        /// resource savepoint reached
        upgrade_mod_savepoint(true, 2009080501, 'resource');
    }
    // MDL-10906. Removing resource_allowlocalfiles setting.
    if ($oldversion < 2010083000) {
        unset_config('resource_allowlocalfiles');
        upgrade_mod_savepoint(true, 2010083000, 'resource');
    }
    if ($oldversion < 2011022700) {
        // refresh resource links breakage caused by invalid sortorder
        require_once $CFG->dirroot . '/course/lib.php';
        rebuild_course_cache(0, true);
        upgrade_mod_savepoint(true, 2011022700, 'resource');
    }
    // Moodle v2.1.0 release upgrade line
    // Put any upgrade step following this
    return true;
}
/**
 * Extracts GGB package, sets up all variables.
 * @param int $cmid
 * @return filename
 */
function geogebra_extract_package($cmid)
{
    global $DB;
    $fs = get_file_storage();
    $context = context_module::instance($cmid);
    $files = $fs->get_area_files($context->id, 'mod_geogebra', 'content', 0, 'sortorder', false);
    if (count($files) == 1) {
        // only one file attached, set it as main file automatically
        $package = reset($files);
        file_set_sortorder($context->id, 'mod_geogebra', 'content', 0, $package->get_filepath(), $package->get_filename(), 1);
        $filename = $package->get_filename();
        // Extract files
        $fs->delete_area_files($context->id, 'mod_geogebra', 'extracted_files');
        $packer = get_file_packer('application/zip');
        $package->extract_to_storage($packer, $context->id, 'mod_geogebra', 'extracted_files', 0, '/');
    }
    return $filename;
}
Beispiel #8
0
 public function set_mainfile()
 {
     $fs = get_file_storage();
     $fileid = $this->instance->url;
     if ($fileid) {
         file_save_draft_area_files($fileid, $this->context->id, 'mod_jclic', 'content', 0, self::get_filemanager_options());
     }
     $files = $fs->get_area_files($this->context->id, 'mod_jclic', 'content', 0, 'sortorder', false);
     if (count($files) == 1) {
         // only one file attached, set it as main file automatically
         $file = reset($files);
         file_set_sortorder($this->context->id, 'mod_jclic', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
         return $file->get_filename();
     }
     return null;
 }