Project

General

Profile

Feature #4842 ยป exercise_result.php

Yoselyn Castillo, 21/06/2012 22:46

 
1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
*	Exercise result
5
*	This script gets informations from the script "exercise_submit.php",
6
*	through the session, and calculates the score of the student for
7
*	that exercise.
8
*	Then it shows the results on the screen.
9
*	@package chamilo.exercise
10
*	@author Olivier Brouckaert, main author
11
*	@author Roan Embrechts, some refactoring
12
* 	@author Julio Montoya Armas switchable fill in blank option added
13
*
14
*	@todo	split more code up in functions, move functions to library?
15
*/
16
/**
17
 * Code
18
 */
19

    
20
use \ChamiloSession as Session;
21

    
22
require_once 'exercise.class.php';
23
require_once 'question.class.php';
24
require_once 'answer.class.php';
25

    
26
// Name of the language file that needs to be included
27
$language_file = 'exercice';
28

    
29
require_once '../inc/global.inc.php';
30
require_once 'exercise.lib.php';
31

    
32
if ($_GET['origin']=='learnpath') {
33
	require_once '../newscorm/learnpath.class.php';
34
	require_once '../newscorm/learnpathItem.class.php';
35
	require_once '../newscorm/scorm.class.php';
36
	require_once '../newscorm/scormItem.class.php';
37
	require_once '../newscorm/aicc.class.php';
38
	require_once '../newscorm/aiccItem.class.php';
39
}
40
require_once api_get_path(LIBRARY_PATH).'exercise_show_functions.lib.php';
41
require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
42

    
43
$this_section = SECTION_COURSES;
44

    
45
/* 	ACCESS RIGHTS  */
46
api_protect_course_script(true);
47

    
48
if ($debug){ error_log('Entering exercise_result.php: '.print_r($_POST,1));}
49

    
50
// general parameters passed via POST/GET
51
if ( empty ( $origin ) ) {                  $origin                 = Security::remove_XSS($_REQUEST['origin']);}
52
if ( empty ( $objExercise ) ) {             $objExercise            = $_SESSION['objExercise'];}
53
if ( empty ( $remind_list ) ) {             $remind_list            = $_REQUEST['remind_list'];}
54

    
55
$exe_id = isset($_REQUEST['exe_id']) ? intval($_REQUEST['exe_id']) : 0;
56

    
57
if (empty($objExercise)) {
58
	//Redirect to the exercise overview
59
	//Check if the exe_id exists
60
	$objExercise = new Exercise();
61
	$exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
62
	if (!empty($exercise_stat_info) && isset($exercise_stat_info['exe_exo_id'])) {
63
		header("Location: overview.php?exerciseId=".$exercise_stat_info['exe_exo_id']);
64
		exit;
65
	}
66
    api_not_allowed();
67
}
68

    
69
$gradebook = '';
70
if (isset($_SESSION['gradebook'])) {
71
	$gradebook=	$_SESSION['gradebook'];
72
}
73
if (!empty($gradebook) && $gradebook=='view') {
74
	$interbreadcrumb[]= array ('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('ToolGradebook'));
75
}
76

    
77
$nameTools = get_lang('Exercice');
78

    
79
$interbreadcrumb[]= array("url" => "exercice.php?gradebook=$gradebook","name" => get_lang('Exercices'));
80

    
81
if ($origin != 'learnpath') {
82
	//so we are not in learnpath tool
83
	Display::display_header($nameTools,get_lang('Exercise'));
84
} else {
85
    Display::display_reduced_header();
86
}
87

    
88
//Hide results
89
$show_results     = false;
90
$show_only_score  = false;
91

    
92
if ($objExercise->results_disabled == 0) {
93
    $show_results = true;
94
}
95

    
96
if ($objExercise->results_disabled == 2) {
97
    $show_only_score = true;
98
}
99

    
100
/* DISPLAY AND MAIN PROCESS */
101

    
102
// I'm in a preview mode as course admin. Display the action menu.
103
if (api_is_course_admin() && $origin != 'learnpath') {
104
	echo '<div class="actions">';
105
	echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'), array(), 32).'</a>';
106
	echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.Display::return_icon('edit.png', get_lang('ModifyExercise'), array(), 32).'</a>';
107
	echo '</div>';
108
}
109

    
110
$feedback_type           = $objExercise->feedback_type;
111
$exercise_stat_info      = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
112

    
113
if (!empty($exercise_stat_info['data_tracking'])) {
114
	$question_list		= explode(',', $exercise_stat_info['data_tracking']);
115
}
116

    
117
$safe_lp_id              = $exercise_stat_info['orig_lp_id'];
118
$safe_lp_item_id         = $exercise_stat_info['orig_lp_item_id'];
119
$safe_lp_item_view_id    = $exercise_stat_info['orig_lp_item_view_id'];
120

    
121
if ($origin == 'learnpath') {
122
?>
123
	<form method="get" action="exercice.php?<?php echo api_get_cidreq() ?>">
124
	<input type="hidden" name="origin" 					value="<?php echo $origin; ?>" />
125
    <input type="hidden" name="learnpath_id" 			value="<?php echo $safe_lp_id; ?>" />
126
    <input type="hidden" name="learnpath_item_id" 		value="<?php echo $safe_lp_item_id; ?>" />
127
    <input type="hidden" name="learnpath_item_view_id"  value="<?php echo $safe_lp_item_view_id; ?>" />
128
<?php
129
}
130

    
131
$i = $total_score = $total_weight = 0;
132

    
133
//We check if the user attempts before sending to the exercise_result.php
134
if ($objExercise->selectAttempts() > 0) {
135
    $attempt_count = get_attempt_count(api_get_user_id(), $objExercise->id, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id);
136
    if ($attempt_count >= $objExercise->selectAttempts()) {
137
        Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $objExercise->selectTitle(), $objExercise->selectAttempts()), false);
138
        if ($origin != 'learnpath') {
139
            //we are not in learnpath tool
140
            Display::display_footer();
141
        }
142
        exit;
143
    }
144
}
145

    
146

    
147
$user_info   = api_get_user_info(api_get_user_id());
148
if ($show_results || $show_only_score) {
149
    echo $exercise_header = $objExercise->show_exercise_result_header(api_get_person_name($user_info['firstName'], $user_info['lastName']));
150
}
151

    
152
Display :: display_normal_message(get_lang('Saved').'<br />',false);
153

    
154
// Display text when test is finished #4074
155
// Don't display the text when finished message if we are from a LP #4227
156
// but display it from page exercice_show.php
157
$end_of_message = $objExercise->selectTextWhenFinished();
158
if (!empty($end_of_message) && ($origin != 'learnpath')) {
159
    Display::display_normal_message($end_of_message, false);
160
    echo "<div class='clear'>&nbsp;</div>";
161
}
162

    
163
$counter = 1;
164
$total_score = $total_weight = 0;
165

    
166
// Loop over all question to show results for each of them, one by one
167
if (!empty($question_list)) {
168
    if ($debug) { error_log('Looping question_list '.print_r($question_list,1));}
169
	foreach ($question_list as $questionId) {
170
	    // destruction of the Question object
171
		unset($objQuestionTmp);
172

    
173
		// gets the student choice for this question
174
		$choice                = $exerciseResult[$questionId];
175

    
176
		// creates a temporary Question object
177
		$objQuestionTmp        = Question :: read($questionId);
178

    
179
		//this variable commes from exercise_submit_modal.php
180

    
181
		//$hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$quesId];
182

    
183
		if ($show_results) {
184
		    // show category
185
		    Testcategory::displayCategoryAndTitle($objQuestionTmp->id);
186
	    	// show titles
187
	    	echo $objQuestionTmp->return_header($objExercise->feedback_type, $counter);
188
	    	$counter++;
189
		}
190

    
191
	    // We're inside *one* question. Go through each possible answer for this question
192
	    $result = $objExercise->manage_answer($exercise_stat_info['exe_id'], $questionId, null ,'exercise_result', array(), false, true, $show_results, $objExercise->selectPropagateNeg(), $hotspot_delineation_result);
193

    
194
	    $total_score     += $result['score'];
195
	    $total_weight    += $result['weight'];
196
	} // end foreach() block that loops over all questions
197
}
198

    
199
if ($origin != 'learnpath') {
200
    if ($show_results || $show_only_score) {
201
        echo '<div id="question_score">';
202
        echo get_lang('YourTotalScore')." ";
203
        if ($objExercise->selectPropagateNeg() == 0 && $total_score < 0) {
204
    	    $total_score = 0;
205
        }
206
        echo show_score($total_score, $total_weight, false, true, true, $objExercise->selectPassPercentage());
207
        echo '</div>';
208
    }
209
}
210

    
211
// Tracking of results
212

    
213
//	Updates the empty exercise
214

    
215
$quiz_duration = 0;
216
$end_date = api_get_utc_datetime();
217
if (isset($_SESSION['quizStartTime']) && !empty($_SESSION['quizStartTime'])) {
218
    if (isset($_SESSION['quizStartTime'][$objExercise->selectId()])) {
219
        $start_date = $_SESSION['quizStartTime'][$objExercise->selectId()];
220
        $end_date_datetime = new DateTime($end_date);
221
        $start_date_date_time = new DateTime($start_date);
222
        $interval = $end_date_datetime->diff($start_date_date_time);
223
        $hours   = $interval->format('%h');
224
        $minutes = $interval->format('%i');
225
        $quiz_duration = $hours * 60 + $minutes;
226
    }
227
}
228

    
229
if (api_is_allowed_to_session_edit()) {
230
	update_event_exercice($exercise_stat_info['exe_id'], $objExercise->selectId(), $total_score, $total_weight, api_get_session_id(), $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id, $quiz_duration, $question_list, '', array(), $end_date);
231
}
232

    
233
//If is not valid
234
$session_control_key = get_session_time_control_key($objExercise->id);
235
if (isset($session_control_key) && !exercise_time_control_is_valid($objExercise->id)) {
236
	$TBL_TRACK_ATTEMPT		= Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
237
	$sql_fraud = "UPDATE $TBL_TRACK_ATTEMPT SET answer = 0, marks=0, position=0 WHERE exe_id = $exe_id ";
238
	Database::query($sql_fraud);
239
}
240

    
241
//Unset session for clock time
242
exercise_time_control_delete($objExercise->id);
243

    
244
if ($origin != 'learnpath') {
245
    echo '<hr><div style="float:right">';
246
    echo Display::url(get_lang('ReturnToCourseHomepage'), api_get_course_url(), array('class' => 'btn btn-large'));
247
    echo '</div>';
248
	Display::display_footer();
249
} else {
250
	$lp_mode =  $_SESSION['lp_mode'];
251
	$url = '../newscorm/lp_controller.php?cidReq='.api_get_course_id().'&action=view&lp_id='.$safe_lp_id.'&lp_item_id='.$safe_lp_item_id.'&exeId='.$exercise_stat_info['exe_id'].'&fb_type='.$objExercise->feedback_type;
252
	//echo $total_score.','.$total_weight;	exit;
253
	$href = ($lp_mode == 'fullscreen')?' window.opener.location.href="'.$url.'" ':' top.location.href="'.$url.'" ';
254
	echo '<script type="text/javascript">'.$href.'</script>'."\n";
255
	//record the results in the learning path, using the SCORM interface (API)
256
	echo '<script type="text/javascript">window.parent.API.void_save_asset('.$total_score.','.$total_weight.');</script>'."\n";
257
	echo '</body></html>';
258
}
259

    
260
// Send notification..
261
if (!api_is_allowed_to_edit(null,true)) {
262
    $objExercise->send_notification($arrques, $arrans, $origin);
263
}
264
if (api_is_allowed_to_session_edit()) {
265
	Session::erase('objExercise');
266
	Session::erase('exe_id');
267
}
    (1-1/1)