upload_exercise.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. use \ChamiloSession as Session;
  10. // setting the help
  11. $help_content = 'exercise_upload';
  12. // including the global Dokeos file
  13. require_once '../inc/global.inc.php';
  14. require_once api_get_path(LIBRARY_PATH) . 'pear/excelreader/reader.php';
  15. // Security check
  16. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  17. if (!$is_allowed_to_edit) {
  18. api_not_allowed(true);
  19. }
  20. // setting the tabs
  21. $this_section = SECTION_COURSES;
  22. $htmlHeadXtra[] = "<script>
  23. $(document).ready( function(){
  24. $('#user_custom_score').click(function() {
  25. $('#options').toggle();
  26. });
  27. });
  28. </script>";
  29. // Action handling
  30. lp_upload_quiz_action_handling();
  31. $interbreadcrumb[]= array ("url"=>"exercise.php", "name"=> get_lang('Exercises'));
  32. // Display the header
  33. Display :: display_header(get_lang('ImportExcelQuiz'), 'Exercises');
  34. if (isset($_GET['message'])) {
  35. if (in_array($_GET['message'], array('ExerciseEdited'))) {
  36. Display :: display_confirmation_message(get_lang($_GET['message']));
  37. }
  38. }
  39. // display the actions
  40. echo '<div class="actions">';
  41. echo lp_upload_quiz_actions();
  42. echo '</div>';
  43. // the main content
  44. lp_upload_quiz_main();
  45. function lp_upload_quiz_actions() {
  46. $return = '<a href="exercise.php?'.api_get_cidReq().'">'.
  47. Display::return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  48. return $return;
  49. }
  50. function lp_upload_quiz_secondary_actions() {
  51. $lp_id = Security::remove_XSS($_GET['lp_id']);
  52. $return = '';
  53. $return .= '<a href="exercise_report.php?' . api_get_cidreq() . '">' .
  54. Display :: return_icon('reporting32.png', get_lang('Tracking')) . get_lang('Tracking') . '</a>';
  55. return $return;
  56. }
  57. function lp_upload_quiz_main() {
  58. // variable initialisation
  59. $lp_id = isset($_GET['lp_id']) ? Security::remove_XSS($_GET['lp_id']) : null;
  60. $form = new FormValidator('upload', 'POST', api_get_self() . '?' . api_get_cidreq() . '&lp_id='.$lp_id, '', array('enctype' => 'multipart/form-data'));
  61. $form->addElement('header', get_lang('ImportExcelQuiz'));
  62. $form->addElement('file', 'user_upload_quiz', get_lang('FileUpload'));
  63. $link = '<a href="../exercice/quiz_template.xls">'.
  64. Display::return_icon('export_excel.png', get_lang('DownloadExcelTemplate')).get_lang('DownloadExcelTemplate').'</a>';
  65. $form->addElement('label', '', $link);
  66. $form->addElement('checkbox', 'user_custom_score', null, get_lang('UseCustomScoreForAllQuestions'), array('id'=> 'user_custom_score'));
  67. $form->addElement('html', '<div id="options" style="display:none">');
  68. $form->addElement('text', 'correct_score', get_lang('CorrectScore'));
  69. $form->addElement('text', 'incorrect_score', get_lang('IncorrectScore'));
  70. $form->addElement('html', '</div>');
  71. $form->addRule('user_upload_quiz', get_lang('ThisFieldIsRequired'), 'required');
  72. $form->add_progress_bar();
  73. $form->addButtonUpload(get_lang('Send'), 'submit_upload_quiz');
  74. // Display the upload field
  75. $form->display();
  76. }
  77. /**
  78. * Handles a given Excel spreadsheets as in the template provided
  79. */
  80. function lp_upload_quiz_action_handling() {
  81. global $debug;
  82. $_course = api_get_course_info();
  83. $courseId = $_course['real_id'];
  84. if (!isset($_POST['submit_upload_quiz'])) {
  85. return;
  86. }
  87. // Get the extension of the document.
  88. $path_info = pathinfo($_FILES['user_upload_quiz']['name']);
  89. // Check if the document is an Excel document
  90. if ($path_info['extension'] != 'xls') {
  91. return;
  92. }
  93. // Read the Excel document
  94. $data = new Spreadsheet_Excel_Reader();
  95. // Set output Encoding.
  96. $data->setOutputEncoding(api_get_system_encoding());
  97. // Reading the xls document.
  98. $data->read($_FILES['user_upload_quiz']['tmp_name']);
  99. $correctScore = isset($_POST['correct_score']) ? $_POST['correct_score'] : null;
  100. $incorrectScore = isset($_POST['incorrect_score']) ? $_POST['incorrect_score'] : null;
  101. $useCustomScore = isset($_POST['user_custom_score']) ? true : false;
  102. $propagateNegative = 0;
  103. if ($useCustomScore && !empty($incorrectScore)) {
  104. if ($incorrectScore < 0) {
  105. $propagateNegative = 1;
  106. }
  107. }
  108. // Variables
  109. $quiz_index = 0;
  110. $question_title_index = array();
  111. $question_name_index_init = array();
  112. $question_name_index_end = array();
  113. $score_index = array();
  114. $feedback_true_index = array();
  115. $feedback_false_index = array();
  116. $number_questions = 0;
  117. $question_description_index = array();
  118. // Reading all the first column items sequentially 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. } elseif ($data->sheets[0]['cells'][$i][1] == 'EnrichQuestion') {
  134. $question_description_index[] = $i;
  135. }
  136. }
  137. // Variables
  138. $quiz = array();
  139. $question = array();
  140. $new_answer = array();
  141. $score_list = array();
  142. $feedback_true_list = array();
  143. $feedback_false_list = array();
  144. $question_description = array();
  145. // Getting questions.
  146. $k = $z = $q = $l = $m = 0;
  147. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
  148. if (is_array($data->sheets[0]['cells'][$i])) {
  149. $column_data = $data->sheets[0]['cells'][$i];
  150. // Fill all column with data to have a full array
  151. for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) {
  152. if (empty($column_data[$x])) {
  153. $data->sheets[0]['cells'][$i][$x] = '';
  154. }
  155. }
  156. // Array filled with data
  157. $column_data = $data->sheets[0]['cells'][$i];
  158. } else {
  159. $column_data = '';
  160. }
  161. // Fill quiz data
  162. if ($quiz_index == $i) {
  163. // The title always in the first position
  164. $quiz = $column_data;
  165. } elseif (in_array($i, $question_title_index)) {
  166. //a complete line where 1st column is 'Question'
  167. $question[$k] = $column_data;
  168. $k++;
  169. } elseif (in_array($i, $score_index)) {
  170. //a complete line where 1st column is 'Score'
  171. $score_list[$z] = $column_data;
  172. $z++;
  173. } elseif (in_array($i, $feedback_true_index)) {
  174. //a complete line where 1st column is 'FeedbackTrue'
  175. $feedback_true_list[$q] = $column_data;
  176. $q++;
  177. } elseif (in_array($i, $feedback_false_index)) {
  178. //a complete line where 1st column is 'FeedbackFalse' for wrong answers
  179. $feedback_false_list[$l] = $column_data;
  180. $l++;
  181. } elseif (in_array($i, $question_description_index)) {
  182. //a complete line where 1st column is 'EnrichQuestion'
  183. $question_description[$m] = $column_data;
  184. $m++;
  185. }
  186. }
  187. // Get answers
  188. for ($i = 0; $i < count($question_name_index_init); $i++) {
  189. for ($j = $question_name_index_init[$i]; $j <= $question_name_index_end[$i]; $j++) {
  190. if (is_array($data->sheets[0]['cells'][$j])) {
  191. $column_data = $data->sheets[0]['cells'][$j];
  192. // Fill all column with data
  193. for ($x = 1; $x <= $data->sheets[0]['numCols']; $x++) {
  194. if (empty($column_data[$x])) {
  195. $data->sheets[0]['cells'][$j][$x] = '';
  196. }
  197. }
  198. $column_data = $data->sheets[0]['cells'][$j];
  199. // Array filled of data
  200. if (is_array($data->sheets[0]['cells'][$j]) && count($data->sheets[0]['cells'][$j]) > 0) {
  201. $new_answer[$i][$j] = $data->sheets[0]['cells'][$j];
  202. }
  203. }
  204. }
  205. }
  206. // Quiz title.
  207. $quiz_title = $quiz[2];
  208. if ($quiz_title != '') {
  209. // Variables
  210. $type = 2;
  211. $random = $active = $results = $max_attempt = $expired_time = 0;
  212. // Make sure feedback is enabled (3 to disable), otherwise the fields
  213. // added to the XLS are not shown, which is confusing
  214. $feedback = 0;
  215. // Quiz object
  216. $exercise = new Exercise();
  217. //
  218. $quiz_id = $exercise->createExercise(
  219. $quiz_title,
  220. $expired_time,
  221. $type,
  222. $random,
  223. $active,
  224. $results,
  225. $max_attempt,
  226. $feedback,
  227. $propagateNegative
  228. );
  229. if ($quiz_id) {
  230. // insert into the item_property table
  231. api_item_property_update(
  232. $_course,
  233. TOOL_QUIZ,
  234. $quiz_id,
  235. 'QuizAdded',
  236. api_get_user_id()
  237. );
  238. // Import questions.
  239. for ($i = 0; $i < $number_questions; $i++) {
  240. // Question name
  241. $question_title = $question[$i][2];
  242. $question_description_text = "<p></p>";
  243. if (isset($question_description[$i][2])) {
  244. // Question description.
  245. $question_description_text = "<p>".$question_description[$i][2]."</p>";
  246. }
  247. // Unique answers are the only question types available for now
  248. // through xls-format import
  249. $question_id = null;
  250. $detectQuestionType = detectQuestionType(
  251. $new_answer[$i],
  252. $score_list
  253. );
  254. /** @var Question $answer */
  255. switch ($detectQuestionType) {
  256. case FREE_ANSWER:
  257. $answer = new FreeAnswer();
  258. break;
  259. case GLOBAL_MULTIPLE_ANSWER:
  260. $answer = new GlobalMultipleAnswer();
  261. break;
  262. case MULTIPLE_ANSWER:
  263. $answer = new MultipleAnswer();
  264. break;
  265. case UNIQUE_ANSWER:
  266. default:
  267. $answer = new UniqueAnswer();
  268. break;
  269. }
  270. if ($question_title != '') {
  271. $question_id = $answer->create_question(
  272. $quiz_id,
  273. $question_title,
  274. $question_description_text,
  275. 0, // max score
  276. $answer->type
  277. );
  278. }
  279. $total = 0;
  280. if (is_array($new_answer[$i]) && !empty($question_id)) {
  281. $id = 1;
  282. $answers_data = $new_answer[$i];
  283. $globalScore = null;
  284. $objAnswer = new Answer($question_id, $courseId);
  285. $globalScore = $score_list[$i][3];
  286. // Calculate the number of correct answers to divide the
  287. // score between them when importing from CSV
  288. $numberRightAnswers = 0;
  289. foreach ($answers_data as $answer_data) {
  290. if (strtolower($answer_data[3]) == 'x') {
  291. $numberRightAnswers++;
  292. }
  293. }
  294. foreach ($answers_data as $answer_data) {
  295. $answerValue = $answer_data[2];
  296. $correct = 0;
  297. $score = 0;
  298. if (strtolower($answer_data[3]) == 'x') {
  299. $correct = 1;
  300. $score = $score_list[$i][3];
  301. $comment = $feedback_true_list[$i][2];
  302. } else {
  303. $comment = $feedback_false_list[$i][2];
  304. $floatVal = (float)$answer_data[3];
  305. if (is_numeric($floatVal)) {
  306. $score = $answer_data[3];
  307. }
  308. }
  309. if ($useCustomScore) {
  310. if ($correct) {
  311. $score = $correctScore;
  312. } else {
  313. $score = $incorrectScore;
  314. }
  315. }
  316. // Fixing scores:
  317. switch ($detectQuestionType) {
  318. case GLOBAL_MULTIPLE_ANSWER:
  319. $score /= $numberRightAnswers;
  320. break;
  321. case UNIQUE_ANSWER:
  322. break;
  323. case MULTIPLE_ANSWER:
  324. if (!$correct) {
  325. //$total = $total - $score;
  326. }
  327. break;
  328. }
  329. $objAnswer->createAnswer(
  330. $answerValue,
  331. $correct,
  332. $comment,
  333. $score,
  334. $id
  335. );
  336. $total += $score;
  337. $id++;
  338. }
  339. $objAnswer->save();
  340. $questionObj = Question::read($question_id, $courseId);
  341. switch ($detectQuestionType) {
  342. case GLOBAL_MULTIPLE_ANSWER:
  343. $questionObj->updateWeighting($globalScore);
  344. break;
  345. case UNIQUE_ANSWER:
  346. case MULTIPLE_ANSWER:
  347. default:
  348. $questionObj->updateWeighting($total);
  349. break;
  350. }
  351. $questionObj->save();
  352. } else if ($detectQuestionType === FREE_ANSWER) {
  353. $questionObj = Question::read($question_id, $courseId);
  354. $globalScore = $score_list[$i][3];
  355. $questionObj->updateWeighting($globalScore);
  356. $questionObj->save();
  357. }
  358. }
  359. }
  360. if (isset($_SESSION['lpobject'])) {
  361. if ($debug > 0) {
  362. error_log('New LP - SESSION[lpobject] is defined', 0);
  363. }
  364. $oLP = unserialize($_SESSION['lpobject']);
  365. if (is_object($oLP)) {
  366. if ($debug > 0) {
  367. error_log('New LP - oLP is object', 0);
  368. }
  369. if ((empty($oLP->cc)) OR $oLP->cc != api_get_course_id()) {
  370. if ($debug > 0) {
  371. error_log('New LP - Course has changed, discard lp object', 0);
  372. }
  373. $oLP = null;
  374. Session::erase('oLP');
  375. Session::erase('lpobject');
  376. } else {
  377. $_SESSION['oLP'] = $oLP;
  378. }
  379. }
  380. }
  381. if (isset($_SESSION['oLP']) && isset($_GET['lp_id'])) {
  382. $previous = $_SESSION['oLP']->select_previous_item_id();
  383. $parent = 0;
  384. // Add a Quiz as Lp Item
  385. $_SESSION['oLP']->add_item($parent, $previous, TOOL_QUIZ, $quiz_id, $quiz_title, '');
  386. // Redirect to home page for add more content
  387. header('location: ../newscorm/lp_controller.php?'.api_get_cidreq().'&action=add_item&type=step&lp_id='.Security::remove_XSS($_GET['lp_id']));
  388. exit;
  389. } else {
  390. // header('location: exercise.php?' . api_get_cidreq());
  391. echo '<script>window.location.href = "'.api_get_path(WEB_CODE_PATH).'exercice/admin.php?'.api_get_cidReq().'&exerciseId='.$quiz_id.'&session_id='.api_get_session_id().'"</script>';
  392. }
  393. }
  394. }
  395. /**
  396. * @param array $answers_data
  397. * @return int
  398. */
  399. function detectQuestionType($answers_data)
  400. {
  401. $correct = 0;
  402. $isNumeric = false;
  403. if (!empty($answers_data)) {
  404. foreach ($answers_data as $answer_data) {
  405. if (strtolower($answer_data[3]) == 'x') {
  406. $correct++;
  407. } else {
  408. if (is_numeric($answer_data[3])) {
  409. $isNumeric = true;
  410. }
  411. }
  412. }
  413. }
  414. if ($correct == 1) {
  415. $type = UNIQUE_ANSWER;
  416. } else if ($correct > 1) {
  417. $type = MULTIPLE_ANSWER;
  418. } else {
  419. $type = FREE_ANSWER;
  420. }
  421. if ($type == MULTIPLE_ANSWER) {
  422. if ($isNumeric) {
  423. $type = MULTIPLE_ANSWER;
  424. } else {
  425. $type = GLOBAL_MULTIPLE_ANSWER;
  426. }
  427. }
  428. return $type;
  429. }
  430. if (!isset($origin) || isset($origin) && $origin != 'learnpath') {
  431. //so we are not in learnpath tool
  432. Display :: display_footer();
  433. }