public function postQuestion($id) { $note = 0; $qcm_student = []; $nb_choices = Choice::where('question_id', $id)->count(); for ($i = 0; $i < $nb_choices; $i++) { array_push($qcm_student, Input::get("status{$i}")); } $qcm_teacher = []; $choices = Choice::where('question_id', $id)->get(); foreach ($choices as $choice) { array_push($qcm_teacher, $choice->status); } for ($i = 0; $i < count($qcm_teacher); $i++) { if ($qcm_teacher[$i] == $qcm_student[$i]) { $note++; } } $test_score = DB::table('scores')->where('user_id', '=', Auth::user()->id)->where('question_id', '=', Question::where('id', $id)->first()->id)->first(); if (!is_null($test_score)) { //echo "fait"; Session::flash('message_error', 'Vous avez déjà fait ce QCM'); return Redirect::to('/student'); } else { $score = new Score(); $score->user_id = Auth::user()->id; $score->question_id = Question::where('id', $id)->first()->id; $score->status_question = 'fait'; $score->note = $note; $score->save(); Session::flash('message_success', 'Votre score a bien été enregistré'); return Redirect::to('/student'); } return Redirect::to('/student'); }
/** * Class constructor. * * @param array $options Element options * @param array $attr HTML attributes */ public function __construct(array $options = [], array $attr = []) { if (!isset($attr['multiple'])) { $attr['multiple'] = function () { return (bool) $this->getOption('multiple'); }; } return parent::__construct($options, $attr); }
public function postEdit($id) { $question = Question::find($id); $choices = Choice::where('question_id', '=', $question->id)->get(); if (Input::has('title')) { DB::table('questions')->where('id', $question->id)->update(['title' => Input::get('title')]); } if (Input::has('class_level')) { DB::table('questions')->where('id', $question->id)->update(['class_level' => Input::get('class_level')]); } /* update pour des réponses pour + tard $i = 0; foreach ($choices as $choice) { if (Input::has("choice_content$i")) { $choice->content = Input::get("choice_content$i"); $choice->status = Input::get("status$i"); $choice->save(); } $i++; } */ return Redirect::to('/teacher/questions'); }
/** * Adds a form. * * Called when this component receives an HTTP POST request to * /form(/). */ public function addForm() { Logger::Log('starts POST AddForm', LogLevel::DEBUG); $header = $this->app->request->headers->all(); $body = $this->app->request->getBody(); $this->app->response->setStatus(201); $forms = Form::decodeForm($body); // always been an array $arr = true; if (!is_array($forms)) { $forms = array($forms); $arr = false; } // this array contains the indices of the inserted objects $res = array(); $choices = array(); foreach ($forms as $key => $form) { $choices[] = $form->getChoices(); $forms[$key]->setChoices(null); } $resForms = array(); foreach ($forms as $form) { $method = 'POST'; $URL = '/form'; if ($form->getFormId() !== null) { $method = 'PUT'; $URL = '/form/' . $form->getFormId(); } $result = Request::routeRequest($method, $URL, array(), Form::encodeForm($form), $this->_form, 'form'); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299 && isset($result['content'])) { $newform = Form::decodeForm($result['content']); if ($form->getFormId() === null) { $form->setFormId($newform->getFormId()); } $resForms[] = $form; } else { $f = new Form(); $f->setStatus(409); $resForms[] = $f; } } $forms = $resForms; $i = 0; foreach ($choices as &$choicelist) { foreach ($choicelist as $key2 => $choice) { if ($forms[$i]->getFormId() !== null) { $formId = $forms[$i]->getFormId(); $choicelist[$key2]->setFormId($formId); $method = 'POST'; $URL = '/choice'; if ($choicelist[$key2]->getChoiceId() !== null) { $method = 'PUT'; $URL = '/choice/' . $choice->getChoiceId(); } $result = Request::routeRequest($method, $URL, array(), Choice::encodeChoice($choicelist[$key2]), $this->_choice, 'choice'); if ($result['status'] >= 200 && $result['status'] <= 299) { $newchoice = Choice::decodeChoice($result['content']); if ($choicelist[$key2]->getChoiceId() === null) { $choicelist[$key2]->setChoiceId($newchoice->getChoiceId()); } $choicelist[$key2]->setStatus(201); } else { $choicelist[$key2]->setStatus(409); } } } $forms[$i]->setChoices($choicelist); $i++; } // checks the correctness of the query /*if ( $result['status'] >= 200 && $result['status'] <= 299 && isset($result['content'])){ $newforms = Form::decodeForm($result['content']); if ( !is_array( $newforms ) ) $newforms = array($newforms); $i=0; foreach ($forms as &$form){ if ($form->getFormId() === null) $form->setFormId($newforms[$i]->getFormId()); $i++; } $sendChoices = array(); $i=0; foreach ( $choices as $choicelist ){ foreach ( $choicelist as $choice ){ $choice->setFormId($forms[$i]->getFormId()); $sendChoices[] = $choice; } $i++; } $choices = $sendChoices; $result = Request::routeRequest( 'POST', '/choice', $this->app->request->headers->all( ), Choice::encodeChoice($choices), $this->_choice, 'choice' ); // checks the correctness of the query if ( $result['status'] >= 200 && $result['status'] <= 299 ){ $newchoices = Choice::decodeChoice($result['content']); $choicelist = array(); $i=0; foreach ( $choices as &$choice ){ $choice->setChoiceId($newchoices[$i]->getChoiceId()); if (!isset($choicelist[$choice->getFormId()])) $choicelist[$choice->getFormId()] = array(); $choicelist[$choice->getFormId()][] = $choice; $i++; } foreach ( $forms as &$form ){ $form->setChoices($choicelist[$form->getFormId()]); } $res[] = $forms; } else{ // remove forms on failure foreach ($forms as $form){ $result = Request::routeRequest( 'DELETE', '/form/'.$form->getFormId(), $this->app->request->headers->all( ), '', $this->_form, 'form' ); } $res[] = null; $this->app->response->setStatus( 409 ); } } else { $res[] = null; $this->app->response->setStatus( 409 ); }*/ if ($this->app->response->getStatus() != 201) { Logger::Log('POST AddForms failed', LogLevel::ERROR); } if (!$arr && count($res) == 1) { $this->app->response->setBody(Form::encodeForm($res[0])); } else { $this->app->response->setBody(Form::encodeForm($res)); } }
function delete_choice($id) { $choice = new Choice($id); $choice->answer->delete(); $choice->delete(); }
$fileName = "file{$exerciseId}"; #region generate form-data $formdata = array(); if (isset($exercise['choices'])) { $formtext = $exercise['choices']; foreach ($formtext as $formId => $choiceData2) { $form = new Form(); $form->setFormId($formId); $form->setExerciseId($exerciseId); $choiceText = $choiceData2; $choices = array(); foreach ($choiceText as $tempKey => $choiceData) { if (trim($choiceData) === '') { continue; } $choice = new Choice(); $choice->SetText(htmlentities(htmlentities(htmlspecialchars_decode($choiceData)))); $choice->SetFormId($formId); $choices[] = $choice; } if ($choices !== null && $choices !== array()) { $form->setChoices($choices); $formdata[] = $form; } } } #endregion if (isset($_FILES[$fileName]) || $formdata !== array()) { $error = 0; if (isset($_FILES[$fileName])) { $file = $_FILES[$fileName];
public static function ExtractForm($data, $singleResult = false, $FormsExtension = '', $ChoiceExtension = '', $isResult = true) { // generates an assoc array of an forms by using a defined // list of its attributes $forms = DBJson::getObjectsByAttributes($data, Form::getDBPrimaryKey(), Form::getDBConvert(), $FormsExtension); // generates an assoc array of choices by using a defined // list of its attributes $choices = DBJson::getObjectsByAttributes($data, Choice::getDBPrimaryKey(), Choice::getDBConvert(), $ChoiceExtension); // concatenates the forms and the associated choices $res = DBJson::concatObjectListResult($data, $forms, Form::getDBPrimaryKey(), Form::getDBConvert()['FO_choices'], $choices, Choice::getDBPrimaryKey(), $ChoiceExtension, $FormsExtension); if ($isResult) { // to reindex $res = array_values($res); $res = Form::decodeForm($res, false); if ($singleResult) { // only one object as result if (count($res) > 0) { $res = $res[0]; } } } return $res; }
public static function ExtractChoice($data, $singleResult = false, $ChoiceExtension = '', $isResult = true) { // generates an assoc array of choices by using a defined list of // its attributes $res = DBJson::getResultObjectsByAttributes($data, Choice::getDBPrimaryKey(), Choice::getDBConvert(), $ChoiceExtension); if ($isResult) { // to reindex $res = array_values($res); $res = Choice::decodeChoice($res, false); if ($singleResult == true) { // only one object as result if (count($res) > 0) { $res = $res[0]; } } } return $res; }
public function get($functionName, $sqlFile, $preChoice, $preForm, $preExercise, $formid, $choiceid, $courseid, $esid, $eid, $singleResult = false, $checkSession = true) { $this->loadConfig($preChoice, $preForm, $preExercise); $preChoice = ($preChoice === '' ? '' : '_') . $preChoice; $preForm = ($preForm === '' ? '' : '_') . $preForm; $preExercise = ($preExercise === '' ? '' : '_') . $preExercise; Logger::Log('starts GET ' . $functionName, LogLevel::DEBUG); // checks whether incoming data has the correct data type $choiceid = DBJson::mysql_real_escape_string($choiceid); $formid = DBJson::mysql_real_escape_string($formid); $preChoice = DBJson::mysql_real_escape_string($preChoice); $preForm = DBJson::mysql_real_escape_string($preForm); $preExercise = DBJson::mysql_real_escape_string($preExercise); // starts a query, by using a given file $result = DBRequest::getRoutedSqlFile($this->query, $sqlFile, array('formid' => $formid, 'choiceid' => $choiceid, 'courseid' => $courseid, 'esid' => $esid, 'eid' => $eid, 'preChoice' => $preChoice, 'preForm' => $preForm, 'preExercise' => $preExercise), $checkSession); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299) { $query = Query::decodeQuery($result['content']); if (is_array($query)) { $query = $query[count($query) - 1]; } if ($query->getNumRows() > 0) { $res = Choice::ExtractChoice($query->getResponse(), $singleResult); $this->_app->response->setBody(Choice::encodeChoice($res)); $this->_app->response->setStatus(200); if (isset($result['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']); } $this->_app->stop(); } else { $result['status'] = 404; } } Logger::Log('GET ' . $functionName . ' failed', LogLevel::ERROR); $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409); $this->_app->response->setBody(Choice::encodeChoice(new Choice())); $this->_app->stop(); }
/** * Class constructor. * * @param array $options Element options * @param array $attr HTML attributes */ public function __construct(array $options = [], array $attr = []) { parent::__construct($options, $attr); $this->addClass('choicelist'); unset($this->attr['name'], $this->attr['multiple'], $this->attr['required']); }
public function edit($data, $field, $newdata, $olddata, $object) { $user = FW4_User::get_user(); if (isset($field['limit']) && $user['id'] != 0) { $dolimit = true; if (isset($field['limit_condition'])) { $dolimit = false; $invert = false; if (substr($field['limit_condition'], 0, 1) == '!') { $invert = true; $field['limit_condition'] = substr($field['limit_condition'], 1); } $limit_fields = explode('.', $field['limit_condition']); $limit_field = $user; foreach ($limit_fields as $current_field) { if (isset($limit_field[$current_field]) && $limit_field[$current_field]) { $limit_field = $limit_field[$current_field]; } else { if (isset($limit_field[$current_field])) { $limit_field = false; break; } else { $limit_field = true; break; } } } $dolimit = $invert ? !$limit_field : $limit_field; } if ($dolimit) { $limit_fields = explode('.', $field['limit']); $limit_field = $user; foreach ($limit_fields as $current_field) { if (isset($limit_field[$current_field])) { $limit_field = $limit_field[$current_field]; } else { $limit_field = false; break; } } $data[strval($field['name']) . '_id'] = $limit_field; return $data; } } $fieldname = strval($field['name']); if (isset($field['multiple'])) { if (!isset($newdata[strval($field['name'])])) { $newdata[strval($field['name'])] = array(); } if (isset($olddata->id)) { foreach (self::get_current_multiple_choices($object, $field, $olddata->id) as $row) { if (in_array($row->{$fieldname}, $newdata[$fieldname])) { unset($newdata[$fieldname][array_search($row->{$fieldname}, $newdata[strval($field['name'])])]); } else { where('id = %d', $row->id)->delete($object['stack'] . '/' . $field['name']); } } } self::$choice_ids_to_insert = $newdata[strval($field['name'])]; } else { if (isset($newdata[strval($field['name'])])) { if (isset($field['source'])) { $data[strval($field['name']) . '_id'] = $newdata[strval($field['name'])]; if (isset($field['parent_name'])) { $data[strval($field['parent_name']) . '_id'] = 0; if ($source_rows = self::get_source_rows(strval($field['source']), $field)) { if ($row = $source_rows->row_with_id($newdata[strval($field['name'])])) { $data[strval($field['parent_name']) . '_id'] = $row->parent()->id; } } } } else { $data[strval($field['name'])] = $newdata[strval($field['name'])]; } } } return $data; }
$mathTex = substr($solution, $pos + strlen($first), $pos2 - $pos - strlen($first)); $replace = false; $replace = texify('$' . $mathTex . '$'); if ($replace === false) { $replace = '<img src="http://latex.codecogs.com/gif.latex?' . rawurlencode($mathTex) . '">'; } $solution = substr($solution, 0, $pos) . $replace . substr($solution, $pos2 + strlen($second)); } $pos = strpos($solution, $first, $pos + strlen($first) + strlen($second)); } $formId = isset($subexercise['formId']) ? $subexercise['formId'] : null; $form = Form::createForm($formId, $exerciseId, $solution, $task, isset($subexercise['type']) ? $subexercise['type'] : null); $choiceText = $subexercise['choice']; $choices = array(); foreach ($choiceText as $tempKey => $choiceData) { $choice = new Choice(); $choice->SetText($choiceData); $choices[$tempKey] = $choice; } if (isset($subexercise['correct'])) { $choiceCorrect = $subexercise['correct']; foreach ($choiceCorrect as $tempKey => $choiceData) { $keykey = $choiceData; if ($keykey == '') { $keykey = $tempKey; } if (isset($choices[$keykey])) { $choices[$keykey]->setCorrect(1); } } }