Exemplo n.º 1
0
 public function test_grade_update_mod_grades()
 {
     $this->resetAfterTest(true);
     // Create a course and instance of mod_assignment.
     $course = $this->getDataGenerator()->create_course();
     $assigndata['course'] = $course->id;
     $assigndata['name'] = 'lightwork assignment';
     $modinstance = self::getDataGenerator()->create_module('assignment', $assigndata);
     // grade_update_mod_grades() requires 2 additional properties, cmidnumber and modname.
     $cm = get_coursemodule_from_instance('assignment', $modinstance->id, 0, false, MUST_EXIST);
     $modinstance->cmidnumber = $cm->id;
     $modinstance->modname = 'assignment';
     $this->assertTrue(grade_update_mod_grades($modinstance));
 }
Exemplo n.º 2
0
 public function test_grade_update_mod_grades()
 {
     $this->resetAfterTest(true);
     // Create a broken module instance.
     $modinstance = new stdClass();
     $modinstance->modname = 'doesntexist';
     $this->assertFalse(grade_update_mod_grades($modinstance));
     // A debug message should have been generated.
     $this->assertDebuggingCalled();
     // Create a course and instance of mod_assign.
     $course = $this->getDataGenerator()->create_course();
     $assigndata['course'] = $course->id;
     $assigndata['name'] = 'lightwork assignment';
     $modinstance = self::getDataGenerator()->create_module('assign', $assigndata);
     // Function grade_update_mod_grades() requires 2 additional properties, cmidnumber and modname.
     $cm = get_coursemodule_from_instance('assign', $modinstance->id, 0, false, MUST_EXIST);
     $modinstance->cmidnumber = $cm->id;
     $modinstance->modname = 'assign';
     $this->assertTrue(grade_update_mod_grades($modinstance));
 }
Exemplo n.º 3
0
             } else {
                 $module->name = clean_param($title, PARAM_CLEANHTML);
             }
             if (!empty($module->name)) {
                 $DB->update_record($cm->modname, $module);
                 $cm->name = $module->name;
                 \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
                 rebuild_course_cache($cm->course);
             } else {
                 $module->name = $cm->name;
             }
             // Attempt to update the grade item if relevant
             $grademodule = $DB->get_record($cm->modname, array('id' => $cm->instance));
             $grademodule->cmidnumber = $cm->idnumber;
             $grademodule->modname = $cm->modname;
             grade_update_mod_grades($grademodule);
             // We need to return strings after they've been through filters for multilang
             $stringoptions = new stdClass();
             $stringoptions->context = $coursecontext;
             echo json_encode(array('instancename' => html_entity_decode(format_string($module->name, true, $stringoptions))));
             break;
     }
     break;
 case 'course':
     switch ($field) {
         case 'marker':
             require_capability('moodle/course:setcurrentsection', $coursecontext);
             course_set_marker($course->id, $value);
             break;
     }
     break;
Exemplo n.º 4
0
/**
 * For backwards compatibility with old third-party modules, this function can
 * be used to import all grades from activities with legacy grading.
 * @param int $courseid or null if all courses
 */
function grade_grab_legacy_grades($courseid = null)
{
    global $CFG;
    if (!($mods = get_list_of_plugins('mod'))) {
        error('No modules installed!');
    }
    if ($courseid) {
        $course_sql = " AND cm.course={$courseid}";
    } else {
        $course_sql = "";
    }
    foreach ($mods as $mod) {
        if ($mod == 'NEWMODULE') {
            // Someone has unzipped the template, ignore it
            continue;
        }
        if (!($module = get_record('modules', 'name', $mod))) {
            //not installed
            continue;
        }
        if (!$module->visible) {
            //disabled module
            continue;
        }
        $fullmod = $CFG->dirroot . '/mod/' . $mod;
        // include the module lib once
        if (file_exists($fullmod . '/lib.php')) {
            include_once $fullmod . '/lib.php';
            // look for modname_grades() function - old gradebook pulling function
            // if present sync the grades with new grading system
            $gradefunc = $mod . '_grades';
            if (function_exists($gradefunc)) {
                // get all instance of the activity
                $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname\n                          FROM {$CFG->prefix}{$mod} a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n                         WHERE m.name='{$mod}' AND m.id=cm.module AND cm.instance=a.id {$course_sql}";
                if ($modinstances = get_records_sql($sql)) {
                    foreach ($modinstances as $modinstance) {
                        grade_update_mod_grades($modinstance);
                    }
                }
            }
        }
    }
}
Exemplo n.º 5
0
/**
 * Refetches grade data from course activities
 *
 * @param int $courseid The course ID
 * @param string $modname Limit the grade fetch to a single module type. For example 'forum'
 * @param int $userid limit the grade fetch to a single user
 */
function grade_grab_course_grades($courseid, $modname = null, $userid = 0)
{
    global $CFG, $DB;
    if ($modname) {
        $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname\n                  FROM {" . $modname . "} a, {course_modules} cm, {modules} m\n                 WHERE m.name=:modname AND m.visible=1 AND m.id=cm.module AND cm.instance=a.id AND cm.course=:courseid";
        $params = array('modname' => $modname, 'courseid' => $courseid);
        if ($modinstances = $DB->get_records_sql($sql, $params)) {
            foreach ($modinstances as $modinstance) {
                grade_update_mod_grades($modinstance, $userid);
            }
        }
        return;
    }
    if (!($mods = get_plugin_list('mod'))) {
        print_error('nomodules', 'debug');
    }
    foreach ($mods as $mod => $fullmod) {
        if ($mod == 'NEWMODULE') {
            // Someone has unzipped the template, ignore it
            continue;
        }
        // include the module lib once
        if (file_exists($fullmod . '/lib.php')) {
            // get all instance of the activity
            $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname\n                      FROM {" . $mod . "} a, {course_modules} cm, {modules} m\n                     WHERE m.name=:mod AND m.visible=1 AND m.id=cm.module AND cm.instance=a.id AND cm.course=:courseid";
            $params = array('mod' => $mod, 'courseid' => $courseid);
            if ($modinstances = $DB->get_records_sql($sql, $params)) {
                foreach ($modinstances as $modinstance) {
                    grade_update_mod_grades($modinstance, $userid);
                }
            }
        }
    }
}
Exemplo n.º 6
0
/**
 * Changes the course module name
 *
 * @param int $id course module id
 * @param string $name new value for a name
 * @return bool whether a change was made
 */
function set_coursemodule_name($id, $name)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/gradelib.php';
    $cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST);
    $module = new \stdClass();
    $module->id = $cm->instance;
    // Escape strings as they would be by mform.
    if (!empty($CFG->formatstringstriptags)) {
        $module->name = clean_param($name, PARAM_TEXT);
    } else {
        $module->name = clean_param($name, PARAM_CLEANHTML);
    }
    if ($module->name === $cm->name || strval($module->name) === '') {
        return false;
    }
    if (\core_text::strlen($module->name) > 255) {
        throw new \moodle_exception('maximumchars', 'moodle', '', 255);
    }
    $module->timemodified = time();
    $DB->update_record($cm->modname, $module);
    $cm->name = $module->name;
    \core\event\course_module_updated::create_from_cm($cm)->trigger();
    rebuild_course_cache($cm->course, true);
    // Attempt to update the grade item if relevant.
    $grademodule = $DB->get_record($cm->modname, array('id' => $cm->instance));
    $grademodule->cmidnumber = $cm->idnumber;
    $grademodule->modname = $cm->modname;
    grade_update_mod_grades($grademodule);
    return true;
}
Exemplo n.º 7
0
 /**
  * Refetch grades from modules, plugins.
  * @param int $userid optional, one user only
  */
 function refresh_grades($userid = 0)
 {
     if ($this->itemtype == 'mod') {
         if ($this->is_outcome_item()) {
             //nothing to do
             return;
         }
         if (!($activity = get_record($this->itemmodule, 'id', $this->iteminstance))) {
             debugging("Can not find {$this->itemmodule} activity with id {$this->iteminstance}");
             return;
         }
         if (!($cm = get_coursemodule_from_instance($this->itemmodule, $activity->id, $this->courseid))) {
             debugging('Can not find course module');
             return;
         }
         $activity->modname = $this->itemmodule;
         $activity->cmidnumber = $cm->idnumber;
         grade_update_mod_grades($activity);
     }
 }
Exemplo n.º 8
0
 /**
  * Refetch grades from modules, plugins.
  *
  * @param int $userid optional, limit the refetch to a single user
  * @return bool Returns true on success or if there is nothing to do
  */
 public function refresh_grades($userid = 0)
 {
     global $DB;
     if ($this->itemtype == 'mod') {
         if ($this->is_outcome_item()) {
             //nothing to do
             return true;
         }
         if (!($activity = $DB->get_record($this->itemmodule, array('id' => $this->iteminstance)))) {
             debugging("Can not find {$this->itemmodule} activity with id {$this->iteminstance}");
             return false;
         }
         if (!($cm = get_coursemodule_from_instance($this->itemmodule, $activity->id, $this->courseid))) {
             debugging('Can not find course module');
             return false;
         }
         $activity->modname = $this->itemmodule;
         $activity->cmidnumber = $cm->idnumber;
         return grade_update_mod_grades($activity, $userid);
     }
     return true;
 }
function mod_stopwatch_update_grades($cm, $stopwatch, $durationarray, $gradearray)
{
    global $DB, $CFG;
    require_once $CFG->libdir . '/gradelib.php';
    $currentgrades = mod_stopwatch_get_all_users($cm, $stopwatch);
    $newgrades = array();
    foreach ($currentgrades as $userid => $record) {
        if ($record->id) {
            $params = array('id' => $record->id);
        } else {
            $params = array('userid' => $userid, 'courseid' => $cm->course, 'stopwatchid' => $stopwatch->id);
        }
        $now = time();
        $updateobj = array();
        if (array_key_exists($userid, $durationarray)) {
            if (empty($durationarray[$userid])) {
                if (!empty($record->duration)) {
                    $updateobj['duration'] = 0;
                }
            } else {
                $duration = mod_stopwatch_string_to_duration($durationarray[$userid]);
                if ($record->duration != $duration) {
                    $updateobj['duration'] = $duration;
                }
            }
        }
        if ($stopwatch->grade && array_key_exists($userid, $gradearray)) {
            $grade = empty($gradearray[$userid]) ? null : (double) $gradearray[$userid];
            $existinggrade = !strlen($record->grade) ? null : (double) $record->grade;
            if ($existinggrade !== $grade) {
                $updateobj['grade'] = $grade;
                $updateobj['timegraded'] = $now;
                $newgrades[$userid] = array('userid' => $userid, 'rawgrade' => $grade);
            }
        }
        if (empty($updateobj)) {
            continue;
        }
        $updateobj += $params;
        $updateobj['timemodified'] = $now;
        if (!empty($updateobj['id'])) {
            $DB->update_record('stopwatch_user', $updateobj);
        } else {
            $updateobj['timecreated'] = $now;
            $DB->insert_record('stopwatch_user', $updateobj);
        }
    }
    if ($newgrades) {
        // Attempt to update the grade item if relevant
        $grademodule = fullclone($stopwatch);
        $grademodule->cmidnumber = $cm->idnumber;
        $grademodule->modname = $cm->modname;
        grade_update_mod_grades($grademodule);
    }
}
Exemplo n.º 10
0
/**
 * Refetches data from all course activities
 * @param int $courseid
 * @param string $modname
 * @return success
 */
function grade_grab_course_grades($courseid, $modname = null)
{
    global $CFG;
    if ($modname) {
        $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname\n                  FROM {$CFG->prefix}{$modname} a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n                 WHERE m.name='{$modname}' AND m.visible=1 AND m.id=cm.module AND cm.instance=a.id AND cm.course={$courseid}";
        if ($modinstances = get_records_sql($sql)) {
            foreach ($modinstances as $modinstance) {
                grade_update_mod_grades($modinstance);
            }
        }
        return;
    }
    if (!($mods = get_list_of_plugins('mod'))) {
        error('No modules installed!');
    }
    foreach ($mods as $mod) {
        if ($mod == 'NEWMODULE') {
            // Someone has unzipped the template, ignore it
            continue;
        }
        $fullmod = $CFG->dirroot . '/mod/' . $mod;
        // include the module lib once
        if (file_exists($fullmod . '/lib.php')) {
            // get all instance of the activity
            $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname\n                      FROM {$CFG->prefix}{$mod} a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n                     WHERE m.name='{$mod}' AND m.visible=1 AND m.id=cm.module AND cm.instance=a.id AND cm.course={$courseid}";
            if ($modinstances = get_records_sql($sql)) {
                foreach ($modinstances as $modinstance) {
                    grade_update_mod_grades($modinstance);
                }
            }
        }
    }
}
Exemplo n.º 11
0
function restore_execute(&$restore, $info, $course_header, &$errorstr)
{
    global $CFG, $USER;
    $status = true;
    //Checks for the required files/functions to restore every module
    //and include them
    if ($allmods = get_records("modules")) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = "{$CFG->dirroot}/mod/{$modname}/restorelib.php";
            //If file exists and we have selected to restore that type of module
            if (file_exists($modfile) and !empty($restore->mods[$modname]) and $restore->mods[$modname]->restore) {
                include_once $modfile;
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //Start the main table
        echo "<table cellpadding=\"5\">";
        echo "<tr><td>";
        //Start the main ul
        echo "<ul>";
    }
    //Localtion of the xml file
    $xml_file = $CFG->dataroot . "/temp/backup/" . $restore->backup_unique_code . "/moodle.xml";
    //If we've selected to restore into new course
    //create it (course)
    //Saving conversion id variables into backup_tables
    if ($restore->restoreto == 2) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('creatingnewcourse') . '</li>';
        }
        $oldidnumber = $course_header->course_idnumber;
        if (!($status = restore_create_new_course($restore, $course_header))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Error while creating the new empty course.");
            } else {
                $errorstr = "Error while creating the new empty course.";
                return false;
            }
        }
        //Print course fullname and shortname and category
        if ($status) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . $course_header->course_fullname . " (" . $course_header->course_shortname . ")" . '</li>';
                echo "<li>" . get_string("category") . ": " . $course_header->category->name . '</li>';
                if (!empty($oldidnumber)) {
                    echo "<li>" . get_string("nomoreidnumber", "moodle", $oldidnumber) . "</li>";
                }
                echo "</ul>";
                //Put the destination course_id
            }
            $restore->course_id = $course_header->course_id;
        }
        if ($status = restore_open_html($restore, $course_header)) {
            echo "<li>Creating the Restorelog.html in the course backup folder</li>";
        }
    } else {
        $course = get_record("course", "id", $restore->course_id);
        if ($course) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("usingexistingcourse");
                echo "<ul>";
                echo "<li>" . get_string("from") . ": " . $course_header->course_fullname . " (" . $course_header->course_shortname . ")" . '</li>';
                echo "<li>" . get_string("to") . ": " . format_string($course->fullname) . " (" . format_string($course->shortname) . ")" . '</li>';
                if ($restore->deleting) {
                    echo "<li>" . get_string("deletingexistingcoursedata") . '</li>';
                } else {
                    echo "<li>" . get_string("addingdatatoexisting") . '</li>';
                }
                echo "</ul></li>";
            }
            //If we have selected to restore deleting, we do it now.
            if ($restore->deleting) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string("deletingolddata") . '</li>';
                }
                $status = remove_course_contents($restore->course_id, false) and delete_dir_contents($CFG->dataroot . "/" . $restore->course_id, "backupdata");
                if ($status) {
                    //Now , this situation is equivalent to the "restore to new course" one (we
                    //have a course record and nothing more), so define it as "to new course"
                    $restore->restoreto = 2;
                } else {
                    if (!defined('RESTORE_SILENTLY')) {
                        notify("An error occurred while deleting some of the course contents.");
                    } else {
                        $errrostr = "An error occurred while deleting some of the course contents.";
                        return false;
                    }
                }
            }
        } else {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Error opening existing course.");
                $status = false;
            } else {
                $errorstr = "Error opening existing course.";
                return false;
            }
        }
    }
    //Now create users as needed
    if ($status and ($restore->users == 0 or $restore->users == 1)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingusers") . "<br />";
        }
        if (!($status = restore_create_users($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore users.");
            } else {
                $errorstr = "Could not restore users.";
                return false;
            }
        }
        //Now print info about the work done
        if ($status) {
            $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids\n                                     where backup_code = '{$restore->backup_unique_code}' and\n                                     table_name = 'user'");
            //We've records
            if ($recs) {
                $new_count = 0;
                $exists_count = 0;
                $student_count = 0;
                $teacher_count = 0;
                $counter = 0;
                //Iterate, filling counters
                foreach ($recs as $rec) {
                    //Get full record, using backup_getids
                    $record = backup_getid($restore->backup_unique_code, "user", $rec->old_id);
                    if (strpos($record->info, "new") !== false) {
                        $new_count++;
                    }
                    if (strpos($record->info, "exists") !== false) {
                        $exists_count++;
                    }
                    if (strpos($record->info, "student") !== false) {
                        $student_count++;
                    } else {
                        if (strpos($record->info, "teacher") !== false) {
                            $teacher_count++;
                        }
                    }
                    //Do some output
                    $counter++;
                    if ($counter % 10 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 200 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
                if (!defined('RESTORE_SILENTLY')) {
                    //Now print information gathered
                    echo " (" . get_string("new") . ": " . $new_count . ", " . get_string("existing") . ": " . $exists_count . ")";
                    echo "<ul>";
                    echo "<li>" . get_string("students") . ": " . $student_count . '</li>';
                    echo "<li>" . get_string("teachers") . ": " . $teacher_count . '</li>';
                    echo "</ul>";
                }
            } else {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("No users were found!");
                }
                // no need to return false here, it's recoverable.
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create groups as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroups");
        }
        if (!($status = restore_create_groups($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groups!");
            } else {
                $errorstr = "Could not restore groups!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create groupings as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroupings");
        }
        if (!($status = restore_create_groupings($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groupings!");
            } else {
                $errorstr = "Could not restore groupings!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create groupingsgroups as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroupingsgroups");
        }
        if (!($status = restore_create_groupings_groups($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groups in groupings!");
            } else {
                $errorstr = "Could not restore groups in groupings!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create the course_sections and their associated course_modules
    //we have to do this after groups and groupings are restored, because we need the new groupings id
    if ($status) {
        //Into new course
        if ($restore->restoreto == 2) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("creatingsections");
            }
            if (!($status = restore_create_sections($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Error creating sections in the existing course.");
                } else {
                    $errorstr = "Error creating sections in the existing course.";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
            //Into existing course
        } else {
            if ($restore->restoreto == 0 or $restore->restoreto == 1) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string("checkingsections");
                }
                if (!($status = restore_create_sections($restore, $xml_file))) {
                    if (!defined('RESTORE_SILENTLY')) {
                        notify("Error creating sections in the existing course.");
                    } else {
                        $errorstr = "Error creating sections in the existing course.";
                        return false;
                    }
                }
                if (!defined('RESTORE_SILENTLY')) {
                    echo '</li>';
                }
                //Error
            } else {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Neither a new course or an existing one was specified.");
                    $status = false;
                } else {
                    $errorstr = "Neither a new course or an existing one was specified.";
                    return false;
                }
            }
        }
    }
    //Now create metacourse info
    if ($status and $restore->metacourse) {
        //Only to new courses!
        if ($restore->restoreto == 2) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("creatingmetacoursedata");
            }
            if (!($status = restore_create_metacourse($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Error creating metacourse in the course.");
                } else {
                    $errorstr = "Error creating metacourse in the course.";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    //Now create categories and questions as needed
    if ($status) {
        include_once "{$CFG->dirroot}/question/restorelib.php";
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingcategoriesandquestions");
            echo "<ul>";
        }
        if (!($status = restore_create_questions($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore categories and questions!");
            } else {
                $errorstr = "Could not restore categories and questions!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</ul></li>";
        }
    }
    //Now create user_files as needed
    if ($status and $restore->user_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("copyinguserfiles");
        }
        if (!($status = restore_user_files($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore user files!");
            } else {
                $errorstr = "Could not restore user files!";
                return false;
            }
        }
        //If all is ok (and we have a counter)
        if ($status and $status !== true) {
            //Inform about user dirs created from backup
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . get_string("userzones") . ": " . $status;
                echo "</li></ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create course files as needed
    if ($status and $restore->course_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("copyingcoursefiles");
        }
        if (!($status = restore_course_files($restore))) {
            if (empty($status)) {
                notify("Could not restore course files!");
            } else {
                $errorstr = "Could not restore course files!";
                return false;
            }
        }
        //If all is ok (and we have a counter)
        if ($status and $status !== true) {
            //Inform about user dirs created from backup
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . get_string("filesfolders") . ": " . $status . '</li>';
                echo "</ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create site files as needed
    if ($status and $restore->site_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string('copyingsitefiles');
        }
        if (!($status = restore_site_files($restore))) {
            if (empty($status)) {
                notify("Could not restore site files!");
            } else {
                $errorstr = "Could not restore site files!";
                return false;
            }
        }
        //If all is ok (and we have a counter)
        if ($status and $status !== true) {
            //Inform about user dirs created from backup
            if (!defined('RESTORE_SILENTLY')) {
                echo "<ul>";
                echo "<li>" . get_string("filesfolders") . ": " . $status . '</li>';
                echo "</ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create messages as needed
    if ($status and $restore->messages) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingmessagesinfo");
        }
        if (!($status = restore_create_messages($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore messages!");
            } else {
                $errorstr = "Could not restore messages!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create scales as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingscales");
        }
        if (!($status = restore_create_scales($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore custom scales!");
            } else {
                $errorstr = "Could not restore custom scales!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create events as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingevents");
        }
        if (!($status = restore_create_events($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore course events!");
            } else {
                $errorstr = "Could not restore course events!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create course modules as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingcoursemodules");
        }
        if (!($status = restore_create_modules($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore modules!");
            } else {
                $errorstr = "Could not restore modules!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create gradebook as needed -- AFTER modules!!!
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggradebook");
        }
        if (!($status = restore_create_gradebook($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore gradebook!");
            } else {
                $errorstr = "Could not restore gradebook!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Bring back the course blocks -- do it AFTER the modules!!!
    if ($status) {
        //If we are deleting and bringing into a course or making a new course, same situation
        if ($restore->restoreto == 0 || $restore->restoreto == 2) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('creatingblocks');
            }
            $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
            if (!($status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify('Error while creating the course blocks');
                } else {
                    $errorstr = "Error while creating the course blocks";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    if ($status) {
        //If we are deleting and bringing into a course or making a new course, same situation
        if ($restore->restoreto == 0 || $restore->restoreto == 2) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('courseformatdata');
            }
            if (!($status = restore_set_format_data($restore, $xml_file))) {
                $error = "Error while setting the course format data";
                if (!defined('RESTORE_SILENTLY')) {
                    notify($error);
                } else {
                    $errorstr = $error;
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    //Now create log entries as needed
    if ($status and $restore->logs) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinglogentries");
        }
        if (!($status = restore_create_logs($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore logs!");
            } else {
                $errorstr = "Could not restore logs!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, if all is OK, adjust the instance field in course_modules !!
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("checkinginstances");
        }
        if (!($status = restore_check_instances($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not adjust instances in course_modules!");
            } else {
                $errorstr = "Could not adjust instances in course_modules!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, if all is OK, adjust activity events
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("refreshingevents");
        }
        if (!($status = restore_refresh_events($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not refresh events for activities!");
            } else {
                $errorstr = "Could not refresh events for activities!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, if all is OK, adjust inter-activity links
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("decodinginternallinks");
        }
        if (!($status = restore_decode_content_links($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not decode content links!");
            } else {
                $errorstr = "Could not decode content links!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, with backup files prior to version 2005041100,
    //convert all the wiki texts in the course to markdown
    if ($status && $restore->backup_version < 2005041100) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("convertingwikitomarkdown");
        }
        if (!($status = restore_convert_wiki2markdown($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not convert wiki texts to markdown!");
            } else {
                $errorstr = "Could not convert wiki texts to markdown!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    // for moodle versions before 1.9, those grades need to be converted to use the new gradebook
    // this code needs to execute *after* the course_modules are sorted out
    if ($status && $restore->backup_version < 2007090500) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("migratinggrades");
        }
        // we need need to worry about mods that are restored
        // the others in the course are not relevent
        if (!empty($restore->mods)) {
            require_once $CFG->dirroot . '/lib/gradelib.php';
            foreach ($restore->mods as $mod => $modtype) {
                if (!empty($modtype->instances)) {
                    foreach ($modtype->instances as $modinstance) {
                        $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname\n                                    FROM {$CFG->prefix}{$mod} a,  \n                                         {$CFG->prefix}course_modules cm, \n                                         {$CFG->prefix}modules m\n                                    WHERE m.name='{$mod}' \n                                        AND m.id=cm.module \n                                        AND cm.instance=a.id \n                                        AND cm.id= {$modinstance->restored_as_course_module}";
                        if ($module = get_record_sql($sql)) {
                            grade_update_mod_grades($module);
                        }
                    }
                }
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    /*******************************************************************************
     ************* Restore of Roles and Capabilities happens here ******************
     *******************************************************************************/
    // try to restore roles even when restore is going to fail - teachers might have
    // at least some role assigned - this is not correct though
    $status = restore_create_roles($restore, $xml_file) && $status;
    $status = restore_roles_settings($restore, $xml_file) && $status;
    //Now if all is OK, update:
    //   - course modinfo field
    //   - categories table
    //   - add user as teacher
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("checkingcourse");
        }
        //modinfo field
        rebuild_course_cache($restore->course_id);
        //categories table
        $course = get_record("course", "id", $restore->course_id);
        fix_course_sortorder();
        // Check if the user has course update capability in the newly restored course
        // there is no need to load his capabilities again, because restore_roles_settings
        // would have loaded it anyway, if there is any assignments.
        // fix for MDL-6831
        $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
        if (!has_capability('moodle/course:manageactivities', $newcontext)) {
            // fix for MDL-9065, use the new config setting if exists
            if ($CFG->creatornewroleid) {
                role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
            } else {
                if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID))) {
                    if ($legacyteacher = array_shift($legacyteachers)) {
                        role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
                    }
                } else {
                    notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
                }
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Cleanup temps (files and db)
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("cleaningtempdata");
        }
        if (!($status = clean_temp_data($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not clean up temporary data from files and database");
            } else {
                $errorstr = "Could not clean up temporary data from files and database";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    // this is not a critical check - the result can be ignored
    if (restore_close_html($restore)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>Closing the Restorelog.html file.</li>';
        }
    } else {
        if (!defined('RESTORE_SILENTLY')) {
            notify("Could not close the restorelog.html file");
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //End the main ul
        echo "</ul>";
        //End the main table
        echo "</td></tr>";
        echo "</table>";
    }
    return $status;
}
 /**
  * Update all of the target user's grades.
  * @param int $toid User id
  */
 private function updateGrades($toid, $fromid)
 {
     global $DB, $CFG;
     require_once $CFG->libdir . '/gradelib.php';
     $sql = "SELECT iteminstance, itemmodule, courseid\n                FROM {grade_grades} gg\n                INNER JOIN {grade_items} gi on gg.itemid = gi.id\n                WHERE itemtype = 'mod' AND (gg.userid = :toid OR gg.userid = :fromid)";
     $iteminstances = $DB->get_records_sql($sql, array('toid' => $toid, 'fromid' => $fromid));
     foreach ($iteminstances as $iteminstance) {
         if (!($activity = $DB->get_record($iteminstance->itemmodule, array('id' => $iteminstance->iteminstance)))) {
             throw new \Exception("Can not find {$iteminstance->itemmodule} activity with id {$iteminstance->iteminstance}");
         }
         if (!($cm = get_coursemodule_from_instance($iteminstance->itemmodule, $activity->id, $iteminstance->courseid))) {
             throw new \Exception('Can not find course module');
         }
         $activity->modname = $iteminstance->itemmodule;
         $activity->cmidnumber = $cm->idnumber;
         grade_update_mod_grades($activity, $toid);
     }
 }