upload_exercise.php 12 KB

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