Esempio n. 1
0
 public function result($title, User $creator, $total = 20)
 {
     $result = new Result();
     $result->setTotal($total);
     if (!$this->resultType) {
         $this->resultType = $this->om->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('claroline_result');
     }
     $node = new ResourceNode();
     $node->setName($title);
     $node->setCreator($creator);
     $node->setResourceType($this->resultType);
     $node->setWorkspace($creator->getPersonalWorkspace());
     $node->setClass('Claroline\\ResultBundle\\Entity\\Result');
     $node->setGuid(time());
     $result->setResourceNode($node);
     $this->om->persist($result);
     $this->om->persist($node);
     return $result;
 }
Esempio n. 2
0
 /**
  * Import marks from a CSV file into a result resource.
  *
  * @param Result       $result
  * @param UploadedFile $csvFile
  * @param string       $importType Either "fullname", "code" or "username"
  *
  * @return array
  */
 public function importMarksFromCsv(Result $result, UploadedFile $csvFile, $importType = 'fullname')
 {
     $repo = $this->om->getRepository('ClarolineCoreBundle:User');
     $roles = $result->getResourceNode()->getWorkspace()->getRoles()->toArray();
     $users = $repo->findUsersByRolesIncludingGroups($roles);
     $data = ['marks' => [], 'errors' => []];
     $fileData = file_get_contents($csvFile);
     $fileData = $this->utils->formatCsvOutput($fileData);
     $lines = str_getcsv($fileData, PHP_EOL);
     if (count($lines) === 1 && $lines[0] === null) {
         $data['errors'][] = ['code' => self::ERROR_EMPTY_CSV, 'message' => 'errors.csv_empty', 'line' => null];
         return $data;
     }
     $countRowEl = $importType === 'fullname' ? 3 : 2;
     foreach (file($csvFile->getPathname()) as $index => $line) {
         $values = array_map('trim', str_getcsv($line, ';'));
         $lineNumber = $index + 1;
         if (count($values) < $countRowEl) {
             $data['errors'][] = ['code' => self::ERROR_MISSING_VALUES, 'message' => 'errors.csv_missing_values', 'line' => $lineNumber];
         } elseif (in_array('', $values)) {
             $data['errors'][] = ['code' => self::ERROR_EMPTY_VALUES, 'message' => 'errors.csv_empty_values', 'line' => $lineNumber];
         } elseif (!$this->isValidMark($result, $values[$countRowEl - 1])) {
             $data['errors'][] = ['code' => self::ERROR_INVALID_MARK, 'message' => 'errors.invalid_mark', 'line' => $lineNumber];
         } else {
             $matchedUser = false;
             foreach ($users as $user) {
                 switch ($importType) {
                     case 'fullname':
                         if ($user->getFirstName() === $values[0] && $user->getLastName() === $values[1]) {
                             $matchedUser = $user;
                             break 2;
                         }
                     case 'code':
                         if ($user->getAdministrativeCode() === $values[0]) {
                             $matchedUser = $user;
                             break 2;
                         }
                     case 'username':
                         if ($user->getUsername() === $values[0]) {
                             $matchedUser = $user;
                             break 2;
                         }
                 }
             }
             if (!$matchedUser) {
                 $data['errors'][] = ['code' => self::ERROR_EXTRA_USERS, 'message' => 'errors.csv_extra_users', 'line' => $lineNumber];
             } else {
                 $mark = new Mark($result, $user, $values[$countRowEl - 1]);
                 $this->om->persist($mark);
                 $this->om->flush();
                 $data['marks'][] = $mark;
                 //Create log for mark
                 $newMarkEvent = new LogResultsNewMarkEvent($mark);
                 $this->dispatcher->dispatch('log', $newMarkEvent);
             }
         }
     }
     return $data;
 }