public function index(Request $request) { /* * session()->keep here is used to retain the lesson id even if the user refreshes the page * this keeps the lesson id hidden to the user and inaccessible by anyone else * the lesson id is not retained when navigating away from the page so the page will be inaccessible once done */ $lessonId = session('lessonId'); // If lesson id does not exist in the session, do not allow access to the page if (!isset($lessonId)) { return redirect('lessons'); } $user = auth()->user(); $words = Word::orderBy(\DB::raw('RAND()'))->take(80)->get(); $questions = LessonWord::with('word')->where('lesson_id', $lessonId)->get(); session()->flash('maxQuestions', count($questions)); if (empty(session('questionIndex'))) { session()->flash('questionIndex', 0); // Start with index zero } else { session()->keep('questionIndex'); } $generatedOptions = $this->generateOptions($questions, $words); // Pass this to view if ($generatedOptions == null) { return redirect('lessons'); // Return users to lesson page if they try to go back to the finished exam } return view('lessons.exam', ['user' => $user, 'questions' => $questions, 'options' => $generatedOptions]); }
public function actionGetquestion() { $user = User::find()->where(['id' => (int) Yii::$app->request->get('user')])->one(); $test = json_decode($user->test, true); //После 5 вопросов заканчиваем тест if (count($test['rights']) >= 5) { return 'finish'; } //Если уже есть ответы, то берем из азы любые кроме отвеченных, иначе все if (count($test['rights']) > 0) { for ($i = 0; $i < count($test['rights']); $i++) { $exclude[] = $test['rights'][$i]['id']; } $rows = Word::find()->select(['id', 'rus', 'eng'])->from('word')->where(['not in', 'id', $exclude])->orderBy('RAND()')->limit(4)->all(); } else { $rows = Word::find()->select(['id', 'rus', 'eng'])->from('word')->orderBy('RAND()')->limit(4)->all(); } //получаем случайны вопрос $right_key = array_rand($rows); //записываем его в пользователя $test['rights'][] = ['id' => $rows[$right_key]['id'], 'a' => null, 't' => 0]; $user->test = json_encode($test, true); $user->save(); //Формируем вопрос и варианты ответа на фронтенд for ($i = 0; $i < count($rows); $i++) { $variation[] = ['id' => $rows[$i]['id'], 'word' => $rows[$i]['eng']]; } $words = ['question' => ['word' => $rows[$right_key]['rus']], 'words' => $variation]; return $words; }
public function getAutoCompleteWordsAjax(Request $request) { if (!$request->ajax()) { return response()->json(['response' => 'reject', 'msg' => 'request Not Allowed', 'searchWord' => $request->searchWord, 'language' => $request->languageName, 'Ajax' => $request->ajax(), 'Request' => $request->method()]); } $searchWord = $request->searchWord; $languageId = Language::where('id', '=', $request->languageId)->value('id'); $Words = Word::where('language_id', '=', $languageId)->where('word', 'LIKE', $searchWord . '%')->orderBy('word', 'asc')->limit(10)->get(); return $Words; }
public function destroy($id) { try { $word = Word::findOrFail($id)->delete(); session()->flash('flash_success', 'Delete successful!'); } catch (ModelNotFoundException $e) { session()->flash('flash_error', 'Delete failed. The word you are trying to delete cannot be found.'); } return redirect()->back(); }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $data['mem_limit'] = ini_get('memory_limit'); $data['english_words'] = Word::where('language_id', '1')->count(); $data['danish_words'] = Word::where('language_id', '2')->count(); $data['pashto_words'] = Word::where('language_id', '3')->count(); $data['unique_users'] = User::count(); $data['users'] = User::limit(4)->get(); $data['words'] = Word::with(['language', 'user'])->limit(8)->orderBy('created_at', 'desc')->get(); $used_mem = memory_get_peak_usage(false); $data['mem_using'] = round($used_mem / 1024 / 1024, 1) . 'M'; return view('admin.dashboard', $data); }
public static function create(array $args = array()) { $v = new Word(); if ($v->validate($args)) { $w = new Word(); $word = preg_replace("[^a-zA-Z]", "", $args['word']); $w->word = $word; $w->length = strlen($args['word']); $w->user_id = $args['user_id']; $w->timestamp_utc = time(); $w->save(); for ($i = 0; $i < strlen($word); $i++) { $l = new Letter(); $l->letter = substr($word, $i, 1); $l->word_id = $w->id; $l->ordinal = $i + 1; $l->save(); } return $w; } else { return array('errors' => $v->errors); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Word::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'projekt_id' => $this->projekt_id, 'group_id' => $this->group_id]); $query->andFilterWhere(['like', 'word_name', $this->word_name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'status', $this->status]); return $dataProvider; }
public function testAWordCanBeCreated() { $faker = Faker\Factory::create(); $user = factory(User::class)->create(['username' => 'janedoe2']); $word_str = $faker->word; $args = array('word' => $word_str, 'user_id' => $user->id); $word = Word::create($args); $first_letter = Letter::where('word_id', $word->id)->where('ordinal', '1')->first(); $this->assertEquals($word->word, $word_str); $this->assertEquals($first_letter->letter, substr($word_str, 0, 1)); if (strlen($word_str) > 1) { $second_letter = Letter::where('word_id', $word->id)->where('ordinal', 2)->first(); $this->assertEquals($second_letter->letter, substr($word_str, 1, 1)); } }
public function run() { $user = User::where('username', 'system')->first(); $filename = "data/58000words.txt"; $words_str = File::get($filename); if (env("COMPUTER") == "HOMEMAC") { $words = explode("\n", $words_str); } else { $words = explode("\r\n", $words_str); } foreach ($words as $word_str) { $word = Word::where('word', $word_str)->first(); if (!$word) { $args = array('word' => $word_str, 'user_id' => $user->id); $word = Word::create($args); } } }
/** * Creates a new UrQuestions model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new UrQuestions(); $model->user_id = \Yii::$app->user->identity->id; $UrRecords = Ur::find()->orderBy('name'); if (\Yii::$app->user->identity->status == 2) { $UrRecords = $UrRecords->Where(['regional_id' => \Yii::$app->user->identity->id]); } if (\Yii::$app->user->identity->status == 3) { // $UrRecords = $UrRecords->Where(['pi_id' => \Yii::$app->user->identity->id]); } $UrRecords = $UrRecords->all(); //->where(['pi_id' => $user]) if (Yii::$app->request->isPost) { // print_r($model->qfile); $model->qfile = UploadedFile::getInstances($model, 'qfile'); if (!empty($model->qfile)) { $model->qfiles = ""; foreach ($model->qfile as $file) { $fileName = 'files/qfile/' . Functions::str2url($file->baseName) . rand(555, 99999) . '.' . $file->extension; $model->qfiles .= $fileName . "|"; $file->saveAs($fileName); } $model->qfiles = substr($model->qfiles, 0, -1); } } if ($model->load(Yii::$app->request->post()) && ($id = $model->save())) { $info = UrQuestions::getInfo($model->id); Word::ur_questions($info); return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'UrRecords' => $UrRecords]); } }
/** * Finds the Word model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Word the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Word::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function activate($user, $sure = false) { $missing_clues = array(); $missing_clues_for_one_letter_words = array(); $missing_letters = array(); $words = array(); $pt = $this->puzzle_template; $pt->blackSquares = $pt->blackSquares(); $squares = $this->puzzle_squares(true); $clues = $this->clues; $ordinal = 1; for ($row = 1; $row <= $pt->height; $row++) { for ($col = 1; $col <= $pt->width; $col++) { if (!in_array($row . '-' . $col, $pt->blackSquares) && $squares[$row . '-' . $col]['square_type'] != 'black' && (!@$squares[$row . '-' . $col]['letter'] || $squares[$row . '-' . $col]['letter'] == "")) { $missing_letters[] = $row . '-' . $col; } $square_should_have_across_clue = false; $square_should_have_down_clue = false; if ($col != $pt->width && $squares[$row . '-' . $col]['square_type'] != 'black' && ($col == 1 || $squares[$row . '-' . ($col - 1)]['square_type'] == 'black')) { $square_should_have_across_clue = true; } if ($row != $pt->height && $squares[$row . '-' . $col]['square_type'] != 'black' && ($row == 1 || $squares[$row - 1 . '-' . $col]['square_type'] == 'black')) { $square_should_have_down_clue = true; } if ($square_should_have_down_clue) { $found_clue = false; foreach ($clues as $clue) { if ($clue->ordinal == $ordinal && $clue->direction == 'down') { $found_clue = $clue; } } if (!$found_clue) { if ($row == $pt->height || $squares[$row + 1 . '-' . $col]['square_type'] == 'black') { $missing_clues_for_one_letter_words[] = $ordinal . ' down'; } else { $missing_clues[] = $ordinal . ' down'; } } //find word $word = ""; for ($word_row = $row; $word_row <= $pt->height && $squares[$word_row . '-' . $col]['square_type'] != 'black'; $word_row++) { $word .= $squares[$word_row . '-' . $col]['letter']; } $word_db = Word::where('word', $word)->first(); if (!$word_db) { $word_db = new Word(); $word_db->word = $word; $word_db->length = strlen($word); $word_db->user_id = $user->id; $word_db->timestamp_utc = time(); $word_db->save(); for ($i = 0; $i < strlen($word); $i++) { $letter = new Letter(); $letter->letter = substr($word, $i, 1); $letter->word_id = $word_db->id; $letter->ordinal = $i + 1; $letter->save(); } } if ($found_clue) { $clue_available = ClueAvailable::where('word_id', $word_db->id)->where('clue', $found_clue->clue)->first(); if (!$clue_available) { $clue_available = new ClueAvailable(); $clue_available->word_id = $word_db->id; $clue_available->clue = $found_clue->clue; $clue_available->timestamp_utc = time(); $clue_available->save(); } } } if ($square_should_have_across_clue) { $found_clue = false; foreach ($clues as $clue) { if ($clue->ordinal == $ordinal && $clue->direction == 'across') { $found_clue = $clue; } } if (!$found_clue) { if ($col == $pt->width || $squares[$row . '-' . ($col + 1)]['square_type'] == 'black') { $missing_clues_for_one_letter_words[] = $ordinal . ' across'; } else { $missing_clues[] = $ordinal . ' across'; } } } if ($square_should_have_across_clue || $square_should_have_down_clue) { $ordinal++; } } } if (count($missing_clues) > 0 || count($missing_letters) > 0 || count($missing_clues_for_one_letter_words) > 0 && !$sure) { return array('errors' => compact('missing_clues', 'missing_letters', 'missing_clues_for_one_letter_words')); } else { $this->active = 1; $this->timestamp_utc = time(); $this->save(); return array('success' => 1); } }
/** * @return \yii\db\ActiveQuery */ public function getWords() { return $this->hasMany(Word::className(), ['group_id' => 'id']); }
public function getAvailableQuestions() { return Word::userUnlearnedWords()->get(); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $word = Word::findOrFail($id); if ($word->delete()) { return response()->json(['response' => 'Success', 'msg' => 'The Word Deleted!']); } else { return response()->json(['response' => 'Error', 'msg' => 'Something gone wrong']); } }
/** * Finds the Word model based on its primary key value. * @param string $id * @return Word the loaded model * @throws NotFoundHttpException if the model cannot be found */ private function findModel($id) { $model = Word::find()->userId(Yii::$app->user->id)->andWhere(['id' => $id])->limit(1)->one(); if ($model === null) { throw new NotFoundHttpException('データがありません。'); } return $model; }
/** * @dataProvider searchQueryProvider */ public function testSearchQuery($search, $count, $id) { $word = Word::find()->userId(1)->search($search); $this->assertSame($count, $word->count()); $this->assertSame($id, $word->all()[0]->id); }