/**
  * If no user data was restored after everything has been restored then
  * create a new course in Turnitin
  */
 public function after_restore()
 {
     global $DB, $CFG;
     if (!empty($_SESSION['tii_course_reset'])) {
         $course = turnitintooltwo_assignment::get_course_data($_SESSION['course_id']);
         //Get the main site admin.
         $admins = explode(",", $CFG->siteadmins);
         $ownerid = $admins[0];
         // Get the number of assignments that already exist on this course that aren't part of recreation.
         $assignments = 0;
         if (!empty($_SESSION['assignments_to_create'])) {
             $modules = $_SESSION['assignments_to_create'];
             list($notinsql, $notinparams) = $DB->get_in_or_equal($modules, SQL_PARAMS_QM, 'param', false);
             $assignments = $DB->count_records_select('turnitintooltwo', " course = ? AND id " . $notinsql, array_merge(array($_SESSION['course_id']), $notinparams));
         }
         // Only recreate course on Turnitin if Turnitin Assignments don't exist on destination course.
         if ($assignments == 0) {
             // Remove Turnitin link from course
             $turnitin_course = new stdClass();
             $turnitin_course->id = $course->tii_rel_id;
             $turnitin_course->turnitin_cid = 0;
             $DB->update_record('turnitintooltwo_courses', $turnitin_course);
             // Recreate course in Turnitin
             $course->turnitin_cid = 0;
             $tmpassignment = new turnitintooltwo_assignment(0, '', '');
             $turnitin_course = $tmpassignment->create_tii_course($course, $ownerid);
             // Join the course as Instructor
             $owner = new turnitintooltwo_user($ownerid, 'Instructor');
             $owner->join_user_to_class($turnitin_course->turnitin_cid);
         }
         unset($_SESSION['tii_course_reset']);
         unset($_SESSION['course_id']);
     }
     if (!empty($_SESSION['assignments_to_create'])) {
         foreach ($_SESSION["assignments_to_create"] as $new_assignment_id) {
             $assignment = new turnitintooltwo_assignment($new_assignment_id);
             $assignment->unlink_assignment();
             $assignment->edit_moodle_assignment(true, true);
         }
         unset($_SESSION['tii_assignment_reset']);
         unset($_SESSION['assignments_to_create']);
     }
 }