exercice.php 41 KB

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