public function actionIndex() { $session = Yii::$app->session; $user = $session->get('user'); $examTemplates = ExamTemplate::findByMajorJobAndProvince($user['majorJobId'], $user['provinceId']); $totalNumber = count($examTemplates); if ($totalNumber == 0) { $url = Url::to(['site/test-library-not-found']); header("Location:{$url}"); exit; } $rand = rand(1, $totalNumber); $examTemplate = $examTemplates[$rand - 1]; $session->set('examTemplate', $examTemplate); //存入session,在考试结束后计算分数要用到 $examTemplateDetails = ExamTemplateDetail::findByExamTemplate($examTemplate['examTemplateId']); $examTemplateDetails = ExamTemplateDetail::remakeArray($examTemplateDetails); $testLibraries = TestLibrary::findByTemplateDetails($examTemplateDetails, $user); $majorJob = MajorJob::findNameByMajorJobId($user['majorJobId']); //将一些必要参数存入session,方便后续页面调用 $session->set('testLibraries', $testLibraries); //所有同类型题目 $session->set('totalNumber', count($testLibraries)); //总题数 $session->set('testTitle', "模拟考试"); //测试标题 $session->set('majorJob', $majorJob); //测试岗位 return $this->render('index', ['testLibraries' => $testLibraries]); }
public function beforeAction($action) { $session = Yii::$app->session; $user = $session->get('user'); if (!TestLibrary::checkIsExist($user)) { $url = Url::to(['site/test-library-not-found']); header("Location:{$url}"); } $practiceRecordFlag = $session->getFlash('practiceRecordFlag'); if ($practiceRecordFlag) { //支付方案如果已经生成直接显示过去 return parent::beforeAction($action); } $practiceRecord = PracticeRecord::findByUser($user['userId']); if (!$practiceRecord) { //如果没有练习权 //获取在线练习支付方案 /** @var $scheme \common\models\Scheme */ $schemes = Scheme::findPracticeScheme(); $session->setFlash('practice-schemes', $schemes); //存入session,在练习首页使用 $session->setFlash('practiceRecordFlag', true); //支付方案生成的标志 $url = Url::to(['practice/index', true]); header("Location:{$url}"); return false; } return parent::beforeAction($action); }
public function actionSearch() { Url::remember(); //记录当前页,为更新数据后还是跳转到当前页做记录 $request = Yii::$app->request; $query = Yii::$app->session->getFlash('query'); if ($request->isPost) { $type = $request->post('type'); $content = $request->post('content'); } else { $type = $request->get('type'); $content = trim($request->get('content')); } if ($type || !$query) { switch ($type) { case 'testLibraryId': $query = TestLibrary::find()->where(['testLibraryId' => $content]); break; case 'testType': $table_a = TestLibrary::tableName(); $table_b = TestType::tableName(); $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.testTypeId={$table_b}.testTypeId")->where(['like', "{$table_b}.name", $content]); break; case 'province': $table_a = TestLibrary::tableName(); $table_b = Province::tableName(); $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.provinceId={$table_b}.provinceId")->where(['like', "{$table_b}.name", $content]); break; case 'majorJob': $table_a = TestLibrary::tableName(); $table_b = MajorJob::tableName(); $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.majorJobId={$table_b}.majorJobId")->where(['like', $table_b . ".name", $content]); break; case 'testChapter': $table_a = TestLibrary::tableName(); $table_b = TestChapter::tableName(); $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.testChapterId={$table_b}.testChapterId")->where(['like', $table_b . ".name", $content]); break; case 'question': case 'problem': case 'options': $query = TestLibrary::find()->where(['like', $type, $content]); break; default: $query = TestLibrary::find(); break; } } Yii::$app->session->setFlash('query', $query); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]); $model = $query->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('index', ['models' => $model, 'pages' => $pagination]); }
public function update() { /** @var $testLibrary \common\models\TestLibrary */ $testLibrary = TestLibrary::findOne($this->testLibraryId); $testLibrary->problem = $this->problem; $testLibrary->question = $this->question; $testLibrary->options = $this->options; $testLibrary->answer = $this->answer; $testLibrary->createDate = DateFunctions::getCurrentDate(); $user = \Yii::$app->session->get('user'); $testLibrary->createUserId = $user['userId']; if (!$testLibrary->update()) { throw new Exception("UpdateTestLibraryForm update error"); } }
/** * 根据用户查询所有收藏以及与其相关的testLibrary * @param $userId * @return mixed * @throws \Exception */ public static function findAllByUserWithTestLibrary($userId) { $table_a = Collection::tableName(); $table_b = TestLibrary::tableName(); return (new Query())->from([$table_a, $table_b])->where(["{$table_a}.userId" => $userId])->andWhere("{$table_b}.testLibraryId = {$table_a}.testLibraryId")->orderBy(["{$table_a}.createDate" => SORT_DESC])->all(); }
/** * 根据用户判断用户所在的省份和专业是否有题目,缓存24小时、依赖题库数量的变化 * @param $user * @return bool */ public static function checkIsExist($user) { $dependency = new DbDependency(['sql' => 'select count(*) from testLibrary']); $result = TestLibrary::getDb()->cache(function () use($user) { return TestLibrary::findOne(['provinceId' => $user['provinceId'], 'majorJobId' => $user['majorJobId']]); }, 24 * 3600, $dependency); if ($result) { return true; } return false; }
/** * 重置用户的练习进度,并返回第一题编号 * @param $user * @param $testTypeId * @return int * @throws Exception */ public static function resetCurrent($user, $testTypeId) { $testLibrary = TestLibrary::findFirstByUserAndTestType($user, $testTypeId); self::saveOrUpdate($user['userId'], $testTypeId, $testLibrary['testLibraryId']); return 0; }
/** * 单项训练 * @param $type * @return string */ public function actionSingle($type) { $session = Yii::$app->session; $user = $session->get('user'); $testTypeId = 0; $testTitle = "单项练习-"; switch ($type) { case 'danxuan': $testTypeId = 1; $testTitle .= "单选题"; break; case 'duoxuan': $testTypeId = 2; $testTitle .= "多选题"; break; case 'panduan': $testTypeId = 3; $testTitle .= "判断题"; break; case 'anli': $testTypeId = 4; $testTitle .= "案例计算题"; break; default: break; } $currentNumber = TestLibrary::findCurrentNumber($user, $testTypeId); $testLibraries = TestLibrary::findAllByUserAndTestType($user, $testTypeId); if ($currentNumber >= count($testLibraries)) { //当前题号达到最大题号,重新从头开始 $currentNumber = 0; } //测试图片 //$testLibraries = TestLibrary::find()->where('pictureBig is not null')->orWhere('pictureSmall is not null')->all(); $countNumber = count($testLibraries); if ($countNumber == 0) { //避免没有题目生成出题页面出错 $url = Url::to(['site/test-library-not-found']); header("Location:{$url}"); exit; } $majorJob = MajorJob::findNameByMajorJobId($user['majorJobId']); //将一些必要参数存入session,方便后续页面调用 $session->set('testLibraries', $testLibraries); //所有题目 $session->set('totalNumber', $countNumber); //总题数 $session->set('testTypeId', $testTypeId); //测试类型id $session->set('testTitle', $testTitle); //测试标题 $session->set('majorJob', $majorJob); //测试岗位 //取出特定的题目量,$testLibraries为引用 $startNumber = TestLibrary::takeOutSpecialNumber($currentNumber, $testLibraries); return $this->render('test', ['testLibraries' => $testLibraries, 'startNumber' => $startNumber, 'currentNumber' => $currentNumber]); }