upload_exercise.php 12 KB

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