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_JS_PATH).'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['exerciseResult'])) {
  69. Session::erase('exerciseResult');
  70. }
  71. //General POST/GET/SESSION/COOKIES parameters recovery
  72. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : null;
  73. $choice = isset($_REQUEST['choice']) ? Security::remove_XSS($_REQUEST['choice']) : null;
  74. $hpchoice = isset($_REQUEST['hpchoice']) ? Security::remove_XSS($_REQUEST['hpchoice']) : null;
  75. $exerciseId = isset($_REQUEST['exerciseId']) ? Security::remove_XSS($_REQUEST['exerciseId']) : null;
  76. $file = isset($_REQUEST['file']) ? Database::escape_string($_REQUEST['file']) : null;
  77. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  78. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  79. $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : null;
  80. $course_info = api_get_course_info();
  81. $course_id = api_get_course_int_id();
  82. if ($page < 0) {
  83. $page = 1;
  84. }
  85. if (!empty($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  86. $_SESSION['gradebook'] = Security::remove_XSS($_GET['gradebook']);
  87. $gradebook = $_SESSION['gradebook'];
  88. } elseif (empty($_GET['gradebook'])) {
  89. unset($_SESSION['gradebook']);
  90. $gradebook = '';
  91. }
  92. if (!empty($gradebook) && $gradebook == 'view') {
  93. $interbreadcrumb[] = array(
  94. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  95. 'name' => get_lang('ToolGradebook')
  96. );
  97. }
  98. $nameTools = get_lang('Exercices');
  99. if ($is_allowedToEdit && !empty ($choice) && $choice == 'exportqti2') {
  100. require_once 'export/qti2/qti2_export.php';
  101. $export = export_exercise($exerciseId, true);
  102. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  103. $temp_dir_short = api_get_unique_id();
  104. $temp_zip_dir = $archive_path."/".$temp_dir_short;
  105. if (!is_dir($temp_zip_dir)) {
  106. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  107. }
  108. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  109. $temp_xml_file = $temp_zip_dir."/qti2export_".$exerciseId.'.xml';
  110. file_put_contents($temp_xml_file, $export);
  111. $zip_folder = new PclZip($temp_zip_file);
  112. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  113. $name = 'qti2_export_'.$exerciseId.'.zip';
  114. //DocumentManager::string_send_for_download($export,true,'qti2export_'.$exerciseId.'.xml');
  115. DocumentManager :: file_send_for_download($temp_zip_file, true, $name);
  116. unlink($temp_zip_file);
  117. unlink($temp_xml_file);
  118. rmdir($temp_zip_dir);
  119. exit; //otherwise following clicks may become buggy
  120. }
  121. $htmlHeadXtra[] = '<script>
  122. $(document).ready(function() {
  123. //this makes google chrome to crash ...
  124. /* $(".link_tooltip").each(function(){
  125. $(this).qtip({
  126. content: $(this).find(".exercise_tooltip"),
  127. position: { at:"top right", my:"bottom left"},
  128. show: {
  129. event: false,
  130. ready: true // ... but show the tooltip when ready
  131. },
  132. hide: true, //
  133. });
  134. });*/
  135. $(".data_table tbody").sortable({
  136. cursor: "move", // works?
  137. update: function(event, ui) {
  138. var order = $(this).sortable("serialize") + "&a=update_exercise_list_order";
  139. $.get("'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php", order, function(reponse) {
  140. $("#message").html(reponse);
  141. });
  142. },
  143. axis: "y",
  144. placeholder: "ui-state-highlight", //defines the yellow highlight
  145. handle: ".moved", //only the class "moved"
  146. });
  147. });
  148. </script>';
  149. if ($origin != 'learnpath') {
  150. //so we are not in learnpath tool
  151. Display :: display_header($nameTools, get_lang('Exercise'));
  152. if (isset($_GET['message'])) {
  153. if (in_array($_GET['message'], array('ExerciseEdited'))) {
  154. Display :: display_confirmation_message(get_lang($_GET['message']));
  155. }
  156. }
  157. } else {
  158. Display :: display_reduced_header();
  159. }
  160. Event::event_access_tool(TOOL_QUIZ);
  161. // Tool introduction
  162. Display :: display_introduction_section(TOOL_QUIZ);
  163. HotPotGCt($documentPath, 1, api_get_user_id());
  164. // Only for administrator
  165. if ($is_allowedToEdit) {
  166. if (!empty($choice)) {
  167. // Construction of Exercise
  168. $objExerciseTmp = new Exercise();
  169. $check = Security::check_token('get');
  170. $exercise_action_locked = api_resource_is_locked_by_gradebook($exerciseId, LINK_EXERCISE);
  171. if ($objExerciseTmp->read($exerciseId)) {
  172. if ($check) {
  173. switch ($choice) {
  174. case 'delete': // deletes an exercise
  175. if ($exercise_action_locked == false) {
  176. $objExerciseTmp->delete();
  177. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  178. $link_info = is_resource_in_course_gradebook(
  179. api_get_course_id(),
  180. 1,
  181. $exerciseId,
  182. api_get_session_id()
  183. );
  184. if ($link_info !== false) {
  185. remove_resource_from_course_gradebook($link_info['id']);
  186. }
  187. Display :: display_confirmation_message(get_lang('ExerciseDeleted'));
  188. }
  189. break;
  190. case 'enable': // enables an exercise
  191. $objExerciseTmp->enable();
  192. $objExerciseTmp->save();
  193. api_item_property_update(
  194. $course_info,
  195. TOOL_QUIZ,
  196. $objExerciseTmp->id,
  197. 'visible',
  198. api_get_user_id()
  199. );
  200. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  201. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  202. break;
  203. case 'disable': // disables an exercise
  204. $objExerciseTmp->disable();
  205. $objExerciseTmp->save();
  206. api_item_property_update(
  207. $course_info,
  208. TOOL_QUIZ,
  209. $objExerciseTmp->id,
  210. 'invisible',
  211. api_get_user_id()
  212. );
  213. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  214. break;
  215. case 'disable_results': //disable the results for the learners
  216. $objExerciseTmp->disable_results();
  217. $objExerciseTmp->save();
  218. Display :: display_confirmation_message(get_lang('ResultsDisabled'));
  219. break;
  220. case 'enable_results': //disable the results for the learners
  221. $objExerciseTmp->enable_results();
  222. $objExerciseTmp->save();
  223. Display :: display_confirmation_message(get_lang('ResultsEnabled'));
  224. break;
  225. case 'clean_results': //clean student results
  226. if ($exercise_action_locked == false) {
  227. $quantity_results_deleted = $objExerciseTmp->clean_results();
  228. Display :: display_confirmation_message(
  229. sprintf(get_lang('XResultsCleaned'), $quantity_results_deleted)
  230. );
  231. }
  232. break;
  233. case 'copy_exercise':
  234. $objExerciseTmp->copy_exercise();
  235. Display :: display_confirmation_message(get_lang('ExerciseCopied'));
  236. break;
  237. case 'set_autolaunch':
  238. $objExerciseTmp->set_autolaunch();
  239. Display :: display_confirmation_message(get_lang('ItemUpdated'));
  240. break;
  241. }
  242. }
  243. }
  244. // destruction of Exercise
  245. unset($objExerciseTmp);
  246. Security::clear_token();
  247. }
  248. if (!empty($hpchoice)) {
  249. switch ($hpchoice) {
  250. case 'delete': // deletes an exercise
  251. $imgparams = array();
  252. $imgcount = 0;
  253. GetImgParams($file, $documentPath, $imgparams, $imgcount);
  254. $fld = GetFolderName($file);
  255. for ($i = 0; $i < $imgcount; $i++) {
  256. FileManager::my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
  257. FileManager::update_db_info("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
  258. }
  259. if (FileManager::my_delete($documentPath.$file)) {
  260. FileManager::update_db_info("delete", $file);
  261. }
  262. // hotpotatoes folder may contains several tests so don't delete folder if not empty : #2165
  263. if (!(strstr($uploadPath, DIR_HOTPOTATOES) && !FileManager::folder_is_empty(
  264. $documentPath.$uploadPath."/".$fld."/"
  265. ))
  266. ) {
  267. FileManager::my_delete($documentPath.$uploadPath."/".$fld."/");
  268. }
  269. break;
  270. case 'enable': // enables an exercise
  271. $newVisibilityStatus = "1"; //"visible"
  272. $query = "SELECT id FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path='".Database :: escape_string($file)."'";
  273. $res = Database::query($query);
  274. $row = Database :: fetch_array($res, 'ASSOC');
  275. api_item_property_update($_course, TOOL_DOCUMENT, $row['id'], 'visible', $_user['user_id']);
  276. break;
  277. case 'disable': // disables an exercise
  278. $newVisibilityStatus = "0"; //"invisible"
  279. $query = "SELECT id FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path='".Database :: escape_string($file)."'";
  280. $res = Database::query($query);
  281. $row = Database :: fetch_array($res, 'ASSOC');
  282. api_item_property_update($_course, TOOL_DOCUMENT, $row['id'], 'invisible', $_user['user_id']);
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. }
  289. // Actions div bar
  290. if ($is_allowedToEdit) {
  291. echo '<div class="actions">';
  292. }
  293. // Selects $limit exercises at the same time
  294. // maximum number of exercises on a same page
  295. $limit = 50;
  296. // Display the next and previous link if needed
  297. $from = $page * $limit;
  298. HotPotGCt($documentPath, 1, api_get_user_id());
  299. //condition for the session
  300. $course_code = api_get_course_id();
  301. $courseId = api_get_course_int_id();
  302. $session_id = api_get_session_id();
  303. $condition_session = api_get_session_condition($session_id, true, true);
  304. // Only for administrators
  305. if ($is_allowedToEdit) {
  306. $total_sql = "SELECT count(iid) as count FROM $TBL_EXERCICES WHERE c_id = $course_id AND active<>'-1' $condition_session ";
  307. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title LIMIT ".$from.",".$limit;
  308. } else {
  309. // Only for students
  310. $total_sql = "SELECT count(iid) as count FROM $TBL_EXERCICES WHERE c_id = $course_id AND active = '1' $condition_session ";
  311. $sql = "SELECT * FROM $TBL_EXERCICES
  312. WHERE c_id = $course_id AND
  313. active='1' $condition_session
  314. ORDER BY title LIMIT ".$from.",".$limit;
  315. }
  316. $result = Database::query($sql);
  317. $exercises_count = Database :: num_rows($result);
  318. $result_total = Database::query($total_sql);
  319. $total_exercises = 0;
  320. if (Database :: num_rows($result_total)) {
  321. $result_total = Database::fetch_array($result_total);
  322. $total_exercises = $result_total['count'];
  323. }
  324. // Get HotPotatoes files (active and inactive)
  325. if ($is_allowedToEdit) {
  326. $sql = "SELECT * FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path LIKE '".Database :: escape_string($uploadPath)."/%/%'";
  327. $res = Database::query($sql);
  328. $hp_count = Database :: num_rows($res);
  329. } else {
  330. $sql = "SELECT * FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  331. WHERE d.id = ip.ref AND
  332. ip.tool = '".TOOL_DOCUMENT."' AND
  333. d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND
  334. ip.visibility ='1' AND
  335. d.c_id = ".$course_id." AND
  336. ip.c_id = ".$course_id;
  337. $res = Database::query($sql);
  338. $hp_count = Database :: num_rows($res);
  339. }
  340. $total = $total_exercises + $hp_count;
  341. if ($is_allowedToEdit && $origin != 'learnpath') {
  342. echo '<a href="exercise_admin.php?'.api_get_cidreq().'">'.Display :: return_icon(
  343. 'new_exercice.png',
  344. get_lang('NewEx'),
  345. '',
  346. ICON_SIZE_MEDIUM
  347. ).'</a>';
  348. echo '<a href="question_create.php?'.api_get_cidreq().'">'.Display :: return_icon(
  349. 'new_question.png',
  350. get_lang('AddQ'),
  351. '',
  352. ICON_SIZE_MEDIUM
  353. ).'</a>';
  354. // Question category
  355. echo '<a href="tests_category.php">';
  356. echo Display::return_icon('question_category_show.gif', get_lang('QuestionCategory'));
  357. echo '</a>';
  358. if (api_is_platform_admin()) {
  359. echo '<a href="tests_category.php?type=global">';
  360. echo Display::return_icon('folder_global_category.png', get_lang('QuestionGlobalCategory'), array(), ICON_SIZE_MEDIUM);
  361. echo '</a>';
  362. }
  363. echo '<a href="'.api_get_path(WEB_PUBLIC_PATH).'courses/'.api_get_course_path().'/'.api_get_session_id().'/exercise/question-pool">';
  364. echo Display::return_icon('database.png', get_lang('QuestionPool'), array('style' => 'width:32px'));
  365. echo '</a>';
  366. echo '<a href="media.php?'.api_get_cidreq().'">';
  367. echo Display::return_icon('media.png', get_lang('Media'), array(), ICON_SIZE_MEDIUM);
  368. echo '</a>';
  369. // end question category
  370. echo '<a href="hotpotatoes.php?'.api_get_cidreq().'">'.Display :: return_icon(
  371. 'import_hotpotatoes.png',
  372. get_lang('ImportHotPotatoesQuiz'),
  373. '',
  374. ICON_SIZE_MEDIUM
  375. ).'</a>';
  376. // link to import qti2 ...
  377. echo '<a href="qti2.php?'.api_get_cidreq().'">'.Display :: return_icon(
  378. 'import_qti2.png',
  379. get_lang('ImportQtiQuiz'),
  380. '',
  381. ICON_SIZE_MEDIUM
  382. ).'</a>';
  383. echo '<a href="upload_exercise.php?'.api_get_cidreq().'">'.Display :: return_icon(
  384. 'import_excel.png',
  385. get_lang('ImportExcelQuiz'),
  386. '',
  387. ICON_SIZE_MEDIUM
  388. ).'</a>';
  389. }
  390. if ($is_allowedToEdit) {
  391. echo '</div>'; // closing the actions div
  392. echo '<div id="message"></div>';
  393. }
  394. if ($total > $limit) {
  395. echo '<div style="float:right;height:20px;">';
  396. //show pages navigation link for previous page
  397. if ($page) {
  398. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;page=".($page - 1)."\">".Display :: return_icon(
  399. 'action_prev.png',
  400. get_lang('PreviousPage')
  401. )."</a>";
  402. } elseif ($total_exercises + $hp_count > $limit) {
  403. echo Display :: return_icon('action_prev_na.png', get_lang('PreviousPage'));
  404. }
  405. //show pages navigation link for previous page
  406. if ($total_exercises > $from + $limit || $hp_count > $from + $limit) {
  407. echo ' '."<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;page=".($page + 1)."\">".Display::return_icon(
  408. 'action_next.png',
  409. get_lang('NextPage')
  410. )."</a>";
  411. } elseif ($page) {
  412. echo ' '.Display :: return_icon('action_next_na.png', get_lang('NextPage'));
  413. }
  414. echo '</div>';
  415. }
  416. $i = 1;
  417. $online_icon = Display::return_icon('online.png', get_lang('Visible'), array('width' => '12px'));
  418. $offline_icon = Display::return_icon('offline.png', get_lang('Invisible'), array('width' => '12px'));
  419. $exercise_list = array();
  420. $exercise_obj = new Exercise();
  421. $list_ordered = $exercise_obj->get_exercise_list_ordered();
  422. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  423. $exercise_list[$row['iid']] = $row;
  424. }
  425. if (isset($list_ordered) && !empty($list_ordered)) {
  426. $new_question_list = array();
  427. foreach ($list_ordered as $exercise_id) {
  428. if (isset($exercise_list[$exercise_id])) {
  429. $new_question_list[] = $exercise_list[$exercise_id];
  430. }
  431. }
  432. $exercise_list = $new_question_list;
  433. }
  434. echo '<table class="'.Display::return_default_table_class().'">';
  435. /* Listing exercises */
  436. if (!empty($exercise_list)) {
  437. if ($origin != 'learnpath') {
  438. //avoid sending empty parameters
  439. $myorigin = (empty($origin) ? '' : '&origin='.$origin);
  440. $mylpid = (empty($learnpath_id) ? '' : '&learnpath_id='.$learnpath_id);
  441. $mylpitemid = (empty($learnpath_item_id) ? '' : '&learnpath_item_id='.$learnpath_item_id);
  442. $token = Security::get_token();
  443. $i = 1;
  444. if ($is_allowedToEdit) {
  445. $headers = array(
  446. array('name' => get_lang('ExerciseName')),
  447. array('name' => get_lang('QuantityQuestions'), 'params' => array('width' => '100px')),
  448. array('name' => get_lang('Actions'), 'params' => array('width' => '180px'))
  449. );
  450. } else {
  451. $headers = array(
  452. array('name' => get_lang('ExerciseName')),
  453. array('name' => get_lang('Status')),
  454. );
  455. }
  456. $header_list = '';
  457. foreach ($headers as $header) {
  458. $params = isset($header['params']) ? $header['params'] : null;
  459. $header_list .= Display::tag('th', $header['name'], $params);
  460. }
  461. echo Display::tag('tr', $header_list);
  462. $autolaunch_setting_on = api_get_course_setting('enable_exercise_auto_launch') == 1;
  463. $count = 0;
  464. if (!empty($exercise_list)) {
  465. foreach ($exercise_list as $row) {
  466. $my_exercise_id = $row['iid'];
  467. $exercise_obj = new Exercise();
  468. $exercise_obj->read($my_exercise_id, false);
  469. $locked = $exercise_obj->is_gradebook_locked;
  470. //echo '<div id="tabs-'.$i.'">';
  471. $i++;
  472. // Validation when belongs to a session
  473. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  474. $time_limits = false;
  475. if ($row['start_time'] != '0000-00-00 00:00:00' || $row['end_time'] != '0000-00-00 00:00:00') {
  476. $time_limits = true;
  477. }
  478. if ($time_limits) {
  479. // check if start time
  480. $start_time = false;
  481. if ($row['start_time'] != '0000-00-00 00:00:00') {
  482. $start_time = api_strtotime($row['start_time'], 'UTC');
  483. }
  484. $end_time = false;
  485. if ($row['end_time'] != '0000-00-00 00:00:00') {
  486. $end_time = api_strtotime($row['end_time'], 'UTC');
  487. }
  488. $now = time();
  489. $is_actived_time = false;
  490. //If both "clocks" are enable
  491. if ($start_time && $end_time) {
  492. if ($now > $start_time && $end_time > $now) {
  493. $is_actived_time = true;
  494. }
  495. } else {
  496. //we check the start and end
  497. if ($start_time) {
  498. if ($now > $start_time) {
  499. $is_actived_time = true;
  500. }
  501. }
  502. if ($end_time) {
  503. if ($end_time > $now) {
  504. $is_actived_time = true;
  505. }
  506. }
  507. }
  508. }
  509. //Blocking empty start times see BT#2800
  510. global $_custom;
  511. if (isset($_custom['exercises_hidden_when_no_start_date']) && $_custom['exercises_hidden_when_no_start_date']) {
  512. if (empty($row['start_time']) || $row['start_time'] == '0000-00-00 00:00:00') {
  513. $time_limits = true;
  514. $is_actived_time = false;
  515. }
  516. }
  517. $cut_title = $exercise_obj->getCutTitle();
  518. $alt_title = '';
  519. if ($cut_title != $row['title']) {
  520. $alt_title = ' title = "'.$row['title'].'" ';
  521. }
  522. // Teacher only
  523. if ($is_allowedToEdit) {
  524. $lp_blocked = null;
  525. if ($exercise_obj->exercise_was_added_in_lp == true) {
  526. $lp_blocked = Display::div(
  527. get_lang('AddedToLPCannotBeAccessed'),
  528. array('class' => 'lp_content_type_label')
  529. );
  530. }
  531. $visibility = api_get_item_visibility($course_info, TOOL_QUIZ, $my_exercise_id);
  532. if ($row['active'] == 0 || $visibility == 0) {
  533. $title = Display::tag('font', $cut_title, array('style' => 'color:grey'));
  534. } else {
  535. $title = $cut_title;
  536. }
  537. $count_exercise_not_validated = intval(
  538. count_exercise_result_not_validated($my_exercise_id, $courseId, $session_id)
  539. );
  540. $move = Display::return_icon(
  541. 'all_directions.png',
  542. get_lang('Move'),
  543. array('class' => 'moved', 'style' => 'margin-bottom:-0.5em;')
  544. );
  545. $class_tip = '';
  546. if (!empty($count_exercise_not_validated)) {
  547. $results_text = $count_exercise_not_validated == 1 ? get_lang('ResultNotRevised') : get_lang(
  548. 'ResultsNotRevised'
  549. );
  550. $title .= '<span class="exercise_tooltip" style="display: none;">'.$count_exercise_not_validated.' '.$results_text.' </span>';
  551. $class_tip = 'link_tooltip';
  552. }
  553. //$class_tip = 'exercise_link';
  554. $urlOverview = 'overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$my_exercise_id;
  555. $url = $move.'<a '.$alt_title.' class="'.$class_tip.'" id="tooltip_'.$my_exercise_id.'" href="'.$urlOverview.'">'.Display::return_icon('quiz.gif').' '.$title.' </a>';
  556. $item = Display::tag('td', $url.' '.$session_img.$lp_blocked);
  557. //Count number exercice - teacher
  558. $sqlquery = "SELECT count(*) FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND exercice_id = $my_exercise_id";
  559. $sqlresult = Database::query($sqlquery);
  560. $rowi = Database :: result($sqlresult, 0);
  561. if ($session_id == $row['session_id']) {
  562. //Settings
  563. $actions = Display::url(Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL), 'admin.php?'.api_get_cidreq().'&exerciseId='.$my_exercise_id);
  564. //Exercise results
  565. $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>';
  566. //Export
  567. $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));
  568. if ($autolaunch_setting_on) {
  569. $icon = Display::return_icon('launch.png', get_lang('AutoLaunch'));
  570. if ($row['autolaunch'] == 0) {
  571. $icon = Display::return_icon('launch_na.png', get_lang('AutoLaunch'));
  572. }
  573. $actions .= Display::url($icon, 'exercice.php?'.api_get_cidreq().'&choice=set_autolaunch&sec_token='.$token.'&exerciseId='.$my_exercise_id);
  574. }
  575. //Clean exercise
  576. if ($locked == false) {
  577. $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));
  578. } else {
  579. $actions .= Display::return_icon('clean_na.png', get_lang('ResourceLockedByGradebook'), '', ICON_SIZE_SMALL);
  580. }
  581. //Visible / invisible
  582. //Check if this exercise was added in a LP
  583. if ($exercise_obj->exercise_was_added_in_lp == true) {
  584. $actions .= Display::return_icon('invisible.png', get_lang('AddedToLPCannotBeAccessed'), '', ICON_SIZE_SMALL);
  585. } else {
  586. if ($row['active'] == 0 || $visibility == 0) {
  587. $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);
  588. } else {
  589. // else if not active
  590. $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);
  591. }
  592. }
  593. // Export qti ...
  594. $actions .= Display::url(Display::return_icon('export_qti2.png', 'IMS/QTI', '', ICON_SIZE_SMALL), 'exercice.php?choice=exportqti2&exerciseId='.$my_exercise_id);
  595. } else {
  596. // not session
  597. $actions = Display::return_icon('edit_na.png', get_lang('ExerciseEditionNotAvailableInSession'));
  598. $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>';
  599. $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));
  600. }
  601. //Delete
  602. if ($session_id == $row['session_id']) {
  603. if ($locked == false) {
  604. $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));
  605. } else {
  606. $actions .= Display::return_icon('delete_na.png', get_lang('ResourceLockedByGradebook'), '', ICON_SIZE_SMALL);
  607. }
  608. /*if ($rowi > 50) {
  609. // Showing the direct link to exercise settings only
  610. // if there are more than 50 questions, so the list of
  611. // questions does not slow down everything
  612. $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']);
  613. }*/
  614. }
  615. // Number of questions
  616. /*
  617. $random_label = null;
  618. if ($row['random'] > 0 || $row['random'] == -1) {
  619. // if random == -1 means use random questions with all questions
  620. $random_number_of_question = $row['random'];
  621. if ($random_number_of_question == -1) {
  622. $random_number_of_question = $rowi;
  623. }
  624. if ($row['random_by_category'] > 0) {
  625. $nbQuestionsTotal = $exercise_obj->getNumberQuestionExerciseCategory();
  626. //$real_question_count = count($exercise_obj->selectQuestionList());
  627. $warning = null;
  628. if ($nbQuestionsTotal != $real_question_count) {
  629. $warning = Display::return_icon('warning.png', get_lang('CheckThatYouHaveEnoughQuestionsInYourCategories'));
  630. }
  631. $number_of_questions = $nbQuestionsTotal." ".$warning;
  632. } else {
  633. $random_label = ' ('.get_lang('Random').') ';
  634. $number_of_questions = $random_number_of_question.' '.$random_label;
  635. //Bug if we set a random value bigger than the real number of questions
  636. if ($random_number_of_question > $rowi) {
  637. $number_of_questions = $rowi.' '.$random_label;
  638. }
  639. }
  640. } else {
  641. $number_of_questions = $rowi;
  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. Display::return_icon('hotpotatoes_s.png').'
  832. <a href="showinframes.php?file='.$path.'&cid='.api_get_course_id(
  833. ).'&uid='.api_get_user_id().'"'.(!$active ? 'class="invisible"' : '').'>'.$title.'</a> '
  834. );
  835. $item .= Display::tag('td', '-');
  836. $actions = Display::url(
  837. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL),
  838. 'adminhp.php?'.api_get_cidreq().'&hotpotatoesName='.$path
  839. );
  840. $actions .= '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq(
  841. ).'&path='.$path.'">'.Display :: return_icon(
  842. 'test_results.png',
  843. get_lang('Results'),
  844. '',
  845. ICON_SIZE_SMALL
  846. ).'</a>';
  847. // if active
  848. if ($active) {
  849. $nbrActiveTests = $nbrActiveTests + 1;
  850. $actions .= ' <a href="'.$exercicePath.'?'.api_get_cidreq(
  851. ).'&hpchoice=disable&amp;page='.$page.'&amp;file='.$path.'">'.Display::return_icon(
  852. 'visible.png',
  853. get_lang('Deactivate'),
  854. '',
  855. ICON_SIZE_SMALL
  856. ).'</a>';
  857. } else { // else if not active
  858. $actions .= ' <a href="'.$exercicePath.'?'.api_get_cidreq(
  859. ).'&hpchoice=enable&amp;page='.$page.'&amp;file='.$path.'">'.Display::return_icon(
  860. 'invisible.png',
  861. get_lang('Activate'),
  862. '',
  863. ICON_SIZE_SMALL
  864. ).'</a>';
  865. }
  866. $actions .= '<a href="'.$exercicePath.'?'.api_get_cidreq(
  867. ).'&amp;hpchoice=delete&amp;file='.$path.'" onclick="javascript:if(!confirm(\''.addslashes(
  868. api_htmlentities(get_lang('AreYouSureToDelete'), ENT_QUOTES, $charset).' '.$title."?"
  869. ).'\')) return false;">'.Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  870. $item .= Display::tag('td', $actions);
  871. echo Display::tag('tr', $item, array('class' => $class));
  872. } else {
  873. // Student only
  874. if ($active == 1) {
  875. $nbrActiveTests = $nbrActiveTests + 1;
  876. $item .= Display::tag(
  877. 'td',
  878. '<a href="showinframes.php?'.api_get_cidreq().'&file='.$path.'&cid='.api_get_course_id(
  879. ).'&uid='.api_get_user_id().'"'.(!$active ? 'class="invisible"' : '').'">'.$title.'</a>'
  880. );
  881. //$item .= Display::tag('td', '');
  882. $actions = '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq(
  883. ).'&path='.$path.'&filter_by_user='.api_get_user_id().'">'.Display :: return_icon(
  884. 'test_results.png',
  885. get_lang('Results'),
  886. '',
  887. ICON_SIZE_SMALL
  888. ).'</a>';
  889. $item .= Display::tag('td', $actions);
  890. echo Display::tag('tr', $item, array('class' => $class));
  891. }
  892. }
  893. $count++;
  894. }
  895. }
  896. echo '</table>';
  897. if (empty($exercise_list) && $hotpotatoes_exist == false) {
  898. if ($is_allowedToEdit && $origin != 'learnpath') {
  899. echo '<div id="no-data-view">';
  900. echo '<h2>'.get_lang('Quiz').'</h2>';
  901. echo Display::return_icon('quiz.png', '', array(), 64);
  902. echo '<div class="controls">';
  903. echo Display::url(get_lang('NewEx'), 'exercise_admin.php?'.api_get_cidreq(), array('class' => 'btn'));
  904. echo '</div>';
  905. echo '</div>';
  906. }
  907. }
  908. if ($origin != 'learnpath') { //so we are not in learnpath tool
  909. Display :: display_footer();
  910. }
  911. Session::erase('objExercise');
  912. Session::erase('objQuestion');
  913. Session::erase('objAnswer');