public function tearDown()
 {
     global $CFG;
     if (empty($CFG->keeptempdirectoriesonbackup)) {
         fulldelete("{$CFG->dataroot}/temp/backup/{$this->tempdir}");
     }
 }
 /**
  * Does any post-processing that may be desired
  * Clean the temporary directory if a zip file was imported
  * @return bool success
  */
 public function importpostprocess()
 {
     if ($this->tempdir != '') {
         fulldelete($this->tempdir);
     }
     return true;
 }
Example #3
0
/**
 * Invalidate all server and client side caches.
 *
 * This method deletes the phsyical directory that is used to cache the theme
 * files used for serving.
 * Because it deletes the main theme cache directoy all themes are reset by
 * this function.
 */
function theme_reset_all_caches()
{
    global $CFG;
    require_once "{$CFG->libdir}/filelib.php";
    set_config('themerev', empty($CFG->themerev) ? 1 : $CFG->themerev + 1);
    fulldelete("{$CFG->cachedir}/theme");
}
Example #4
0
/**
 * Invalidate all server and client side caches.
 * @return void
 */
function theme_reset_all_caches() {
    global $CFG;
    require_once("$CFG->libdir/filelib.php");

    set_config('themerev', empty($CFG->themerev) ? 1 : $CFG->themerev+1);
    fulldelete("$CFG->dataroot/cache/theme");
}
function webquest_upgrade($oldversion)
{
    /// This function does anything necessary to upgrade
    /// older versions to match current functionality
    $status = true;
    global $CFG;
    if ($oldversion < 2007081222) {
        require_once $CFG->dirroot . '/backup/lib.php';
        //make the change into each course
        $courses = get_records("course");
        foreach ($courses as $course) {
            $newdir = "{$course->id}/{$CFG->moddata}/webquest";
            if (make_upload_directory($newdir)) {
                $olddir = "{$CFG->dataroot}/{$course->id}/{$CFG->moddata}/webquest/submissions";
                //chec k if the old directory exists
                if (is_dir($olddir)) {
                    $status = backup_copy_file($olddir, $CFG->dataroot . "/" . $newdir);
                }
                if ($status) {
                    fulldelete($olddir);
                }
            }
        }
    }
    return $status;
}
Example #6
0
 protected function tearDown()
 {
     global $CFG;
     if (empty($CFG->keeptempdirectoriesonbackup)) {
         fulldelete("{$CFG->tempdir}/backup/{$this->tempdir}");
     }
 }
Example #7
0
/**
 * Delete profile images associated with user or group
 * @param int $id user or group id
 * @param string $dir type of entity - 'groups' or 'users'
 * @return boolean success
 */
function delete_profile_image($id, $dir = 'users')
{
    global $CFG;
    require_once $CFG->libdir . '/filelib.php';
    $location = $CFG->dataroot . '/' . $dir . '/' . $id;
    if (file_exists($location)) {
        return fulldelete($location);
    }
    return true;
}
function delete_certificate_files($certificate = '')
{
    global $CFG;
    require_once $CFG->libdir . '/filelib.php';
    $dir = $CFG->dataroot . '/' . $certificate->course . '/' . $CFG->moddata . '/certificate/' . $certificate->id . '/' . $user->id;
    if ($certificateid) {
        $dir .= '/' . $certificateid;
    }
    return fulldelete($dir);
}
Example #9
0
/**
 * Invalidate all server and client side caches.
 *
 * This method deletes the physical directory that is used to cache the theme
 * files used for serving.
 * Because it deletes the main theme cache directory all themes are reset by
 * this function.
 */
function theme_reset_all_caches()
{
    global $CFG;
    require_once "{$CFG->libdir}/filelib.php";
    $next = time();
    if (isset($CFG->themerev) and $next <= $CFG->themerev and $CFG->themerev - $next < 60 * 60) {
        // This resolves problems when reset is requested repeatedly within 1s,
        // the < 1h condition prevents accidental switching to future dates
        // because we might not recover from it.
        $next = $CFG->themerev + 1;
    }
    set_config('themerev', $next);
    // time is unique even when you reset/switch database
    fulldelete("{$CFG->cachedir}/theme");
}
Example #10
0
function webquest_delete_instance($id)
{
    /// Given an ID of an instance of this module,
    /// delete the instance and any data that depends on it.
    global $CFG;
    if (!($webquest = get_record("webquest", "id", "{$id}"))) {
        return false;
    }
    $result = true;
    if (!delete_records("webquest", "id", "{$webquest->id}")) {
        $result = false;
    }
    if (!delete_records("webquest_resources", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    if (!delete_records("webquest_tasks", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    if (!delete_records("webquest_rubrics", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    if (!delete_records("webquest_grades", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    if (!delete_records("webquest_teams", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    if (!delete_records("webquest_team_members", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    if ($submissions = get_records("webquest_submissions", "webquestid", "{$webquest->id}")) {
        foreach ($submissions as $submission) {
            $dirpath = "{$CFG->dataroot}/{$webquest->course}/{$CFG->moddata}/webquest/{$submission->id}";
            fulldelete($dirpath);
        }
    }
    if (!delete_records("webquest_submissions", "webquestid", "{$webquest->id}")) {
        $result = false;
    }
    return $result;
}
Example #11
0
 public function execute()
 {
     global $CFG, $DB;
     if ($this->expandedOptions['flush']) {
         if (isset($CFG->trashdir)) {
             $trashdir = $CFG->trashdir;
         } else {
             $trashdir = $CFG->dataroot . '/trashdir';
         }
         require_once $CFG->libdir . '/filelib.php';
         fulldelete($trashdir);
         exit(0);
     }
     if ($this->expandedOptions['stdin']) {
         while ($line = fgets(STDIN)) {
             $this->fileDelete($line);
         }
     } else {
         foreach ($this->arguments as $argument) {
             $this->fileDelete($argument);
         }
     }
 }
Example #12
0
 /**
  * Deletes an assignment activity
  *
  * Deletes all database records, files and calendar events for this assignment.
  * @param $assignment object The assignment to be deleted
  * @return boolean False indicates error
  */
 function delete_instance($assignment)
 {
     global $CFG;
     $assignment->courseid = $assignment->course;
     $result = true;
     if (!delete_records('assignment_submissions', 'assignment', $assignment->id)) {
         $result = false;
     }
     if (!delete_records('assignment', 'id', $assignment->id)) {
         $result = false;
     }
     if (!delete_records('event', 'modulename', 'assignment', 'instance', $assignment->id)) {
         $result = false;
     }
     // delete file area with all attachments - ignore errors
     require_once $CFG->libdir . '/filelib.php';
     fulldelete($CFG->dataroot . '/' . $assignment->course . '/' . $CFG->moddata . '/assignment/' . $assignment->id);
     assignment_grade_item_delete($assignment);
     return $result;
 }
// restored copy of the module
$newcmid = null;
$tasks = $rc->get_plan()->get_tasks();
foreach ($tasks as $task) {
    if (is_subclass_of($task, 'restore_activity_task')) {
        if ($task->get_old_contextid() == $cmcontext->id) {
            $newcmid = $task->get_moduleid();
            break;
        }
    }
}
// if we know the cmid of the new course module, let us move it
// right below the original one. otherwise it will stay at the
// end of the section
if ($newcmid) {
    $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
    moveto_module($newcm, $section, $cm);
    moveto_module($cm, $section, $newcm);
}
$rc->destroy();
if (empty($CFG->keeptempdirectoriesonbackup)) {
    fulldelete($backupbasepath);
}
echo $output->header();
if ($newcmid) {
    echo $output->confirm(get_string('duplicatesuccess', 'core', $a), new single_button(new moodle_url('/course/modedit.php', array('update' => $newcmid, 'sr' => $sectionreturn)), get_string('duplicatecontedit'), 'get'), new single_button(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)), get_string('duplicatecontcourse'), 'get'));
} else {
    echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
    echo $output->continue_button(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
}
echo $output->footer();
Example #14
0
 /**
  * Reset all submissions
  */
 function reset_userdata($data)
 {
     global $CFG;
     require_once $CFG->libdir . '/filelib.php';
     if (!count_records('assignment', 'course', $data->courseid, 'assignmenttype', $this->type)) {
         return array();
         // no assignments of this type present
     }
     $componentstr = get_string('modulenameplural', 'assignment');
     $status = array();
     $typestr = get_string('type' . $this->type, 'assignment');
     if (!empty($data->reset_assignment_submissions)) {
         $assignmentssql = "SELECT a.id\n                                 FROM {$CFG->prefix}assignment a\n                                WHERE a.course={$data->courseid} AND a.assignmenttype='{$this->type}'";
         delete_records_select('assignment_submissions', "assignment IN ({$assignmentssql})");
         if ($assignments = get_records_sql($assignmentssql)) {
             foreach ($assignments as $assignmentid => $unused) {
                 fulldelete($CFG->dataroot . '/' . $data->courseid . '/moddata/assignment/' . $assignmentid);
             }
         }
         $status[] = array('component' => $componentstr, 'item' => get_string('deleteallsubmissions', 'assignment') . ': ' . $typestr, 'error' => false);
         if (empty($data->reset_gradebook_grades)) {
             // remove all grades from gradebook
             assignment_reset_gradebook($data->courseid, $this->type);
         }
     }
     /// updating dates - shift may be negative too
     if ($data->timeshift) {
         shift_course_mod_dates('assignment', array('timedue', 'timeavailable'), $data->timeshift, $data->courseid);
         $status[] = array('component' => $componentstr, 'item' => get_string('datechanged') . ': ' . $typestr, 'error' => false);
     }
     return $status;
 }
Example #15
0
         echo "<form action=\"coursefiles.php\" method=\"get\">\n";
         echo " <input type=\"hidden\" name=\"id\" value=\"{$id}\" />\n";
         echo " <input type=\"hidden\" name=\"wdir\" value=\"{$wdir}\" />\n";
         echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />\n";
         echo " <input type=\"submit\" value=\"{$strcancel}\" />\n";
         echo "</form>\n";
         echo "</td>\n</tr>\n</table>\n";
     }
     html_footer();
     break;
 case "delete":
     if ($confirm and confirm_sesskey()) {
         html_header($course, $wdir);
         foreach ($USER->filelist as $file) {
             $fullfile = $basedir . $file;
             if (!fulldelete($fullfile)) {
                 echo "<br />Error: Could not delete: {$fullfile}";
             }
         }
         clearfilelist();
         displaydir($wdir);
         html_footer();
     } else {
         html_header($course, $wdir);
         if (setfilelist($_POST)) {
             echo "<p align=center>" . get_string("deletecheckwarning") . ":</p>";
             print_simple_box_start("center");
             printfilelist($USER->filelist);
             print_simple_box_end();
             echo "<br />";
             $frameold = $CFG->framename;
Example #16
0
 public function cleanup() {
     return fulldelete($this->directory);
 }
 /**
  * Processes this restore stage
  * @return bool
  * @throws restore_ui_exception
  */
 public function process()
 {
     global $CFG;
     if ($this->filename) {
         $archivepath = $CFG->tempdir . '/backup/' . $this->filename;
         if (!file_exists($archivepath)) {
             throw new restore_ui_exception('invalidrestorefile');
         }
         $outcome = $this->extract_file_to_dir($archivepath);
         if ($outcome) {
             fulldelete($archivepath);
         }
     } else {
         $fs = get_file_storage();
         $storedfile = $fs->get_file_by_hash($this->pathnamehash);
         if (!$storedfile || $storedfile->get_contenthash() !== $this->contenthash) {
             throw new restore_ui_exception('invalidrestorefile');
         }
         $outcome = $this->extract_file_to_dir($storedfile);
     }
     return $outcome;
 }
/**
 * Invalidate all server and client side JS caches.
 * @return void
 */
function js_reset_all_caches()
{
    global $CFG;
    require_once "{$CFG->libdir}/filelib.php";
    set_config('jsrev', empty($CFG->jsrev) ? 1 : $CFG->jsrev + 1);
    fulldelete("{$CFG->dataroot}/cache/js");
}
Example #19
0
 /**
  * This function takes the combined pdf and embeds all the comments and annotations.
  * @param int|\assign $assignment
  * @param int $userid
  * @param int $attemptnumber (-1 means latest attempt)
  * @return stored_file
  */
 public static function generate_feedback_document($assignment, $userid, $attemptnumber)
 {
     $assignment = self::get_assignment_from_param($assignment);
     if (!$assignment->can_view_submission($userid)) {
         \print_error('nopermission');
     }
     if (!$assignment->can_grade()) {
         \print_error('nopermission');
     }
     // Need to generate the page images - first get a combined pdf.
     $file = self::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
     if (!$file) {
         throw new \moodle_exception('Could not generate combined pdf.');
     }
     $tmpdir = \make_temp_directory('assignfeedback_editpdf/final/' . self::hash($assignment, $userid, $attemptnumber));
     $combined = $tmpdir . '/' . self::COMBINED_PDF_FILENAME;
     $file->copy_content_to($combined);
     // Copy the file.
     $pdf = new pdf();
     $fs = \get_file_storage();
     $stamptmpdir = \make_temp_directory('assignfeedback_editpdf/stamps/' . self::hash($assignment, $userid, $attemptnumber));
     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
     // Copy any new stamps to this instance.
     if ($files = $fs->get_area_files($assignment->get_context()->id, 'assignfeedback_editpdf', 'stamps', $grade->id, "filename", false)) {
         foreach ($files as $file) {
             $filename = $stamptmpdir . '/' . $file->get_filename();
             $file->copy_content_to($filename);
             // Copy the file.
         }
     }
     $pagecount = $pdf->set_pdf($combined);
     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
     page_editor::release_drafts($grade->id);
     for ($i = 0; $i < $pagecount; $i++) {
         $pdf->copy_page();
         $comments = page_editor::get_comments($grade->id, $i, false);
         $annotations = page_editor::get_annotations($grade->id, $i, false);
         foreach ($comments as $comment) {
             $pdf->add_comment($comment->rawtext, $comment->x, $comment->y, $comment->width, $comment->colour);
         }
         foreach ($annotations as $annotation) {
             $pdf->add_annotation($annotation->x, $annotation->y, $annotation->endx, $annotation->endy, $annotation->colour, $annotation->type, $annotation->path, $stamptmpdir);
         }
     }
     fulldelete($stamptmpdir);
     $filename = self::get_downloadable_feedback_filename($assignment, $userid, $attemptnumber);
     $filename = clean_param($filename, PARAM_FILE);
     $generatedpdf = $tmpdir . '/' . $filename;
     $pdf->save_pdf($generatedpdf);
     $record = new \stdClass();
     $record->contextid = $assignment->get_context()->id;
     $record->component = 'assignfeedback_editpdf';
     $record->filearea = self::FINAL_PDF_FILEAREA;
     $record->itemid = $grade->id;
     $record->filepath = '/';
     $record->filename = $filename;
     // Only keep one current version of the generated pdf.
     $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid);
     $file = $fs->create_file_from_pathname($record, $generatedpdf);
     // Cleanup.
     @unlink($generatedpdf);
     @unlink($combined);
     @rmdir($tmpdir);
     return $file;
 }
Example #20
0
/**
 * Clear a course out completely, deleting all content but don't delete the course itself.
 *
 * This function does not verify any permissions.
 *
 * Please note this function also deletes all user enrolments,
 * enrolment instances and role assignments by default.
 *
 * $options:
 *  - 'keep_roles_and_enrolments' - false by default
 *  - 'keep_groups_and_groupings' - false by default
 *
 * @param int $courseid The id of the course that is being deleted
 * @param bool $showfeedback Whether to display notifications of each action the function performs.
 * @param array $options extra options
 * @return bool true if all the removals succeeded. false if there were any failures. If this
 *             method returns false, some of the removals will probably have succeeded, and others
 *             failed, but you have no way of knowing which.
 */
function remove_course_contents($courseid, $showfeedback = true, array $options = null)
{
    global $CFG, $DB, $OUTPUT;
    require_once $CFG->libdir . '/badgeslib.php';
    require_once $CFG->libdir . '/completionlib.php';
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->dirroot . '/group/lib.php';
    require_once $CFG->dirroot . '/comment/lib.php';
    require_once $CFG->dirroot . '/rating/lib.php';
    require_once $CFG->dirroot . '/notes/lib.php';
    // Handle course badges.
    badges_handle_course_deletion($courseid);
    // NOTE: these concatenated strings are suboptimal, but it is just extra info...
    $strdeleted = get_string('deleted') . ' - ';
    // Some crazy wishlist of stuff we should skip during purging of course content.
    $options = (array) $options;
    $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
    $coursecontext = context_course::instance($courseid);
    $fs = get_file_storage();
    // Delete course completion information, this has to be done before grades and enrols.
    $cc = new completion_info($course);
    $cc->clear_criteria();
    if ($showfeedback) {
        echo $OUTPUT->notification($strdeleted . get_string('completion', 'completion'), 'notifysuccess');
    }
    // Remove all data from gradebook - this needs to be done before course modules
    // because while deleting this information, the system may need to reference
    // the course modules that own the grades.
    remove_course_grades($courseid, $showfeedback);
    remove_grade_letters($coursecontext, $showfeedback);
    // Delete course blocks in any all child contexts,
    // they may depend on modules so delete them first.
    $childcontexts = $coursecontext->get_child_contexts();
    // Returns all subcontexts since 2.2.
    foreach ($childcontexts as $childcontext) {
        blocks_delete_all_for_context($childcontext->id);
    }
    unset($childcontexts);
    blocks_delete_all_for_context($coursecontext->id);
    if ($showfeedback) {
        echo $OUTPUT->notification($strdeleted . get_string('type_block_plural', 'plugin'), 'notifysuccess');
    }
    // Get the list of all modules that are properly installed.
    $allmodules = $DB->get_records_menu('modules', array(), '', 'name, id');
    // Delete every instance of every module,
    // this has to be done before deleting of course level stuff.
    $locations = core_component::get_plugin_list('mod');
    foreach ($locations as $modname => $moddir) {
        if ($modname === 'NEWMODULE') {
            continue;
        }
        if (array_key_exists($modname, $allmodules)) {
            $sql = "SELECT cm.*, m.id AS modinstance, m.name, '{$modname}' AS modname\n              FROM {" . $modname . "} m\n                   LEFT JOIN {course_modules} cm ON cm.instance = m.id AND cm.module = :moduleid\n             WHERE m.course = :courseid";
            $instances = $DB->get_records_sql($sql, array('courseid' => $course->id, 'modulename' => $modname, 'moduleid' => $allmodules[$modname]));
            include_once "{$moddir}/lib.php";
            // Shows php warning only if plugin defective.
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance.
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon).
            if ($instances) {
                foreach ($instances as $cm) {
                    if ($cm->id) {
                        // Delete activity context questions and question categories.
                        question_delete_activity($cm, $showfeedback);
                        // Notify the competency subsystem.
                        \core_competency\api::hook_course_module_deleted($cm);
                    }
                    if (function_exists($moddelete)) {
                        // This purges all module data in related tables, extra user prefs, settings, etc.
                        $moddelete($cm->modinstance);
                    } else {
                        // NOTE: we should not allow installation of modules with missing delete support!
                        debugging("Defective module '{$modname}' detected when deleting course contents: missing function {$moddelete}()!");
                        $DB->delete_records($modname, array('id' => $cm->modinstance));
                    }
                    if ($cm->id) {
                        // Delete cm and its context - orphaned contexts are purged in cron in case of any race condition.
                        context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
                        $DB->delete_records('course_modules', array('id' => $cm->id));
                    }
                }
            }
            if (function_exists($moddeletecourse)) {
                // Execute optional course cleanup callback. Deprecated since Moodle 3.2. TODO MDL-53297 remove in 3.6.
                debugging("Callback delete_course is deprecated. Function {$moddeletecourse} should be converted " . 'to observer of event \\core\\event\\course_content_deleted', DEBUG_DEVELOPER);
                $moddeletecourse($course, $showfeedback);
            }
            if ($instances and $showfeedback) {
                echo $OUTPUT->notification($strdeleted . get_string('pluginname', $modname), 'notifysuccess');
            }
        } else {
            // Ooops, this module is not properly installed, force-delete it in the next block.
        }
    }
    // We have tried to delete everything the nice way - now let's force-delete any remaining module data.
    // Remove all data from availability and completion tables that is associated
    // with course-modules belonging to this course. Note this is done even if the
    // features are not enabled now, in case they were enabled previously.
    $DB->delete_records_select('course_modules_completion', 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', array($courseid));
    // Remove course-module data that has not been removed in modules' _delete_instance callbacks.
    $cms = $DB->get_records('course_modules', array('course' => $course->id));
    $allmodulesbyid = array_flip($allmodules);
    foreach ($cms as $cm) {
        if (array_key_exists($cm->module, $allmodulesbyid)) {
            try {
                $DB->delete_records($allmodulesbyid[$cm->module], array('id' => $cm->instance));
            } catch (Exception $e) {
                // Ignore weird or missing table problems.
            }
        }
        context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
        $DB->delete_records('course_modules', array('id' => $cm->id));
    }
    if ($showfeedback) {
        echo $OUTPUT->notification($strdeleted . get_string('type_mod_plural', 'plugin'), 'notifysuccess');
    }
    // Cleanup the rest of plugins. Deprecated since Moodle 3.2. TODO MDL-53297 remove in 3.6.
    $cleanuplugintypes = array('report', 'coursereport', 'format');
    $callbacks = get_plugins_with_function('delete_course', 'lib.php');
    foreach ($cleanuplugintypes as $type) {
        if (!empty($callbacks[$type])) {
            foreach ($callbacks[$type] as $pluginfunction) {
                debugging("Callback delete_course is deprecated. Function {$pluginfunction} should be converted " . 'to observer of event \\core\\event\\course_content_deleted', DEBUG_DEVELOPER);
                $pluginfunction($course->id, $showfeedback);
            }
            if ($showfeedback) {
                echo $OUTPUT->notification($strdeleted . get_string('type_' . $type . '_plural', 'plugin'), 'notifysuccess');
            }
        }
    }
    // Delete questions and question categories.
    question_delete_course($course, $showfeedback);
    if ($showfeedback) {
        echo $OUTPUT->notification($strdeleted . get_string('questions', 'question'), 'notifysuccess');
    }
    // Make sure there are no subcontexts left - all valid blocks and modules should be already gone.
    $childcontexts = $coursecontext->get_child_contexts();
    // Returns all subcontexts since 2.2.
    foreach ($childcontexts as $childcontext) {
        $childcontext->delete();
    }
    unset($childcontexts);
    // Remove all roles and enrolments by default.
    if (empty($options['keep_roles_and_enrolments'])) {
        // This hack is used in restore when deleting contents of existing course.
        role_unassign_all(array('contextid' => $coursecontext->id, 'component' => ''), true);
        enrol_course_delete($course);
        if ($showfeedback) {
            echo $OUTPUT->notification($strdeleted . get_string('type_enrol_plural', 'plugin'), 'notifysuccess');
        }
    }
    // Delete any groups, removing members and grouping/course links first.
    if (empty($options['keep_groups_and_groupings'])) {
        groups_delete_groupings($course->id, $showfeedback);
        groups_delete_groups($course->id, $showfeedback);
    }
    // Filters be gone!
    filter_delete_all_for_context($coursecontext->id);
    // Notes, you shall not pass!
    note_delete_all($course->id);
    // Die comments!
    comment::delete_comments($coursecontext->id);
    // Ratings are history too.
    $delopt = new stdclass();
    $delopt->contextid = $coursecontext->id;
    $rm = new rating_manager();
    $rm->delete_ratings($delopt);
    // Delete course tags.
    core_tag_tag::remove_all_item_tags('core', 'course', $course->id);
    // Notify the competency subsystem.
    \core_competency\api::hook_course_deleted($course);
    // Delete calendar events.
    $DB->delete_records('event', array('courseid' => $course->id));
    $fs->delete_area_files($coursecontext->id, 'calendar');
    // Delete all related records in other core tables that may have a courseid
    // This array stores the tables that need to be cleared, as
    // table_name => column_name that contains the course id.
    $tablestoclear = array('backup_courses' => 'courseid', 'user_lastaccess' => 'courseid');
    foreach ($tablestoclear as $table => $col) {
        $DB->delete_records($table, array($col => $course->id));
    }
    // Delete all course backup files.
    $fs->delete_area_files($coursecontext->id, 'backup');
    // Cleanup course record - remove links to deleted stuff.
    $oldcourse = new stdClass();
    $oldcourse->id = $course->id;
    $oldcourse->summary = '';
    $oldcourse->cacherev = 0;
    $oldcourse->legacyfiles = 0;
    if (!empty($options['keep_groups_and_groupings'])) {
        $oldcourse->defaultgroupingid = 0;
    }
    $DB->update_record('course', $oldcourse);
    // Delete course sections.
    $DB->delete_records('course_sections', array('course' => $course->id));
    // Delete legacy, section and any other course files.
    $fs->delete_area_files($coursecontext->id, 'course');
    // Files from summary and section.
    // Delete all remaining stuff linked to context such as files, comments, ratings, etc.
    if (empty($options['keep_roles_and_enrolments']) and empty($options['keep_groups_and_groupings'])) {
        // Easy, do not delete the context itself...
        $coursecontext->delete_content();
    } else {
        // Hack alert!!!!
        // We can not drop all context stuff because it would bork enrolments and roles,
        // there might be also files used by enrol plugins...
    }
    // Delete legacy files - just in case some files are still left there after conversion to new file api,
    // also some non-standard unsupported plugins may try to store something there.
    fulldelete($CFG->dataroot . '/' . $course->id);
    // Delete from cache to reduce the cache size especially makes sense in case of bulk course deletion.
    $cachemodinfo = cache::make('core', 'coursemodinfo');
    $cachemodinfo->delete($courseid);
    // Trigger a course content deleted event.
    $event = \core\event\course_content_deleted::create(array('objectid' => $course->id, 'context' => $coursecontext, 'other' => array('shortname' => $course->shortname, 'fullname' => $course->fullname, 'options' => $options)));
    $event->add_record_snapshot('course', $course);
    $event->trigger();
    return true;
}
Example #21
0
 /**
  * Return content of all files containing questions,
  * as an array one element for each file found,
  * For each file, the corresponding element is an array of lines.
  * @param string filename name of file
  * @return mixed contents array or false on failure
  */
 public function readdata($filename)
 {
     // Find if we are importing a .txt file.
     if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) == 'txt') {
         if (!is_readable($filename)) {
             $this->error(get_string('filenotreadable', 'error'));
             return false;
         }
         return file($filename);
     }
     // We are importing a zip file.
     // Create name for temporary directory.
     $uniquecode = time();
     $this->tempdir = make_temp_directory('webct_import/' . $uniquecode);
     if (is_readable($filename)) {
         if (!copy($filename, $this->tempdir . '/webct.zip')) {
             $this->error(get_string('cannotcopybackup', 'question'));
             fulldelete($this->tempdir);
             return false;
         }
         if (unzip_file($this->tempdir . '/webct.zip', '', false)) {
             $dir = $this->tempdir;
             if (($handle = opendir($dir)) == false) {
                 // The directory could not be opened.
                 fulldelete($this->tempdir);
                 return false;
             }
             // Create arrays to store files and directories.
             $dirfiles = array();
             $dirsubdirs = array();
             $slash = '/';
             // Loop through all directory entries, and construct two temporary arrays containing files and sub directories.
             while (false !== ($entry = readdir($handle))) {
                 if (is_dir($dir . $slash . $entry) && $entry != '..' && $entry != '.') {
                     $dirsubdirs[] = $dir . $slash . $entry;
                 } else {
                     if ($entry != '..' && $entry != '.') {
                         $dirfiles[] = $dir . $slash . $entry;
                     }
                 }
             }
             if (($handle = opendir($dirsubdirs[0])) == false) {
                 // The directory could not be opened.
                 fulldelete($this->tempdir);
                 return false;
             }
             while (false !== ($entry = readdir($handle))) {
                 if (is_dir($dirsubdirs[0] . $slash . $entry) && $entry != '..' && $entry != '.') {
                     $dirsubdirs[] = $dirsubdirs[0] . $slash . $entry;
                 } else {
                     if ($entry != '..' && $entry != '.') {
                         $dirfiles[] = $dirsubdirs[0] . $slash . $entry;
                     }
                 }
             }
             return file($dirfiles[1]);
         } else {
             $this->error(get_string('cannotunzip', 'question'));
             fulldelete($this->temp_dir);
         }
     } else {
         $this->error(get_string('cannotreaduploadfile', 'error'));
         fulldelete($this->tempdir);
     }
     return false;
 }
Example #22
0
 /**
  * Cleans up stuff after the execution
  *
  * Note that we do not know if the execution was successful or not.
  * An exception might have been thrown.
  */
 protected function destroy()
 {
     global $CFG;
     if (empty($CFG->keeptempdirectoriesonbackup)) {
         fulldelete($this->get_workdir_path());
     }
 }
Example #23
0
/**
 * Main upgrade tasks to be executed on Moodle version bump
 *
 * This function is automatically executed after one bump in the Moodle core
 * version is detected. It's in charge of performing the required tasks
 * to raise core from the previous version to the next one.
 *
 * It's a collection of ordered blocks of code, named "upgrade steps",
 * each one performing one isolated (from the rest of steps) task. Usually
 * tasks involve creating new DB objects or performing manipulation of the
 * information for cleanup/fixup purposes.
 *
 * Each upgrade step has a fixed structure, that can be summarised as follows:
 *
 * if ($oldversion < XXXXXXXXXX.XX) {
 *     // Explanation of the update step, linking to issue in the Tracker if necessary
 *     upgrade_set_timeout(XX); // Optional for big tasks
 *     // Code to execute goes here, usually the XMLDB Editor will
 *     // help you here. See {@link http://docs.moodle.org/dev/XMLDB_editor}.
 *     upgrade_main_savepoint(true, XXXXXXXXXX.XX);
 * }
 *
 * All plugins within Moodle (modules, blocks, reports...) support the existence of
 * their own upgrade.php file, using the "Frankenstyle" component name as
 * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
 *     - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
 *     - {@link xmldb_auth_manual_upgrade($oldversion)}.
 *     - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
 *     - ....
 *
 * In order to keep the contents of this file reduced, it's allowed to create some helper
 * functions to be used here in the {@link upgradelib.php} file at the same directory. Note
 * that such a file must be manually included from upgrade.php, and there are some restrictions
 * about what can be used within it.
 *
 * For more information, take a look to the documentation available:
 *     - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
 *     - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
 *
 * @param int $oldversion
 * @return bool always true
 */
function xmldb_main_upgrade($oldversion)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/db/upgradelib.php';
    // Core Upgrade-related functions.
    $dbman = $DB->get_manager();
    // Loads ddl manager and xmldb classes.
    // Always keep this upgrade step with version being the minimum
    // allowed version to upgrade from (v2.7.0 right now).
    if ($oldversion < 2014051200) {
        // Just in case somebody hacks upgrade scripts or env, we really can not continue.
        echo "You need to upgrade to 2.7.x or higher first!\n";
        exit(1);
        // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
        upgrade_main_savepoint(true, 2014051200);
    }
    // MDL-32543 Make sure that the log table has correct length for action and url fields.
    if ($oldversion < 2014051200.02) {
        $table = new xmldb_table('log');
        $columns = $DB->get_columns('log');
        if ($columns['action']->max_length < 40) {
            $index1 = new xmldb_index('course-module-action', XMLDB_INDEX_NOTUNIQUE, array('course', 'module', 'action'));
            if ($dbman->index_exists($table, $index1)) {
                $dbman->drop_index($table, $index1);
            }
            $index2 = new xmldb_index('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
            if ($dbman->index_exists($table, $index2)) {
                $dbman->drop_index($table, $index2);
            }
            $field = new xmldb_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'cmid');
            $dbman->change_field_precision($table, $field);
            $dbman->add_index($table, $index1);
            $dbman->add_index($table, $index2);
        }
        if ($columns['url']->max_length < 100) {
            $field = new xmldb_field('url', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'action');
            $dbman->change_field_precision($table, $field);
        }
        upgrade_main_savepoint(true, 2014051200.02);
    }
    if ($oldversion < 2014060300.0) {
        $gspath = get_config('assignfeedback_editpdf', 'gspath');
        if ($gspath !== false) {
            set_config('pathtogs', $gspath);
            unset_config('gspath', 'assignfeedback_editpdf');
        }
        upgrade_main_savepoint(true, 2014060300.0);
    }
    if ($oldversion < 2014061000.0) {
        // Fixing possible wrong MIME type for Publisher files.
        $filetypes = array('%.pub' => 'application/x-mspublisher');
        upgrade_mimetypes($filetypes);
        upgrade_main_savepoint(true, 2014061000.0);
    }
    if ($oldversion < 2014062600.01) {
        // We only want to delete DragMath if the directory no longer exists. If the directory
        // is present then it means it has been restored, so do not perform the uninstall.
        if (!check_dir_exists($CFG->libdir . '/editor/tinymce/plugins/dragmath', false)) {
            // Purge DragMath plugin which is incompatible with GNU GPL license.
            unset_all_config_for_plugin('tinymce_dragmath');
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014062600.01);
    }
    // Switch the order of the fields in the files_reference index, to improve the performance of search_references.
    if ($oldversion < 2014070100.0) {
        $table = new xmldb_table('files_reference');
        $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        upgrade_main_savepoint(true, 2014070100.0);
    }
    if ($oldversion < 2014070101.0) {
        $table = new xmldb_table('files_reference');
        $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('referencehash', 'repositoryid'));
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        upgrade_main_savepoint(true, 2014070101.0);
    }
    if ($oldversion < 2014072400.01) {
        $table = new xmldb_table('user_devices');
        $oldindex = new xmldb_index('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
        if ($dbman->index_exists($table, $oldindex)) {
            $key = new xmldb_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
            $dbman->drop_key($table, $key);
        }
        upgrade_main_savepoint(true, 2014072400.01);
    }
    if ($oldversion < 2014080801.0) {
        // Define index behaviour (not unique) to be added to question_attempts.
        $table = new xmldb_table('question_attempts');
        $index = new xmldb_index('behaviour', XMLDB_INDEX_NOTUNIQUE, array('behaviour'));
        // Conditionally launch add index behaviour.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014080801.0);
    }
    if ($oldversion < 2014082900.01) {
        // Fixing possible wrong MIME type for 7-zip and Rar files.
        $filetypes = array('%.7z' => 'application/x-7z-compressed', '%.rar' => 'application/x-rar-compressed');
        upgrade_mimetypes($filetypes);
        upgrade_main_savepoint(true, 2014082900.01);
    }
    if ($oldversion < 2014082900.02) {
        // Replace groupmembersonly usage with new availability system.
        $transaction = $DB->start_delegated_transaction();
        if ($CFG->enablegroupmembersonly) {
            // If it isn't already enabled, we need to enable availability.
            if (!$CFG->enableavailability) {
                set_config('enableavailability', 1);
            }
            // Count all course-modules with groupmembersonly set (for progress
            // bar).
            $total = $DB->count_records('course_modules', array('groupmembersonly' => 1));
            $pbar = new progress_bar('upgradegroupmembersonly', 500, true);
            // Get all these course-modules, one at a time.
            $rs = $DB->get_recordset('course_modules', array('groupmembersonly' => 1), 'course, id');
            $i = 0;
            foreach ($rs as $cm) {
                // Calculate and set new availability value.
                $availability = upgrade_group_members_only($cm->groupingid, $cm->availability);
                $DB->set_field('course_modules', 'availability', $availability, array('id' => $cm->id));
                // Update progress.
                $i++;
                $pbar->update($i, $total, "Upgrading groupmembersonly settings - {$i}/{$total}.");
            }
            $rs->close();
        }
        // Define field groupmembersonly to be dropped from course_modules.
        $table = new xmldb_table('course_modules');
        $field = new xmldb_field('groupmembersonly');
        // Conditionally launch drop field groupmembersonly.
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // Unset old config variable.
        unset_config('enablegroupmembersonly');
        $transaction->allow_commit();
        upgrade_main_savepoint(true, 2014082900.02);
    }
    if ($oldversion < 2014100100.0) {
        // Define table messageinbound_handlers to be created.
        $table = new xmldb_table('messageinbound_handlers');
        // Adding fields to table messageinbound_handlers.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
        $table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('defaultexpiration', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '86400');
        $table->add_field('validateaddress', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
        $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
        // Adding keys to table messageinbound_handlers.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('classname', XMLDB_KEY_UNIQUE, array('classname'));
        // Conditionally launch create table for messageinbound_handlers.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table messageinbound_datakeys to be created.
        $table = new xmldb_table('messageinbound_datakeys');
        // Adding fields to table messageinbound_datakeys.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('handler', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('datavalue', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('datakey', XMLDB_TYPE_CHAR, '64', null, null, null, null);
        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        // Adding keys to table messageinbound_datakeys.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('handler_datavalue', XMLDB_KEY_UNIQUE, array('handler', 'datavalue'));
        $table->add_key('handler', XMLDB_KEY_FOREIGN, array('handler'), 'messageinbound_handlers', array('id'));
        // Conditionally launch create table for messageinbound_datakeys.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100100.0);
    }
    if ($oldversion < 2014100600.01) {
        // Define field aggregationstatus to be added to grade_grades.
        $table = new xmldb_table('grade_grades');
        $field = new xmldb_field('aggregationstatus', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'unknown', 'timemodified');
        // Conditionally launch add field aggregationstatus.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('aggregationweight', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'aggregationstatus');
        // Conditionally launch add field aggregationweight.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field aggregationcoef2 to be added to grade_items.
        $table = new xmldb_table('grade_items');
        $field = new xmldb_field('aggregationcoef2', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'aggregationcoef');
        // Conditionally launch add field aggregationcoef2.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('weightoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'needsupdate');
        // Conditionally launch add field weightoverride.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100600.01);
    }
    if ($oldversion < 2014100600.02) {
        // Define field aggregationcoef2 to be added to grade_items_history.
        $table = new xmldb_table('grade_items_history');
        $field = new xmldb_field('aggregationcoef2', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'aggregationcoef');
        // Conditionally launch add field aggregationcoef2.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100600.02);
    }
    if ($oldversion < 2014100600.03) {
        // Define field weightoverride to be added to grade_items_history.
        $table = new xmldb_table('grade_items_history');
        $field = new xmldb_field('weightoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'decimals');
        // Conditionally launch add field weightoverride.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100600.03);
    }
    if ($oldversion < 2014100600.04) {
        // Set flags so we can display a notice on all courses that might
        // be affected by the uprade to natural aggregation.
        if (!get_config('grades_sumofgrades_upgrade_flagged', 'core')) {
            // 13 == SUM_OF_GRADES.
            $sql = 'SELECT DISTINCT courseid
                      FROM {grade_categories}
                     WHERE aggregation = ?';
            $courses = $DB->get_records_sql($sql, array(13));
            foreach ($courses as $course) {
                set_config('show_sumofgrades_upgrade_' . $course->courseid, 1);
                // Set each of the grade items to needing an update so that when the user visits the grade reports the
                // figures will be updated.
                $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $course->courseid));
            }
            set_config('grades_sumofgrades_upgrade_flagged', 1);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100600.04);
    }
    if ($oldversion < 2014100700.0) {
        // Define table messageinbound_messagelist to be created.
        $table = new xmldb_table('messageinbound_messagelist');
        // Adding fields to table messageinbound_messagelist.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('messageid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('address', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        // Adding keys to table messageinbound_messagelist.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
        // Conditionally launch create table for messageinbound_messagelist.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100700.0);
    }
    if ($oldversion < 2014100700.01) {
        // Define field visible to be added to cohort.
        $table = new xmldb_table('cohort');
        $field = new xmldb_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'descriptionformat');
        // Conditionally launch add field visible.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100700.01);
    }
    if ($oldversion < 2014100800.0) {
        // Remove qformat_learnwise (unless it has manually been added back).
        if (!file_exists($CFG->dirroot . '/question/format/learnwise/format.php')) {
            unset_all_config_for_plugin('qformat_learnwise');
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014100800.0);
    }
    if ($oldversion < 2014101001.0) {
        // Some blocks added themselves to the my/ home page, but they did not declare the
        // subpage of the default my home page. While the upgrade script has been fixed, this
        // upgrade script will fix the data that was wrongly added.
        // We only proceed if we can find the right entry from my_pages. Private => 1 refers to
        // the constant value MY_PAGE_PRIVATE.
        if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
            // Select the blocks there could have been automatically added. showinsubcontexts is hardcoded to 0
            // because it is possible for administrators to have forced it on the my/ page by adding it to the
            // system directly rather than updating the default my/ page.
            $blocks = array('course_overview', 'private_files', 'online_users', 'badges', 'calendar_month', 'calendar_upcoming');
            list($blocksql, $blockparams) = $DB->get_in_or_equal($blocks, SQL_PARAMS_NAMED);
            $select = "parentcontextid = :contextid\n                    AND pagetypepattern = :page\n                    AND showinsubcontexts = 0\n                    AND subpagepattern IS NULL\n                    AND blockname {$blocksql}";
            $params = array('contextid' => context_system::instance()->id, 'page' => 'my-index');
            $params = array_merge($params, $blockparams);
            $DB->set_field_select('block_instances', 'subpagepattern', $systempage->id, $select, $params);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014101001.0);
    }
    if ($oldversion < 2014102000.0) {
        // Define field aggregatesubcats to be dropped from grade_categories.
        $table = new xmldb_table('grade_categories');
        $field = new xmldb_field('aggregatesubcats');
        // Conditionally launch drop field aggregatesubcats.
        if ($dbman->field_exists($table, $field)) {
            $sql = 'SELECT DISTINCT courseid
                      FROM {grade_categories}
                     WHERE aggregatesubcats = ?';
            $courses = $DB->get_records_sql($sql, array(1));
            foreach ($courses as $course) {
                set_config('show_aggregatesubcats_upgrade_' . $course->courseid, 1);
                // Set each of the grade items to needing an update so that when the user visits the grade reports the
                // figures will be updated.
                $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $course->courseid));
            }
            $dbman->drop_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014102000.0);
    }
    if ($oldversion < 2014110300.0) {
        // Run script restoring missing folder records for draft file areas.
        upgrade_fix_missing_root_folders_draft();
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014110300.0);
    }
    // Moodle v2.8.0 release upgrade line.
    // Put any upgrade step following this.
    if ($oldversion < 2014111000.0) {
        // Coming from 2.7 or older, we need to flag the step minmaxgrade to be ignored.
        set_config('upgrade_minmaxgradestepignored', 1);
        // Coming from 2.7 or older, we need to flag the step for changing calculated grades to be regraded.
        set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014111000.0);
    }
    if ($oldversion < 2014120100.0) {
        // Define field sslverification to be added to mnet_host.
        $table = new xmldb_table('mnet_host');
        $field = new xmldb_field('sslverification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'applicationid');
        // Conditionally launch add field sslverification.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014120100.0);
    }
    if ($oldversion < 2014120101.0) {
        // Define field component to be added to comments.
        $table = new xmldb_table('comments');
        $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'contextid');
        // Conditionally launch add field component.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014120101.0);
    }
    if ($oldversion < 2014120102.0) {
        // Define table user_password_history to be created.
        $table = new xmldb_table('user_password_history');
        // Adding fields to table user_password_history.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('hash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        // Adding keys to table user_password_history.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
        // Conditionally launch create table for user_password_history.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2014120102.0);
    }
    if ($oldversion < 2015010800.01) {
        // Make sure the private files handler is not set to expire.
        $DB->set_field('messageinbound_handlers', 'defaultexpiration', 0, array('classname' => '\\core\\message\\inbound\\private_files_handler'));
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015010800.01);
    }
    if ($oldversion < 2015012600.0) {
        // If the site is using internal and external storage, or just external
        // storage, and the external path specified is empty we change the setting
        // to internal only. That is how the backup code is handling this
        // misconfiguration.
        $storage = (int) get_config('backup', 'backup_auto_storage');
        $folder = get_config('backup', 'backup_auto_destination');
        if ($storage !== 0 && empty($folder)) {
            set_config('backup_auto_storage', 0, 'backup');
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015012600.0);
    }
    if ($oldversion < 2015012600.01) {
        // Convert calendar_lookahead to nearest new value.
        $value = $DB->get_field('config', 'value', array('name' => 'calendar_lookahead'));
        if ($value > 90) {
            set_config('calendar_lookahead', '120');
        } else {
            if ($value > 60 and $value < 90) {
                set_config('calendar_lookahead', '90');
            } else {
                if ($value > 30 and $value < 60) {
                    set_config('calendar_lookahead', '60');
                } else {
                    if ($value > 21 and $value < 30) {
                        set_config('calendar_lookahead', '30');
                    } else {
                        if ($value > 14 and $value < 21) {
                            set_config('calendar_lookahead', '21');
                        } else {
                            if ($value > 7 and $value < 14) {
                                set_config('calendar_lookahead', '14');
                            }
                        }
                    }
                }
            }
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015012600.01);
    }
    if ($oldversion < 2015021100.0) {
        // Define field timemodified to be added to registration_hubs.
        $table = new xmldb_table('registration_hubs');
        $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
        // Conditionally launch add field timemodified.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015021100.0);
    }
    if ($oldversion < 2015022401.0) {
        // Define index useridfromto (not unique) to be added to message.
        $table = new xmldb_table('message');
        $index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
        // Conditionally launch add index useridfromto.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Define index useridfromto (not unique) to be added to message_read.
        $table = new xmldb_table('message_read');
        $index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
        // Conditionally launch add index useridfromto.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015022401.0);
    }
    if ($oldversion < 2015022500.0) {
        $table = new xmldb_table('user_devices');
        $index = new xmldb_index('uuid-userid', XMLDB_INDEX_NOTUNIQUE, array('uuid', 'userid'));
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        upgrade_main_savepoint(true, 2015022500.0);
    }
    if ($oldversion < 2015030400.0) {
        // We have long since switched to storing timemodified per hub rather than a single 'registered' timestamp.
        unset_config('registered');
        upgrade_main_savepoint(true, 2015030400.0);
    }
    if ($oldversion < 2015031100.0) {
        // Unset old config variable.
        unset_config('enabletgzbackups');
        upgrade_main_savepoint(true, 2015031100.0);
    }
    if ($oldversion < 2015031400.0) {
        // Define index useridfrom (not unique) to be dropped form message.
        $table = new xmldb_table('message');
        $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
        // Conditionally launch drop index useridfrom.
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        // Define index useridfrom (not unique) to be dropped form message_read.
        $table = new xmldb_table('message_read');
        $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
        // Conditionally launch drop index useridfrom.
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015031400.0);
    }
    if ($oldversion < 2015031900.01) {
        unset_config('crontime', 'registration');
        upgrade_main_savepoint(true, 2015031900.01);
    }
    if ($oldversion < 2015032000.0) {
        $table = new xmldb_table('badge_criteria');
        $field = new xmldb_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
        // Conditionally add description field to the badge_criteria table.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, null, XMLDB_NOTNULL, null, 0);
        // Conditionally add description format field to the badge_criteria table.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_main_savepoint(true, 2015032000.0);
    }
    if ($oldversion < 2015040200.01) {
        // Force uninstall of deleted tool.
        if (!file_exists("{$CFG->dirroot}/{$CFG->admin}/tool/timezoneimport")) {
            // Remove capabilities.
            capabilities_cleanup('tool_timezoneimport');
            // Remove all other associated config.
            unset_all_config_for_plugin('tool_timezoneimport');
        }
        upgrade_main_savepoint(true, 2015040200.01);
    }
    if ($oldversion < 2015040200.02) {
        // Define table timezone to be dropped.
        $table = new xmldb_table('timezone');
        // Conditionally launch drop table for timezone.
        if ($dbman->table_exists($table)) {
            $dbman->drop_table($table);
        }
        upgrade_main_savepoint(true, 2015040200.02);
    }
    if ($oldversion < 2015040200.03) {
        if (isset($CFG->timezone) and $CFG->timezone == 99) {
            // Migrate to real server timezone.
            unset_config('timezone');
        }
        upgrade_main_savepoint(true, 2015040200.03);
    }
    if ($oldversion < 2015040700.01) {
        $DB->delete_records('config_plugins', array('name' => 'requiremodintro'));
        upgrade_main_savepoint(true, 2015040700.01);
    }
    if ($oldversion < 2015040900.01) {
        // Add "My grades" to the user menu.
        $oldconfig = get_config('core', 'customusermenuitems');
        if (strpos("mygrades,grades|/grade/report/mygrades.php|grades", $oldconfig) === false) {
            $newconfig = "mygrades,grades|/grade/report/mygrades.php|grades\n" . $oldconfig;
            set_config('customusermenuitems', $newconfig);
        }
        upgrade_main_savepoint(true, 2015040900.01);
    }
    if ($oldversion < 2015040900.02) {
        // Update the default user menu (add preferences, remove my files and my badges).
        $oldconfig = get_config('core', 'customusermenuitems');
        // Add "My preferences" at the end.
        if (strpos($oldconfig, "mypreferences,moodle|/user/preference.php|preferences") === false) {
            $newconfig = $oldconfig . "\nmypreferences,moodle|/user/preferences.php|preferences";
        } else {
            $newconfig = $oldconfig;
        }
        // Remove my files.
        $newconfig = str_replace("myfiles,moodle|/user/files.php|download", "", $newconfig);
        // Remove my badges.
        $newconfig = str_replace("mybadges,badges|/badges/mybadges.php|award", "", $newconfig);
        // Remove holes.
        $newconfig = preg_replace('/\\n+/', "\n", $newconfig);
        $newconfig = preg_replace('/(\\r\\n)+/', "\n", $newconfig);
        set_config('customusermenuitems', $newconfig);
        upgrade_main_savepoint(true, 2015040900.02);
    }
    if ($oldversion < 2015050400.0) {
        $config = get_config('core', 'customusermenuitems');
        // Change "My preferences" in the user menu to "Preferences".
        $config = str_replace("mypreferences,moodle|/user/preferences.php|preferences", "preferences,moodle|/user/preferences.php|preferences", $config);
        // Change "My grades" in the user menu to "Grades".
        $config = str_replace("mygrades,grades|/grade/report/mygrades.php|grades", "grades,grades|/grade/report/mygrades.php|grades", $config);
        set_config('customusermenuitems', $config);
        upgrade_main_savepoint(true, 2015050400.0);
    }
    if ($oldversion < 2015050401.0) {
        // Make sure we have messages in the user menu because it's no longer in the nav tree.
        $oldconfig = get_config('core', 'customusermenuitems');
        $messagesconfig = "messages,message|/message/index.php|message";
        $preferencesconfig = "preferences,moodle|/user/preferences.php|preferences";
        // See if it exists.
        if (strpos($oldconfig, $messagesconfig) === false) {
            // See if preferences exists.
            if (strpos($oldconfig, "preferences,moodle|/user/preferences.php|preferences") !== false) {
                // Insert it before preferences.
                $newconfig = str_replace($preferencesconfig, $messagesconfig . "\n" . $preferencesconfig, $oldconfig);
            } else {
                // Custom config - we can only insert it at the end.
                $newconfig = $oldconfig . "\n" . $messagesconfig;
            }
            set_config('customusermenuitems', $newconfig);
        }
        upgrade_main_savepoint(true, 2015050401.0);
    }
    // Moodle v2.9.0 release upgrade line.
    // Put any upgrade step following this.
    if ($oldversion < 2015060400.02) {
        // Sites that were upgrading from 2.7 and older will ignore this step.
        if (empty($CFG->upgrade_minmaxgradestepignored)) {
            upgrade_minmaxgrade();
            // Flags this upgrade step as already run to prevent it from running multiple times.
            set_config('upgrade_minmaxgradestepignored', 1);
        }
        upgrade_main_savepoint(true, 2015060400.02);
    }
    if ($oldversion < 2015061900.0) {
        // MDL-49257. Changed the algorithm of calculating automatic weights of extra credit items.
        // Before the change, in case when grade category (in "Natural" agg. method) had items with
        // overridden weights, the automatic weight of extra credit items was illogical.
        // In order to prevent grades changes after the upgrade we need to freeze gradebook calculation
        // for the affected courses.
        // This script in included in each major version upgrade process so make sure we don't run it twice.
        if (empty($CFG->upgrade_extracreditweightsstepignored)) {
            upgrade_extra_credit_weightoverride();
            // To skip running the same script on the upgrade to the next major release.
            set_config('upgrade_extracreditweightsstepignored', 1);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015061900.0);
    }
    if ($oldversion < 2015062500.01) {
        // MDL-48239. Changed calculated grade items so that the maximum and minimum grade can be set.
        // If the changes are accepted and a regrade is done on the gradebook then some grades may change significantly.
        // This is here to freeze the gradebook in affected courses.
        // This script is included in each major version upgrade process so make sure we don't run it twice.
        if (empty($CFG->upgrade_calculatedgradeitemsignored)) {
            upgrade_calculated_grade_items();
            // To skip running the same script on the upgrade to the next major release.
            set_config('upgrade_calculatedgradeitemsignored', 1);
            // This config value is never used again.
            unset_config('upgrade_calculatedgradeitemsonlyregrade');
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015062500.01);
    }
    if ($oldversion < 2015081300.01) {
        // Define field importtype to be added to grade_import_values.
        $table = new xmldb_table('grade_import_values');
        $field = new xmldb_field('importonlyfeedback', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'importer');
        // Conditionally launch add field importtype.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015081300.01);
    }
    if ($oldversion < 2015082400.0) {
        // Define table webdav_locks to be dropped.
        $table = new xmldb_table('webdav_locks');
        // Conditionally launch drop table for webdav_locks.
        if ($dbman->table_exists($table)) {
            $dbman->drop_table($table);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015082400.0);
    }
    if ($oldversion < 2015090200.0) {
        $table = new xmldb_table('message');
        // Define the deleted fields to be added to the message tables.
        $field1 = new xmldb_field('timeuserfromdeleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
        $field2 = new xmldb_field('timeusertodeleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
        $oldindex = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
        $newindex = new xmldb_index('useridfromtodeleted', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto', 'timeuserfromdeleted', 'timeusertodeleted'));
        // Conditionally launch add field timeuserfromdeleted.
        if (!$dbman->field_exists($table, $field1)) {
            $dbman->add_field($table, $field1);
        }
        // Conditionally launch add field timeusertodeleted.
        if (!$dbman->field_exists($table, $field2)) {
            $dbman->add_field($table, $field2);
        }
        // Conditionally launch drop index useridfromto.
        if ($dbman->index_exists($table, $oldindex)) {
            $dbman->drop_index($table, $oldindex);
        }
        // Conditionally launch add index useridfromtodeleted.
        if (!$dbman->index_exists($table, $newindex)) {
            $dbman->add_index($table, $newindex);
        }
        // Now add them to the message_read table.
        $table = new xmldb_table('message_read');
        // Conditionally launch add field timeuserfromdeleted.
        if (!$dbman->field_exists($table, $field1)) {
            $dbman->add_field($table, $field1);
        }
        // Conditionally launch add field timeusertodeleted.
        if (!$dbman->field_exists($table, $field2)) {
            $dbman->add_field($table, $field2);
        }
        // Conditionally launch drop index useridfromto.
        if ($dbman->index_exists($table, $oldindex)) {
            $dbman->drop_index($table, $oldindex);
        }
        // Conditionally launch add index useridfromtodeleted.
        if (!$dbman->index_exists($table, $newindex)) {
            $dbman->add_index($table, $newindex);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015090200.0);
    }
    if ($oldversion < 2015090801.0) {
        // This upgrade script merges all tag instances pointing to the same course tag.
        // User id is no longer used for those tag instances.
        upgrade_course_tags();
        // If configuration variable "Show course tags" is set, disable the block
        // 'tags' because it can not be used for tagging courses any more.
        if (!empty($CFG->block_tags_showcoursetags)) {
            if ($record = $DB->get_record('block', array('name' => 'tags'), 'id, visible')) {
                if ($record->visible) {
                    $DB->update_record('block', array('id' => $record->id, 'visible' => 0));
                }
            }
        }
        // Define index idname (unique) to be dropped form tag (it's really weird).
        $table = new xmldb_table('tag');
        $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
        // Conditionally launch drop index idname.
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015090801.0);
    }
    if ($oldversion < 2015092200.0) {
        // Define index qtype (not unique) to be added to question.
        $table = new xmldb_table('question');
        $index = new xmldb_index('qtype', XMLDB_INDEX_NOTUNIQUE, array('qtype'));
        // Conditionally launch add index qtype.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015092200.0);
    }
    if ($oldversion < 2015092900.0) {
        // Rename backup_auto_keep setting to backup_auto_max_kept.
        $keep = get_config('backup', 'backup_auto_keep');
        if ($keep !== false) {
            set_config('backup_auto_max_kept', $keep, 'backup');
            unset_config('backup_auto_keep', 'backup');
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015092900.0);
    }
    if ($oldversion < 2015100600.0) {
        // Define index notification (not unique) to be added to message_read.
        $table = new xmldb_table('message_read');
        $index = new xmldb_index('notificationtimeread', XMLDB_INDEX_NOTUNIQUE, array('notification', 'timeread'));
        // Conditionally launch add index notification.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2015100600.0);
    }
    if ($oldversion < 2015100800.01) {
        // The only flag for preventing all plugins installation features is
        // now $CFG->disableupdateautodeploy in config.php.
        unset_config('updateautodeploy');
        upgrade_main_savepoint(true, 2015100800.01);
    }
    // Moodle v3.0.0 release upgrade line.
    // Put any upgrade step following this.
    if ($oldversion < 2016011300.01) {
        // This is a big upgrade script. We create new table tag_coll and the field
        // tag.tagcollid pointing to it.
        // Define table tag_coll to be created.
        $table = new xmldb_table('tag_coll');
        // Adding fields to table tagcloud.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
        $table->add_field('isdefault', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
        $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
        $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, '0');
        $table->add_field('searchable', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
        $table->add_field('customurl', XMLDB_TYPE_CHAR, '255', null, null, null, null);
        // Adding keys to table tagcloud.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for tagcloud.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Table {tag}.
        // Define index name (unique) to be dropped form tag - we will replace it with index on (tagcollid,name) later.
        $table = new xmldb_table('tag');
        $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
        // Conditionally launch drop index name.
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        // Define field tagcollid to be added to tag, we create it as null first and will change to notnull later.
        $table = new xmldb_table('tag');
        $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'userid');
        // Conditionally launch add field tagcloudid.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.01);
    }
    if ($oldversion < 2016011300.02) {
        // Create a default tag collection if not exists and update the field tag.tagcollid to point to it.
        if (!($tcid = $DB->get_field_sql('SELECT id FROM {tag_coll} ORDER BY isdefault DESC, sortorder, id', null, IGNORE_MULTIPLE))) {
            $tcid = $DB->insert_record('tag_coll', array('isdefault' => 1, 'sortorder' => 0));
        }
        $DB->execute('UPDATE {tag} SET tagcollid = ? WHERE tagcollid IS NULL', array($tcid));
        // Define index tagcollname (unique) to be added to tag.
        $table = new xmldb_table('tag');
        $index = new xmldb_index('tagcollname', XMLDB_INDEX_UNIQUE, array('tagcollid', 'name'));
        $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
        // Conditionally launch add index tagcollname.
        if (!$dbman->index_exists($table, $index)) {
            // Launch change of nullability for field tagcollid.
            $dbman->change_field_notnull($table, $field);
            $dbman->add_index($table, $index);
        }
        // Define key tagcollid (foreign) to be added to tag.
        $table = new xmldb_table('tag');
        $key = new xmldb_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
        // Launch add key tagcloudid.
        $dbman->add_key($table, $key);
        // Define index tagcolltype (not unique) to be added to tag.
        $table = new xmldb_table('tag');
        $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
        // Conditionally launch add index tagcolltype.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.02);
    }
    if ($oldversion < 2016011300.03) {
        // Define table tag_area to be created.
        $table = new xmldb_table('tag_area');
        // Adding fields to table tag_area.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
        $table->add_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
        $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
        $table->add_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('callback', XMLDB_TYPE_CHAR, '100', null, null, null, null);
        $table->add_field('callbackfile', XMLDB_TYPE_CHAR, '100', null, null, null, null);
        // Adding keys to table tag_area.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
        // Adding indexes to table tag_area.
        $table->add_index('compitemtype', XMLDB_INDEX_UNIQUE, array('component', 'itemtype'));
        // Conditionally launch create table for tag_area.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.03);
    }
    if ($oldversion < 2016011300.04) {
        // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance.
        $table = new xmldb_table('tag_instance');
        $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
        // Conditionally launch drop index itemtype-itemid-tagid-tiuserid.
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.04);
    }
    if ($oldversion < 2016011300.05) {
        $DB->execute("UPDATE {tag_instance} SET component = ? WHERE component IS NULL", array(''));
        // Changing nullability of field component on table tag_instance to not null.
        $table = new xmldb_table('tag_instance');
        $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'tagid');
        // Launch change of nullability for field component.
        $dbman->change_field_notnull($table, $field);
        // Changing type of field itemtype on table tag_instance to char.
        $table = new xmldb_table('tag_instance');
        $field = new xmldb_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'component');
        // Launch change of type for field itemtype.
        $dbman->change_field_type($table, $field);
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.05);
    }
    if ($oldversion < 2016011300.06) {
        // Define index taggeditem (unique) to be added to tag_instance.
        $table = new xmldb_table('tag_instance');
        $index = new xmldb_index('taggeditem', XMLDB_INDEX_UNIQUE, array('component', 'itemtype', 'itemid', 'tiuserid', 'tagid'));
        // Conditionally launch add index taggeditem.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.06);
    }
    if ($oldversion < 2016011300.07) {
        // Define index taglookup (not unique) to be added to tag_instance.
        $table = new xmldb_table('tag_instance');
        $index = new xmldb_index('taglookup', XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'component', 'tagid', 'contextid'));
        // Conditionally launch add index taglookup.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016011300.07);
    }
    if ($oldversion < 2016011301.0) {
        // Force uninstall of deleted tool.
        if (!file_exists("{$CFG->dirroot}/webservice/amf")) {
            // Remove capabilities.
            capabilities_cleanup('webservice_amf');
            // Remove all other associated config.
            unset_all_config_for_plugin('webservice_amf');
        }
        upgrade_main_savepoint(true, 2016011301.0);
    }
    if ($oldversion < 2016011901.0) {
        // Convert calendar_lookahead to nearest new value.
        $transaction = $DB->start_delegated_transaction();
        // Count all users who curretly have that preference set (for progress bar).
        $total = $DB->count_records_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
        $pbar = new progress_bar('upgradecalendarlookahead', 500, true);
        // Get all these users, one at a time.
        $rs = $DB->get_recordset_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
        $i = 0;
        foreach ($rs as $userpref) {
            // Calculate and set new lookahead value.
            if ($userpref->value > 90) {
                $newvalue = 120;
            } else {
                if ($userpref->value > 60 and $userpref->value < 90) {
                    $newvalue = 90;
                } else {
                    if ($userpref->value > 30 and $userpref->value < 60) {
                        $newvalue = 60;
                    } else {
                        if ($userpref->value > 21 and $userpref->value < 30) {
                            $newvalue = 30;
                        } else {
                            if ($userpref->value > 14 and $userpref->value < 21) {
                                $newvalue = 21;
                            } else {
                                if ($userpref->value > 7 and $userpref->value < 14) {
                                    $newvalue = 14;
                                } else {
                                    $newvalue = $userpref->value;
                                }
                            }
                        }
                    }
                }
            }
            $DB->set_field('user_preferences', 'value', $newvalue, array('id' => $userpref->id));
            // Update progress.
            $i++;
            $pbar->update($i, $total, "Upgrading user preference settings - {$i}/{$total}.");
        }
        $rs->close();
        $transaction->allow_commit();
        upgrade_main_savepoint(true, 2016011901.0);
    }
    if ($oldversion < 2016020200.0) {
        // Define field isstandard to be added to tag.
        $table = new xmldb_table('tag');
        $field = new xmldb_field('isstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'rawname');
        // Conditionally launch add field isstandard.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define index tagcolltype (not unique) to be dropped form tag.
        $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
        // Conditionally launch drop index tagcolltype.
        if ($dbman->index_exists($table, $index)) {
            $dbman->drop_index($table, $index);
        }
        // Define index tagcolltype (not unique) to be added to tag.
        $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'isstandard'));
        // Conditionally launch add index tagcolltype.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Define field tagtype to be dropped from tag.
        $field = new xmldb_field('tagtype');
        // Conditionally launch drop field tagtype and update isstandard.
        if ($dbman->field_exists($table, $field)) {
            $DB->execute("UPDATE {tag} SET isstandard=(CASE WHEN (tagtype = ?) THEN 1 ELSE 0 END)", array('official'));
            $dbman->drop_field($table, $field);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016020200.0);
    }
    if ($oldversion < 2016020201.0) {
        // Define field showstandard to be added to tag_area.
        $table = new xmldb_table('tag_area');
        $field = new xmldb_field('showstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'callbackfile');
        // Conditionally launch add field showstandard.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // By default set user area to hide standard tags. 2 = core_tag_tag::HIDE_STANDARD (can not use constant here).
        $DB->execute("UPDATE {tag_area} SET showstandard = ? WHERE itemtype = ? AND component = ?", array(2, 'user', 'core'));
        // Changing precision of field enabled on table tag_area to (1).
        $table = new xmldb_table('tag_area');
        $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'itemtype');
        // Launch change of precision for field enabled.
        $dbman->change_field_precision($table, $field);
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016020201.0);
    }
    if ($oldversion < 2016021500.0) {
        $root = $CFG->tempdir . '/download';
        if (is_dir($root)) {
            // Fetch each repository type - include all repos, not just enabled.
            $repositories = $DB->get_records('repository', array(), '', 'type');
            foreach ($repositories as $id => $repository) {
                $directory = $root . '/repository_' . $repository->type;
                if (is_dir($directory)) {
                    fulldelete($directory);
                }
            }
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016021500.0);
    }
    if ($oldversion < 2016021501.0) {
        // This could take a long time. Unfortunately, no way to know how long, and no way to do progress, so setting for 1 hour.
        upgrade_set_timeout(3600);
        // Define index userid-itemid (not unique) to be added to grade_grades_history.
        $table = new xmldb_table('grade_grades_history');
        $index = new xmldb_index('userid-itemid-timemodified', XMLDB_INDEX_NOTUNIQUE, array('userid', 'itemid', 'timemodified'));
        // Conditionally launch add index userid-itemid.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Main savepoint reached.
        upgrade_main_savepoint(true, 2016021501.0);
    }
    return true;
}
Example #24
0
/**
 * Duplicate a module on the course.
 *
 * @param object $course The course
 * @param object $cm The course module to duplicate
 * @throws moodle_exception if the plugin doesn't support duplication
 * @return Object containing:
 * - fullcontent: The HTML markup for the created CM
 * - cmid: The CMID of the newly created CM
 * - redirect: Whether to trigger a redirect following this change
 */
function mod_duplicate_activity($course, $cm, $sr = null)
{
    global $CFG, $USER, $PAGE, $DB;
    require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
    require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
    require_once $CFG->libdir . '/filelib.php';
    $a = new stdClass();
    $a->modtype = get_string('modulename', $cm->modname);
    $a->modname = format_string($cm->name);
    if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
        throw new moodle_exception('duplicatenosupport', 'error');
    }
    // backup the activity
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
    $backupid = $bc->get_backupid();
    $backupbasepath = $bc->get_plan()->get_basepath();
    $bc->execute_plan();
    $bc->destroy();
    // restore the backup immediately
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
    $cmcontext = context_module::instance($cm->id);
    if (!$rc->execute_precheck()) {
        $precheckresults = $rc->get_precheck_results();
        if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
            if (empty($CFG->keeptempdirectoriesonbackup)) {
                fulldelete($backupbasepath);
            }
        }
    }
    $rc->execute_plan();
    // now a bit hacky part follows - we try to get the cmid of the newly
    // restored copy of the module
    $newcmid = null;
    $tasks = $rc->get_plan()->get_tasks();
    foreach ($tasks as $task) {
        error_log("Looking at a task");
        if (is_subclass_of($task, 'restore_activity_task')) {
            error_log("Looking at a restore_activity_task task");
            if ($task->get_old_contextid() == $cmcontext->id) {
                error_log("Contexts match");
                $newcmid = $task->get_moduleid();
                break;
            }
        }
    }
    // if we know the cmid of the new course module, let us move it
    // right below the original one. otherwise it will stay at the
    // end of the section
    if ($newcmid) {
        $info = get_fast_modinfo($course);
        $newcm = $info->get_cm($newcmid);
        $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
        moveto_module($newcm, $section, $cm);
        moveto_module($cm, $section, $newcm);
        // Trigger course module created event. We can trigger the event only if we know the newcmid.
        $event = \core\event\course_module_created::create_from_cm($newcm);
        $event->trigger();
    }
    rebuild_course_cache($cm->course);
    $rc->destroy();
    if (empty($CFG->keeptempdirectoriesonbackup)) {
        fulldelete($backupbasepath);
    }
    $resp = new stdClass();
    if ($newcm) {
        $courserenderer = $PAGE->get_renderer('core', 'course');
        $completioninfo = new completion_info($course);
        $modulehtml = $courserenderer->course_section_cm($course, $completioninfo, $newcm, null, array());
        $resp->fullcontent = $courserenderer->course_section_cm_list_item($course, $completioninfo, $newcm, $sr);
        $resp->cmid = $newcm->id;
    } else {
        // Trigger a redirect
        $resp->redirect = true;
    }
    return $resp;
}
Example #25
0
    /**
     * Imports a course
     *
     * @param int $importfrom The id of the course we are importing from
     * @param int $importto The id of the course we are importing to
     * @param bool $deletecontent Whether to delete the course we are importing to content
     * @param array $options List of backup options
     * @return null
     * @since Moodle 2.4
     */
    public static function import_course($importfrom, $importto, $deletecontent = 0, $options = array()) {
        global $CFG, $USER, $DB;
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');

        // Parameter validation.
        $params = self::validate_parameters(
            self::import_course_parameters(),
            array(
                'importfrom' => $importfrom,
                'importto' => $importto,
                'deletecontent' => $deletecontent,
                'options' => $options
            )
        );

        if ($params['deletecontent'] !== 0 and $params['deletecontent'] !== 1) {
            throw new moodle_exception('invalidextparam', 'webservice', '', $params['deletecontent']);
        }

        // Context validation.

        if (! ($importfrom = $DB->get_record('course', array('id'=>$params['importfrom'])))) {
            throw new moodle_exception('invalidcourseid', 'error');
        }

        if (! ($importto = $DB->get_record('course', array('id'=>$params['importto'])))) {
            throw new moodle_exception('invalidcourseid', 'error');
        }

        $importfromcontext = context_course::instance($importfrom->id);
        self::validate_context($importfromcontext);

        $importtocontext = context_course::instance($importto->id);
        self::validate_context($importtocontext);

        $backupdefaults = array(
            'activities' => 1,
            'blocks' => 1,
            'filters' => 1
        );

        $backupsettings = array();

        // Check for backup and restore options.
        if (!empty($params['options'])) {
            foreach ($params['options'] as $option) {

                // Strict check for a correct value (allways 1 or 0, true or false).
                $value = clean_param($option['value'], PARAM_INT);

                if ($value !== 0 and $value !== 1) {
                    throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
                }

                if (!isset($backupdefaults[$option['name']])) {
                    throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']);
                }

                $backupsettings[$option['name']] = $value;
            }
        }

        // Capability checking.

        require_capability('moodle/backup:backuptargetimport', $importfromcontext);
        require_capability('moodle/restore:restoretargetimport', $importtocontext);

        $bc = new backup_controller(backup::TYPE_1COURSE, $importfrom->id, backup::FORMAT_MOODLE,
                backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);

        foreach ($backupsettings as $name => $value) {
            $bc->get_plan()->get_setting($name)->set_value($value);
        }

        $backupid       = $bc->get_backupid();
        $backupbasepath = $bc->get_plan()->get_basepath();

        $bc->execute_plan();
        $bc->destroy();

        // Restore the backup immediately.

        // Check if we must delete the contents of the destination course.
        if ($params['deletecontent']) {
            $restoretarget = backup::TARGET_EXISTING_DELETING;
        } else {
            $restoretarget = backup::TARGET_EXISTING_ADDING;
        }

        $rc = new restore_controller($backupid, $importto->id,
                backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, $restoretarget);

        foreach ($backupsettings as $name => $value) {
            $rc->get_plan()->get_setting($name)->set_value($value);
        }

        if (!$rc->execute_precheck()) {
            $precheckresults = $rc->get_precheck_results();
            if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
                if (empty($CFG->keeptempdirectoriesonbackup)) {
                    fulldelete($backupbasepath);
                }

                $errorinfo = '';

                foreach ($precheckresults['errors'] as $error) {
                    $errorinfo .= $error;
                }

                if (array_key_exists('warnings', $precheckresults)) {
                    foreach ($precheckresults['warnings'] as $warning) {
                        $errorinfo .= $warning;
                    }
                }

                throw new moodle_exception('backupprecheckerrors', 'webservice', '', $errorinfo);
            }
        } else {
            if ($restoretarget == backup::TARGET_EXISTING_DELETING) {
                restore_dbops::delete_course_content($importto->id);
            }
        }

        $rc->execute_plan();
        $rc->destroy();

        if (empty($CFG->keeptempdirectoriesonbackup)) {
            fulldelete($backupbasepath);
        }

        return null;
    }
 function judge_local($sub)
 {
     global $CFG;
     // Make temp dir
     $temp_dir = $CFG->dataroot . '/temp/assignment_onlinejudge/' . $sub->id;
     if (!check_dir_exists($temp_dir, true, true)) {
         mtrace("Can't mkdir " . $temp_dir);
         return false;
     }
     if ($result = $this->compile($sub, $temp_dir)) {
         $result->grade = -1;
         if ($result->status === 'compileok' && !$this->onlinejudge->compileonly) {
             //Run and test!
             $results = array();
             $cases = $this->get_tests();
             foreach ($cases as $case) {
                 $results[] = $this->run_in_sandbox($temp_dir . '/a.out', $case);
             }
             $result = $this->merge_results($results, $cases);
         } else {
             if ($result->status === 'ce') {
                 $result->grade = $this->grade_marker('ce', $this->assignment->grade);
                 $result->output = '';
             }
         }
     }
     // Clean temp dir
     fulldelete($temp_dir);
     return $result;
 }
Example #27
0
/**
 * Recursively delete the file or folder with path $location. That is,
 * if it is a file delete it. If it is a folder, delete all its content
 * then delete it. If $location does not exist to start, that is not
 * considered an error.
 *
 * @param string $location the path to remove.
 * @return bool
 */
function fulldelete($location)
{
    if (empty($location)) {
        // extra safety against wrong param
        return false;
    }
    if (is_dir($location)) {
        if (!($currdir = opendir($location))) {
            return false;
        }
        while (false !== ($file = readdir($currdir))) {
            if ($file != ".." && $file != ".") {
                $fullfile = $location . "/" . $file;
                if (is_dir($fullfile)) {
                    if (!fulldelete($fullfile)) {
                        return false;
                    }
                } else {
                    if (!unlink($fullfile)) {
                        return false;
                    }
                }
            }
        }
        closedir($currdir);
        if (!rmdir($location)) {
            return false;
        }
    } else {
        if (file_exists($location)) {
            if (!unlink($location)) {
                return false;
            }
        }
    }
    return true;
}
Example #28
0
 /**
  * Cron cleanup job.
  */
 public function cron()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/cronlib.php';
     // find out all stale draft areas (older than 4 days) and purge them
     // those are identified by time stamp of the /. root dir
     mtrace('Deleting old draft files... ', '');
     cron_trace_time_and_memory();
     $old = time() - 60 * 60 * 24 * 4;
     $sql = "SELECT *\n                  FROM {files}\n                 WHERE component = 'user' AND filearea = 'draft' AND filepath = '/' AND filename = '.'\n                       AND timecreated < :old";
     $rs = $DB->get_recordset_sql($sql, array('old' => $old));
     foreach ($rs as $dir) {
         $this->delete_area_files($dir->contextid, $dir->component, $dir->filearea, $dir->itemid);
     }
     $rs->close();
     mtrace('done.');
     // remove orphaned preview files (that is files in the core preview filearea without
     // the existing original file)
     mtrace('Deleting orphaned preview files... ', '');
     cron_trace_time_and_memory();
     $sql = "SELECT p.*\n                  FROM {files} p\n             LEFT JOIN {files} o ON (p.filename = o.contenthash)\n                 WHERE p.contextid = ? AND p.component = 'core' AND p.filearea = 'preview' AND p.itemid = 0\n                       AND o.id IS NULL";
     $syscontext = context_system::instance();
     $rs = $DB->get_recordset_sql($sql, array($syscontext->id));
     foreach ($rs as $orphan) {
         $file = $this->get_file_instance($orphan);
         if (!$file->is_directory()) {
             $file->delete();
         }
     }
     $rs->close();
     mtrace('done.');
     // Remove orphaned converted files (that is files in the core documentconversion filearea without
     // the existing original file).
     mtrace('Deleting orphaned document conversion files... ', '');
     cron_trace_time_and_memory();
     $sql = "SELECT p.*\n                  FROM {files} p\n             LEFT JOIN {files} o ON (p.filename = o.contenthash)\n                 WHERE p.contextid = ? AND p.component = 'core' AND p.filearea = 'documentconversion' AND p.itemid = 0\n                       AND o.id IS NULL";
     $syscontext = context_system::instance();
     $rs = $DB->get_recordset_sql($sql, array($syscontext->id));
     foreach ($rs as $orphan) {
         $file = $this->get_file_instance($orphan);
         if (!$file->is_directory()) {
             $file->delete();
         }
     }
     $rs->close();
     mtrace('done.');
     // remove trash pool files once a day
     // if you want to disable purging of trash put $CFG->fileslastcleanup=time(); into config.php
     if (empty($CFG->fileslastcleanup) or $CFG->fileslastcleanup < time() - 60 * 60 * 24) {
         require_once $CFG->libdir . '/filelib.php';
         // Delete files that are associated with a context that no longer exists.
         mtrace('Cleaning up files from deleted contexts... ', '');
         cron_trace_time_and_memory();
         $sql = "SELECT DISTINCT f.contextid\n                    FROM {files} f\n                    LEFT OUTER JOIN {context} c ON f.contextid = c.id\n                    WHERE c.id IS NULL";
         $rs = $DB->get_recordset_sql($sql);
         if ($rs->valid()) {
             $fs = get_file_storage();
             foreach ($rs as $ctx) {
                 $fs->delete_area_files($ctx->contextid);
             }
         }
         $rs->close();
         mtrace('done.');
         mtrace('Deleting trash files... ', '');
         cron_trace_time_and_memory();
         fulldelete($this->trashdir);
         set_config('fileslastcleanup', time());
         mtrace('done.');
     }
 }
Example #29
0
 protected function define_execution()
 {
     global $CFG;
     // Get basepath
     $basepath = $this->get_basepath();
     require_once $CFG->dirroot . '/backup/cc/cc_includes.php';
     $tempdir = $CFG->dataroot . '/temp/backup/' . uniqid('', true);
     if (mkdir($tempdir, 0777, true)) {
         cc_convert_moodle2::convert($basepath, $tempdir);
         //Switch the directories
         if (empty($CFG->keeptempdirectoriesonbackup)) {
             fulldelete($basepath);
         } else {
             if (!rename($basepath, $basepath . '_moodle2_source')) {
                 throw new backup_task_exception('failed_rename_source_tempdir');
             }
         }
         if (!rename($tempdir, $basepath)) {
             throw new backup_task_exception('failed_move_converted_into_place');
         }
     }
 }
Example #30
0
 /**
  * Exports the translator database into disk files
  *
  * @param mixed $lang language code
  */
 public static function checkin($lang)
 {
     global $DB, $USER, $CFG;
     require_once $CFG->libdir . '/filelib.php';
     if ($lang !== clean_param($lang, PARAM_LANG)) {
         return false;
     }
     // get all customized strings from updated components
     $sql = "SELECT s.*, c.name AS component\n                  FROM {tool_customlang} s\n                  JOIN {tool_customlang_components} c ON s.componentid = c.id\n                 WHERE s.lang = ?\n                       AND (s.local IS NOT NULL OR s.modified = 1)\n              ORDER BY componentid, stringid";
     $strings = $DB->get_records_sql($sql, array($lang));
     $files = array();
     foreach ($strings as $string) {
         if (!is_null($string->local)) {
             $files[$string->component][$string->stringid] = $string->local;
         }
     }
     fulldelete(self::get_localpack_location($lang));
     foreach ($files as $component => $strings) {
         self::dump_strings($lang, $component, $strings);
     }
     $DB->set_field_select('tool_customlang', 'modified', 0, 'lang = ?', array($lang));
     $sm = get_string_manager();
     $sm->reset_caches();
 }