Beispiel #1
0
function clean_temp_data($preferences)
{
    global $CFG, $DB;
    $status = true;
    //true->do it, false->don't do it. To debug if necessary.
    if (true) {
        //Now delete from tables
        $status = $DB->delete_records('backup_ids', array('backup_code' => $preferences->backup_unique_code)) && $DB->delete_records('backup_files', array('backup_code' => $preferences->backup_unique_code));
        //Now, delete temp directory (if exists)
        $file_path = $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code;
        if (is_dir($file_path)) {
            $status = delete_dir_contents($file_path);
            //There is nothing, delete the directory itself
            if ($status) {
                $status = rmdir($file_path);
            }
        }
    }
    return $status;
}
function wiki_export_html(&$WS)
{
    global $CFG;
    check_dir_exists("{$CFG->dataroot}/temp", true);
    check_dir_exists("{$CFG->dataroot}/temp/html", true);
    check_dir_exists("{$CFG->dataroot}/temp/html/dfwiki{$WS->cm->id}", true);
    check_dir_exists("{$CFG->dataroot}/temp/html/dfwiki{$WS->cm->id}/atachments", true);
    check_dir_exists("{$CFG->dataroot}/{$WS->dfwiki->course}", true);
    check_dir_exists("{$CFG->dataroot}/{$WS->dfwiki->course}/moddata", true);
    check_dir_exists("{$CFG->dataroot}/{$WS->dfwiki->course}/moddata/dfwiki{$WS->cm->id}", true);
    //export contents
    wiki_export_html_content($WS);
    //export attached files
    $flist = list_directories_and_files("{$CFG->dataroot}/{$WS->dfwiki->course}/moddata/dfwiki{$WS->cm->id}");
    if ($flist != null) {
        foreach ($flist as $fil) {
            $from_file = "{$CFG->dataroot}/{$WS->dfwiki->course}/moddata/dfwiki{$WS->cm->id}/{$fil}";
            $to_file = "{$CFG->dataroot}/temp/html/dfwiki{$WS->cm->id}/atachments/{$fil}";
            copy($from_file, $to_file);
        }
    }
    //zip file name
    $times = time();
    $name = $WS->dfwiki->name . '-' . $times . '.zip';
    $cleanzipname = clean_filename($name);
    //List of files and directories
    $filelist = list_directories_and_files("{$CFG->dataroot}/temp/html");
    //Convert them to full paths
    $files = array();
    if ($filelist != null) {
        foreach ($filelist as $file) {
            $files[] = "{$CFG->dataroot}/temp/html/{$file}";
        }
    }
    check_dir_exists("{$CFG->dataroot}/{$WS->dfwiki->course}", true);
    check_dir_exists("{$CFG->dataroot}/{$WS->dfwiki->course}/exportedhtml", true);
    $destination = "{$CFG->dataroot}/{$WS->dfwiki->course}/exportedhtml/{$cleanzipname}";
    $status = zip_files($files, $destination);
    //delete the folder created in temp
    $filelist2 = list_directories_and_files("{$CFG->dataroot}/temp/html");
    if ($filelist2 != null) {
        $del = delete_dir_contents("{$CFG->dataroot}/temp/html");
    }
    //show it all to be albe to download the file
    $prop = null;
    $prop->class = "textcenter";
    wiki_div_start($prop);
    wiki_size_text(get_string("exporthtmlcorrectly", "wiki"), 2);
    wiki_div_end();
    $prop = null;
    $prop->border = "0";
    $prop->class = "boxaligncenter";
    $prop->classtd = "nwikileftnow";
    wiki_table_start($prop);
    $wdir = '/exportedhtml';
    $fileurl = "{$wdir}/{$cleanzipname}";
    $ffurl = "/file.php?file=/{$WS->cm->course}{$fileurl}";
    $icon = mimeinfo("icon", $cleanzipname);
    link_to_popup_window($ffurl, "display", "<img src=\"{$CFG->pixpath}/f/{$icon}\" height=\"16\" width=\"16\" alt=\"File\" />", 480, 640);
    echo "\n" . '&nbsp;';
    link_to_popup_window($ffurl, "display", htmlspecialchars($cleanzipname), 480, 640);
    $prop = null;
    $prop->class = "nwikileftnow";
    wiki_change_row($prop);
    echo '&nbsp;';
    wiki_table_end();
    $prop = null;
    $prop->border = "0";
    $prop->class = "boxaligncenter";
    $prop->classtd = "nwikileftnow";
    wiki_table_start($prop);
    $prop = null;
    $prop->id = "form";
    $prop->method = "post";
    $prop->action = '../xml/index.php?id=' . $WS->dfwiki->course . '&amp;wdir=/exportedhtml';
    wiki_form_start($prop);
    wiki_div_start();
    $prop = null;
    $prop->name = "dfform[viewexported]";
    $prop->value = get_string('viewexported', 'wiki');
    wiki_input_submit($prop);
    wiki_div_end();
    wiki_form_end();
    wiki_change_column();
    print_continue("{$CFG->wwwroot}/mod/wiki/view.php?id={$WS->cm->id}");
    wiki_table_end();
}
Beispiel #3
0
    print_error('confirmsesskeybad', 'error');
} else {
    if (!has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $courseid))) {
        print_error('onlyeditingteachers', 'error');
    }
}
///
/// Main process, where everything is deployed
///
/// Set some variables
/// Create directories
if (!($resourcedir = make_upload_directory($courseid . '/' . $CFG->moddata . '/resource/' . $resource->id))) {
    print_error('errorcreatingdirectory', 'error', '', $CFG->moddata . '/resource/' . $resource->id);
}
/// Ensure it's empty
if (!delete_dir_contents($resourcedir)) {
    print_error('errorcleaningdirectory', 'error', '', $resourcedir);
}
/// Copy files
$origin = $CFG->dataroot . '/' . $courseid . '/' . $file;
if (!is_file($origin)) {
    print_error('filenotfound', 'error', '', $file);
}
$mimetype = mimeinfo("type", $file);
if ($mimetype != "application/zip") {
    print_error('invalidfiletype', 'error', '', $file);
}
$resourcefile = $resourcedir . '/' . basename($origin);
if (!backup_copy_file($origin, $resourcefile)) {
    print_error('errorcopyingfiles', 'error');
}
Beispiel #4
0
function restore_execute(&$restore, $info, $course_header, &$errorstr)
{
    global $CFG, $USER;
    $status = true;
    //Checks for the required files/functions to restore every module
    //and include them
    if ($allmods = get_records("modules")) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = "{$CFG->dirroot}/mod/{$modname}/restorelib.php";
            //If file exists and we have selected to restore that type of module
            if (file_exists($modfile) and !empty($restore->mods[$modname]) and $restore->mods[$modname]->restore) {
                include_once $modfile;
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //Start the main table
        echo "<table cellpadding=\"5\">";
        echo "<tr><td>";
        //Start the main ul
        echo "<ul>";
    }
    //Location of the xml file
    $xml_file = $CFG->dataroot . "/temp/backup/" . $restore->backup_unique_code . "/moodle.xml";
    //Preprocess the moodle.xml file spliting into smaller chucks (modules, users, logs...)
    //for optimal parsing later in the restore process.
    if (!empty($CFG->experimentalsplitrestore)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('preprocessingbackupfile') . '</li>';
        }
        //First of all, split moodle.xml into handy files
        if (!restore_split_xml($xml_file, $restore)) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Error proccessing moodle.xml file. Process ended.");
            } else {
                $errorstr = "Error proccessing moodle.xml file. Process ended.";
            }
            return false;
        }
    }
    //If we've selected to restore into new course
    //create it (course)
    //Saving conversion id variables into backup_tables
    if ($restore->restoreto == RESTORETO_NEW_COURSE) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('creatingnewcourse') . '</li>';
        }
        $oldidnumber = $course_header->course_idnumber;
        if (!($status = restore_create_new_course($restore, $course_header))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Error while creating the new empty course.");
            } else {
                $errorstr = "Error while creating the new empty course.";
                return false;
            }
        }
        //Print course fullname and shortname and category
        if ($status) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . $course_header->course_fullname . " (" . $course_header->course_shortname . ")" . '</li>';
                echo "<li>" . get_string("category") . ": " . $course_header->category->name . '</li>';
                if (!empty($oldidnumber)) {
                    echo "<li>" . get_string("nomoreidnumber", "moodle", $oldidnumber) . "</li>";
                }
                echo "</ul>";
                //Put the destination course_id
            }
            $restore->course_id = $course_header->course_id;
        }
        if ($status = restore_open_html($restore, $course_header)) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>Creating the Restorelog.html in the course backup folder</li>";
            }
        }
    } else {
        $course = get_record("course", "id", $restore->course_id);
        if ($course) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("usingexistingcourse");
                echo "<ul>";
                echo "<li>" . get_string("from") . ": " . $course_header->course_fullname . " (" . $course_header->course_shortname . ")" . '</li>';
                echo "<li>" . get_string("to") . ": " . format_string($course->fullname) . " (" . format_string($course->shortname) . ")" . '</li>';
                if ($restore->deleting) {
                    echo "<li>" . get_string("deletingexistingcoursedata") . '</li>';
                } else {
                    echo "<li>" . get_string("addingdatatoexisting") . '</li>';
                }
                echo "</ul></li>";
            }
            //If we have selected to restore deleting, we do it now.
            if ($restore->deleting) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string("deletingolddata") . '</li>';
                }
                $status = remove_course_contents($restore->course_id, false) and delete_dir_contents($CFG->dataroot . "/" . $restore->course_id, "backupdata");
                if ($status) {
                    //Now , this situation is equivalent to the "restore to new course" one (we
                    //have a course record and nothing more), so define it as "to new course"
                    $restore->restoreto = RESTORETO_NEW_COURSE;
                } else {
                    if (!defined('RESTORE_SILENTLY')) {
                        notify("An error occurred while deleting some of the course contents.");
                    } else {
                        $errrostr = "An error occurred while deleting some of the course contents.";
                        return false;
                    }
                }
            }
        } else {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Error opening existing course.");
                $status = false;
            } else {
                $errorstr = "Error opening existing course.";
                return false;
            }
        }
    }
    //Now create users as needed
    if ($status and ($restore->users == 0 or $restore->users == 1)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingusers") . "<br />";
        }
        if (!($status = restore_create_users($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore users.");
            } else {
                $errorstr = "Could not restore users.";
                return false;
            }
        }
        //Now print info about the work done
        if ($status) {
            $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids\n                                     where backup_code = '{$restore->backup_unique_code}' and\n                                     table_name = 'user'");
            //We've records
            if ($recs) {
                $new_count = 0;
                $exists_count = 0;
                $student_count = 0;
                $teacher_count = 0;
                $counter = 0;
                //Iterate, filling counters
                foreach ($recs as $rec) {
                    //Get full record, using backup_getids
                    $record = backup_getid($restore->backup_unique_code, "user", $rec->old_id);
                    if (strpos($record->info, "new") !== false) {
                        $new_count++;
                    }
                    if (strpos($record->info, "exists") !== false) {
                        $exists_count++;
                    }
                    if (strpos($record->info, "student") !== false) {
                        $student_count++;
                    } else {
                        if (strpos($record->info, "teacher") !== false) {
                            $teacher_count++;
                        }
                    }
                    //Do some output
                    $counter++;
                    if ($counter % 10 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 200 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
                if (!defined('RESTORE_SILENTLY')) {
                    //Now print information gathered
                    echo " (" . get_string("new") . ": " . $new_count . ", " . get_string("existing") . ": " . $exists_count . ")";
                    echo "<ul>";
                    echo "<li>" . get_string("students") . ": " . $student_count . '</li>';
                    echo "<li>" . get_string("teachers") . ": " . $teacher_count . '</li>';
                    echo "</ul>";
                }
            } else {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("No users were found!");
                }
                // no need to return false here, it's recoverable.
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create groups as needed
    if ($status and ($restore->groups == RESTORE_GROUPS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroups");
        }
        if (!($status = restore_create_groups($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groups!");
            } else {
                $errorstr = "Could not restore groups!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create groupings as needed
    if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroupings");
        }
        if (!($status = restore_create_groupings($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groupings!");
            } else {
                $errorstr = "Could not restore groupings!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create groupingsgroups as needed
    if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroupingsgroups");
        }
        if (!($status = restore_create_groupings_groups($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groups in groupings!");
            } else {
                $errorstr = "Could not restore groups in groupings!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create the course_sections and their associated course_modules
    //we have to do this after groups and groupings are restored, because we need the new groupings id
    if ($status) {
        //Into new course
        if ($restore->restoreto == RESTORETO_NEW_COURSE) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("creatingsections");
            }
            if (!($status = restore_create_sections($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Error creating sections in the existing course.");
                } else {
                    $errorstr = "Error creating sections in the existing course.";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
            //Into existing course
        } else {
            if ($restore->restoreto != RESTORETO_NEW_COURSE) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string("checkingsections");
                }
                if (!($status = restore_create_sections($restore, $xml_file))) {
                    if (!defined('RESTORE_SILENTLY')) {
                        notify("Error creating sections in the existing course.");
                    } else {
                        $errorstr = "Error creating sections in the existing course.";
                        return false;
                    }
                }
                if (!defined('RESTORE_SILENTLY')) {
                    echo '</li>';
                }
                //Error
            } else {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Neither a new course or an existing one was specified.");
                    $status = false;
                } else {
                    $errorstr = "Neither a new course or an existing one was specified.";
                    return false;
                }
            }
        }
    }
    //Now create metacourse info
    if ($status and $restore->metacourse) {
        //Only to new courses!
        if ($restore->restoreto == RESTORETO_NEW_COURSE) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("creatingmetacoursedata");
            }
            if (!($status = restore_create_metacourse($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Error creating metacourse in the course.");
                } else {
                    $errorstr = "Error creating metacourse in the course.";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    //Now create categories and questions as needed
    if ($status) {
        include_once "{$CFG->dirroot}/question/restorelib.php";
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingcategoriesandquestions");
            echo "<ul>";
        }
        if (!($status = restore_create_questions($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore categories and questions!");
            } else {
                $errorstr = "Could not restore categories and questions!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</ul></li>";
        }
    }
    //Now create user_files as needed
    if ($status and $restore->user_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("copyinguserfiles");
        }
        if (!($status = restore_user_files($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore user files!");
            } else {
                $errorstr = "Could not restore user files!";
                return false;
            }
        }
        //If all is ok (and we have a counter)
        if ($status and $status !== true) {
            //Inform about user dirs created from backup
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . get_string("userzones") . ": " . $status;
                echo "</li></ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create course files as needed
    if ($status and $restore->course_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("copyingcoursefiles");
        }
        if (!($status = restore_course_files($restore))) {
            if (empty($status)) {
                notify("Could not restore course files!");
            } else {
                $errorstr = "Could not restore course files!";
                return false;
            }
        }
        //If all is ok (and we have a counter)
        if ($status and $status !== true) {
            //Inform about user dirs created from backup
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . get_string("filesfolders") . ": " . $status . '</li>';
                echo "</ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create site files as needed
    if ($status and $restore->site_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string('copyingsitefiles');
        }
        if (!($status = restore_site_files($restore))) {
            if (empty($status)) {
                notify("Could not restore site files!");
            } else {
                $errorstr = "Could not restore site files!";
                return false;
            }
        }
        //If all is ok (and we have a counter)
        if ($status and $status !== true) {
            //Inform about user dirs created from backup
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . get_string("filesfolders") . ": " . $status . '</li>';
                echo "</ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create messages as needed
    if ($status and $restore->messages) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingmessagesinfo");
        }
        if (!($status = restore_create_messages($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore messages!");
            } else {
                $errorstr = "Could not restore messages!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create blogs as needed
    if ($status and $restore->blogs) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingblogsinfo");
        }
        if (!($status = restore_create_blogs($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore blogs!");
            } else {
                $errorstr = "Could not restore blogs!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create scales as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingscales");
        }
        if (!($status = restore_create_scales($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore custom scales!");
            } else {
                $errorstr = "Could not restore custom scales!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create events as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingevents");
        }
        if (!($status = restore_create_events($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore course events!");
            } else {
                $errorstr = "Could not restore course events!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create course modules as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingcoursemodules");
        }
        if (!($status = restore_create_modules($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore modules!");
            } else {
                $errorstr = "Could not restore modules!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Bring back the course blocks -- do it AFTER the modules!!!
    if ($status) {
        //If we are deleting and bringing into a course or making a new course, same situation
        if ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_EXISTING_DELETING || $restore->restoreto == RESTORETO_NEW_COURSE) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('creatingblocks');
            }
            $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
            if (!($status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify('Error while creating the course blocks');
                } else {
                    $errorstr = "Error while creating the course blocks";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    if ($status) {
        //If we are deleting and bringing into a course or making a new course, same situation
        if ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_EXISTING_DELETING || $restore->restoreto == RESTORETO_NEW_COURSE) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('courseformatdata');
            }
            if (!($status = restore_set_format_data($restore, $xml_file))) {
                $error = "Error while setting the course format data";
                if (!defined('RESTORE_SILENTLY')) {
                    notify($error);
                } else {
                    $errorstr = $error;
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    //Now create log entries as needed
    if ($status and $restore->logs) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinglogentries");
        }
        if (!($status = restore_create_logs($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore logs!");
            } else {
                $errorstr = "Could not restore logs!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, if all is OK, adjust the instance field in course_modules !!
    //this also calculates the final modinfo information so, after this,
    //code needing it can be used (like role_assignments. MDL-13740)
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("checkinginstances");
        }
        if (!($status = restore_check_instances($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not adjust instances in course_modules!");
            } else {
                $errorstr = "Could not adjust instances in course_modules!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, if all is OK, adjust activity events
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("refreshingevents");
        }
        if (!($status = restore_refresh_events($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not refresh events for activities!");
            } else {
                $errorstr = "Could not refresh events for activities!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, if all is OK, adjust inter-activity links
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("decodinginternallinks");
        }
        if (!($status = restore_decode_content_links($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not decode content links!");
            } else {
                $errorstr = "Could not decode content links!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, with backup files prior to version 2005041100,
    //convert all the wiki texts in the course to markdown
    if ($status && $restore->backup_version < 2005041100) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("convertingwikitomarkdown");
        }
        if (!($status = restore_convert_wiki2markdown($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not convert wiki texts to markdown!");
            } else {
                $errorstr = "Could not convert wiki texts to markdown!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create gradebook as needed -- AFTER modules and blocks!!!
    if ($status) {
        if ($restore->backup_version > 2007090500) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("creatinggradebook");
            }
            if (!($status = restore_create_gradebook($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Could not restore gradebook!");
                } else {
                    $errorstr = "Could not restore gradebook!";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        } else {
            // for moodle versions before 1.9, those grades need to be converted to use the new gradebook
            // this code needs to execute *after* the course_modules are sorted out
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("migratinggrades");
            }
            /// force full refresh of grading data before migration == crete all items first
            if (!($status = restore_migrate_old_gradebook($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Could not migrate gradebook!");
                } else {
                    $errorstr = "Could not migrade gradebook!";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
        /// force full refresh of grading data after all items are created
        grade_force_full_regrading($restore->course_id);
        grade_grab_course_grades($restore->course_id);
    }
    /*******************************************************************************
     ************* Restore of Roles and Capabilities happens here ******************
     *******************************************************************************/
    // try to restore roles even when restore is going to fail - teachers might have
    // at least some role assigned - this is not correct though
    $status = restore_create_roles($restore, $xml_file) && $status;
    $status = restore_roles_settings($restore, $xml_file) && $status;
    //Now if all is OK, update:
    //   - course modinfo field
    //   - categories table
    //   - add user as teacher
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("checkingcourse");
        }
        //categories table
        $course = get_record("course", "id", $restore->course_id);
        fix_course_sortorder();
        // Check if the user has course update capability in the newly restored course
        // there is no need to load his capabilities again, because restore_roles_settings
        // would have loaded it anyway, if there is any assignments.
        // fix for MDL-6831
        $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
        if (!has_capability('moodle/course:manageactivities', $newcontext)) {
            // fix for MDL-9065, use the new config setting if exists
            if ($CFG->creatornewroleid) {
                role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
            } else {
                if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM))) {
                    if ($legacyteacher = array_shift($legacyteachers)) {
                        role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
                    }
                } else {
                    notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
                }
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Cleanup temps (files and db)
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("cleaningtempdata");
        }
        if (!($status = clean_temp_data($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not clean up temporary data from files and database");
            } else {
                $errorstr = "Could not clean up temporary data from files and database";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    // this is not a critical check - the result can be ignored
    if (restore_close_html($restore)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>Closing the Restorelog.html file.</li>';
        }
    } else {
        if (!defined('RESTORE_SILENTLY')) {
            notify("Could not close the restorelog.html file");
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //End the main ul
        echo "</ul>";
        //End the main table
        echo "</td></tr>";
        echo "</table>";
    }
    return $status;
}
Beispiel #5
0
function judge($config)
{
    global $CFG, $course, $id, $block;
    print_box_start('generalbox', 'notice');
    print_string('prepareing', 'block_anti_plagiarism');
    flush();
    $submission_path = extract_to_temp($CFG->dataroot . '/' . $course->id . '/' . $CFG->moddata . '/assignment/' . $id . '/');
    $command = eval('return ' . $config->judger . '_command($config, $submission_path);');
    if (debugging('', DEBUG_DEVELOPER)) {
        print_object($command);
    }
    $output = array();
    $return = null;
    print_string('done', 'block_anti_plagiarism');
    echo '<br />';
    flush();
    $descriptorspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
    $proc = proc_open($command, $descriptorspec, $pipes);
    if (!is_resource($proc)) {
        delete_dir_contents($submission_path);
        rmdir($submission_path);
        error(get_string('failed', 'block_anti_plagiarism'));
    }
    //Wait for the process to finish.
    $output = eval('return ' . $config->judger . '_waiting($pipes[1], $pipes[2]);');
    $return = proc_close($proc);
    print_string('done', 'block_anti_plagiarism');
    echo '<br />';
    if (debugging('', DEBUG_DEVELOPER)) {
        print_object($output);
    }
    if ($return) {
        //Error
        error(get_string('failed', 'block_anti_plagiarism'));
    } else {
        $results = eval('return ' . $config->judger . '_parse($output);');
        foreach ($results as $result) {
            insert_record('block_anti_plagiarism_pairs', $result);
        }
        print_string('done', 'block_anti_plagiarism');
        echo '<br />';
        print_string('numberofplagiarism', 'block_anti_plagiarism', count($results));
    }
    if (!debugging('', DEBUG_DEVELOPER)) {
        fulldelete($submission_path);
    }
    print_box_end();
    print_continue($CFG->wwwroot . '/blocks/anti_plagiarism/view.php?id=' . $id . '&block=' . $block . '&action=view');
}
function wiki_config_course_module($wiki)
{
    global $CFG;
    if ($entry = get_record_sql('SELECT *
            FROM ' . $CFG->prefix . 'wiki_entries
            WHERE wikiid=\'' . $wiki->id . '\'')) {
        $dfwiki->pagename = $entry->pagename;
    } else {
        $dfwiki->pagename = 'first';
    }
    $dfwiki->course = $wiki->course;
    $dfwiki->name = $wiki->name;
    $dfwiki->timemodified = time();
    $dfwiki->editable = '1';
    $dfwiki->attach = '0';
    $dfwiki->restore = '0';
    switch ($wiki->htmlmode) {
        case '0':
            $dfwiki->editor = 'ewiki';
            break;
        case '1':
            $dfwiki->editor = 'ewiki';
            break;
        case '2':
            $dfwiki->editor = 'htmleditor';
            break;
        default:
            break;
    }
    //look for the old wiki cm->id
    $modul = get_record("modules", "name", 'wiki');
    $coursemodule = get_record_sql('SELECT *
                    FROM ' . $CFG->prefix . 'course_modules
                    WHERE module=' . $modul->id . ' AND instance=' . $wiki->id);
    $dfwiki->groupmode = $coursemodule->groupmode;
    $dfwikiid = insert_record("dfwiki", addslashes($dfwiki));
    backup_flush(300);
    //modify the course_modules entry which was pointing to the old wiki so as to point to the new dfwiki
    $moduldfwiki = get_record("modules", "name", 'dfwiki');
    $quer = 'UPDATE ' . $CFG->prefix . 'course_modules
                SET module=\'' . $moduldfwiki->id . '\'
                WHERE id=\'' . $coursemodule->id . '\'';
    execute_sql($quer, false);
    $quer2 = 'UPDATE ' . $CFG->prefix . 'course_modules
                SET instance=\'' . $dfwikiid . '\'
                WHERE id=\'' . $coursemodule->id . '\'';
    execute_sql($quer2, false);
    //copy the attached files to the new dfwiki
    $oldentryids = get_records_sql('SELECT *
                    FROM ' . $CFG->prefix . 'wiki_entries
                    WHERE wikiid=\'' . $wiki->id . '\'');
    //get all the wiki entries
    foreach ($oldentryids as $oldentryid) {
        wiki_copy_attachments($wiki->id, $wiki->course, $coursemodule->id, $oldentryid->id);
    }
    //delete all old wiki attached files
    delete_dir_contents("{$CFG->dataroot}/{$wiki->course}/moddata/wiki/{$wiki->id}");
    return $dfwikiid;
}
Beispiel #7
0
function clean_temp_data($preferences)
{
    global $CFG;
    $status = true;
    //true->do it, false->don't do it. To debug if necessary.
    if (true) {
        //Now delete from tables
        $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids\n                                   WHERE backup_code = '{$preferences->backup_unique_code}'", false);
        if ($status) {
            $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files\n                                       WHERE backup_code = '{$preferences->backup_unique_code}'", false);
        }
        //Now, delete temp directory (if exists)
        $file_path = $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code;
        if (is_dir($file_path)) {
            $status = delete_dir_contents($file_path);
            //There is nothing, delete the directory itself
            if ($status) {
                $status = rmdir($file_path);
            }
        }
    }
    return $status;
}
$strtop = get_string('top', 'book');
add_to_log($course->id, 'book', 'generateimscp', 'generateimscp.php?id=' . $cm->id, $book->id, $cm->id);
/// Get all the chapters
$chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum');
/// Generate the manifest and all the contents
chapters2imsmanifest($chapters, $book, $cm);
/// Now zip everything
make_upload_directory('temp');
$zipfile = $CFG->dataroot . "/temp/" . time() . '.zip';
$files = get_directory_list($CFG->dataroot . "/{$cm->course}/moddata/book/{$book->id}", basename($zipfile), false, true, true);
foreach ($files as $key => $value) {
    $files[$key] = $CFG->dataroot . "/{$cm->course}/moddata/book/{$book->id}/" . $value;
}
zip_files($files, $zipfile);
/// Now delete all the temp dirs
delete_dir_contents($CFG->dataroot . "/{$cm->course}/moddata/book/{$book->id}");
/// Now serve the file
send_temp_file($zipfile, clean_filename($book->name) . '.zip');
/**
 * This function will create the default imsmanifest plus contents for the book chapters passed as array
 * Everything will be created under the book moddata file area *
 */
function chapters2imsmanifest($chapters, $book, $cm)
{
    global $CFG;
    /// Init imsmanifest and others
    $imsmanifest = '';
    $imsitems = '';
    $imsresources = '';
    /// Moodle and Book version
    $moodle_release = $CFG->release;
function get_ewikis($zip)
{
    global $CFG;
    $listewikis = null;
    //unpack the .zip
    check_dir_exists("{$CFG->dataroot}/temp", true);
    check_dir_exists("{$CFG->dataroot}/temp/ewikis", true);
    $destination = "{$CFG->dataroot}/temp/ewikis";
    unzip_file($zip, $destination, false);
    //take the .xml
    $filelist = list_directories_and_files("{$CFG->dataroot}/temp/ewikis");
    if ($filelist == null) {
        return $listewikis;
    }
    foreach ($filelist as $file) {
        $extension = explode(".", $file);
        $num = count($extension) - 1;
        if ($extension[$num] == "xml") {
            $goodfile = $file;
        }
    }
    $newfile = "{$CFG->dataroot}/temp/ewikis/{$goodfile}";
    $info = restore_read_xml_bis($newfile);
    if ($info != null) {
        foreach ($info as $mod) {
            if ($mod->modtype == 'wiki' || $mod->modtype == 'dfwiki') {
                $listewikis[] = $mod;
            }
        }
    }
    //delete the folder created in temp
    $filelist2 = list_directories_and_files("{$CFG->dataroot}/temp/ewikis");
    if ($filelist2 != null) {
        $del = delete_dir_contents("{$CFG->dataroot}/temp/ewikis");
    }
    return $listewikis;
}
function wiki_import_wiki_XML(&$WS)
{
    global $CFG, $file, $infopages, $oldid, $oldentryid;
    $e_wiki = get_ewikis_bis($WS->path, $file);
    if (is_array($e_wiki)) {
        wiki_validate_and_insert_content($e_wiki[0], $WS);
    } else {
        error("Not exists wiki pages to import in the backup file!!");
    }
    wiki_import_atachment_wiki($e_wiki[0]['importfrombackup'][1], $WS);
    //delete the folder that we've created on temp
    $filelist2 = list_directories_and_files("{$CFG->dataroot}/temp/ewikis");
    if ($filelist2 != null) {
        $del = delete_dir_contents("{$CFG->dataroot}/temp/ewikis");
    }
}
Beispiel #11
0
/**
 * @see /course/lib.php - create_course
 * - empty course contents, and clear backups
 * - doesn't create new course records
 * - doesn't change sort order
 * - doesn't update restricted modules
 */
function rebuild_course($cid)
{
    global $CFG;
    require_once $CFG->dirroot . '/backup/lib.php';
    require_once $CFG->libdir . '/pagelib.php';
    empty_course_contents($cid);
    delete_dir_contents($CFG->dataroot . '/' . $cid, 'backupdata');
    $course = get_record('course', 'id', $cid);
    // Setup the blocks
    $page = page_create_object(PAGE_COURSE_VIEW, $cid);
    blocks_repopulate_page($page);
    // Return value not checked because you can always edit later
}