/**
  * Deletes the usertype specified by ID.
  * @return bool Was it successful?
  */
 protected function body()
 {
     if (!$this->userHasPrivileges(User::usersPrivPresets)) {
         return false;
     }
     if (!$this->isInputSet('id')) {
         return false;
     }
     $id = $this->getParams('id');
     if ($id == Repositories::StudentUserType) {
         return $this->death(StringID::CannotRemoveBasicStudentType);
     }
     /**
      * @var $deletedType \UserType
      */
     $deletedType = Repositories::findEntity(Repositories::UserType, $id);
     $users = Repositories::getRepository(Repositories::User)->findBy(['type' => $id]);
     /** @var \UserType $studentType */
     $studentType = Repositories::findEntity(Repositories::UserType, Repositories::StudentUserType);
     foreach ($users as $user) {
         /** @var $user \User */
         $user->setType($studentType);
         Repositories::persist($user);
     }
     Repositories::remove($deletedType);
     Repositories::flushAll();
     return true;
 }
 protected function body()
 {
     $plugins = Repositories::getRepository(Repositories::Plugin)->findAll();
     $errors = [];
     foreach ($plugins as $plugin) {
         /** @var $plugin \Plugin */
         $dbPhpFile = $plugin->getMainfile();
         $dbDescription = $plugin->getDescription();
         $dbIdentifier = $plugin->getIdentifier();
         $pluginDirectory = $this->getMainDirectory($dbPhpFile);
         if ($pluginDirectory === false) {
             $errors[] = $plugin->getName() . ": " . Language::get(StringID::ReloadManifests_InvalidFolder);
             continue;
         }
         $manifestFile = Filesystem::combinePaths(Config::get('paths', 'plugins'), $pluginDirectory, "manifest.xml");
         $xml = new \DOMDocument();
         $success = $xml->load(realpath($manifestFile));
         if ($success === false) {
             $errors[] = $plugin->getName() . ": " . Language::get(StringID::ReloadManifests_MalformedXmlOrFileMissing);
             continue;
         }
         $fileDescription = $xml->getElementsByTagName('description')->item(0);
         $fileArguments = $xml->getElementsByTagName('argument');
         $fileIdentifier = $xml->getElementsByTagName('identifier')->item(0);
         $fileArgumentsArray = [];
         for ($i = 0; $i < $fileArguments->length; $i++) {
             $fileArgumentsArray[] = trim($fileArguments->item($i)->nodeValue);
         }
         $fileArgumentsString = implode(';', $fileArgumentsArray);
         if ($dbDescription !== trim($fileDescription->nodeValue)) {
             $errors[] = $plugin->getName() . ": " . Language::get(StringID::ReloadManifests_DescriptionMismatch);
             $plugin->setDescription(trim($fileDescription->nodeValue));
             Repositories::persist($plugin);
         }
         if ($dbIdentifier !== trim($fileIdentifier->nodeValue)) {
             $errors[] = $plugin->getName() . ": " . Language::get(StringID::ReloadManifests_IdentifierMismatch);
             $plugin->setIdentifier(trim($fileIdentifier->nodeValue));
             Repositories::persist($plugin);
         }
         if ($plugin->getConfig() !== $fileArgumentsString) {
             $errors[] = $plugin->getName() . ": " . Language::get(StringID::ReloadManifests_ArgumentsMismatch);
             $plugin->setConfig($fileArgumentsString);
             Repositories::persist($plugin);
         }
     }
     Repositories::flushAll();
     if (count($errors) === 0) {
         $this->addOutput("text", Language::get(StringID::ReloadManifests_DatabaseCorrespondsToManifests));
     } else {
         $this->addOutput("text", implode('<br>', $errors));
     }
     return true;
 }
 /**
  * Runs this script.
  * @return bool Is it successful?
  */
 protected function body()
 {
     if (!$this->isInputSet(array('id'))) {
         return false;
     }
     $id = $this->getParams('id');
     /**
      * @var $submission \Submission
      */
     $submission = Repositories::findEntity(Repositories::Submission, $id);
     if ($submission->getUser()->getId() !== User::instance()->getId()) {
         return $this->death(StringID::InsufficientPrivileges);
     }
     if ($submission->getStatus() === \Submission::STATUS_GRADED) {
         return $this->death(StringID::CannotDeleteGradedSubmissions);
     }
     if ($submission->getStatus() === \Submission::STATUS_REQUESTING_GRADING) {
         return $this->death(StringID::CannotDeleteHandsoffSubmissions);
     }
     $status = $submission->getStatus();
     $submission->setStatus(\Submission::STATUS_DELETED);
     Repositories::persist($submission);
     // Make something else latest
     if ($status === \Submission::STATUS_LATEST) {
         $latestSubmission = null;
         /**
          * @var $submissions \Submission[]
          * @var $latestSubmission \Submission
          */
         $submissions = Repositories::getRepository(Repositories::Submission)->findBy(['status' => \Submission::STATUS_NORMAL, 'assignment' => $submission->getAssignment()->getId(), 'user' => User::instance()->getId()]);
         foreach ($submissions as $olderSolution) {
             if ($latestSubmission === null || $olderSolution->getDate() > $latestSubmission->getDate()) {
                 $latestSubmission = $olderSolution;
             }
         }
         if ($latestSubmission !== null) {
             $latestSubmission->setStatus(\Submission::STATUS_LATEST);
             Repositories::persist($latestSubmission);
         }
     }
     Repositories::flushAll();
     return true;
 }
 /**
  * Deletes plugin with supplied ID. This is a very destructive operation because all problems associated with this plugin will lose any reference to it and thus the submissions will lose reference and therefore we won't be able to use them for sooth.similarity comparison, for example.
  *
  * @param int $id plugin ID
  * @return array error properties provided by removalError() or retrievalError(),
  * or false in case of success
  */
 public static function deletePluginById($id)
 {
     /**
      * @var $plugin \Plugin
      * @var $tests \PluginTest[]
      * @var $problems \Problem[]
      */
     $plugin = Repositories::findEntity(Repositories::Plugin, $id);
     // Destroy plugin tests testing this plugin
     $tests = Repositories::getRepository(Repositories::PluginTest)->findBy(['plugin' => $id]);
     foreach ($tests as $test) {
         self::deleteTestById($test->getId());
     }
     // Problems that relied on this plugin are now without plugin
     $problems = Repositories::getRepository(Repositories::Problem)->findBy(['plugin' => $id]);
     foreach ($problems as $problem) {
         $problem->setPlugin(null);
         Repositories::persist($problem);
     }
     Repositories::flushAll();
     // Delete the plugin
     Repositories::remove($plugin);
     return false;
 }