upload_exercise.php 12 KB

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