exercice.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise list: This script shows the list of exercises for administrators and students.
  5. * @package chamilo.exercise
  6. * @author Olivier Brouckaert, original author
  7. * @author Denes Nagy, HotPotatoes integration
  8. * @author Wolfgang Schneider, code/html cleanup
  9. * @author Julio Montoya <gugli100@gmail.com>, lots of cleanup + several improvements
  10. * Modified by hubert.borderiou (question category)
  11. */
  12. /**
  13. * Code
  14. */
  15. // name of the language file that needs to be included
  16. use \ChamiloSession as Session;
  17. $language_file = array('exercice', 'tracking');
  18. // including the global library
  19. require_once '../inc/global.inc.php';
  20. $current_course_tool = TOOL_QUIZ;
  21. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  22. // Setting the tabs
  23. $this_section = SECTION_COURSES;
  24. $htmlHeadXtra[] = api_get_js('qtip2/jquery.qtip.min.js');
  25. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/qtip2/jquery.qtip.min.css');
  26. // Access control
  27. api_protect_course_script(true);
  28. // including additional libraries
  29. require_once 'exercise.class.php';
  30. require_once 'question.class.php';
  31. require_once 'answer.class.php';
  32. require_once 'hotpotatoes.lib.php';
  33. /* Constants and variables */
  34. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  35. $is_tutor = api_is_allowed_to_edit(true);
  36. $is_tutor_course = api_is_course_tutor();
  37. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  38. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  39. $TBL_EXERCICE_QUESTION = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  40. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  41. $TBL_TRACK_EXERCICES = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  42. // document path
  43. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path']."/document";
  44. // picture path
  45. $picturePath = $documentPath.'/images';
  46. // audio path
  47. $audioPath = $documentPath.'/audio';
  48. // hotpotatoes
  49. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  50. $exercicePath = api_get_self();
  51. $exfile = explode('/', $exercicePath);
  52. $exfile = strtolower($exfile[sizeof($exfile) - 1]);
  53. $exercicePath = substr($exercicePath, 0, strpos($exercicePath, $exfile));
  54. $exercicePath = $exercicePath."exercice.php";
  55. // Clear the exercise session
  56. if (isset($_SESSION['objExercise'])) {
  57. Session::erase('objExercise');
  58. }
  59. if (isset($_SESSION['objQuestion'])) {
  60. Session::erase('objQuestion');
  61. }
  62. if (isset($_SESSION['objAnswer'])) {
  63. Session::erase('objAnswer');
  64. }
  65. if (isset($_SESSION['questionList'])) {
  66. Session::erase('questionList');
  67. }
  68. if (isset($_SESSION['question_list_uncompressed'])) {
  69. Session::erase('question_list_uncompressed');
  70. }
  71. if (isset($_SESSION['exerciseResult'])) {
  72. Session::erase('exerciseResult');
  73. }
  74. //General POST/GET/SESSION/COOKIES parameters recovery
  75. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : null;
  76. $choice = isset($_REQUEST['choice']) ? Security::remove_XSS($_REQUEST['choice']) : null;
  77. $hpchoice = isset($_REQUEST['hpchoice']) ? Security::remove_XSS($_REQUEST['hpchoice']) : null;
  78. $exerciseId = isset($_REQUEST['exerciseId']) ? Security::remove_XSS($_REQUEST['exerciseId']) : null;
  79. $file = isset($_REQUEST['file']) ? Database::escape_string($_REQUEST['file']) : null;
  80. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  81. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  82. $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : null;
  83. $course_info = api_get_course_info();
  84. $course_id = api_get_course_int_id();
  85. if ($page < 0) {
  86. $page = 1;
  87. }
  88. if (!empty($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  89. $_SESSION['gradebook'] = Security::remove_XSS($_GET['gradebook']);
  90. $gradebook = $_SESSION['gradebook'];
  91. } elseif (empty($_GET['gradebook'])) {
  92. unset($_SESSION['gradebook']);
  93. $gradebook = '';
  94. }
  95. if (!empty($gradebook) && $gradebook == 'view') {
  96. $interbreadcrumb[] = array(
  97. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  98. 'name' => get_lang('ToolGradebook')
  99. );
  100. }
  101. $nameTools = get_lang('Exercices');
  102. if ($is_allowedToEdit && !empty ($choice) && $choice == 'exportqti2') {
  103. require_once 'export/qti2/qti2_export.php';
  104. $export = export_exercise($exerciseId, true);
  105. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  106. $temp_dir_short = api_get_unique_id();
  107. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  108. if (!is_dir($temp_zip_dir)) {
  109. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  110. }
  111. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  112. $temp_xml_file = $temp_zip_dir."/qti2export_".$exerciseId.'.xml';
  113. file_put_contents($temp_xml_file, $export);
  114. $zip_folder = new PclZip($temp_zip_file);
  115. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  116. $name = 'qti2_export_'.$exerciseId.'.zip';
  117. //DocumentManager::string_send_for_download($export,true,'qti2export_'.$exerciseId.'.xml');
  118. DocumentManager :: file_send_for_download($temp_zip_file, true, $name);
  119. unlink($temp_zip_file);
  120. unlink($temp_xml_file);
  121. rmdir($temp_zip_dir);
  122. exit; //otherwise following clicks may become buggy
  123. }
  124. $htmlHeadXtra[] = '<script>
  125. $(document).ready(function() {
  126. //this makes google chrome to crash ...
  127. /* $(".link_tooltip").each(function(){
  128. $(this).qtip({
  129. content: $(this).find(".exercise_tooltip"),
  130. position: { at:"top right", my:"bottom left"},
  131. show: {
  132. event: false,
  133. ready: true // ... but show the tooltip when ready
  134. },
  135. hide: true, //
  136. });
  137. });*/
  138. $(".data_table tbody").sortable({
  139. cursor: "move", // works?
  140. update: function(event, ui) {
  141. var order = $(this).sortable("serialize") + "&a=update_exercise_list_order";
  142. $.get("'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php", order, function(reponse) {
  143. $("#message").html(reponse);
  144. });
  145. },
  146. axis: "y",
  147. placeholder: "ui-state-highlight", //defines the yellow highlight
  148. handle: ".moved", //only the class "moved"
  149. });
  150. });
  151. </script>';
  152. if ($origin != 'learnpath') {
  153. //so we are not in learnpath tool
  154. Display :: display_header($nameTools, get_lang('Exercise'));
  155. if (isset($_GET['message'])) {
  156. if (in_array($_GET['message'], array('ExerciseEdited'))) {
  157. Display :: display_confirmation_message(get_lang($_GET['message']));
  158. }
  159. }
  160. } else {
  161. Display :: display_reduced_header();
  162. }
  163. event_access_tool(TOOL_QUIZ);
  164. // Tool introduction
  165. Display :: display_introduction_section(TOOL_QUIZ);
  166. HotPotGCt($documentPath, 1, api_get_user_id());
  167. // Only for administrator
  168. if ($is_allowedToEdit) {
  169. if (!empty($choice)) {
  170. // Construction of Exercise
  171. $objExerciseTmp = new Exercise();
  172. $check = Security::check_token('get');
  173. $exercise_action_locked = api_resource_is_locked_by_gradebook($exerciseId, LINK_EXERCISE);
  174. if ($objExerciseTmp->read($exerciseId)) {
  175. if ($check) {
  176. switch ($choice) {
  177. case 'delete': // deletes an exercise
  178. if ($exercise_action_locked == false) {
  179. $objExerciseTmp->delete();
  180. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  181. $link_info = is_resource_in_course_gradebook(
  182. api_get_course_id(),
  183. 1,
  184. $exerciseId,
  185. api_get_session_id()
  186. );
  187. if ($link_info !== false) {
  188. remove_resource_from_course_gradebook($link_info['id']);
  189. }
  190. Display :: display_confirmation_message(get_lang('ExerciseDeleted'));
  191. }
  192. break;
  193. case 'enable': // enables an exercise
  194. $objExerciseTmp->enable();
  195. $objExerciseTmp->save();
  196. api_item_property_update(
  197. $course_info,
  198. TOOL_QUIZ,
  199. $objExerciseTmp->id,
  200. 'visible',
  201. api_get_user_id()
  202. );
  203. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  204. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  205. break;
  206. case 'disable': // disables an exercise
  207. $objExerciseTmp->disable();
  208. $objExerciseTmp->save();
  209. api_item_property_update(
  210. $course_info,
  211. TOOL_QUIZ,
  212. $objExerciseTmp->id,
  213. 'invisible',
  214. api_get_user_id()
  215. );
  216. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  217. break;
  218. case 'disable_results': //disable the results for the learners
  219. $objExerciseTmp->disable_results();
  220. $objExerciseTmp->save();
  221. Display :: display_confirmation_message(get_lang('ResultsDisabled'));
  222. break;
  223. case 'enable_results': //disable the results for the learners
  224. $objExerciseTmp->enable_results();
  225. $objExerciseTmp->save();
  226. Display :: display_confirmation_message(get_lang('ResultsEnabled'));
  227. break;
  228. case 'clean_results': //clean student results
  229. if ($exercise_action_locked == false) {
  230. $quantity_results_deleted = $objExerciseTmp->clean_results();
  231. Display :: display_confirmation_message(
  232. sprintf(get_lang('XResultsCleaned'), $quantity_results_deleted)
  233. );
  234. }
  235. break;
  236. case 'copy_exercise':
  237. $objExerciseTmp->copy_exercise();
  238. Display :: display_confirmation_message(get_lang('ExerciseCopied'));
  239. break;
  240. case 'set_autolaunch':
  241. $objExerciseTmp->set_autolaunch();
  242. Display :: display_confirmation_message(get_lang('ItemUpdated'));
  243. break;
  244. }
  245. }
  246. }
  247. // destruction of Exercise
  248. unset($objExerciseTmp);
  249. Security::clear_token();
  250. }
  251. if (!empty($hpchoice)) {
  252. switch ($hpchoice) {
  253. case 'delete': // deletes an exercise
  254. $imgparams = array();
  255. $imgcount = 0;
  256. GetImgParams($file, $documentPath, $imgparams, $imgcount);
  257. $fld = GetFolderName($file);
  258. for ($i = 0; $i < $imgcount; $i++) {
  259. FileManager::my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
  260. FileManager::update_db_info("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
  261. }
  262. if (FileManager::my_delete($documentPath.$file)) {
  263. FileManager::update_db_info("delete", $file);
  264. }
  265. // hotpotatoes folder may contains several tests so don't delete folder if not empty : #2165
  266. if (!(strstr($uploadPath, DIR_HOTPOTATOES) && !FileManager::folder_is_empty(
  267. $documentPath.$uploadPath."/".$fld."/"
  268. ))
  269. ) {
  270. FileManager::my_delete($documentPath.$uploadPath."/".$fld."/");
  271. }
  272. break;
  273. case 'enable': // enables an exercise
  274. $newVisibilityStatus = "1"; //"visible"
  275. $query = "SELECT id FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path='".Database :: escape_string($file)."'";
  276. $res = Database::query($query);
  277. $row = Database :: fetch_array($res, 'ASSOC');
  278. api_item_property_update($_course, TOOL_DOCUMENT, $row['id'], 'visible', $_user['user_id']);
  279. break;
  280. case 'disable': // disables an exercise
  281. $newVisibilityStatus = "0"; //"invisible"
  282. $query = "SELECT id FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path='".Database :: escape_string($file)."'";
  283. $res = Database::query($query);
  284. $row = Database :: fetch_array($res, 'ASSOC');
  285. api_item_property_update($_course, TOOL_DOCUMENT, $row['id'], 'invisible', $_user['user_id']);
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. }
  292. // Actions div bar
  293. if ($is_allowedToEdit) {
  294. echo '<div class="actions">';
  295. }
  296. // Selects $limit exercises at the same time
  297. // maximum number of exercises on a same page
  298. $limit = 50;
  299. // Display the next and previous link if needed
  300. $from = $page * $limit;
  301. HotPotGCt($documentPath, 1, api_get_user_id());
  302. //condition for the session
  303. $course_code = api_get_course_id();
  304. $courseId = api_get_course_int_id();
  305. $session_id = api_get_session_id();
  306. $condition_session = api_get_session_condition($session_id, true, true);
  307. // Only for administrators
  308. if ($is_allowedToEdit) {
  309. $total_sql = "SELECT count(iid) as count FROM $TBL_EXERCICES WHERE c_id = $course_id AND active<>'-1' $condition_session ";
  310. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title LIMIT ".$from.",".$limit;
  311. } else {
  312. // Only for students
  313. $total_sql = "SELECT count(iid) as count FROM $TBL_EXERCICES WHERE c_id = $course_id AND active = '1' $condition_session ";
  314. $sql = "SELECT * FROM $TBL_EXERCICES
  315. WHERE c_id = $course_id AND
  316. active='1' $condition_session
  317. ORDER BY title LIMIT ".$from.",".$limit;
  318. }
  319. $result = Database::query($sql);
  320. $exercises_count = Database :: num_rows($result);
  321. $result_total = Database::query($total_sql);
  322. $total_exercises = 0;
  323. if (Database :: num_rows($result_total)) {
  324. $result_total = Database::fetch_array($result_total);
  325. $total_exercises = $result_total['count'];
  326. }
  327. // Get HotPotatoes files (active and inactive)
  328. if ($is_allowedToEdit) {
  329. $sql = "SELECT * FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path LIKE '".Database :: escape_string($uploadPath)."/%/%'";
  330. $res = Database::query($sql);
  331. $hp_count = Database :: num_rows($res);
  332. } else {
  333. $sql = "SELECT * FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  334. WHERE d.id = ip.ref AND
  335. ip.tool = '".TOOL_DOCUMENT."' AND
  336. d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND
  337. ip.visibility ='1' AND
  338. d.c_id = ".$course_id." AND
  339. ip.c_id = ".$course_id;
  340. $res = Database::query($sql);
  341. $hp_count = Database :: num_rows($res);
  342. }
  343. $total = $total_exercises + $hp_count;
  344. if ($is_allowedToEdit && $origin != 'learnpath') {
  345. echo '<a href="exercise_admin.php?'.api_get_cidreq().'">'.Display :: return_icon(
  346. 'new_exercice.png',
  347. get_lang('NewEx'),
  348. '',
  349. ICON_SIZE_MEDIUM
  350. ).'</a>';
  351. echo '<a href="question_create.php?'.api_get_cidreq().'">'.Display :: return_icon(
  352. 'new_question.png',
  353. get_lang('AddQ'),
  354. '',
  355. ICON_SIZE_MEDIUM
  356. ).'</a>';
  357. // Question category
  358. echo '<a href="tests_category.php">';
  359. echo Display::return_icon('question_category_show.gif', get_lang('QuestionCategory'));
  360. echo '</a>';
  361. if (api_is_platform_admin()) {
  362. echo '<a href="tests_category.php?type=global">';
  363. echo Display::return_icon('folder_global_category.png', get_lang('QuestionGlobalCategory'), array(), ICON_SIZE_MEDIUM);
  364. echo '</a>';
  365. }
  366. echo '<a href="'.api_get_path(WEB_PUBLIC_PATH).'courses/'.api_get_course_path().'/'.api_get_session_id().'/exercise/question-pool">';
  367. echo Display::return_icon('database.png', get_lang('QuestionPool'), array('style' => 'width:32px'));
  368. echo '</a>';
  369. echo '<a href="media.php?'.api_get_cidreq().'">';
  370. echo Display::return_icon('media.png', get_lang('Media'), array(), ICON_SIZE_MEDIUM);
  371. echo '</a>';
  372. // end question category
  373. echo '<a href="hotpotatoes.php?'.api_get_cidreq().'">'.Display :: return_icon(
  374. 'import_hotpotatoes.png',
  375. get_lang('ImportHotPotatoesQuiz'),
  376. '',
  377. ICON_SIZE_MEDIUM
  378. ).'</a>';
  379. // link to import qti2 ...
  380. echo '<a href="qti2.php?'.api_get_cidreq().'">'.Display :: return_icon(
  381. 'import_qti2.png',
  382. get_lang('ImportQtiQuiz'),
  383. '',
  384. ICON_SIZE_MEDIUM
  385. ).'</a>';
  386. echo '<a href="upload_exercise.php?'.api_get_cidreq().'">'.Display :: return_icon(
  387. 'import_excel.png',
  388. get_lang('ImportExcelQuiz'),
  389. '',
  390. ICON_SIZE_MEDIUM
  391. ).'</a>';
  392. }
  393. if ($is_allowedToEdit) {
  394. echo '</div>'; // closing the actions div
  395. echo '<div id="message"></div>';
  396. }
  397. if ($total > $limit) {
  398. echo '<div style="float:right;height:20px;">';
  399. //show pages navigation link for previous page
  400. if ($page) {
  401. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;page=".($page - 1)."\">".Display :: return_icon(
  402. 'action_prev.png',
  403. get_lang('PreviousPage')
  404. )."</a>";
  405. } elseif ($total_exercises + $hp_count > $limit) {
  406. echo Display :: return_icon('action_prev_na.png', get_lang('PreviousPage'));
  407. }
  408. //show pages navigation link for previous page
  409. if ($total_exercises > $from + $limit || $hp_count > $from + $limit) {
  410. echo ' '."<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;page=".($page + 1)."\">".Display::return_icon(
  411. 'action_next.png',
  412. get_lang('NextPage')
  413. )."</a>";
  414. } elseif ($page) {
  415. echo ' '.Display :: return_icon('action_next_na.png', get_lang('NextPage'));
  416. }
  417. echo '</div>';
  418. }
  419. $i = 1;
  420. $online_icon = Display::return_icon('online.png', get_lang('Visible'), array('width' => '12px'));
  421. $offline_icon = Display::return_icon('offline.png', get_lang('Invisible'), array('width' => '12px'));
  422. $exercise_list = array();
  423. $exercise_obj = new Exercise();
  424. $list_ordered = $exercise_obj->get_exercise_list_ordered();
  425. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  426. $exercise_list[$row['iid']] = $row;
  427. }
  428. if (isset($list_ordered) && !empty($list_ordered)) {
  429. $new_question_list = array();
  430. foreach ($list_ordered as $exercise_id) {
  431. if (isset($exercise_list[$exercise_id])) {
  432. $new_question_list[] = $exercise_list[$exercise_id];
  433. }
  434. }
  435. $exercise_list = $new_question_list;
  436. }
  437. echo '<table class="'.Display::return_default_table_class().'">';
  438. /* Listing exercises */
  439. if (!empty($exercise_list)) {
  440. if ($origin != 'learnpath') {
  441. //avoid sending empty parameters
  442. $myorigin = (empty($origin) ? '' : '&origin='.$origin);
  443. $mylpid = (empty($learnpath_id) ? '' : '&learnpath_id='.$learnpath_id);
  444. $mylpitemid = (empty($learnpath_item_id) ? '' : '&learnpath_item_id='.$learnpath_item_id);
  445. $token = Security::get_token();
  446. $i = 1;
  447. if ($is_allowedToEdit) {
  448. $headers = array(
  449. array('name' => get_lang('ExerciseName')),
  450. array('name' => get_lang('QuantityQuestions'), 'params' => array('width' => '100px')),
  451. array('name' => get_lang('Actions'), 'params' => array('width' => '180px'))
  452. );
  453. } else {
  454. $headers = array(
  455. array('name' => get_lang('ExerciseName')),
  456. array('name' => get_lang('Status')),
  457. );
  458. }
  459. $header_list = '';
  460. foreach ($headers as $header) {
  461. $params = isset($header['params']) ? $header['params'] : null;
  462. $header_list .= Display::tag('th', $header['name'], $params);
  463. }
  464. echo Display::tag('tr', $header_list);
  465. $autolaunch_setting_on = api_get_course_setting('enable_exercise_auto_launch') == 1;
  466. $count = 0;
  467. if (!empty($exercise_list)) {
  468. foreach ($exercise_list as $row) {
  469. $my_exercise_id = $row['iid'];
  470. $exercise_obj = new Exercise();
  471. $exercise_obj->read($my_exercise_id, false);
  472. $locked = $exercise_obj->is_gradebook_locked;
  473. $i++;
  474. // Validation when belongs to a session
  475. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  476. $time_limits = false;
  477. if ($row['start_time'] != '0000-00-00 00:00:00' || $row['end_time'] != '0000-00-00 00:00:00') {
  478. $time_limits = true;
  479. }
  480. if ($time_limits) {
  481. // check if start time
  482. $start_time = false;
  483. if ($row['start_time'] != '0000-00-00 00:00:00') {
  484. $start_time = api_strtotime($row['start_time'], 'UTC');
  485. }
  486. $end_time = false;
  487. if ($row['end_time'] != '0000-00-00 00:00:00') {
  488. $end_time = api_strtotime($row['end_time'], 'UTC');
  489. }
  490. $now = time();
  491. $is_actived_time = false;
  492. //If both "clocks" are enable
  493. if ($start_time && $end_time) {
  494. if ($now > $start_time && $end_time > $now) {
  495. $is_actived_time = true;
  496. }
  497. } else {
  498. //we check the start and end
  499. if ($start_time) {
  500. if ($now > $start_time) {
  501. $is_actived_time = true;
  502. }
  503. }
  504. if ($end_time) {
  505. if ($end_time > $now) {
  506. $is_actived_time = true;
  507. }
  508. }
  509. }
  510. }
  511. //Blocking empty start times see BT#2800
  512. global $_custom;
  513. if (isset($_custom['exercises_hidden_when_no_start_date']) && $_custom['exercises_hidden_when_no_start_date']) {
  514. if (empty($row['start_time']) || $row['start_time'] == '0000-00-00 00:00:00') {
  515. $time_limits = true;
  516. $is_actived_time = false;
  517. }
  518. }
  519. $cut_title = $exercise_obj->getCutTitle();
  520. $alt_title = '';
  521. if ($cut_title != $row['title']) {
  522. $alt_title = ' title = "'.$row['title'].'" ';
  523. }
  524. // Teacher only
  525. if ($is_allowedToEdit) {
  526. $lp_blocked = null;
  527. if ($exercise_obj->exercise_was_added_in_lp == true) {
  528. $lp_blocked = Display::div(
  529. get_lang('AddedToLPCannotBeAccessed'),
  530. array('class' => 'lp_content_type_label')
  531. );
  532. }
  533. $visibility = api_get_item_visibility($course_info, TOOL_QUIZ, $my_exercise_id);
  534. if ($row['active'] == 0 || $visibility == 0) {
  535. $title = Display::tag('font', $cut_title, array('style' => 'color:grey'));
  536. } else {
  537. $title = $cut_title;
  538. }
  539. $count_exercise_not_validated = intval(
  540. count_exercise_result_not_validated($my_exercise_id, $courseId, $session_id)
  541. );
  542. $move = Display::return_icon(
  543. 'all_directions.png',
  544. get_lang('Move'),
  545. array('class' => 'moved', 'style' => 'margin-bottom:-0.5em;')
  546. );
  547. $class_tip = '';
  548. if (!empty($count_exercise_not_validated)) {
  549. $results_text = $count_exercise_not_validated == 1 ? get_lang('ResultNotRevised') : get_lang(
  550. 'ResultsNotRevised'
  551. );
  552. $title .= '<span class="exercise_tooltip" style="display: none;">'.$count_exercise_not_validated.' '.$results_text.' </span>';
  553. $class_tip = 'link_tooltip';
  554. }
  555. //$class_tip = 'exercise_link';
  556. $urlOverview = 'overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$my_exercise_id;
  557. $url = $move.'<a '.$alt_title.' class="'.$class_tip.'" id="tooltip_'.$my_exercise_id.'" href="'.$urlOverview.'">'.Display::return_icon('quiz.gif').' '.$title.' </a>';
  558. $item = Display::tag('td', $url.' '.$session_img.$lp_blocked);
  559. //Count number exercice - teacher
  560. $sqlquery = "SELECT count(*) FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND exercice_id = $my_exercise_id";
  561. $sqlresult = Database::query($sqlquery);
  562. $rowi = Database :: result($sqlresult, 0);
  563. if ($session_id == $row['session_id']) {
  564. //Settings
  565. $actions = Display::url(Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL), 'admin.php?'.api_get_cidreq().'&exerciseId='.$my_exercise_id);
  566. //Exercise results
  567. $actions .='<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$my_exercise_id.'">'.Display :: return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  568. //Export
  569. $actions .= Display::url(Display::return_icon('cd.gif', get_lang('CopyExercise')), '', array('onclick' => "javascript:if(!confirm('".addslashes(get_lang('AreYouSureToCopy'))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=copy_exercise&sec_token='.$token.'&exerciseId='.$my_exercise_id));
  570. if ($autolaunch_setting_on) {
  571. $icon = Display::return_icon('launch.png', get_lang('AutoLaunch'));
  572. if ($row['autolaunch'] == 0) {
  573. $icon = Display::return_icon('launch_na.png', get_lang('AutoLaunch'));
  574. }
  575. $actions .= Display::url($icon, 'exercice.php?'.api_get_cidreq().'&choice=set_autolaunch&sec_token='.$token.'&exerciseId='.$my_exercise_id);
  576. }
  577. //Clean exercise
  578. if ($locked == false) {
  579. $actions .= Display::url(Display::return_icon('clean.png', get_lang('CleanStudentResults'), '', ICON_SIZE_SMALL), '', array('onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToDeleteResults'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=clean_results&sec_token='.$token.'&exerciseId='.$my_exercise_id));
  580. } else {
  581. $actions .= Display::return_icon('clean_na.png', get_lang('ResourceLockedByGradebook'), '', ICON_SIZE_SMALL);
  582. }
  583. //Visible / invisible
  584. //Check if this exercise was added in a LP
  585. if ($exercise_obj->exercise_was_added_in_lp == true) {
  586. $actions .= Display::return_icon('invisible.png', get_lang('AddedToLPCannotBeAccessed'), '', ICON_SIZE_SMALL);
  587. } else {
  588. if ($row['active'] == 0 || $visibility == 0) {
  589. $actions .= Display::url(Display::return_icon('invisible.png', get_lang('Activate'), '', ICON_SIZE_SMALL), 'exercice.php?'.api_get_cidreq().'&choice=enable&sec_token='.$token.'&page='.$page.'&exerciseId='.$my_exercise_id);
  590. } else {
  591. // else if not active
  592. $actions .= Display::url(Display::return_icon('visible.png', get_lang('Deactivate'), '', ICON_SIZE_SMALL), 'exercice.php?'.api_get_cidreq().'&choice=disable&sec_token='.$token.'&page='.$page.'&exerciseId='.$my_exercise_id);
  593. }
  594. }
  595. // Export qti ...
  596. $actions .= Display::url(Display::return_icon('export_qti2.png', 'IMS/QTI', '', ICON_SIZE_SMALL), 'exercice.php?choice=exportqti2&exerciseId='.$my_exercise_id);
  597. } else {
  598. // not session
  599. $actions = Display::return_icon('edit_na.png', get_lang('ExerciseEditionNotAvailableInSession'));
  600. $actions .='<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$my_exercise_id.'">'.Display :: return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  601. $actions .= Display::url(Display::return_icon('cd.gif', get_lang('CopyExercise')), '', array('onclick' => "javascript:if(!confirm('".addslashes(get_lang('AreYouSureToCopy'))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=copy_exercise&sec_token='.$token.'&exerciseId='.$my_exercise_id));
  602. }
  603. //Delete
  604. if ($session_id == $row['session_id']) {
  605. if ($locked == false) {
  606. $actions .= Display::url(Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL), '', array('onclick' => "javascript:if(!confirm('".addslashes(get_lang('AreYouSureToDelete'))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=delete&sec_token='.$token.'&exerciseId='.$my_exercise_id));
  607. } else {
  608. $actions .= Display::return_icon('delete_na.png', get_lang('ResourceLockedByGradebook'), '', ICON_SIZE_SMALL);
  609. }
  610. /*if ($rowi > 50) {
  611. // Showing the direct link to exercise settings only
  612. // if there are more than 50 questions, so the list of
  613. // questions does not slow down everything
  614. $actions .= Display::url(Display::return_icon('settings.png',get_lang('EditSettings'),'',ICON_SIZE_SMALL), 'exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$row['id']);
  615. }*/
  616. }
  617. if ($app['security']->isGranted('ROLE_SESSION_MANAGER') && !empty($my_exercise_id)) {
  618. $actions .= Display::url(
  619. Display::return_icon('admin_star.png', get_lang('Distribution'), '', ICON_SIZE_SMALL),
  620. $app['url_generator']->generate(
  621. 'exercise_distribution.controller:indexAction',
  622. array(
  623. 'exerciseId' => $my_exercise_id,
  624. 'cidReq' => api_get_course_id(),
  625. 'id_session' => api_get_session_id()
  626. )
  627. )
  628. );
  629. }
  630. if ($app['security']->isGranted('ROLE_EXERCISE_STATISTICS') && !empty($my_exercise_id)) {
  631. $actions .= Display::url(
  632. Display::return_icon('add.png', get_lang('Distribution'), '', ICON_SIZE_SMALL),
  633. $app['url_generator']->generate(
  634. 'exercise_statistics.controller:indexAction',
  635. array(
  636. 'exerciseId' => $my_exercise_id,
  637. 'cidReq' => api_get_course_id(),
  638. 'id_session' => api_get_session_id()
  639. )
  640. )
  641. );
  642. }
  643. $number_of_questions = $exercise_obj->getQuestionCount();
  644. if ($row['random'] > 0 && $row['random'] != -1) {
  645. $number_of_questions = $number_of_questions.' ('.$row['random'].' '.get_lang('Random').') ';
  646. }
  647. //Attempts
  648. //$attempts = get_count_exam_results($my_exercise_id).' '.get_lang('Attempts');
  649. //$item .= Display::tag('td',$attempts);
  650. $item .= Display::tag('td', $number_of_questions);
  651. } else {
  652. // Student only
  653. $visibility = api_get_item_visibility($course_info, TOOL_QUIZ, $my_exercise_id);
  654. if ($visibility == 0) {
  655. continue;
  656. }
  657. // if time is actived show link to exercise
  658. if ($time_limits) {
  659. if ($is_actived_time) {
  660. $url = '<a '.$alt_title.' href="overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$my_exercise_id.'">'.$cut_title.'</a>';
  661. } else {
  662. $url = $row['title'];
  663. }
  664. } else {
  665. $url = '<a '.$alt_title.' href="overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$my_exercise_id.'">'.$cut_title.'</a>';
  666. }
  667. //Link of the exercise
  668. $item = Display::tag('td', $url.' '.$session_img);
  669. //count number exercise questions
  670. $sqlquery = "SELECT count(*) FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND exercice_id = ".$my_exercise_id;
  671. $sqlresult = Database::query($sqlquery);
  672. $rowi = Database::result($sqlresult, 0);
  673. if ($row['random'] > 0) {
  674. $row['random'].' '.api_strtolower(get_lang(($row['random'] > 1 ? 'Questions' : 'Question')));
  675. } else {
  676. //show results student
  677. $rowi.' '.api_strtolower(get_lang(($rowi > 1 ? 'Questions' : 'Question')));
  678. }
  679. //This query might be improved later on by ordering by the new "tms" field rather than by exe_id
  680. //Don't remove this marker: note-query-exe-results
  681. $qry = "SELECT * FROM $TBL_TRACK_EXERCICES
  682. WHERE exe_exo_id = ".$my_exercise_id." AND
  683. exe_user_id = ".api_get_user_id()." AND
  684. c_id = '".api_get_course_int_id()."' AND
  685. status <> 'incomplete' AND
  686. orig_lp_id = 0 AND
  687. orig_lp_item_id = 0 AND
  688. session_id = '".api_get_session_id()."'
  689. ORDER BY exe_id DESC";
  690. $qryres = Database::query($qry);
  691. $num = Database :: num_rows($qryres);
  692. //Hide the results
  693. $my_result_disabled = $row['results_disabled'];
  694. //Time limits are on
  695. if ($time_limits) {
  696. // Examn is ready to be taken
  697. if ($is_actived_time) {
  698. //Show results
  699. if ($my_result_disabled == 0 || $my_result_disabled == 2) {
  700. //More than one attempt
  701. if ($num > 0) {
  702. $row_track = Database :: fetch_array($qryres);
  703. $attempt_text = get_lang('LatestAttempt').' : ';
  704. $attempt_text .= ExerciseLib::show_score($row_track['exe_result'], $row_track['exe_weighting']);
  705. } else {
  706. //No attempts
  707. $attempt_text = get_lang('NotAttempted');
  708. }
  709. } else {
  710. $attempt_text = get_lang('CantShowResults');
  711. }
  712. } else {
  713. //Quiz not ready due to time limits
  714. //@todo use the is_visible function
  715. if ($row['start_time'] != '0000-00-00 00:00:00' && $row['end_time'] != '0000-00-00 00:00:00') {
  716. $attempt_text = sprintf(
  717. get_lang('ExerciseWillBeActivatedFromXToY'),
  718. api_convert_and_format_date($row['start_time']),
  719. api_convert_and_format_date($row['end_time'])
  720. );
  721. } else {
  722. //$attempt_text = get_lang('ExamNotAvailableAtThisTime');
  723. if ($row['start_time'] != '0000-00-00 00:00:00') {
  724. $attempt_text = sprintf(
  725. get_lang('ExerciseAvailableFromX'),
  726. api_convert_and_format_date($row['start_time'])
  727. );
  728. }
  729. if ($row['end_time'] != '0000-00-00 00:00:00') {
  730. $attempt_text = sprintf(
  731. get_lang('ExerciseAvailableUntilX'),
  732. api_convert_and_format_date($row['end_time'])
  733. );
  734. }
  735. }
  736. }
  737. } else {
  738. //Normal behaviour
  739. //Show results
  740. if ($my_result_disabled == 0 || $my_result_disabled == 2) {
  741. if ($num > 0) {
  742. $row_track = Database :: fetch_array($qryres);
  743. $attempt_text = get_lang('LatestAttempt').' : ';
  744. $attempt_text .= ExerciseLib::show_score($row_track['exe_result'], $row_track['exe_weighting']);
  745. } else {
  746. $attempt_text = get_lang('NotAttempted');
  747. }
  748. } else {
  749. $attempt_text = get_lang('CantShowResults');
  750. }
  751. }
  752. $class_tip = '';
  753. if (empty($num)) {
  754. $num = '';
  755. } else {
  756. $class_tip = 'link_tooltip';
  757. //@todo use sprintf and show the results validated by the teacher
  758. if ($num == 1) {
  759. $num = $num.' '.get_lang('Result');
  760. } else {
  761. $num = $num.' '.get_lang('Results');
  762. }
  763. $num = '<span class="tooltip" style="display: none;">'.$num.'</span>';
  764. }
  765. $item .= Display::tag('td', $attempt_text);
  766. }
  767. $class = 'row_even';
  768. if ($count % 2) {
  769. $class = 'row_odd';
  770. }
  771. if ($is_allowedToEdit) {
  772. $item .= Display::tag('td', $actions, array('class' => 'td_actions'));
  773. }
  774. echo Display::tag('tr', $item, array('id' => 'exercise_list_'.$my_exercise_id, 'class' => $class));
  775. $count++;
  776. }
  777. } // end foreach()
  778. }
  779. }
  780. // end exercise list
  781. //Hotpotatoes results
  782. $hotpotatoes_exist = false;
  783. if ($is_allowedToEdit) {
  784. $sql = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility
  785. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  786. WHERE d.c_id = $course_id AND
  787. ip.c_id = $course_id AND
  788. d.id = ip.ref AND
  789. ip.tool = '".TOOL_DOCUMENT."' AND
  790. (d.path LIKE '%htm%') AND
  791. d.path LIKE '".Database :: escape_string($uploadPath)."/%/%'
  792. LIMIT ".$from.",".$limit; // only .htm or .html files listed
  793. } else {
  794. $sql = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility
  795. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  796. WHERE d.c_id = $course_id AND
  797. ip.c_id = $course_id AND
  798. d.id = ip.ref AND ip.tool = '".TOOL_DOCUMENT."' AND (d.path LIKE '%htm%')
  799. AND d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND ip.visibility='1'
  800. LIMIT ".$from.",".$limit;
  801. }
  802. $result = Database::query($sql);
  803. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  804. $attribute['path'][] = $row['path'];
  805. $attribute['visibility'][] = $row['visibility'];
  806. $attribute['comment'][] = $row['comment'];
  807. }
  808. $nbrActiveTests = 0;
  809. if (isset($attribute['path']) && is_array($attribute['path'])) {
  810. $hotpotatoes_exist = true;
  811. while (list($key, $path) = each($attribute['path'])) {
  812. $item = '';
  813. list ($a, $vis) = each($attribute['visibility']);
  814. if (strcmp($vis, "1") == 0) {
  815. $active = 1;
  816. } else {
  817. $active = 0;
  818. }
  819. $title = GetQuizName($path, $documentPath);
  820. if ($title == '') {
  821. $title = basename($path);
  822. }
  823. $class = 'row_even';
  824. if ($count % 2) {
  825. $class = 'row_odd';
  826. }
  827. // prof only
  828. if ($is_allowedToEdit) {
  829. $item = Display::tag(
  830. 'td',
  831. '<img src="../img/hotpotatoes_s.png" alt="HotPotatoes" /> <a href="showinframes.php?file='.$path.'&cid='.api_get_course_id(
  832. ).'&uid='.api_get_user_id().'"'.(!$active ? 'class="invisible"' : '').'>'.$title.'</a> '
  833. );
  834. $item .= Display::tag('td', '-');
  835. $actions = Display::url(
  836. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL),
  837. 'adminhp.php?'.api_get_cidreq().'&hotpotatoesName='.$path
  838. );
  839. $actions .= '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq(
  840. ).'&path='.$path.'">'.Display :: return_icon(
  841. 'test_results.png',
  842. get_lang('Results'),
  843. '',
  844. ICON_SIZE_SMALL
  845. ).'</a>';
  846. // if active
  847. if ($active) {
  848. $nbrActiveTests = $nbrActiveTests + 1;
  849. $actions .= ' <a href="'.$exercicePath.'?'.api_get_cidreq(
  850. ).'&hpchoice=disable&amp;page='.$page.'&amp;file='.$path.'">'.Display::return_icon(
  851. 'visible.png',
  852. get_lang('Deactivate'),
  853. '',
  854. ICON_SIZE_SMALL
  855. ).'</a>';
  856. } else { // else if not active
  857. $actions .= ' <a href="'.$exercicePath.'?'.api_get_cidreq(
  858. ).'&hpchoice=enable&amp;page='.$page.'&amp;file='.$path.'">'.Display::return_icon(
  859. 'invisible.png',
  860. get_lang('Activate'),
  861. '',
  862. ICON_SIZE_SMALL
  863. ).'</a>';
  864. }
  865. $actions .= '<a href="'.$exercicePath.'?'.api_get_cidreq(
  866. ).'&amp;hpchoice=delete&amp;file='.$path.'" onclick="javascript:if(!confirm(\''.addslashes(
  867. api_htmlentities(get_lang('AreYouSureToDelete'), ENT_QUOTES, $charset).' '.$title."?"
  868. ).'\')) return false;">'.Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  869. $item .= Display::tag('td', $actions);
  870. echo Display::tag('tr', $item, array('class' => $class));
  871. } else {
  872. // Student only
  873. if ($active == 1) {
  874. $nbrActiveTests = $nbrActiveTests + 1;
  875. $item .= Display::tag(
  876. 'td',
  877. '<a href="showinframes.php?'.api_get_cidreq().'&file='.$path.'&cid='.api_get_course_id(
  878. ).'&uid='.api_get_user_id().'"'.(!$active ? 'class="invisible"' : '').'">'.$title.'</a>'
  879. );
  880. //$item .= Display::tag('td', '');
  881. $actions = '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq(
  882. ).'&path='.$path.'&filter_by_user='.api_get_user_id().'">'.Display :: return_icon(
  883. 'test_results.png',
  884. get_lang('Results'),
  885. '',
  886. ICON_SIZE_SMALL
  887. ).'</a>';
  888. $item .= Display::tag('td', $actions);
  889. echo Display::tag('tr', $item, array('class' => $class));
  890. }
  891. }
  892. $count++;
  893. }
  894. }
  895. echo '</table>';
  896. if (empty($exercise_list) && $hotpotatoes_exist == false) {
  897. if ($is_allowedToEdit && $origin != 'learnpath') {
  898. echo '<div id="no-data-view">';
  899. echo '<h2>'.get_lang('Quiz').'</h2>';
  900. echo Display::return_icon('quiz.png', '', array(), 64);
  901. echo '<div class="controls">';
  902. echo Display::url(get_lang('NewEx'), 'exercise_admin.php?'.api_get_cidreq(), array('class' => 'btn'));
  903. echo '</div>';
  904. echo '</div>';
  905. }
  906. }
  907. if ($origin != 'learnpath') { //so we are not in learnpath tool
  908. Display :: display_footer();
  909. }
  910. Session::erase('objExercise');
  911. Session::erase('objQuestion');
  912. Session::erase('objAnswer');