Ejemplo n.º 1
0
 /**
  * @param $inputs
  */
 public function store($inputs)
 {
     $answersLabels = $inputs['answerLabel'];
     $answers = [];
     foreach ($answersLabels as $key => $answersLabel) {
         //Creating Answer object
         $answerEntity = new Answer();
         $answerEntity->setAttribute('label', $answersLabel);
         if ($key == $inputs['answerChecked']) {
             $answerEntity->setAttribute('correct', true);
         } else {
             $answerEntity->setAttribute('correct', false);
         }
         $answers[] = $answerEntity;
     }
     //Insert the question
     $question = new Question();
     $question->fill($inputs);
     $question->save();
     //Attach this question to one or several quizzs
     if (isset($inputs['quizz'])) {
         $question->quizzs()->attach($inputs['quizz']);
     }
     //Insert all the answers to this question
     $question->answers()->saveMany($answers);
 }
 public function store(InsertQuestionRequest $request)
 {
     if (Session::has('id') && (Session::get('type') === 'Admin' || Session::get('type') === 'SuperAdmin')) {
         /*		$input = Request::all();
         		Question::create($input);
         		$input['created_at'] = Carbon::now();
         		//return 's';
         		return redirect('questions');
         */
         //		$sub = Subject_List::lists('subject_name')->with('sub', array('sub'=> $sub));
         $sub = Input::get('subject');
         $qus_des = Input::get('q_description');
         $opt_1 = Input::get('q_opt_1');
         $opt_2 = Input::get('q_opt_2');
         $opt_3 = Input::get('q_opt_3');
         $opt_4 = Input::get('q_opt_4');
         $ans = Input::get('q_ans');
         $question = new Question();
         $question->subject = $sub;
         $question->q_description = $qus_des;
         $question->q_opt_1 = $opt_1;
         $question->q_opt_2 = $opt_2;
         $question->q_opt_3 = $opt_3;
         $question->q_opt_4 = $opt_4;
         $question->q_ans = $ans;
         //$question->save();
         $question['created_at'] = Carbon::now();
         $question->save(array($request));
     } else {
         echo 'You are not authorized';
     }
 }
Ejemplo n.º 3
0
 /**
  * Creates a new Question model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Question();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['/admin/question/index']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 4
0
 /**
  * Creates a new Question model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Question();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id, 'Survey_id' => $model->Survey_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 5
0
 /**
  * Update the specified resource in storage.
  *
  * @param Question $questions
  * @param CategoryRequest $request
  * @return Response
  */
 public function update(Question $questions, QuestionRequest $request)
 {
     $attachment = ImageUploadFacade::image($request->file('attachment'));
     $questions->fill($request->all());
     if ($attachment) {
         $questions->attachment()->associate($attachment);
     }
     $questions->save();
     return redirect()->route('admin.questions.index');
 }
Ejemplo n.º 6
0
 public function actionCreate($id_ank)
 {
     $model = new Question(['id_ank' => $id_ank]);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Yii::$app->session->addFlash('success', Yii::t('app', "Record was successfully created."));
         return $this->redirect(['update', 'id' => $model->id_q]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 7
0
 /**
  * Creates a new Question model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Question();
     if ($model->load(Yii::$app->request->post())) {
         $model->addtime = time();
         $model->save();
         return $this->redirect(['view', 'id' => (string) $model->_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 8
0
 public function saveQuestion(Request $request)
 {
     // [1] CHECK IF THE BLUEPRINT EXIST AND THE USER CAN CHANGE IT
     $user = Auth::user();
     if ($user->level == 3) {
         $blueprint = Blueprint::find($request->input('blueprint_id'));
     } else {
         $blueprint = Blueprint::where("user_id", $user->id)->find($request->input('blueprint_id'));
     }
     if (!$blueprint) {
         if ($user->level == 3) {
             abort(404, 'El formulario no existe!');
         } else {
             abort(403, 'El formulario no pertenece al usuario');
         }
     }
     // [2] CREATE THE QUESTION OBJECT
     $question = new Question();
     $question->user_id = $user->id;
     $question->blueprint_id = $blueprint->id;
     $question->section_id = (int) $request->input("section_id");
     $question->question = $request->input("question");
     $question->is_description = $request->input("is_description");
     //$question->is_location    = $request->input("is_location");
     $question->type = $request->input("type");
     $question->save();
     $options = [];
     // [3] IF THE QUESTION HAS OPTIONS, CREATE THEM
     if (!empty($request->input('options'))) {
         $val = 1;
         foreach ($request->input('options') as $opt) {
             $option = new Option();
             $option->question_id = $question->id;
             $option->blueprint_id = $blueprint->id;
             $option->description = $opt;
             $option->value = $val;
             $option->name = uniqid();
             $option->order_num = $val;
             $option->save();
             $options[] = $option;
             $val++;
         }
         $remove_rule = 0;
     } else {
         $remove_rule = Rule::where("blueprint_id", $blueprint->id)->where("question_id", $question->id)->delete();
     }
     $question->options = $options;
     $question->remove_rule = $remove_rule;
     // [4] GENERATE A NEW TOKEN TO PREVENT TOKEN MISSMATCH
     $question->new_token = csrf_token();
     // [5] RETURN THE JSON
     return response()->json($question)->header('Access-Control-Allow-Origin', '*');
 }
 public function actionIndex()
 {
     $file = file_get_contents(__DIR__ . '/QuestionImport.txt');
     $line = explode("\n", $file);
     $array = [];
     foreach ($line as $k => $v) {
         $el = explode("\t", $v);
         $count_el = count($el);
         $array[$k]['title'] = $el[0];
         $array[$k]['occured_number'] = $el[1];
         $array[$k]['correct_answer_key'] = $el[2];
         for ($i = 3; $i < $count_el; $i++) {
             $array[$k]['answer'][$i] = $el[$i];
         }
         $array[$k]['number_of_elements'] = $count_el;
     }
     $user = User::findByUsername('admin');
     foreach ($array as $v) {
         $model_question = new Question();
         $model_question->title = $v['title'];
         $model_question->occured_number = $v['occured_number'];
         $model_question->correct_answer_id = '0';
         $model_question->author_id = $user->id;
         $model_question->save();
         $correct_answer_id = 0;
         foreach ($v['answer'] as $k2 => $v2) {
             $model_answer = new Answer();
             $model_answer->question_id = $model_question->id;
             $model_answer->title = $v2;
             $model_answer->author_id = $user->id;
             $model_answer->save();
             if ($k2 - 3 == $v['correct_answer_key']) {
                 $correct_answer_id = $model_answer->id;
             }
         }
         $model_question->correct_answer_id = $correct_answer_id;
         $model_question->save();
     }
 }
Ejemplo n.º 10
0
 /**
  * Creates a new Question model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     if (!\Yii::$app->user->can('7progress')) {
         return $this->goBack();
         // goHome
     }
     $model = new Question();
     if ($model->load(Yii::$app->request->post())) {
         $model->user_created = Yii::$app->user->id;
         $model->created = time();
         $model->save();
         return $this->redirect(['index']);
         // return $this->redirect(['view', 'id' => $model->id]);
     } else {
         if (Yii::$app->request->isAjax) {
             return $this->renderAjax('create', ['model' => $model]);
         } else {
             return $this->render('create', ['model' => $model, 'upDate' => true]);
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * ask user a question through ajax
  * @return string
  */
 public function actionAsk()
 {
     if (\Yii::$app->request->isAjax) {
         $request = \Yii::$app->request;
         $question = new Question();
         if (!is_numeric($request->post('id_user'))) {
             return 0;
         }
         $question->id_user = $request->post('id_user');
         $question->id_from = \Yii::$app->user->id;
         $question->question = $request->post('question');
         $question->date = date('Y-m-d H:i');
         $question->status = 0;
         $question->save();
         return 1;
     } else {
         $model = (new User())->findById(\Yii::$app->user->id);
         $this->view->params['user'] = $model;
         $this->view->params['isProfile'] = false;
         return $this->render('ask');
     }
 }
Ejemplo n.º 12
0
 public function questions(Request $request)
 {
     // [1] validate the title and the CSV
     $messages = ['title.required' => 'El título es requerido', 'the-csv.required' => 'El archivo de excel es requerido', 'the-csv.mimetypes' => 'El archivo debe ser excel'];
     $this->validate($request, ['title' => 'bail|required|max:255', 'the-csv' => 'required|mimetypes:application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], $messages);
     // [2] save the quiz blueprint
     $user = Auth::user();
     $blueprint = new Blueprint();
     $blueprint->title = $request->input("title");
     $blueprint->user_id = $user->id;
     $blueprint->is_closed = 0;
     $blueprint->is_public = 0;
     $blueprint->is_visible = 0;
     $blueprint->type = "generated";
     $blueprint->save();
     // [3] add the questions
     $file = $request->file("the-csv");
     $temp = $file->getPathName();
     Excel::load($temp, function ($reader) use($blueprint) {
         $reader->each(function ($row) use($blueprint) {
             if (trim($row->pregunta) != "") {
                 $question = new Question();
                 //$options  = !empty($row->seccion) ? (int)$row->seccion : 1;
                 $question->blueprint_id = $blueprint->id;
                 $question->question = $row->pregunta;
                 $question->section_id = !empty($row->seccion) ? (int) $row->seccion : 1;
                 $type = strtr(strtolower($row->tipo), "áéíóú", "aeiou");
                 if (in_array($type, ['numero', 'numerico', 'numerica', 'cantidad', 'cifra'])) {
                     $question->type = "number";
                 } elseif ($type == "estado") {
                     $question->type = "location-a";
                 } elseif ($type == "municipio") {
                     $question->type = "location-b";
                 } elseif ($type == "localidad") {
                     $question->type = "location-c";
                 } elseif ($type == "multiple" || $type == "opcion multiple") {
                     $question->type = "multiple";
                 } elseif ($type == "multiple multiple" || $type == "multiple-multiple") {
                     $question->type = "multiple-multiple";
                 } else {
                     $question->type = "text";
                 }
                 $question->save();
                 if (!empty($row->opciones) && ($question->type == "multiple" || $question->type == "multiple-multiple")) {
                     $options = explode("|", $row->opciones);
                     for ($i = 0; $i < count($options); $i++) {
                         $option = new Option();
                         $option->question_id = $question->id;
                         $option->blueprint_id = $blueprint->id;
                         $option->description = $options[$i];
                         $option->value = $i + 1;
                         //$options[$i];//$i+1;
                         $option->name = uniqid();
                         $option->order_num = $i;
                         $option->save();
                     }
                     // for
                 }
                 // if
             }
         });
     })->first();
     $request->session()->flash('status', ['type' => 'create', 'name' => $blueprint->title]);
     return redirect("dashboard/encuestas");
 }
 public function run()
 {
     $tp1cn2 = TP::where('nom', '=', 'TP1 CN2')->first();
     $tp2cn2 = TP::where('nom', '=', 'TP2 CN2')->first();
     $tp1DM1 = TP::where('nom', '=', 'TP1 DM1')->first();
     DB::table('tps_questions')->delete();
     DB::table('questions')->delete();
     $question = new Question();
     $question->nom = "Q1";
     $question->enonce = "Quel est la couleur du cheval blanc de Napoléon";
     $question->sur = 10;
     $question->baliseCorrection = "blanc = 10, noir =0";
     $question->reponse = "blanc";
     $question->save();
     $tp1cn2->addQuestion($question);
     $question = new Question();
     $question->nom = "Exponentielle";
     $question->enonce = "<p>Si x = 2</p>\n\n<p>et y = 3</p>\n\n<p>Quelle est la valeur de <strong>x<sup>y</sup></strong>?</p>\n\t\t";
     $question->sur = 10;
     $question->baliseCorrection = "<p>un nombre pair = 5p</p>\n\n<p>8 = 10p</p>";
     $question->reponse = "8";
     $question->save();
     $tp1cn2->addQuestion($question);
 }
Ejemplo n.º 14
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Requests\Admin\Questions\Store $request, Question $Question)
 {
     $Question->fill($request->input('question'));
     $Question->save();
     if ($request->hasFile('image') && $request->file('image')->isValid()) {
         $QuestionImage = $request->file('image');
         $QuestionImage = $QuestionImage->move('upload\\questions', $Question->id . '.' . $QuestionImage->getClientOriginalExtension());
         $Question->image = $QuestionImage->getPathname();
         $Question->save();
     } elseif ($request->has('question.delete_image') && $request->input('question.delete_image') == 'on') {
         $Question->image = null;
         $Question->save();
     }
     $IdsAnswers = [];
     foreach ($request->input('answers') as $key => $answer) {
         $Answer = Answer::findOrNew($request->input('answers.' . $key . '.id'));
         $Answer->fill(['text' => $request->input('answers.' . $key . '.text'), 'right' => $request->input('answers.' . $key . '.right', false), 'question_id' => $Question->id]);
         $Answer->save();
         $IdsAnswers[] = $Answer->id;
         if ($request->hasFile('answers.' . $key . '.image') && $request->file('answers.' . $key . '.image')->isValid()) {
             $AnswerFile = $request->file('answers.' . $key . '.image');
             $AnswerFile = $AnswerFile->move('upload\\answers', $Answer->id . '.' . $AnswerFile->getClientOriginalExtension());
             $Answer->image = $AnswerFile->getPathname();
             $Answer->save();
         } elseif ($request->has('answers.' . $key . '.delete_image') && $request->input('answers.' . $key . '.delete_image') == 'on') {
             $Answer->image = null;
             $Answer->save();
         }
     }
     $AnswersForDeleting = $Question->answers()->whereNotIn('id', $IdsAnswers)->get();
     if ($AnswersForDeleting->count()) {
         foreach ($AnswersForDeleting as $Answer) {
             $Answer->delete();
         }
     }
     return redirect()->back();
 }
 public function actionImport()
 {
     //print_r($_FILES);
     //exit;
     $imported = 0;
     $dropped = 0;
     $file = UploadedFile::getInstanceByName('filename');
     if ($file) {
         $content = file($file->tempName);
         foreach ($content as $line) {
             $question = explode('@', $line);
             if (in_array($question[1], array_keys(Question::$types))) {
                 $oQuestion = new Question();
                 $oQuestion->frage = $question[0];
                 $oQuestion->display = $question[1];
                 $oQuestion->antworten = $question[2];
                 $oQuestion->suche = $question[3];
                 $oQuestion->save();
                 $imported++;
             } else {
                 $dropped++;
             }
         }
     }
     return $this->render('upload', ['imported' => $imported, 'dropped' => $dropped]);
 }
Ejemplo n.º 16
0
 public function actionAdd()
 {
     $subjects = Subject::find()->all();
     foreach ($subjects as $subject) {
         for ($i = 1; $i <= 25; $i++) {
             $question = new Question();
             if ($i <= 15) {
                 $question->description = 'Легкий вопрос ' . $i;
                 $question->level = 0;
             } elseif ($i > 15 && $i <= 20) {
                 $question->description = 'Средний вопрос ' . $i;
                 $question->level = 1;
             } else {
                 $question->description = 'Сложный вопрос ' . $i;
                 $question->level = 2;
             }
             $question->var1 = 'Вариант 1';
             $question->var2 = 'Вариант 2';
             $question->var3 = 'Вариант 3';
             $question->var4 = 'Вариант 4';
             $question->var5 = 'Вариант 5';
             $question->answer_number = rand(1, 5);
             $question->section = $subject->section;
             $question->cost = 0;
             $question->subject_id = $subject->id;
             $question->save();
         }
     }
     return 111;
 }
 public function actionImport()
 {
     //print_r($_FILES);
     //exit;
     $imported = 0;
     $dropped = 0;
     $postData = Yii::$app->request->post();
     $file = UploadedFile::getInstanceByName('filename');
     if ($file) {
         $content = file($file->tempName);
         $lines = 0;
         try {
             foreach ($content as $line) {
                 $lines++;
                 $question = explode('@', $line);
                 if (in_array($question[1], array_keys(Question::$types))) {
                     $oQuestion = new Question();
                     $oQuestion->frage = $question[0];
                     $oQuestion->display = $question[1];
                     $oQuestion->antworten = $question[2];
                     $oQuestion->suche = isset($question[3]) ? $question[3] : '';
                     if ($oQuestion->save()) {
                         $imported++;
                     } else {
                         $dropped++;
                     }
                 } else {
                     $dropped++;
                 }
             }
             if ($imported == 0) {
                 return $this->render('upload', ['warning' => "Type option is invalid or not found. Check format of the file", 'imported' => 0, 'dropped' => 0]);
             }
         } catch (ErrorException $e) {
             return $this->render('upload', ['warning' => "Invalid character was found at line " . $lines . ". Imported questions " . $imported, 'imported' => 0, 'dropped' => 0]);
         }
     } elseif (!empty($postData)) {
         return $this->render('upload', ['error' => "No file was selected to import", 'imported' => $imported, 'dropped' => $dropped]);
     }
     return $this->render('upload', ['imported' => $imported, 'dropped' => $dropped]);
 }