Beispiel #1
0
 public function buildTestpaper($id, $options)
 {
     $testpaper = $this->getTestpaperDao()->getTestpaper($id);
     if (empty($testpaper)) {
         throw $this->createServiceException("Testpaper #{$id} is not found.");
     }
     $this->getTestpaperItemDao()->deleteItemsByTestpaperId($testpaper['id']);
     $builder = TestpaperBuilderFactory::create($testpaper['pattern']);
     $result = $builder->build($testpaper, $options);
     if ($result['status'] != 'ok') {
         throw $this->createServiceException("Build testpaper #{$id} items error.");
     }
     $items = array();
     $types = array();
     $totalScore = 0;
     $seq = 1;
     foreach ($result['items'] as $item) {
         $questionType = QuestionTypeFactory::create($item['questionType']);
         $item['seq'] = $seq;
         if (!$questionType->canHaveSubQuestion()) {
             $seq++;
             $totalScore += $item['score'];
         }
         $items[] = $this->getTestpaperItemDao()->addItem($item);
         if ($item['parentId'] == 0 && !in_array($item['questionType'], $types)) {
             $types[] = $item['questionType'];
         }
     }
     $metas = empty($testpaper['metas']) ? array() : $testpaper['metas'];
     $metas['question_type_seq'] = $types;
     $metas['missScore'] = $options['missScores'];
     $this->getTestpaperDao()->updateTestpaper($testpaper['id'], array('itemCount' => $seq - 1, 'score' => $totalScore, 'metas' => $metas));
     $this->dispatchEvent("testpaper.items.update", array('testpaper' => $testpaper, 'items' => $items));
     return $items;
 }
 public function itemsResetAction(Request $request, $courseId, $testpaperId)
 {
     $course = $this->getCourseService()->tryManageCourse($courseId);
     $testpaper = $this->getTestpaperService()->getTestpaper($testpaperId);
     if (empty($testpaper)) {
         throw $this->createNotFoundException('试卷不存在');
     }
     if ($request->getMethod() == 'POST') {
         $data = $request->request->all();
         $data['target'] = "course-{$course['id']}";
         $data['ranges'] = explode(',', $data['ranges']);
         $this->getTestpaperService()->buildTestpaper($testpaper['id'], $data);
         return $this->redirect($this->generateUrl('course_manage_testpaper_items', array('courseId' => $courseId, 'testpaperId' => $testpaperId)));
     }
     $typeNames = $this->get('topxia.twig.web_extension')->getDict('questionType');
     $types = array();
     foreach ($typeNames as $type => $name) {
         $typeObj = QuestionTypeFactory::create($type);
         $types[] = array('key' => $type, 'name' => $name, 'hasMissScore' => $typeObj->hasMissScore());
     }
     return $this->render('TopxiaWebBundle:CourseTestpaperManage:items-reset.html.twig', array('course' => $course, 'testpaper' => $testpaper, 'ranges' => $this->getQuestionRanges($course), 'types' => $types));
 }
 public function judgeQuestions(array $answers, $refreshStats = false)
 {
     $questionIds = array_keys($answers);
     $questions = $this->getQuestionDao()->findQuestionsByIds($questionIds);
     $questions = ArrayToolkit::index($questions, 'id');
     $results = array();
     foreach ($answers as $id => $answer) {
         if (empty($answer)) {
             $results[$id] = array('status' => 'noAnswer');
         } elseif (empty($questions[$id])) {
             $results[$id] = array('status' => 'notFound');
         } else {
             $question = $questions[$id];
             $results[$id] = QuestionTypeFactory::create($question['type'])->judge($question, $answer);
         }
     }
     return $results;
 }