upload_exercise.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Upload quiz: This script shows the upload quiz feature
  5. * Initial work by Isaac flores on Nov 4 of 2010
  6. * Encoding fixes Julio Montoya
  7. * @package chamilo.exercise
  8. */
  9. /**
  10. * Language files that should be included
  11. */
  12. $language_file[] = 'learnpath';
  13. $language_file[] = 'exercice';
  14. // setting the help
  15. $help_content = 'exercise_upload';
  16. // including the global Dokeos file
  17. require_once '../inc/global.inc.php';
  18. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  19. require_once api_get_path(LIBRARY_PATH) . 'pear/excelreader/reader.php';
  20. require_once 'exercise.class.php';
  21. require_once 'question.class.php';
  22. require_once 'unique_answer.class.php';
  23. require_once '../newscorm/learnpath.class.php';
  24. require_once '../newscorm/learnpathItem.class.php';
  25. // Security check
  26. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  27. if (!$is_allowed_to_edit) {
  28. api_not_allowed(true);
  29. }
  30. // setting the tabs
  31. $this_section = SECTION_COURSES;
  32. $htmlHeadXtra[] = "<script type='text/javascript'>
  33. $(document).ready( function(){
  34. $(\"div.formw\").attr(\"style\",\"width: 73%;\");
  35. $(\"#img_plus_and_minus\").hide();
  36. });
  37. </script>";
  38. // Action handling
  39. lp_upload_quiz_action_handling();
  40. $interbreadcrumb[]= array ("url"=>"exercice.php", "name"=> get_lang('Exercices'));
  41. // Display the header
  42. if ($origin != 'learnpath') {
  43. //so we are not in learnpath tool
  44. Display :: display_header(get_lang('ImportExcelQuiz'), 'Exercises');
  45. if (isset ($_GET['message'])) {
  46. if (in_array($_GET['message'], array ('ExerciseEdited'))) {
  47. Display :: display_confirmation_message(get_lang($_GET['message']));
  48. }
  49. }
  50. } else {
  51. echo '<link rel="stylesheet" type="text/css" href="' . api_get_path(WEB_CODE_PATH) . 'css/default.css"/>';
  52. }
  53. // display the actions
  54. echo '<div class="actions">';
  55. echo lp_upload_quiz_actions();
  56. echo '</div>';
  57. // start the content div
  58. echo '<div id="content_with_secondary_actions" class="gradient">';
  59. // the main content
  60. lp_upload_quiz_main();
  61. // close the content div
  62. echo '</div>';
  63. function lp_upload_quiz_actions() {
  64. $lp_id = Security::remove_XSS($_GET['lp_id']);
  65. $return = "";
  66. $return .= '<a href="exercice.php?'.api_get_cidReq().'">'.Display::return_icon('back.png', get_lang('BackToExercisesList'),'','32').'</a>';
  67. return $return;
  68. }
  69. function lp_upload_quiz_secondary_actions() {
  70. $lp_id = Security::remove_XSS($_GET['lp_id']);
  71. $return.= '';
  72. $return.='<a href="exercise_report.php?' . api_get_cidreq() . '">' . Display :: return_icon('reporting32.png', get_lang('Tracking')) . get_lang('Tracking') . '</a>';
  73. return $return;
  74. }
  75. function lp_upload_quiz_main() {
  76. // variable initialisation
  77. $lp_id = Security::remove_XSS($_GET['lp_id']);
  78. $form = new FormValidator('upload', 'POST', api_get_self() . '?' . api_get_cidreq() . '&lp_id=' . $lp_id, '', 'enctype="multipart/form-data"');
  79. $form->addElement('html', '<div><h3>' .Display::return_icon('import_excel.png', get_lang('ImportExcelQuiz'), array('style'=>'margin-bottom:-2px;'),32). get_lang('ImportExcelQuiz') . '</h3></div>');
  80. $form->addElement('file', 'user_upload_quiz', '');
  81. //button send document
  82. $form->addElement('style_submit_button', 'submit_upload_quiz', get_lang('Send'), 'class="upload"');
  83. // Display the upload field
  84. echo '<table style="text-align: left; width: 100%;" border="0" cellpadding="2"cellspacing="2"><tbody><tr>';
  85. echo '<td style="vertical-align: top; width: 25%;">';
  86. echo '<a href="../exercice/quiz_template.xls">'.Display::return_icon('export_excel.png', get_lang('DownloadExcelTemplate'),null,16).get_lang('DownloadExcelTemplate').'';
  87. echo '</a>';
  88. echo '</td>';
  89. echo '</tr>';
  90. echo '<tr><td>';
  91. $form->display();
  92. echo '</td></tr></tbody></table>';
  93. }
  94. /**
  95. * Handles a given Excel spreadsheets as in the template provided
  96. */
  97. function lp_upload_quiz_action_handling() {
  98. global $_course, $debug;
  99. if (!isset($_POST['submit_upload_quiz'])) {
  100. return;
  101. }
  102. // Get the extension of the document.
  103. $path_info = pathinfo($_FILES['user_upload_quiz']['name']);
  104. // Check if the document is an Excel document
  105. if ($path_info['extension'] != 'xls') {
  106. return;
  107. }
  108. // Read the Excel document
  109. $data = new Spreadsheet_Excel_Reader();
  110. // Set output Encoding.
  111. $data->setOutputEncoding(api_get_system_encoding());
  112. // Reading the xls document.
  113. $data->read($_FILES['user_upload_quiz']['tmp_name']);
  114. // Variables
  115. $quiz_index = 0;
  116. $question_title_index = array();
  117. $question_name_index_init = array();
  118. $question_name_index_end = array();
  119. $score_index = array();
  120. $feedback_true_index = array();
  121. $feedback_false_index = array();
  122. $number_questions = 0;
  123. // Reading all the first column items sequencially to create breakpoints
  124. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
  125. if ($data->sheets[0]['cells'][$i][1] == 'Quiz' && $i == 1) {
  126. $quiz_index = $i; // Quiz title position, only occurs once
  127. } elseif ($data->sheets[0]['cells'][$i][1] == 'Question') {
  128. $question_title_index[] = $i; // Question title position line
  129. $question_name_index_init[] = $i + 1; // Questions name 1st position line
  130. $number_questions++;
  131. } elseif ($data->sheets[0]['cells'][$i][1] == 'Score') {
  132. $question_name_index_end[] = $i - 1; // Question name position
  133. $score_index[] = $i; // Question score position
  134. } elseif ($data->sheets[0]['cells'][$i][1] == 'FeedbackTrue') {
  135. $feedback_true_index[] = $i; // FeedbackTrue position (line)
  136. } elseif ($data->sheets[0]['cells'][$i][1] == 'FeedbackFalse') {
  137. $feedback_false_index[] = $i; // FeedbackFalse position (line)
  138. }
  139. }
  140. // Variables
  141. $quiz = array();
  142. $question = array();
  143. $answer = array();
  144. $new_answer = array();
  145. $score_list = array();
  146. $feedback_true_list = array();
  147. $feedback_false_list = array();
  148. // Get questions
  149. $k = $z = $q = $l = 0;
  150. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
  151. if (is_array($data->sheets[0]['cells'][$i])) {
  152. $column_data = $data->sheets[0]['cells'][$i];
  153. // Fill all column with data to have a full array
  154. for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) {
  155. if (empty($column_data[$x])) {
  156. $data->sheets[0]['cells'][$i][$x] = '';
  157. }
  158. }
  159. // Array filled with data
  160. $column_data = $data->sheets[0]['cells'][$i];
  161. } else {
  162. $column_data = '';
  163. }
  164. // Fill quiz data
  165. if ($quiz_index == $i) { // The title always in the first position
  166. $quiz = $column_data;
  167. } elseif (in_array($i, $question_title_index)) {
  168. $question[$k] = $column_data; //a complete line where 1st column is 'Question'
  169. $k++;
  170. } elseif (in_array($i, $score_index)) {
  171. $score_list[$z] = $column_data; //a complete line where 1st column is 'Score'
  172. $z++;
  173. } elseif (in_array($i, $feedback_true_index)) {
  174. $feedback_true_list[$q] = $column_data;//a complete line where 1st column is 'FeedbackTrue'
  175. $q++;
  176. } elseif (in_array($i, $feedback_false_index)) {
  177. $feedback_false_list[$l] = $column_data;//a complete line where 1st column is 'FeedbackFalse' for wrong answers
  178. $l++;
  179. }
  180. }
  181. // Get answers
  182. for ($i = 0; $i < count($question_name_index_init); $i++) {
  183. for ($j = $question_name_index_init[$i]; $j <= $question_name_index_end[$i]; $j++) {
  184. if (is_array($data->sheets[0]['cells'][$j])) {
  185. $column_data = $data->sheets[0]['cells'][$j];
  186. // Fill all column with data
  187. for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) {
  188. if (empty($column_data[$x])) {
  189. $data->sheets[0]['cells'][$j][$x] = '';
  190. }
  191. }
  192. $column_data = $data->sheets[0]['cells'][$j];
  193. // Array filled of data
  194. if (is_array($data->sheets[0]['cells'][$j]) && count($data->sheets[0]['cells'][$j]) > 0) {
  195. $new_answer[$i][$j] = $data->sheets[0]['cells'][$j];
  196. }
  197. }
  198. }
  199. }
  200. $quiz_title = $quiz[2]; // Quiz title
  201. if ($quiz_title != '') {
  202. // Variables
  203. $type = 2;
  204. $random = $active = $results = $max_attempt = $expired_time = 0;
  205. //make sure feedback is enabled (3 to disable), otherwise the fields
  206. // added to the XLS are not shown, which is confusing
  207. $feedback = 0;
  208. // Quiz object
  209. $quiz_object = new Exercise();
  210. $quiz_id = $quiz_object->create_quiz(($quiz_title), $expired_time, $type, $random, $active, $results, $max_attempt, $feedback);
  211. // insert into the item_property table
  212. api_item_property_update($_course, TOOL_QUIZ, $quiz_id, 'QuizAdded', api_get_user_id());
  213. // Import questions
  214. for ($i = 0; $i < $number_questions; $i++) {
  215. // Create questions
  216. $question_title = $question[$i][2]; // Question name
  217. if ($question_title != '') {
  218. $question_id = Question::create_question($quiz_id, ($question_title));
  219. }
  220. $unique_answer = new UniqueAnswer();
  221. if (is_array($new_answer[$i])) {
  222. $id = 1;
  223. $answers_data = $new_answer[$i];
  224. foreach ($answers_data as $answer_data) {
  225. $answer = $answer_data[2];
  226. $correct = 0;
  227. $score = 0;
  228. $comment = '';
  229. if (strtolower($answer_data[3]) == 'x') {
  230. $correct = 1;
  231. $score = $score_list[$i][3];
  232. $comment = $feedback_true_list[$i][2];
  233. } else {
  234. $comment = $feedback_false_list[$i][2];
  235. }
  236. /*
  237. if ($id == 1) {
  238. $comment = $feedback_true_list[$i][2];
  239. } elseif ($id == 2) {
  240. $comment = $feedback_false_list[$i][2];
  241. }
  242. */
  243. // Create answer
  244. $unique_answer->create_answer($id, $question_id, ($answer), ($comment), $score, $correct);
  245. $id++;
  246. }
  247. }
  248. }
  249. if (isset($_SESSION['lpobject'])) {
  250. if ($debug > 0) {
  251. error_log('New LP - SESSION[lpobject] is defined', 0);
  252. }
  253. $oLP = unserialize($_SESSION['lpobject']);
  254. if (is_object($oLP)) {
  255. if ($debug > 0) {
  256. error_log('New LP - oLP is object', 0);
  257. }
  258. if ((empty($oLP->cc)) OR $oLP->cc != api_get_course_id()) {
  259. if ($debug > 0) {
  260. error_log('New LP - Course has changed, discard lp object', 0);
  261. }
  262. $oLP = null;
  263. api_session_unregister('oLP');
  264. api_session_unregister('lpobject');
  265. } else {
  266. $_SESSION['oLP'] = $oLP;
  267. $lp_found = true;
  268. }
  269. }
  270. }
  271. if (isset($_SESSION['oLP']) && isset($_GET['lp_id'])) {
  272. $previous = $_SESSION['oLP']->select_previous_item_id();
  273. $parent = 0;
  274. // Add a Quiz as Lp Item
  275. $_SESSION['oLP']->add_item($parent, $previous, TOOL_QUIZ, $quiz_id, ($quiz_title), '');
  276. // Redirect to home page for add more content
  277. header('location: ../newscorm/lp_controller.php?' . api_get_cidreq() . '&action=add_item&type=step&lp_id=' . Security::remove_XSS($_GET['lp_id']).'&session_id='.api_get_session_id());
  278. exit;
  279. } else {
  280. // header('location: exercice.php?' . api_get_cidreq());
  281. echo '<script>window.location.href = "admin.php?'.api_get_cidReq().'&exerciseId='.$quiz_id.'&session_id='.api_get_session_id().'"</script>';
  282. }
  283. }
  284. }