123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <?php
- /* For licensing terms, see /license.txt */
- use ChamiloSession as Session;
- /**
- * UniqueAnswerImage.
- *
- * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
- */
- class UniqueAnswerImage extends UniqueAnswer
- {
- public static $typePicture = 'uaimg.png';
- public static $explanationLangVar = 'UniqueAnswerImage';
- /**
- * UniqueAnswerImage constructor.
- */
- public function __construct()
- {
- parent::__construct();
- $this->type = UNIQUE_ANSWER_IMAGE;
- $this->isContent = $this->getIsContent();
- }
- /**
- * {@inheritdoc}
- *
- * @throws Exception
- */
- public function createAnswersForm($form)
- {
- $objExercise = Session::read('objExercise');
- $editorConfig = [
- 'ToolbarSet' => 'TestProposedAnswer',
- 'Width' => '100%',
- 'Height' => '125',
- ];
- //this line defines how many questions by default appear when creating a choice question
- // The previous default value was 2. See task #1759.
- $numberAnswers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
- $numberAnswers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
- $feedbackTitle = '';
- if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
- //Scenario
- $commentTitle = '<th>'.get_lang('Comment').'</th>';
- $feedbackTitle = '<th>'.get_lang('Scenario').'</th>';
- } else {
- $commentTitle = '<th >'.get_lang('Comment').'</th>';
- }
- $html = '<div class="alert alert-success" role="alert">'.
- get_lang('UniqueAnswerImagePreferredSize200x150').'</div>';
- $html .= '<table class="table table-striped table-hover">
- <thead>
- <tr style="text-align: center;">
- <th width="10">'.get_lang('Number').'</th>
- <th>'.get_lang('True').'</th>
- <th>'.get_lang('Answer').'</th>
- '.$commentTitle.'
- '.$feedbackTitle.'
- <th width="15">'.get_lang('Weighting').'</th>
- </tr>
- </thead>
- <tbody>';
- $form->addHeader(get_lang('Answers'));
- $form->addHtml($html);
- $defaults = [];
- $correct = 0;
- if (!empty($this->id)) {
- $answer = new Answer($this->id);
- $answer->read();
- if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
- $numberAnswers = $answer->nbrAnswers;
- }
- }
- $form->addElement('hidden', 'nb_answers');
- //Feedback SELECT
- $questionList = $objExercise->selectQuestionList();
- $selectQuestion = [];
- $selectQuestion[0] = get_lang('SelectTargetQuestion');
- if (is_array($questionList)) {
- foreach ($questionList as $key => $questionid) {
- //To avoid warning messages
- if (!is_numeric($questionid)) {
- continue;
- }
- $question = Question::read($questionid);
- $questionTitle = strip_tags($question->selectTitle());
- $selectQuestion[$questionid] = "Q$key: $questionTitle";
- }
- }
- $selectQuestion[-1] = get_lang('ExitTest');
- $list = new LearnpathList(api_get_user_id());
- $flatList = $list->get_flat_list();
- $selectLpId = [];
- $selectLpId[0] = get_lang('SelectTargetLP');
- foreach ($flatList as $id => $details) {
- $selectLpId[$id] = cut($details['lp_name'], 20);
- }
- $tempScenario = [];
- if ($numberAnswers < 1) {
- $numberAnswers = 1;
- echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
- }
- for ($i = 1; $i <= $numberAnswers; $i++) {
- $form->addHtml('<tr>');
- if (isset($answer) && is_object($answer)) {
- if ($answer->correct[$i]) {
- $correct = $i;
- }
- $defaults['answer['.$i.']'] = $answer->answer[$i];
- $defaults['comment['.$i.']'] = $answer->comment[$i];
- $defaults['weighting['.$i.']'] = float_format(
- $answer->weighting[$i],
- 1
- );
- $itemList = explode('@@', $answer->destination[$i]);
- $try = $itemList[0];
- $lp = $itemList[1];
- $listDestination = $itemList[2];
- $url = $itemList[3];
- $tryResult = 0;
- if ($try != 0) {
- $tryResult = 1;
- }
- $urlResult = '';
- if ($url != 0) {
- $urlResult = $url;
- }
- $tempScenario['url'.$i] = $urlResult;
- $tempScenario['try'.$i] = $tryResult;
- $tempScenario['lp'.$i] = $lp;
- $tempScenario['destination'.$i] = $listDestination;
- } else {
- $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
- $defaults['weighting[1]'] = 10;
- $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
- $defaults['weighting[2]'] = 0;
- $tempScenario['destination'.$i] = ['0'];
- $tempScenario['lp'.$i] = ['0'];
- }
- $defaults['scenario'] = $tempScenario;
- $renderer = $form->defaultRenderer();
- $renderer->setElementTemplate(
- '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
- 'correct'
- );
- $renderer->setElementTemplate(
- '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
- 'counter['.$i.']'
- );
- $renderer->setElementTemplate(
- '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
- 'answer['.$i.']'
- );
- $renderer->setElementTemplate(
- '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
- 'comment['.$i.']'
- );
- $renderer->setElementTemplate(
- '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
- 'weighting['.$i.']'
- );
- $answerNumber = $form->addElement('text', 'counter['.$i.']', null, ' value = "'.$i.'"');
- $answerNumber->freeze();
- $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox"');
- $form->addHtmlEditor('answer['.$i.']', null, null, false, $editorConfig);
- $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
- if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
- $form->addHtmlEditor(
- 'comment['.$i.']',
- null,
- null,
- false,
- $editorConfig
- );
- // Direct feedback
- // Adding extra feedback fields
- $group = [];
- $group['try'.$i] = $form->createElement('checkbox', 'try'.$i, null, get_lang('TryAgain'));
- $group['lp'.$i] = $form->createElement(
- 'select',
- 'lp'.$i,
- get_lang('SeeTheory').': ',
- $selectLpId
- );
- $group['destination'.$i] = $form->createElement(
- 'select',
- 'destination'.$i,
- get_lang('GoToQuestion').': ',
- $selectQuestion
- );
- $group['url'.$i] = $form->createElement(
- 'text',
- 'url'.$i,
- get_lang('Other').': ',
- [
- 'class' => 'col-md-2',
- 'placeholder' => get_lang('Other'),
- ]
- );
- $form->addGroup($group, 'scenario');
- $renderer->setElementTemplate(
- '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
- 'scenario'
- );
- } else {
- $form->addHtmlEditor('comment['.$i.']', null, null, false, $editorConfig);
- }
- $form->addText('weighting['.$i.']', null, null, ['class' => "col-md-1", 'value' => '0']);
- $form->addHtml('</tr>');
- }
- $form->addHtml('</tbody>');
- $form->addHtml('</table>');
- global $text;
- $buttonGroup = [];
- if ($objExercise->edit_exercise_in_lp == true ||
- (empty($this->exerciseList) && empty($objExercise->id))
- ) {
- //setting the save button here and not in the question class.php
- $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
- $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
- $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
- $form->addGroup($buttonGroup);
- }
- // We check the first radio button to be sure a radio button will be check
- if ($correct == 0) {
- $correct = 1;
- }
- $defaults['correct'] = $correct;
- if (!empty($this->id)) {
- $form->setDefaults($defaults);
- } else {
- if ($this->isContent == 1) {
- // Default sample content.
- $form->setDefaults($defaults);
- } else {
- $form->setDefaults(['correct' => 1]);
- }
- }
- $form->setConstants(['nb_answers' => $numberAnswers]);
- }
- /**
- * {@inheritdoc}
- */
- public function processAnswersCreation($form, $exercise)
- {
- $questionWeighting = $nbrGoodAnswers = 0;
- $correct = $form->getSubmitValue('correct');
- $objAnswer = new Answer($this->id);
- $numberAnswers = $form->getSubmitValue('nb_answers');
- for ($i = 1; $i <= $numberAnswers; $i++) {
- $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
- $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
- $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
- $scenario = $form->getSubmitValue('scenario');
- //$listDestination = $form -> getSubmitValue('destination'.$i);
- //$destinationStr = $form -> getSubmitValue('destination'.$i);
- $try = $scenario['try'.$i];
- $lp = $scenario['lp'.$i];
- $destination = $scenario['destination'.$i];
- $url = trim($scenario['url'.$i]);
- /*
- How we are going to parse the destination value
- here we parse the destination value which is a string
- 1@@3@@2;4;4;@@http://www.chamilo.org
- where: try_again@@lp_id@@selected_questions@@url
- try_again = is 1 || 0
- lp_id = id of a learning path (0 if dont select)
- selected_questions= ids of questions
- url= an url
- $destinationStr='';
- foreach ($listDestination as $destination_id)
- {
- $destinationStr.=$destination_id.';';
- } */
- $goodAnswer = $correct == $i ? true : false;
- if ($goodAnswer) {
- $nbrGoodAnswers++;
- $weighting = abs($weighting);
- if ($weighting > 0) {
- $questionWeighting += $weighting;
- }
- }
- if (empty($try)) {
- $try = 0;
- }
- if (empty($lp)) {
- $lp = 0;
- }
- if (empty($destination)) {
- $destination = 0;
- }
- if ($url == '') {
- $url = 0;
- }
- //1@@1;2;@@2;4;4;@@http://www.chamilo.org
- $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
- $objAnswer->createAnswer(
- $answer,
- $goodAnswer,
- $comment,
- $weighting,
- $i,
- null,
- null,
- $dest
- );
- }
- // saves the answers into the data base
- $objAnswer->save();
- // sets the total weighting of the question
- $this->updateWeighting($questionWeighting);
- $this->save($exercise);
- }
- /**
- * @param Exercise $exercise
- * @param null $counter
- * @param null $score
- *
- * @return string
- */
- public function return_header($exercise, $counter = null, $score = null)
- {
- if ($exercise->showExpectedChoice()) {
- $header = '<table class="'.$this->question_table_class.'">
- <tr>
- <th>'.get_lang('Choice').'</th>
- <th>'.get_lang('ExpectedChoice').'</th>
- <th>'.get_lang('Answer').'</th>';
- $header .= '<th>'.get_lang('Status').'</th>';
- $header .= '<th>'.get_lang('Comment').'</th>';
- $header .= '</tr>';
- } else {
- $header = parent::return_header($exercise, $counter, $score);
- }
- return $header;
- }
- }
|