upload_exercise.php 12 KB

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