exercice.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 '../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 'exercise.lib.php';
  31. require_once 'question.class.php';
  32. require_once 'answer.class.php';
  33. require_once 'testcategory.class.php';
  34. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  35. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  36. require_once 'hotpotatoes.lib.php';
  37. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  38. /* Constants and variables */
  39. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  40. $is_tutor = api_is_allowed_to_edit(true);
  41. $is_tutor_course = api_is_course_tutor();
  42. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  43. $TBL_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  44. $TBL_EXERCICE_QUESTION = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  45. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  46. $TBL_TRACK_EXERCICES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  47. // document path
  48. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path']."/document";
  49. // picture path
  50. $picturePath = $documentPath.'/images';
  51. // audio path
  52. $audioPath = $documentPath.'/audio';
  53. // hotpotatoes
  54. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  55. $exercicePath = api_get_self();
  56. $exfile = explode('/', $exercicePath);
  57. $exfile = strtolower($exfile[sizeof($exfile) - 1]);
  58. $exercicePath = substr($exercicePath, 0, strpos($exercicePath, $exfile));
  59. $exercicePath = $exercicePath."exercice.php";
  60. // Clear the exercise session
  61. if (isset($_SESSION['objExercise'])) {
  62. Session::erase('objExercise');
  63. }
  64. if (isset($_SESSION['objQuestion'])) {
  65. Session::erase('objQuestion');
  66. }
  67. if (isset($_SESSION['objAnswer'])) {
  68. Session::erase('objAnswer');
  69. }
  70. if (isset($_SESSION['questionList'])) {
  71. Session::erase('questionList');
  72. }
  73. if (isset($_SESSION['exerciseResult'])) {
  74. Session::erase('exerciseResult');
  75. }
  76. //General POST/GET/SESSION/COOKIES parameters recovery
  77. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : null;
  78. $choice = isset($_REQUEST['choice']) ? Security::remove_XSS($_REQUEST['choice']) : null;
  79. $hpchoice = isset($_REQUEST['hpchoice']) ? Security::remove_XSS($_REQUEST['hpchoice']) : null;
  80. $exerciseId = isset($_REQUEST['exerciseId']) ? Security::remove_XSS($_REQUEST['exerciseId']) : null;
  81. $file = isset($_REQUEST['file']) ? Database::escape_string($_REQUEST['file']) : null;
  82. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : null;
  83. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : null;
  84. $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : null;
  85. $course_info = api_get_course_info();
  86. $course_id = api_get_course_int_id();
  87. if ($page < 0) {
  88. $page = 1;
  89. }
  90. if (!empty($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  91. $_SESSION['gradebook'] = Security::remove_XSS($_GET['gradebook']);
  92. $gradebook = $_SESSION['gradebook'];
  93. } elseif (empty($_GET['gradebook'])) {
  94. unset($_SESSION['gradebook']);
  95. $gradebook = '';
  96. }
  97. if (!empty($gradebook) && $gradebook == 'view') {
  98. $interbreadcrumb[] = array('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('ToolGradebook'));
  99. }
  100. $nameTools = get_lang('Exercices');
  101. if ($is_allowedToEdit && !empty($choice) && $choice == 'exportqti2') {
  102. require_once 'export/qti2/qti2_export.php';
  103. $export = export_exercise($exerciseId, true);
  104. require_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  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. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  111. $temp_xml_file = $temp_zip_dir."/qti2export_".$exerciseId.'.xml';
  112. file_put_contents($temp_xml_file, $export);
  113. $zip_folder = new PclZip($temp_zip_file);
  114. $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH);
  115. $name = 'qti2_export_'.$exerciseId.'.zip';
  116. //DocumentManager::string_send_for_download($export,true,'qti2export_'.$exerciseId.'.xml');
  117. DocumentManager :: file_send_for_download($temp_zip_file, true, $name);
  118. unlink($temp_zip_file);
  119. unlink($temp_xml_file);
  120. rmdir($temp_zip_dir);
  121. exit; //otherwise following clicks may become buggy
  122. }
  123. $htmlHeadXtra[] = '<script>
  124. $(document).ready(function() {
  125. //this makes google chrome to crash ...
  126. /* $(".link_tooltip").each(function(){
  127. $(this).qtip({
  128. content: $(this).find(".exercise_tooltip"),
  129. position: { at:"top right", my:"bottom left"},
  130. show: {
  131. event: false,
  132. ready: true // ... but show the tooltip when ready
  133. },
  134. hide: true, //
  135. });
  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(api_get_course_id(), 1, $exerciseId, api_get_session_id());
  182. if ($link_info !== false) {
  183. remove_resource_from_course_gradebook($link_info['id']);
  184. }
  185. Display :: display_confirmation_message(get_lang('ExerciseDeleted'));
  186. }
  187. break;
  188. case 'enable' : // enables an exercise
  189. $objExerciseTmp->enable();
  190. $objExerciseTmp->save();
  191. api_item_property_update($course_info, TOOL_QUIZ, $objExerciseTmp->id, 'visible', api_get_user_id());
  192. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  193. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  194. break;
  195. case 'disable' : // disables an exercise
  196. $objExerciseTmp->disable();
  197. $objExerciseTmp->save();
  198. api_item_property_update($course_info, TOOL_QUIZ, $objExerciseTmp->id, 'invisible', api_get_user_id());
  199. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  200. break;
  201. case 'disable_results' : //disable the results for the learners
  202. $objExerciseTmp->disable_results();
  203. $objExerciseTmp->save();
  204. Display :: display_confirmation_message(get_lang('ResultsDisabled'));
  205. break;
  206. case 'enable_results' : //disable the results for the learners
  207. $objExerciseTmp->enable_results();
  208. $objExerciseTmp->save();
  209. Display :: display_confirmation_message(get_lang('ResultsEnabled'));
  210. break;
  211. case 'clean_results' : //clean student results
  212. if ($exercise_action_locked == false) {
  213. $quantity_results_deleted = $objExerciseTmp->clean_results();
  214. Display :: display_confirmation_message(sprintf(get_lang('XResultsCleaned'), $quantity_results_deleted));
  215. }
  216. break;
  217. case 'copy_exercise' : //copy an exercise
  218. $objExerciseTmp->copy_exercise();
  219. Display :: display_confirmation_message(get_lang('ExerciseCopied'));
  220. break;
  221. }
  222. }
  223. }
  224. // destruction of Exercise
  225. unset($objExerciseTmp);
  226. Security::clear_token();
  227. }
  228. if (!empty($hpchoice)) {
  229. switch ($hpchoice) {
  230. case 'delete' : // deletes an exercise
  231. $imgparams = array();
  232. $imgcount = 0;
  233. GetImgParams($file, $documentPath, $imgparams, $imgcount);
  234. $fld = GetFolderName($file);
  235. for ($i = 0; $i < $imgcount; $i++) {
  236. my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
  237. update_db_info("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
  238. }
  239. if (my_delete($documentPath.$file)) {
  240. update_db_info("delete", $file);
  241. }
  242. // hotpotatoes folder may contains several tests so don't delete folder if not empty : http://support.chamilo.org/issues/2165
  243. if (!(strstr($uploadPath, DIR_HOTPOTATOES) && !folder_is_empty($documentPath.$uploadPath."/".$fld."/"))) {
  244. my_delete($documentPath.$uploadPath."/".$fld."/");
  245. } break;
  246. case 'enable' : // enables an exercise
  247. $newVisibilityStatus = "1"; //"visible"
  248. $query = "SELECT id FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path='".Database :: escape_string($file)."'";
  249. $res = Database::query($query);
  250. $row = Database :: fetch_array($res, 'ASSOC');
  251. api_item_property_update($_course, TOOL_DOCUMENT, $row['id'], 'visible', $_user['user_id']);
  252. //$dialogBox = get_lang('ViMod');
  253. break;
  254. case 'disable' : // disables an exercise
  255. $newVisibilityStatus = "0"; //"invisible"
  256. $query = "SELECT id FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path='".Database :: escape_string($file)."'";
  257. $res = Database::query($query);
  258. $row = Database :: fetch_array($res, 'ASSOC');
  259. api_item_property_update($_course, TOOL_DOCUMENT, $row['id'], 'invisible', $_user['user_id']);
  260. break;
  261. default :
  262. break;
  263. }
  264. }
  265. }
  266. // Actions div bar
  267. if ($is_allowedToEdit) {
  268. echo '<div class="actions">';
  269. }
  270. // Selects $limit exercises at the same time
  271. // maximum number of exercises on a same page
  272. $limit = 50;
  273. // Display the next and previous link if needed
  274. $from = $page * $limit;
  275. HotPotGCt($documentPath, 1, api_get_user_id());
  276. //condition for the session
  277. $course_code = api_get_course_id();
  278. $session_id = api_get_session_id();
  279. $condition_session = api_get_session_condition($session_id, true, true);
  280. // Only for administrators
  281. if ($is_allowedToEdit) {
  282. $total_sql = "SELECT count(id) as count FROM $TBL_EXERCICES WHERE c_id = $course_id AND active<>'-1' $condition_session ";
  283. $sql = "SELECT * FROM $TBL_EXERCICES WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title LIMIT ".$from.",".$limit;
  284. } else {
  285. // Only for students
  286. $total_sql = "SELECT count(id) as count FROM $TBL_EXERCICES WHERE c_id = $course_id AND active = '1' $condition_session ";
  287. $sql = "SELECT * FROM $TBL_EXERCICES
  288. WHERE c_id = $course_id AND
  289. active='1' $condition_session
  290. ORDER BY title LIMIT ".$from.",".$limit;
  291. }
  292. $result = Database::query($sql);
  293. $exercises_count = Database :: num_rows($result);
  294. $result_total = Database::query($total_sql);
  295. $total_exercises = 0;
  296. if (Database :: num_rows($result_total)) {
  297. $result_total = Database::fetch_array($result_total);
  298. $total_exercises = $result_total['count'];
  299. }
  300. //get HotPotatoes files (active and inactive)
  301. if ($is_allowedToEdit) {
  302. $sql = "SELECT * FROM $TBL_DOCUMENT WHERE c_id = $course_id AND path LIKE '".Database :: escape_string($uploadPath)."/%/%'";
  303. $res = Database::query($sql);
  304. $hp_count = Database :: num_rows($res);
  305. } else {
  306. $sql = "SELECT * FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  307. WHERE d.id = ip.ref AND
  308. ip.tool = '".TOOL_DOCUMENT."' AND
  309. d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND
  310. ip.visibility ='1' AND
  311. d.c_id = ".$course_id." AND
  312. ip.c_id = ".$course_id;
  313. $res = Database::query($sql);
  314. $hp_count = Database :: num_rows($res);
  315. }
  316. $total = $total_exercises + $hp_count;
  317. if ($is_allowedToEdit && $origin != 'learnpath') {
  318. echo '<a href="exercise_admin.php?'.api_get_cidreq().'">'.Display :: return_icon('new_exercice.png', get_lang('NewEx'), '', ICON_SIZE_MEDIUM).'</a>';
  319. echo '<a href="question_create.php?'.api_get_cidreq().'">'.Display :: return_icon('new_question.png', get_lang('AddQ'), '', ICON_SIZE_MEDIUM).'</a>';
  320. // Question category
  321. echo '<a href="tests_category.php">';
  322. echo Display::return_icon('question_category_show.gif', get_lang('QuestionCategory'));
  323. echo '</a>';
  324. echo '<a href="question_pool.php">';
  325. echo Display::return_icon('database.png', get_lang('QuestionPool'), array('style' => 'width:32px'));
  326. echo '</a>';
  327. //echo Display::url(Display::return_icon('looknfeel.png', get_lang('Media')), 'media.php?' . api_get_cidreq());
  328. // end question category
  329. echo '<a href="hotpotatoes.php?'.api_get_cidreq().'">'.Display :: return_icon('import_hotpotatoes.png', get_lang('ImportHotPotatoesQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  330. // link to import qti2 ...
  331. echo '<a href="qti2.php?'.api_get_cidreq().'">'.Display :: return_icon('import_qti2.png', get_lang('ImportQtiQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  332. echo '<a href="upload_exercise.php?'.api_get_cidreq().'">'.Display :: return_icon('import_excel.png', get_lang('ImportExcelQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  333. }
  334. if ($is_allowedToEdit) {
  335. echo '</div>'; // closing the actions div
  336. //echo '<div id="message"></div>';
  337. }
  338. if ($total > $limit) {
  339. echo '<div style="float:right;height:20px;">';
  340. //show pages navigation link for previous page
  341. if ($page) {
  342. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;page=".($page - 1)."\">".Display :: return_icon('action_prev.png', get_lang('PreviousPage'))."</a>";
  343. } elseif ($total_exercises + $hp_count > $limit) {
  344. echo Display :: return_icon('action_prev_na.png', get_lang('PreviousPage'));
  345. }
  346. //show pages navigation link for previous page
  347. if ($total_exercises > $from + $limit || $hp_count > $from + $limit) {
  348. echo ' '."<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;page=".($page + 1)."\">".Display::return_icon('action_next.png', get_lang('NextPage'))."</a>";
  349. } elseif ($page) {
  350. echo ' '.Display :: return_icon('action_next_na.png', get_lang('NextPage'));
  351. }
  352. echo '</div>';
  353. }
  354. $i = 1;
  355. $online_icon = Display::return_icon('online.png', get_lang('Visible'), array('width' => '12px'));
  356. $offline_icon = Display::return_icon('offline.png', get_lang('Invisible'), array('width' => '12px'));
  357. $exercise_list = array();
  358. $exercise_obj = new Exercise();
  359. //$list_ordered = $exercise_obj->get_exercise_list_ordered();
  360. $list_ordered = null;
  361. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  362. $exercise_list[$row['id']] = $row;
  363. }
  364. if (isset($list_ordered) && !empty($list_ordered)) {
  365. $new_question_list = array();
  366. foreach ($list_ordered as $exercise_id) {
  367. if (isset($exercise_list[$exercise_id])) {
  368. $new_question_list[] = $exercise_list[$exercise_id];
  369. }
  370. }
  371. $exercise_list = $new_question_list;
  372. }
  373. echo '<table class="'.Display::return_default_table_class().'">';
  374. /* Listing exercises */
  375. if (!empty($exercise_list)) {
  376. if ($origin != 'learnpath') {
  377. //avoid sending empty parameters
  378. $myorigin = (empty($origin) ? '' : '&origin='.$origin);
  379. $mylpid = (empty($learnpath_id) ? '' : '&learnpath_id='.$learnpath_id);
  380. $mylpitemid = (empty($learnpath_item_id) ? '' : '&learnpath_item_id='.$learnpath_item_id);
  381. $token = Security::get_token();
  382. $i = 1;
  383. if ($is_allowedToEdit) {
  384. $headers = array(array('name' => get_lang('ExerciseName')),
  385. array('name' => get_lang('QuantityQuestions'), 'params' => array('width' => '100px')),
  386. array('name' => get_lang('Actions'), 'params' => array('width' => '180px')));
  387. } else {
  388. $headers = array(array('name' => get_lang('ExerciseName')),
  389. array('name' => get_lang('Status')),
  390. );
  391. }
  392. $header_list = '';
  393. foreach ($headers as $header) {
  394. $params = isset($header['params']) ? $header['params'] : null;
  395. $header_list .= Display::tag('th', $header['name'], $params);
  396. }
  397. echo Display::tag('tr', $header_list);
  398. $count = 0;
  399. if (!empty($exercise_list))
  400. foreach ($exercise_list as $row) {
  401. $my_exercise_id = $row['id'];
  402. $exercise_obj = new Exercise();
  403. $exercise_obj->read($my_exercise_id);
  404. if (empty($exercise_obj->id)) {
  405. continue;
  406. }
  407. $locked = $exercise_obj->is_gradebook_locked;
  408. //echo '<div id="tabs-'.$i.'">';
  409. $i++;
  410. //validacion when belongs to a session
  411. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  412. $time_limits = false;
  413. if ($row['start_time'] != '0000-00-00 00:00:00' || $row['end_time'] != '0000-00-00 00:00:00') {
  414. $time_limits = true;
  415. }
  416. if ($time_limits) {
  417. // check if start time
  418. $start_time = false;
  419. if ($row['start_time'] != '0000-00-00 00:00:00') {
  420. $start_time = api_strtotime($row['start_time'], 'UTC');
  421. }
  422. $end_time = false;
  423. if ($row['end_time'] != '0000-00-00 00:00:00') {
  424. $end_time = api_strtotime($row['end_time'], 'UTC');
  425. }
  426. $now = time();
  427. $is_actived_time = false;
  428. //If both "clocks" are enable
  429. if ($start_time && $end_time) {
  430. if ($now > $start_time && $end_time > $now) {
  431. $is_actived_time = true;
  432. }
  433. } else {
  434. //we check the start and end
  435. if ($start_time) {
  436. if ($now > $start_time) {
  437. $is_actived_time = true;
  438. }
  439. }
  440. if ($end_time) {
  441. if ($end_time > $now) {
  442. $is_actived_time = true;
  443. }
  444. }
  445. }
  446. }
  447. //Blocking empty start times see BT#2800
  448. global $_custom;
  449. if (isset($_custom['exercises_hidden_when_no_start_date']) && $_custom['exercises_hidden_when_no_start_date']) {
  450. if (empty($row['start_time']) || $row['start_time'] == '0000-00-00 00:00:00') {
  451. $time_limits = true;
  452. $is_actived_time = false;
  453. }
  454. }
  455. $cut_title = $exercise_obj->getCutTitle();
  456. $alt_title = '';
  457. if ($cut_title != $row['title']) {
  458. $alt_title = ' title = "'.$row['title'].'" ';
  459. }
  460. // Teacher only
  461. if ($is_allowedToEdit) {
  462. $lp_blocked = null;
  463. if ($exercise_obj->exercise_was_added_in_lp == true) {
  464. $lp_blocked = Display::div(get_lang('AddedToLPCannotBeAccessed'), array('class' => 'lp_content_type_label'));
  465. }
  466. $visibility = api_get_item_visibility($course_info, TOOL_QUIZ, $my_exercise_id);
  467. if ($row['active'] == 0 || $visibility == 0) {
  468. $title = Display::tag('font', $cut_title, array('style' => 'color:grey'));
  469. } else {
  470. $title = $cut_title;
  471. }
  472. $count_exercise_not_validated = intval(count_exercise_result_not_validated($my_exercise_id, $course_code, $session_id));
  473. $move = Display::return_icon('all_directions.png',get_lang('Move'), array('class'=>'moved', 'style'=>'margin-bottom:-0.5em;'));
  474. $move = null;
  475. $class_tip = '';
  476. if (!empty($count_exercise_not_validated)) {
  477. $results_text = $count_exercise_not_validated == 1 ? get_lang('ResultNotRevised') : get_lang('ResultsNotRevised');
  478. $title .= '<span class="exercise_tooltip" style="display: none;">'.$count_exercise_not_validated.' '.$results_text.' </span>';
  479. $class_tip = 'link_tooltip';
  480. }
  481. //$class_tip = 'exercise_link';
  482. $url = $move.'<a '.$alt_title.' class="'.$class_tip.'" id="tooltip_'.$row['id'].'" href="overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$row['id'].'"><img src="../img/quiz.gif" /> '.$title.' </a>';
  483. $item = Display::tag('td', $url.' '.$session_img.$lp_blocked);
  484. //Count number exercice - teacher
  485. $sqlquery = "SELECT count(*) FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND exercice_id = $my_exercise_id";
  486. $sqlresult = Database::query($sqlquery);
  487. $rowi = Database :: result($sqlresult, 0);
  488. if ($session_id == $row['session_id']) {
  489. //Settings
  490. $actions = Display::url(Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL), 'admin.php?'.api_get_cidreq().'&exerciseId='.$row['id']);
  491. //Exercise results
  492. $actions .='<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'.Display :: return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  493. //Export
  494. $actions .= Display::url(Display::return_icon('cd.gif', get_lang('CopyExercise')), '', array('onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToCopy'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=copy_exercise&sec_token='.$token.'&exerciseId='.$row['id']));
  495. //Clean exercise
  496. if ($locked == false) {
  497. $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='.$row['id']));
  498. } else {
  499. $actions .= Display::return_icon('clean_na.png', get_lang('ResourceLockedByGradebook'), '', ICON_SIZE_SMALL);
  500. }
  501. //Visible / invisible
  502. //Check if this exercise was added in a LP
  503. if ($exercise_obj->exercise_was_added_in_lp == true) {
  504. $actions .= Display::return_icon('invisible.png', get_lang('AddedToLPCannotBeAccessed'), '', ICON_SIZE_SMALL);
  505. } else {
  506. if ($row['active'] == 0 || $visibility == 0) {
  507. $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='.$row['id']);
  508. } else {
  509. // else if not active
  510. $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='.$row['id']);
  511. }
  512. }
  513. // Export qti ...
  514. $actions .= Display::url(Display::return_icon('export_qti2.png', 'IMS/QTI', '', ICON_SIZE_SMALL), 'exercice.php?choice=exportqti2&exerciseId='.$row['id']);
  515. } else {
  516. // not session
  517. $actions = Display::return_icon('edit_na.png', get_lang('ExerciseEditionNotAvailableInSession'));
  518. $actions .='<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'.Display :: return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  519. $actions .= Display::url(Display::return_icon('cd.gif', get_lang('CopyExercise')), '', array('onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToCopy'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=copy_exercise&sec_token='.$token.'&exerciseId='.$row['id']));
  520. }
  521. //Delete
  522. if ($session_id == $row['session_id']) {
  523. if ($locked == false) {
  524. $actions .= Display::url(Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL), '', array('onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToDelete'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice.php?'.api_get_cidreq().'&choice=delete&sec_token='.$token.'&exerciseId='.$row['id']));
  525. } else {
  526. $actions .= Display::return_icon('delete_na.png', get_lang('ResourceLockedByGradebook'), '', ICON_SIZE_SMALL);
  527. }
  528. }
  529. // Number of questions
  530. $random_label = null;
  531. if ($row['random'] > 0 || $row['random'] == -1) {
  532. // if random == -1 means use random questions with all questions
  533. $random_number_of_question = $row['random'];
  534. if ($random_number_of_question == -1) {
  535. $random_number_of_question = $rowi;
  536. }
  537. if ($row['random_by_category'] > 0) {
  538. $nbQuestionsTotal = Testcategory::getNumberOfQuestionRandomByCategory($my_exercise_id, $random_number_of_question);
  539. $number_of_questions = $nbQuestionsTotal." ";
  540. $number_of_questions .= ($nbQuestionsTotal > 1) ? get_lang("QuestionsLowerCase") : get_lang("QuestionLowerCase");
  541. $number_of_questions .= " - ";
  542. $number_of_questions .= min(Testcategory::getNumberMaxQuestionByCat($my_exercise_id), $random_number_of_question).' '.get_lang('QuestionByCategory');
  543. } else {
  544. $random_label = ' ('.get_lang('Random').') ';
  545. $number_of_questions = $random_number_of_question.' '.$random_label;
  546. //Bug if we set a random value bigger than the real number of questions
  547. if ($random_number_of_question > $rowi) {
  548. $number_of_questions = $rowi.' '.$random_label;
  549. }
  550. }
  551. } else {
  552. $number_of_questions = $rowi;
  553. }
  554. //Attempts
  555. //$attempts = get_count_exam_results($row['id']).' '.get_lang('Attempts');
  556. //$item .= Display::tag('td',$attempts);
  557. $item .= Display::tag('td', $number_of_questions);
  558. } else {
  559. // Student only
  560. $visibility = api_get_item_visibility($course_info, TOOL_QUIZ, $my_exercise_id);
  561. if ($visibility == 0) {
  562. continue;
  563. }
  564. // if time is actived show link to exercise
  565. if ($time_limits) {
  566. if ($is_actived_time) {
  567. $url = '<a '.$alt_title.' href="overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$row['id'].'">'.$cut_title.'</a>';
  568. } else {
  569. $url = $row['title'];
  570. }
  571. } else {
  572. $url = '<a '.$alt_title.' href="overview.php?'.api_get_cidreq().$myorigin.$mylpid.$mylpitemid.'&exerciseId='.$row['id'].'">'.$cut_title.'</a>';
  573. }
  574. //Link of the exercise
  575. $item = Display::tag('td', $url.' '.$session_img);
  576. //count number exercise questions
  577. $sqlquery = "SELECT count(*) FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND exercice_id = ".$row['id'];
  578. $sqlresult = Database::query($sqlquery);
  579. $rowi = Database::result($sqlresult, 0);
  580. if ($row['random'] > 0) {
  581. $row['random'].' '.api_strtolower(get_lang(($row['random'] > 1 ? 'Questions' : 'Question')));
  582. } else {
  583. //show results student
  584. $rowi.' '.api_strtolower(get_lang(($rowi > 1 ? 'Questions' : 'Question')));
  585. }
  586. //This query might be improved later on by ordering by the new "tms" field rather than by exe_id
  587. //Don't remove this marker: note-query-exe-results
  588. $qry = "SELECT * FROM $TBL_TRACK_EXERCICES
  589. WHERE exe_exo_id = ".$row['id']." AND
  590. exe_user_id = ".api_get_user_id()." AND
  591. exe_cours_id = '".api_get_course_id()."' AND
  592. status <> 'incomplete' AND
  593. orig_lp_id = 0 AND
  594. orig_lp_item_id = 0 AND
  595. session_id = '".api_get_session_id()."'
  596. ORDER BY exe_id DESC";
  597. $qryres = Database::query($qry);
  598. $num = Database :: num_rows($qryres);
  599. //Hide the results
  600. $my_result_disabled = $row['results_disabled'];
  601. //Time limits are on
  602. if ($time_limits) {
  603. // Examn is ready to be taken
  604. if ($is_actived_time) {
  605. //Show results
  606. if ($my_result_disabled == 0 || $my_result_disabled == 2) {
  607. //More than one attempt
  608. if ($num > 0) {
  609. $row_track = Database :: fetch_array($qryres);
  610. $attempt_text = get_lang('LatestAttempt').' : ';
  611. $attempt_text .= show_score($row_track['exe_result'], $row_track['exe_weighting']);
  612. } else {
  613. //No attempts
  614. $attempt_text = get_lang('NotAttempted');
  615. }
  616. } else {
  617. $attempt_text = get_lang('CantShowResults');
  618. }
  619. } else {
  620. //Quiz not ready due to time limits
  621. //@todo use the is_visible function
  622. if ($row['start_time'] != '0000-00-00 00:00:00' && $row['end_time'] != '0000-00-00 00:00:00') {
  623. $attempt_text = sprintf(get_lang('ExerciseWillBeActivatedFromXToY'), api_convert_and_format_date($row['start_time']), api_convert_and_format_date($row['end_time']));
  624. } else {
  625. //$attempt_text = get_lang('ExamNotAvailableAtThisTime');
  626. if ($row['start_time'] != '0000-00-00 00:00:00') {
  627. $attempt_text = sprintf(get_lang('ExerciseAvailableFromX'), api_convert_and_format_date($row['start_time']));
  628. }
  629. if ($row['end_time'] != '0000-00-00 00:00:00') {
  630. $attempt_text = sprintf(get_lang('ExerciseAvailableUntilX'), api_convert_and_format_date($row['end_time']));
  631. }
  632. }
  633. }
  634. } else {
  635. //Normal behaviour
  636. //Show results
  637. if ($my_result_disabled == 0 || $my_result_disabled == 2) {
  638. if ($num > 0) {
  639. $row_track = Database :: fetch_array($qryres);
  640. $attempt_text = get_lang('LatestAttempt').' : ';
  641. $attempt_text .= show_score($row_track['exe_result'], $row_track['exe_weighting']);
  642. } else {
  643. $attempt_text = get_lang('NotAttempted');
  644. }
  645. } else {
  646. $attempt_text = get_lang('CantShowResults');
  647. }
  648. }
  649. $class_tip = '';
  650. if (empty($num)) {
  651. $num = '';
  652. } else {
  653. $class_tip = 'link_tooltip';
  654. //@todo use sprintf and show the results validated by the teacher
  655. if ($num == 1) {
  656. $num = $num.' '.get_lang('Result');
  657. } else {
  658. $num = $num.' '.get_lang('Results');
  659. }
  660. $num = '<span class="tooltip" style="display: none;">'.$num.'</span>';
  661. }
  662. $item .= Display::tag('td', $attempt_text);
  663. //See results
  664. //$actions = '<a class="'.$class_tip.'" id="tooltip_'.$row['id'].'" href="exercise_report.php?' . api_get_cidreq() . '&exerciseId='.$row['id'].'">'.$num.Display::return_icon('test_results.png', get_lang('Results'),'',ICON_SIZE_SMALL).' </a>';
  665. }
  666. $class = 'row_even';
  667. if ($count % 2) {
  668. $class = 'row_odd';
  669. }
  670. if ($is_allowedToEdit) {
  671. $item .= Display::tag('td', $actions, array('class' => 'td_actions'));
  672. }
  673. echo Display::tag('tr', $item, array('id' => 'exercise_list_'.$my_exercise_id, 'class' => $class));
  674. $count++;
  675. } // end foreach()
  676. }
  677. }
  678. // end exercise list
  679. //Hotpotatoes results
  680. $hotpotatoes_exist = false;
  681. if ($is_allowedToEdit) {
  682. $sql = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility
  683. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  684. WHERE d.c_id = $course_id AND
  685. ip.c_id = $course_id AND
  686. d.id = ip.ref AND
  687. ip.tool = '".TOOL_DOCUMENT."' AND
  688. (d.path LIKE '%htm%') AND
  689. d.path LIKE '".Database :: escape_string($uploadPath)."/%/%'
  690. LIMIT ".$from.",".$limit; // only .htm or .html files listed
  691. } else {
  692. $sql = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility
  693. FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
  694. WHERE d.c_id = $course_id AND
  695. ip.c_id = $course_id AND
  696. d.id = ip.ref AND ip.tool = '".TOOL_DOCUMENT."' AND (d.path LIKE '%htm%')
  697. AND d.path LIKE '".Database :: escape_string($uploadPath)."/%/%' AND ip.visibility='1'
  698. LIMIT ".$from.",".$limit;
  699. }
  700. $result = Database::query($sql);
  701. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  702. $attribute['path'][] = $row['path'];
  703. $attribute['visibility'][] = $row['visibility'];
  704. $attribute['comment'][] = $row['comment'];
  705. }
  706. $nbrActiveTests = 0;
  707. if (isset($attribute['path']) && is_array($attribute['path'])) {
  708. $hotpotatoes_exist = true;
  709. while (list($key, $path) = each($attribute['path'])) {
  710. $item = '';
  711. list ($a, $vis) = each($attribute['visibility']);
  712. if (strcmp($vis, "1") == 0) {
  713. $active = 1;
  714. } else {
  715. $active = 0;
  716. }
  717. $title = GetQuizName($path, $documentPath);
  718. if ($title == '') {
  719. $title = basename($path);
  720. }
  721. $class = 'row_even';
  722. if ($count % 2) {
  723. $class = 'row_odd';
  724. }
  725. // prof only
  726. if ($is_allowedToEdit) {
  727. $item = Display::tag('td', '<img src="../img/hotpotatoes_s.png" alt="HotPotatoes" /> <a href="showinframes.php?file='.$path.'&cid='.api_get_course_id().'&uid='.api_get_user_id().'"'.(!$active ? 'class="invisible"' : '').'>'.$title.'</a> ');
  728. $item .= Display::tag('td', '-');
  729. $actions = Display::url(Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL), 'adminhp.php?'.api_get_cidreq().'&hotpotatoesName='.$path);
  730. $actions .='<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq().'&path='.$path.'">'.Display :: return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  731. // if active
  732. if ($active) {
  733. $nbrActiveTests = $nbrActiveTests + 1;
  734. $actions .= ' <a href="'.$exercicePath.'?'.api_get_cidreq().'&hpchoice=disable&amp;page='.$page.'&amp;file='.$path.'">'.Display::return_icon('visible.png', get_lang('Deactivate'), '', ICON_SIZE_SMALL).'</a>';
  735. } else { // else if not active
  736. $actions .=' <a href="'.$exercicePath.'?'.api_get_cidreq().'&hpchoice=enable&amp;page='.$page.'&amp;file='.$path.'">'.Display::return_icon('invisible.png', get_lang('Activate'), '', ICON_SIZE_SMALL).'</a>';
  737. }
  738. $actions .= '<a href="'.$exercicePath.'?'.api_get_cidreq().'&amp;hpchoice=delete&amp;file='.$path.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('AreYouSureToDelete'), ENT_QUOTES, $charset).' '.$title."?").'\')) return false;">'.Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  739. //$actions .='<img src="../img/lp_quiz_na.gif" border="0" title="'.get_lang('NotMarkActivity').'" alt="" />';
  740. $item .= Display::tag('td', $actions);
  741. echo Display::tag('tr', $item, array('class' => $class));
  742. } else { // student only
  743. if ($active == 1) {
  744. $nbrActiveTests = $nbrActiveTests + 1;
  745. $item .= Display::tag('td', '<a href="showinframes.php?'.api_get_cidreq().'&file='.$path.'&cid='.api_get_course_id().'&uid='.api_get_user_id().'"'.(!$active ? 'class="invisible"' : '').'">'.$title.'</a>');
  746. //$item .= Display::tag('td', '');
  747. $actions = '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq().'&path='.$path.'&filter_by_user='.api_get_user_id().'">'.Display :: return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  748. $item .= Display::tag('td', $actions);
  749. echo Display::tag('tr', $item, array('class' => $class));
  750. }
  751. }
  752. $count++;
  753. }
  754. }
  755. echo '</table>';
  756. if (empty($exercise_list) && $hotpotatoes_exist == false) {
  757. if ($is_allowedToEdit && $origin != 'learnpath') {
  758. echo '<div id="no-data-view">';
  759. echo '<h2>'.get_lang('Quiz').'</h2>';
  760. echo Display::return_icon('quiz.png', '', array(), 64);
  761. echo '<div class="controls">';
  762. echo Display::url(get_lang('NewEx'), 'exercise_admin.php?'.api_get_cidreq(), array('class' => 'btn'));
  763. echo '</div>';
  764. echo '</div>';
  765. }
  766. }
  767. if ($origin != 'learnpath') { //so we are not in learnpath tool
  768. Display :: display_footer();
  769. }