Fixing draggable question

skala
Julio Montoya 13 years ago
parent aaa286944a
commit 9bba9105ce
  1. 4
      main/exercice/answer.class.php
  2. 19
      main/exercice/draggable.class.php
  3. 32
      main/exercice/exercise.class.php
  4. 45
      main/exercice/exercise.lib.php
  5. 88
      main/exercice/exercise_submit.php
  6. 6
      main/exercice/question_create.php
  7. 10
      main/inc/ajax/exercise.ajax.php

@ -1,8 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
/**
* This class allows to instantiate an object of type Answer
* 5 arrays are created to receive the attributes of each answer belonging to a specified question
* This class allows to instantiate an object of type Answer
* 5 arrays are created to receive the attributes of each answer belonging to a specified question
* @package chamilo.exercise
* @author Olivier Brouckaert
* @version $Id: answer.class.php 21172 2009-06-01 20:58:05Z darkvela $

@ -39,7 +39,6 @@ class Draggable extends Matching
public function createAnswersForm($form)
{
$defaults = array();
$navigator_info = api_get_navigator();
$nb_matches = $nb_options = 2;
if ($form->isSubmitted()) {
@ -64,14 +63,15 @@ class Draggable extends Matching
$answer = new Answer($this->id);
$answer->read();
if (count($answer->nbrAnswers) > 0) {
$a_matches = $a_options = array();
$nb_matches = $nb_options = 0;
for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
if ($answer->isCorrect($i)) {
//for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
foreach ($answer->answer as $answerId => $answer_item) {
//$answer_id = $answer->getRealAnswerIdFromList($answerId);
if ($answer->isCorrect($answerId)) {
$nb_matches++;
$defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i);
$defaults['weighting['.$nb_matches.']'] = Text::float_format($answer->selectWeighting($i), 1);
$defaults['matches['.$nb_matches.']'] = $answer->correct[$i];//$nb_matches;
$defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($answerId);
$defaults['weighting['.$nb_matches.']'] = Text::float_format($answer->selectWeighting($answerId), 1);
$defaults['matches['.$nb_matches.']'] = $answer->correct[$answerId];//$nb_matches;
} else {
$nb_options++;
$defaults['option['.$nb_options.']'] = $nb_options;
@ -158,12 +158,8 @@ class Draggable extends Matching
'class="btn minus"'
);
global $text, $class;
$group[] = $form->createElement('style_submit_button', 'submitQuestion', $text, 'class="'.$class.'"');
$form->addGroup($group);
// DISPLAY OPTIONS
@ -199,7 +195,6 @@ class Draggable extends Matching
$form->addElement('html', '</table></div></div>');
if (!empty($this->id)) {
$form->setDefaults($defaults);
} else {

@ -1932,14 +1932,14 @@ class Exercise
$table_track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
$table_track_e_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$sql_select = "SELECT exe_id FROM $table_track_e_exercises
$sql = "SELECT exe_id FROM $table_track_e_exercises
WHERE c_id = '".api_get_course_int_id()."' AND
exe_exo_id = ".$this->id." AND
orig_lp_id = 0 AND
orig_lp_item_id = 0 AND
session_id = ".api_get_session_id()."";
$result = Database::query($sql_select);
$result = Database::query($sql);
$exe_list = Database::store_result($result);
//deleting TRACK_E_ATTEMPT table
@ -3129,20 +3129,24 @@ class Exercise
$i_answer_id = $a_answers['iid']; //3
$s_answer_label = $a_answers['answer']; // your daddy - your mother
$i_answer_correct_answer = $a_answers['correct']; //1 - 2
//$i_answer_id_auto = $a_answers['id_auto']; // 3 - 4
$sql_user_answer = "SELECT answer FROM $TBL_TRACK_ATTEMPT
WHERE exe_id = '$exeId' AND question_id = '$questionId' AND position = '$i_answer_id'";
WHERE exe_id = '$exeId' AND
question_id = '$questionId' AND
position = '$i_answer_id'";
$res_user_answer = Database::query($sql_user_answer);
if (Database::num_rows($res_user_answer) > 0) {
if (Database::num_rows($res_user_answer)) {
// Rich - good looking
$s_user_answer = Database::result($res_user_answer, 0, 0);
$result = Database::fetch_array($res_user_answer, 'ASSOC');
$s_user_answer = $result['answer'];
} else {
$s_user_answer = 0;
}
$i_answerWeighting = $objAnswerTmp->selectWeighting($i_answer_id);
$user_answer = '';
if (!empty($s_user_answer)) {
if ($s_user_answer == $i_answer_correct_answer) {
@ -3179,17 +3183,19 @@ class Exercise
}
break(2); //break the switch and the "for" condition
} else {
//$numAnswer = $objAnswerTmp->selectAutoId($answerId);
$numAnswer = $answerId;
if ($answerCorrect) {
$matchingKey = $choice[$numAnswer];
if ($answerType == DRAGGABLE) {
$matchingKey = $numAnswer;
}
if ($answerCorrect == $choice[$numAnswer]) {
$questionScore += $answerWeighting;
$totalScore += $answerWeighting;
$user_answer = '<span>'.$answer_matching[$choice[$numAnswer]].'</span>';
$user_answer = '<span>'.$answer_matching[$matchingKey].'</span>';
} else {
if ($choice[$numAnswer]) {
$user_answer = '<span style="color: #FF0000; text-decoration: line-through;">'.$answer_matching[$choice[$numAnswer]].'</span>';
$user_answer = '<span style="color: #FF0000; text-decoration: line-through;">'.$answer_matching[$matchingKey].'</span>';
}
}
$matching[$numAnswer] = $choice[$numAnswer];
@ -3215,7 +3221,11 @@ class Exercise
}
}
} else {
$studentChoice = $choice[$answerId];
if (isset($choice[$answerId])) {
$studentChoice = $choice[$answerId];
} else {
$studentChoice = false;
}
if ($studentChoice) {
$questionScore += $answerWeighting;
$totalScore += $answerWeighting;

@ -43,6 +43,8 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
$answerType = $objQuestionTmp->selectType();
$pictureName = $objQuestionTmp->selectPicture();
$s = null;
if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
// Question is not a hotspot
@ -62,8 +64,6 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
}
echo '<div class="question_options">';
$s = '';
// construction of the Answer object (also gets all answers details)
$objAnswerTmp = new Answer($questionId);
@ -474,7 +474,7 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
$s .= '<select id="window_'.$windowId.'_select" name="choice['.$questionId.']['.$numAnswer.']">';
$selectedValue = 0;
// fills the list-box
foreach ($select_items as $key => $val) {
foreach ($select_items as $val) {
// set $debug_mark_answer to true at function start to
// show the correct answer with a suffix '-x'
$selected = '';
@ -540,17 +540,14 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
} elseif ($answerType == DRAGGABLE) {
// matching type, showing suggestions and answers
// TODO: replace $answerId by $numAnswer
if ($answerId == 1) {
//echo $objAnswerTmp->getJs();
//$s .= '<tr>';
}
if ($answerCorrect != 0) {
// only show elements to be answered (not the contents of
// the select boxes, who are correct = 0)
$s .= '<td>';
$parsed_answer = $answer;
$windowId = $questionId.'_'.$lines_count;
$windowId = $questionId.'_'.$numAnswer; //67_293 - 67_294
//left part questions
$s .= '<li class="ui-state-default" id="'.$windowId.'">';
$s .= ' <div id="window_'.$windowId.'" class="window'.$questionId.'_question_draggable question_draggable">
@ -558,11 +555,11 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
</div>';
$s .= '<div style="display:none">';
$s .= '<select id="window_'.$windowId.'_select" name="choice['.$questionId.']['.$numAnswer.']" class="select_option">';
$selectedValue = 0;
// fills the list-box
foreach ($select_items as $key => $val) {
$item = 0;
foreach ($select_items as $val) {
// set $debug_mark_answer to true at function start to
// show the correct answer with a suffix '-x'
$selected = '';
@ -576,8 +573,11 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
$selected = 'selected="selected"';
$selectedValue = $val['id'];
}
$s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
//$s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
$s .= '<option value="'.$item.'" '.$selected.'>'.$val['letter'].'</option>';
$item++;
}
$s .= '</select>';
if (!empty($answerCorrect) && !empty($selectedValue)) {
$s.= '<script>
@ -586,7 +586,7 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
});
</script>';
}
$s .= '</select>';
if (isset($select_items[$lines_count])) {
$s.= '<div id="window_'.$windowId.'_answer" class="">
@ -595,8 +595,6 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
} else {
$s.='&nbsp;';
}
//$s .= '</td>';
$lines_count++;
//if the left side of the "matching" has been completely
// shown but the right side still has values to show...
@ -614,14 +612,12 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
$s .= '</li>';
}
}
//$s.="</tr>";
} // end for()
if ($show_comment) {
$s .= '</table>';
} else {
if ( $answerType == MATCHING || $answerType == UNIQUE_ANSWER_NO_OPTION || $answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
if ($answerType == MATCHING || $answerType == UNIQUE_ANSWER_NO_OPTION || $answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
$answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
$s .= '</table>';
}
@ -631,17 +627,15 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
$s .= '</ul><div class="clear"></div>';
$counterAnswer = 1;
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
foreach ($objAnswerTmp->answer as $answerId => $answer_item) {
//for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
$windowId = $questionId.'_'.$counterAnswer;
if ($answerCorrect) {
if ($answerCorrect == 0) {
$s .= '<div id="drop_'.$windowId.'" class="droppable ui-state-default">'.$counterAnswer.'</div>';
//$s .= '<li id="drop_'.$windowId.'" class="droppable ui-state-default">'.$counterAnswer.'</li>';
$counterAnswer++;
}
}
//$s .= '<ul>';
//$s .= '</div>';
}
if ($answerType == MATCHING) {
@ -689,9 +683,10 @@ function showQuestion($objQuestionTmp, $only_questions = false, $origin = false,
// get answers of hotpost
$answers_hotspot = array();
for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
$answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
$answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
//for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
foreach ($objAnswerTmp->answer as $answerId => $answer_item) {
//$answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
$answers_hotspot[$answerId] = $objAnswerTmp->selectAnswer($answerId);
}
// display answers of hotpost order by id

@ -67,48 +67,43 @@ $htmlHeadXtra[]= api_get_js('epiclock/javascript/jquery.epiclock.min.js');
$htmlHeadXtra[]= api_get_js('epiclock/renderers/minute/epiclock.minute.js');
$htmlHeadXtra[] = api_get_js('d3/jquery.xcolor.js');
$htmlHeadXtra[]= '
<script>
$htmlHeadXtra[]= '<script>
var recycle_icon = "<a href=\'#\' class=\'ui-icon ui-icon-refresh\'>Recycle image</a>";
var trash_icon = "<a href=\'#\' class=\'ui-icon ui-icon-trash\'>Delete image</a>";
function deleteItem($item, $insertHere) {
if ($insertHere.find(".question_draggable").length > 0) {
return false;
}
$item.fadeOut(function() {
/*var $list = $( "ul", $trash ).length ?
$( "ul", $trash ) :
$( "<ul class=\'gallery ui-helper-reset\'/>" ).appendTo( $trash );*/
$list = $( "<div class=\'gallery ui-helper-reset\'/>" ).appendTo($insertHere);
$item.find( "a.ui-icon-trash" ).remove();
var droppedId = $item.attr("id");
var dropedOnId = $insertHere.attr("id");
if ($insertHere.find(".question_draggable").length > 0) {
return false;
}
var originSelectId = "window_"+droppedId+"_select";
$item.fadeOut(function() {
/*var $list = $( "ul", $trash ).length ?
$( "ul", $trash ) :
$( "<ul class=\'gallery ui-helper-reset\'/>" ).appendTo( $trash );*/
value = dropedOnId.split("_")[2];
$list = $( "<div class=\'gallery ui-helper-reset\'/>" ).appendTo($insertHere);
$item.find( "a.ui-icon-trash" ).remove();
//console.log(originSelectId); console.log(value);
var droppedId = $item.attr("id");
var dropedOnId = $insertHere.attr("id");
var originSelectId = "window_"+droppedId+"_select";
console.log(originSelectId);
console.log(dropedOnId);
$("#"+originSelectId +" option").filter(function() {
return $(this).val() == value;
}).attr("selected", true);
value = dropedOnId.split("_")[2];
console.log(value);
//console.log(droppedId);
//console.log(dropedOnId);
$("#"+originSelectId +" option").filter(function() {
return $(this).val() == value;
}).attr("selected", true);
$item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
$item.append( recycle_icon ).appendTo( $list ).fadeIn(function() {
//$item.animate({ width: "48px" }).find( "img" ).animate({ height: "36px" });
});
});
}
});
});
}
// Draggable js script in order to use jsplumb
var oldPositionArray;
@ -158,8 +153,6 @@ $(function() {
}
});
function recycleItem( $item, droppedOn) {
//console.log("recycleItem");
$item.fadeOut(function() {
@ -180,8 +173,6 @@ $(function() {
$("#"+originSelectId+" option:first").attr("selected","selected");
}
$( "ul.drag_question > li" ).click(function( event ) {
var $item = $( this ),
$target = $( event.target );
@ -192,37 +183,9 @@ $(function() {
} else if ( $target.is( "a.ui-icon-refresh" ) ) {
recycleItem( $item );
}
return false;
});
/*$( ".drag_question" ).sortable({
start: function(e, ui) {
// Getting old positions into array before sorting
oldPositionArray = $(this).sortable("toArray");
ui.item.startPos = ui.item.index();
},
stop: function( event, ui ) {
$(this).data("new_position", $(this).sortable("toArray"));
var newPositionArray = $(this).sortable("toArray");
var oldPosition = ui.item.startPos;
var newPosition = ui.item.index();
var oldId = "window_"+newPositionArray[oldPosition]+"_select";
var newId = "window_"+newPositionArray[newPosition]+"_select";
var counter = 0;
$.each(newPositionArray, function(index, value){
newIdValue = "window_"+value+"_select";
$("#"+newIdValue +" option").filter(function() {
return $(this).val() == counter+1;
}).attr("selected", true);
counter++;
});
}
});*/
});
// Matching js script in order to use jsplumb
@ -315,8 +278,6 @@ var connectorType = "Straight";
jsPlumb.addEndpoint(windowDestinationId+"_answer", { anchor:[ "LeftMiddle","LeftMiddle","LeftMiddle","LeftMiddle" ] }, destinationEndPoint);
});
});
//var divsWithWindowClass = jsPlumb.CurrentLibrary.getSelector("#"+questionId+" .window");
//jsPlumb.draggable(divsWithWindowClass);
jsPlumbDemo.attachBehaviour();
@ -440,7 +401,6 @@ if (!isset($objExercise) && isset($_SESSION['objExercise'])) {
$objExercise = $_SESSION['objExercise'];
}
//3. $objExercise is not set, then return to the exercise list
if (!is_object($objExercise)) {
if ($debug) {error_log('3. $objExercise was not set, kill the script'); };

@ -48,11 +48,13 @@ $session_id = api_get_session_id();
$tbl_exercices = Database :: get_course_table(TABLE_QUIZ_TEST);
$course_id = api_get_course_int_id();
$sql = "SELECT id,title,type,description, results_disabled FROM $tbl_exercices WHERE c_id = $course_id AND active<>'-1' AND session_id=".$session_id." ORDER BY title ASC";
$sql = "SELECT iid,title,type,description, results_disabled
FROM $tbl_exercices WHERE c_id = $course_id AND active<>'-1' AND session_id=".$session_id."
ORDER BY title ASC";
$result = Database::query($sql);
$exercises['-'] = '-'.get_lang('SelectExercice').'-';
while ($row = Database :: fetch_array($result)) {
$exercises[$row['id']] = Text::cut($row['title'], EXERCISE_MAX_NAME_SIZE);
$exercises[$row['iid']] = Text::cut($row['title'], EXERCISE_MAX_NAME_SIZE);
}
$form->addElement('select', 'exercice', get_lang('Exercice'), $exercises);

@ -254,6 +254,7 @@ switch ($action) {
//Question info
$question_id = intval($_REQUEST['question_id']);
//$question_list = $_SESSION['questionList'];
$question_list = $_SESSION['question_list_flatten'];
@ -331,6 +332,7 @@ switch ($action) {
if ($debug) error_log("Looping question list".print_r($question_list, 1));
if ($debug) error_log("Trying to save question: $question_id ");
foreach ($question_list as $my_question_id) {
if ($type == 'simple' && $question_id != $my_question_id) {
@ -357,7 +359,7 @@ switch ($action) {
//this variable commes from exercise_submit_modal.php
$hotspot_delineation_result = null;
if (isset($_SESSION['hotspot_delineation_result']) && isset($_SESSION['hotspot_delineation_result'][$objExercise->selectId()])) {
$hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
$hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
}
if ($type == 'simple') {
@ -376,12 +378,12 @@ switch ($action) {
delete_attempt_hotspot($exe_id, api_get_user_id(), $course_id, $my_question_id);
}
if (isset($attempt_list[$my_question_id]) && isset($attempt_list[$my_question_id]['marks'])) {
$total_score -= $attempt_list[$my_question_id]['marks'];
}
$total_score -= $attempt_list[$my_question_id]['marks'];
}
}
// We're inside *one* question. Go through each possible answer for this question
$result = $objExercise->manage_answer($exe_id, $my_question_id, $my_choice, 'exercise_result', $hot_spot_coordinates, true, false, false, $objExercise->selectPropagateNeg(), $hotspot_delineation_result, true);
//Adding the new score

Loading…
Cancel
Save