Esempio n. 1
0
File: lib.php Progetto: dg711/moodle
/**
 * Update imscp instance.
 * @param object $data
 * @param object $mform
 * @return bool true
 */
function imscp_update_instance($data, $mform)
{
    global $CFG, $DB;
    require_once "{$CFG->dirroot}/mod/imscp/locallib.php";
    $cmid = $data->coursemodule;
    $data->timemodified = time();
    $data->id = $data->instance;
    $data->structure = null;
    // Better reparse structure after each update.
    $DB->update_record('imscp', $data);
    $context = context_module::instance($cmid);
    $imscp = $DB->get_record('imscp', array('id' => $data->id), '*', MUST_EXIST);
    if (!empty($data->package) && ($draftareainfo = file_get_draft_area_info($data->package)) && $draftareainfo['filecount']) {
        $fs = get_file_storage();
        $imscp->revision++;
        $DB->update_record('imscp', $imscp);
        // Get a list of existing packages before adding new package.
        if ($imscp->keepold > -1) {
            $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false);
        } else {
            $packages = array();
        }
        file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup', $imscp->revision, array('subdirs' => 0, 'maxfiles' => 1));
        $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', $imscp->revision, '', false);
        $package = reset($files);
        // Purge all extracted content.
        $fs->delete_area_files($context->id, 'mod_imscp', 'content');
        // Extract package content.
        if ($package) {
            $packer = get_file_packer('application/zip');
            $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/');
        }
        // Cleanup old package files, keep current + keep old.
        while ($packages and count($packages) > $imscp->keepold) {
            $package = array_shift($packages);
            $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid());
        }
    }
    $structure = imscp_parse_structure($imscp, $context);
    $imscp->structure = is_array($structure) ? serialize($structure) : null;
    $DB->update_record('imscp', $imscp);
    return true;
}
Esempio n. 2
0
/**
 * Update imscp instance.
 * @param object $data
 * @param object $mform
 * @return bool true
 */
function imscp_update_instance($data, $mform) {
    global $CFG, $DB;
    require_once("$CFG->dirroot/mod/imscp/locallib.php");

    $cmid = $data->coursemodule;

    $data->timemodified = time();
    $data->id           = $data->instance;
    $data->structure   = null; // better reparse structure after each update

    $DB->update_record('imscp', $data);

    $context = get_context_instance(CONTEXT_MODULE, $cmid);
    $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);

    if ($filename = $mform->get_new_filename('package')) {
        $fs = get_file_storage();

        $imscp->revision++;
        $DB->update_record('imscp', $imscp);

        // get a list of existing packages before adding new package
        if ($imscp->keepold > -1) {
            $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false);
        } else {
            $packages = array();
        }

        $package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', $imscp->revision, '/', $filename);

        // purge all extracted content
        $fs->delete_area_files($context->id, 'mod_imscp', 'content');

        // extract package content
        if ($package) {
            $packer = get_file_packer('application/zip');
            $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/');
        }

        // cleanup old package files, keep current + keepold
        while ($packages and (count($packages) > $imscp->keepold)) {
            $package = array_shift($packages);
            $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid());
        }
    }

    $structure = imscp_parse_structure($imscp, $context);
    $imscp->structure = is_array($structure) ? serialize($structure) : null;
    $DB->update_record('imscp', $imscp);

    return true;
}
/**
 * Migrate imscp module data from 1.9 resource_old table to new imscp table
 * @return void
 */
function imscp_20_migrate()
{
    global $CFG, $DB, $OUTPUT;
    require_once "{$CFG->libdir}/filelib.php";
    require_once "{$CFG->dirroot}/course/lib.php";
    require_once "{$CFG->dirroot}/mod/imscp/locallib.php";
    if (!file_exists("{$CFG->dirroot}/mod/resource/db/upgradelib.php")) {
        // bad luck, somebody deleted resource module
        return;
    }
    require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php";
    // create resource_old table and copy resource table there if needed
    if (!resource_20_prepare_migration()) {
        // no modules or fresh install
        return;
    }
    $candidates = $DB->get_recordset('resource_old', array('type' => 'ims', 'migrated' => 0));
    if (!$candidates->valid()) {
        $candidates->close();
        // Not going to iterate (but exit), close rs
        return;
    }
    $fs = get_file_storage();
    foreach ($candidates as $candidate) {
        upgrade_set_timeout(60);
        if ($CFG->texteditors !== 'textarea') {
            $intro = text_to_html($candidate->intro, false, false, true);
            $introformat = FORMAT_HTML;
        } else {
            $intro = $candidate->intro;
            $introformat = FORMAT_MOODLE;
        }
        $imscp = new stdClass();
        $imscp->course = $candidate->course;
        $imscp->name = $candidate->name;
        $imscp->intro = $intro;
        $imscp->introformat = $introformat;
        $imscp->revision = 1;
        $imscp->keepold = 1;
        $imscp->timemodified = time();
        if (!($imscp = resource_migrate_to_module('imscp', $candidate, $imscp))) {
            continue;
        }
        $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
        $root = "{$CFG->dataroot}/{$candidate->course}/{$CFG->moddata}/resource/{$candidate->oldid}";
        // migrate package backup file
        if ($candidate->reference) {
            $package = basename($candidate->reference);
            $fullpath = $root . '/' . $package;
            if (file_exists($fullpath)) {
                $file_record = array('contextid' => $context->id, 'component' => 'mod_imscp', 'filearea' => 'backup', 'itemid' => 1, 'filepath' => '/', 'filename' => $package);
                $fs->create_file_from_pathname($file_record, $fullpath);
            }
        }
        // migrate extracted package data
        $files = imsc_migrate_get_old_files($root, '');
        if (empty($files)) {
            // if ims package doesn't exist, continue loop
            echo $OUTPUT->notification("IMS package data cannot be found, failed migrating activity: \"{$candidate->name}\", please fix it manually");
            continue;
        }
        $file_record = array('contextid' => $context->id, 'component' => 'mod_imscp', 'filearea' => 'content', 'itemid' => 1);
        $error = false;
        foreach ($files as $relname => $fullpath) {
            $parts = explode('/', $relname);
            $file_record['filename'] = array_pop($parts);
            $parts[] = '';
            // keep trailing slash
            $file_record['filepath'] = implode('/', $parts);
            try {
                $fs->create_file_from_pathname($file_record, $fullpath);
            } catch (Exception $e) {
                //continue on error, we can not recover anyway
                $error = true;
                echo $OUTPUT->notification("IMSCP: failed migrating file: {$relname}");
            }
        }
        unset($files);
        // parse manifest
        $structure = imscp_parse_structure($imscp, $context);
        $imscp->structure = is_array($structure) ? serialize($structure) : null;
        $DB->update_record('imscp', $imscp);
        // remove old moddata dir only if no error and manifest ok
        if (!$error and is_array($structure)) {
            fulldelete($root);
        }
    }
    $candidates->close();
    // clear all course modinfo caches
    rebuild_course_cache(0, true);
}