upload_exercise.php 13 KB

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