/**
  * Edit the assignment in Moodle then on Turnitin
  *
  * @global type $USER
  * @global type $DB
  * @param boolean $createevent - setting to determine whether to create a calendar event.
  * @return boolean
  */
 public function edit_moodle_assignment($createevent = true)
 {
     global $USER, $DB, $CFG;
     $config = turnitintooltwo_admin_config();
     $this->turnitintooltwo->id = $this->id;
     $this->turnitintooltwo->timemodified = time();
     // Get Moodle Course Object.
     $course = $this->get_course_data($this->turnitintooltwo->course);
     // Get the Turnitin owner of this this Course or make user the owner if none.
     $ownerid = $this->get_tii_owner($course->id);
     if (!empty($ownerid)) {
         $owner = new turnitintooltwo_user($ownerid, 'Instructor');
     } else {
         $owner = new turnitintooltwo_user($USER->id, 'Instructor');
     }
     // Edit course in Turnitin.
     $this->edit_tii_course($course);
     $course->turnitin_ctl = $course->fullname . " (Moodle TT)";
     // Get Current Moodle Turnitin Tool data (Assignment).
     if (!($turnitintooltwonow = $DB->get_record("turnitintooltwo", array("id" => $this->id)))) {
         turnitintooltwo_print_error('turnitintooltwogeterror', 'turnitintooltwo', null, null, __FILE__, __LINE__);
         exit;
     }
     // Get Current Moodle Turnitin Tool Parts Object.
     if (!($parts = $DB->get_records_select("turnitintooltwo_parts", " turnitintooltwoid = ? ", array($this->id), 'id ASC'))) {
         turnitintooltwo_print_error('partgeterror', 'turnitintooltwo', null, null, __FILE__, __LINE__);
         exit;
     }
     $partids = array_keys($parts);
     // Update GradeMark setting depending on config setting.
     $this->turnitintooltwo->usegrademark = $config->usegrademark;
     // Set the checkbox settings for updates.
     $this->turnitintooltwo->erater_spelling = isset($this->turnitintooltwo->erater_spelling) ? $this->turnitintooltwo->erater_spelling : 0;
     $this->turnitintooltwo->erater_grammar = isset($this->turnitintooltwo->erater_grammar) ? $this->turnitintooltwo->erater_grammar : 0;
     $this->turnitintooltwo->erater_usage = isset($this->turnitintooltwo->erater_usage) ? $this->turnitintooltwo->erater_usage : 0;
     $this->turnitintooltwo->erater_mechanics = isset($this->turnitintooltwo->erater_mechanics) ? $this->turnitintooltwo->erater_mechanics : 0;
     $this->turnitintooltwo->erater_style = isset($this->turnitintooltwo->erater_style) ? $this->turnitintooltwo->erater_style : 0;
     $this->turnitintooltwo->transmatch = isset($this->turnitintooltwo->transmatch) ? $this->turnitintooltwo->transmatch : 0;
     $this->turnitintooltwo->institution_check = isset($this->turnitintooltwo->institution_check) ? $this->turnitintooltwo->institution_check : 0;
     // Update each individual part.
     for ($i = 1; $i <= $this->turnitintooltwo->numparts; $i++) {
         // Update Turnitin Assignment.
         $assignment = new TiiAssignment();
         $assignment->setClassId($course->turnitin_cid);
         $assignment->setAuthorOriginalityAccess($this->turnitintooltwo->studentreports);
         $assignment->setRubricId(!empty($this->turnitintooltwo->rubric) ? $this->turnitintooltwo->rubric : '');
         $assignment->setSubmitPapersTo($this->turnitintooltwo->submitpapersto);
         $assignment->setResubmissionRule($this->turnitintooltwo->reportgenspeed);
         $assignment->setBibliographyExcluded($this->turnitintooltwo->excludebiblio);
         $assignment->setQuotedExcluded($this->turnitintooltwo->excludequoted);
         $assignment->setSmallMatchExclusionType($this->turnitintooltwo->excludetype);
         $assignment->setSmallMatchExclusionThreshold((int) $this->turnitintooltwo->excludevalue);
         $assignment->setLateSubmissionsAllowed($this->turnitintooltwo->allowlate);
         if ($config->repositoryoption == 1) {
             $assignment->setInstitutionCheck(isset($this->turnitintooltwo->institution_check) ? $this->turnitintooltwo->institution_check : 0);
         }
         $attribute = "maxmarks" . $i;
         $assignment->setMaxGrade($this->turnitintooltwo->{$attribute});
         $assignment->setSubmittedDocumentsCheck($this->turnitintooltwo->spapercheck);
         $assignment->setInternetCheck($this->turnitintooltwo->internetcheck);
         $assignment->setPublicationsCheck($this->turnitintooltwo->journalcheck);
         $assignment->setTranslatedMatching($this->turnitintooltwo->transmatch);
         $assignment->setAllowNonOrSubmissions($this->turnitintooltwo->allownonor);
         // Erater settings.
         $assignment->setErater(isset($this->turnitintooltwo->erater) ? $this->turnitintooltwo->erater : 0);
         $assignment->setEraterSpelling($this->turnitintooltwo->erater_spelling);
         $assignment->setEraterGrammar($this->turnitintooltwo->erater_grammar);
         $assignment->setEraterUsage($this->turnitintooltwo->erater_usage);
         $assignment->setEraterMechanics($this->turnitintooltwo->erater_mechanics);
         $assignment->setEraterStyle($this->turnitintooltwo->erater_style);
         $assignment->setEraterSpellingDictionary(isset($this->turnitintooltwo->erater_dictionary) ? $this->turnitintooltwo->erater_dictionary : 'en_US');
         $assignment->setEraterHandbook(isset($this->turnitintooltwo->erater_handbook) ? $this->turnitintooltwo->erater_handbook : 0);
         $attribute = "dtstart" . $i;
         $assignment->setStartDate(gmdate("Y-m-d\\TH:i:s\\Z", $this->turnitintooltwo->{$attribute}));
         $attribute = "dtdue" . $i;
         $assignment->setDueDate(gmdate("Y-m-d\\TH:i:s\\Z", $this->turnitintooltwo->{$attribute}));
         $attribute = "dtpost" . $i;
         $assignment->setFeedbackReleaseDate(gmdate("Y-m-d\\TH:i:s\\Z", $this->turnitintooltwo->{$attribute}));
         $attribute = "partname" . $i;
         $assignment->setTitle($this->turnitintooltwo->name . " " . $this->turnitintooltwo->{$attribute} . " (Moodle TT)");
         // Initialise part.
         $part = new stdClass();
         $part->turnitintooltwoid = $this->id;
         $part->partname = $this->turnitintooltwo->{$attribute};
         $part->deleted = 0;
         $part->maxmarks = $assignment->getMaxGrade();
         $part->dtstart = strtotime($assignment->getStartDate());
         $part->dtdue = strtotime($assignment->getDueDate());
         $part->dtpost = strtotime($assignment->getFeedbackReleaseDate());
         $parttiiassignid = 0;
         if ($i <= count($partids) && !empty($partids[$i - 1])) {
             $partdetails = $this->get_part_details($partids[$i - 1]);
             $part->submitted = $partdetails->submitted;
             $part->unanon = $partdetails->unanon;
             // Set anonymous marking depending on whether part has been unanonymised.
             if ($config->useanon && $partdetails->unanon != 1) {
                 $assignment->setAnonymousMarking($this->turnitintooltwo->anon);
             }
             $parttiiassignid = $partdetails->tiiassignid;
         }
         if ($parttiiassignid > 0) {
             $assignment->setAssignmentId($parttiiassignid);
             $this->edit_tii_assignment($assignment);
         } else {
             $parttiiassignid = $this->create_tii_assignment($assignment, $this->id, $i);
             $part->submitted = 0;
         }
         $part->tiiassignid = $parttiiassignid;
         // Unanonymise part if necessary.
         if ($part->dtpost < time() && $part->submitted == 1) {
             $part->unanon = 1;
         }
         $properties = new stdClass();
         $properties->name = $this->turnitintooltwo->name . ' - ' . $part->partname;
         $properties->description = $this->turnitintooltwo->intro;
         $properties->courseid = $this->turnitintooltwo->course;
         $properties->groupid = 0;
         $properties->userid = 0;
         $properties->modulename = 'turnitintooltwo';
         $properties->instance = $this->id;
         $properties->eventtype = 'due';
         $properties->timestart = $part->dtdue;
         $properties->timeduration = 0;
         if ($i <= count($partids) && !empty($partdetails->id)) {
             $part->id = $partids[$i - 1];
             // Get Current Moodle part data.
             if (!($partnow = $DB->get_record("turnitintooltwo_parts", array("id" => $part->id)))) {
                 turnitintooltwo_print_error('partgeterror', 'turnitintooltwo', null, null, __FILE__, __LINE__);
                 exit;
             }
             if (!($dbpart = $DB->update_record('turnitintooltwo_parts', $part))) {
                 turnitintooltwo_print_error('partupdateerror', 'turnitintooltwo', null, $i, __FILE__, __LINE__);
                 exit;
             } else {
                 turnitintooltwo_activitylog("Moodle Assignment part updated (" . $part->id . ") - " . $part->partname, "REQUEST");
             }
             // Delete existing events for this assignment part.
             $eventname = $turnitintooltwonow->name . " - " . $partnow->partname;
             $dbselect = " modulename = ? AND instance = ? AND name LIKE ? ";
             // Moodle pre 2.5 on SQL Server errors here as queries weren't allowed on ntext fields, the relevant fields
             // are nvarchar from 2.6 onwards so we have to cast the relevant fields in pre 2.5 SQL Server setups.
             if ($CFG->branch <= 25 && $CFG->dbtype == "sqlsrv") {
                 $dbselect = " CAST(modulename AS nvarchar(max)) = ? AND instance = ? AND CAST(name AS nvarchar(max)) = ? ";
             }
             $DB->delete_records_select('event', $dbselect, array('turnitintooltwo', $this->id, $eventname));
         } else {
             if (!($dbpart = $DB->insert_record('turnitintooltwo_parts', $part))) {
                 turnitintooltwo_print_error('partdberror', 'turnitintooltwo', null, $i, __FILE__, __LINE__);
                 exit;
             }
         }
         if ($createevent == true) {
             require_once $CFG->dirroot . '/calendar/lib.php';
             $event = new calendar_event($properties);
             $event->update($properties, false);
         }
     }
     $this->turnitintooltwo->timemodified = time();
     $update = $DB->update_record("turnitintooltwo", $this->turnitintooltwo);
     turnitintooltwo_activitylog("Turnitintool updated (" . $this->id . ") - " . $this->turnitintooltwo->name, "REQUEST");
     // Define grade settings.
     if (!isset($_SESSION['tii_assignment_reset'])) {
         turnitintooltwo_grade_item_update($this->turnitintooltwo);
     }
     return $update;
 }