exercise.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise list: This script shows the list of exercises for administrators and students.
  6. *
  7. * @package chamilo.exercise
  8. *
  9. * @author Olivier Brouckaert, original author
  10. * @author Denes Nagy, HotPotatoes integration
  11. * @author Wolfgang Schneider, code/html cleanup
  12. * @author Julio Montoya <gugli100@gmail.com>, lots of cleanup + several improvements
  13. * Modified by hubert.borderiou (question category)
  14. */
  15. // including the global library
  16. require_once __DIR__.'/../inc/global.inc.php';
  17. $current_course_tool = TOOL_QUIZ;
  18. // Setting the tabs
  19. $this_section = SECTION_COURSES;
  20. $htmlHeadXtra[] = api_get_asset('qtip2/jquery.qtip.min.js');
  21. $htmlHeadXtra[] = api_get_css_asset('qtip2/jquery.qtip.min.css');
  22. // Access control
  23. api_protect_course_script(true);
  24. $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
  25. require_once 'hotpotatoes.lib.php';
  26. /* Constants and variables */
  27. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  28. $is_tutor = api_is_allowed_to_edit(true);
  29. $is_tutor_course = api_is_course_tutor();
  30. $courseInfo = api_get_course_info();
  31. $courseId = $courseInfo['real_id'];
  32. $userInfo = api_get_user_info();
  33. $userId = $userInfo['id'];
  34. $sessionId = api_get_session_id();
  35. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  36. $userId,
  37. $courseInfo
  38. );
  39. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  40. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  41. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  42. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  43. $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  44. // document path
  45. $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
  46. // picture path
  47. $picturePath = $documentPath.'/images';
  48. // audio path
  49. $audioPath = $documentPath.'/audio';
  50. // hot potatoes
  51. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  52. $exercisePath = api_get_self();
  53. $exfile = explode('/', $exercisePath);
  54. $exfile = strtolower($exfile[sizeof($exfile) - 1]);
  55. $exercisePath = substr($exercisePath, 0, strpos($exercisePath, $exfile));
  56. $exercisePath = $exercisePath.'exercise.php';
  57. // Clear the exercise session
  58. Session::erase('objExercise');
  59. Session::erase('objQuestion');
  60. Session::erase('objAnswer');
  61. Session::erase('questionList');
  62. Session::erase('exerciseResult');
  63. Session::erase('firstTime');
  64. Session::erase('calculatedAnswerId');
  65. Session::erase('duration_time_previous');
  66. Session::erase('duration_time');
  67. //General POST/GET/SESSION/COOKIES parameters recovery
  68. $origin = api_get_origin();
  69. $choice = isset($_REQUEST['choice']) ? Security::remove_XSS($_REQUEST['choice']) : null;
  70. $hpchoice = isset($_REQUEST['hpchoice']) ? Security::remove_XSS($_REQUEST['hpchoice']) : null;
  71. $exerciseId = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : null;
  72. $file = isset($_REQUEST['file']) ? Database::escape_string($_REQUEST['file']) : null;
  73. $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : null;
  74. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : null;
  75. $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : null;
  76. if ($page < 0) {
  77. $page = 1;
  78. }
  79. if (api_is_in_gradebook()) {
  80. $interbreadcrumb[] = [
  81. 'url' => Category::getUrl(),
  82. 'name' => get_lang('ToolGradebook'),
  83. ];
  84. }
  85. $autoLaunchAvailable = false;
  86. if (api_get_course_setting('enable_exercise_auto_launch') == 1 &&
  87. api_get_configuration_value('allow_exercise_auto_launch')
  88. ) {
  89. $autoLaunchAvailable = true;
  90. }
  91. $nameTools = get_lang('Exercises');
  92. $errorXmlExport = null;
  93. if ($is_allowedToEdit && !empty($choice) && $choice === 'exportqti2') {
  94. if ($limitTeacherAccess && !api_is_platform_admin()) {
  95. api_not_allowed(true);
  96. }
  97. require_once api_get_path(SYS_CODE_PATH).'exercise/export/qti2/qti2_export.php';
  98. $export = export_exercise_to_qti($exerciseId, true);
  99. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  100. $temp_dir_short = api_get_unique_id();
  101. $temp_zip_dir = $archive_path.$temp_dir_short;
  102. if (!is_dir($temp_zip_dir)) {
  103. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  104. }
  105. $temp_zip_file = $temp_zip_dir."/".api_get_unique_id().".zip";
  106. $temp_xml_file = $temp_zip_dir."/qti2export_".$exerciseId.'.xml';
  107. file_put_contents($temp_xml_file, $export);
  108. $xmlReader = new XMLReader();
  109. $xmlReader->open($temp_xml_file);
  110. $xmlReader->setParserProperty(XMLReader::VALIDATE, true);
  111. $isValid = $xmlReader->isValid();
  112. if ($isValid) {
  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::file_send_for_download($temp_zip_file, true, $name);
  117. unlink($temp_zip_file);
  118. unlink($temp_xml_file);
  119. rmdir($temp_zip_dir);
  120. exit; //otherwise following clicks may become buggy
  121. } else {
  122. $errorXmlExport = Display::return_message(get_lang('ErrorWritingXMLFile'), 'error');
  123. }
  124. }
  125. if ($origin != 'learnpath') {
  126. //so we are not in learnpath tool
  127. Display :: display_header($nameTools, get_lang('Exercise'));
  128. if (isset($_GET['message']) && in_array($_GET['message'], ['ExerciseEdited'])) {
  129. echo Display::return_message(get_lang('ExerciseEdited'), 'confirmation');
  130. }
  131. } else {
  132. Display::display_reduced_header();
  133. }
  134. Event::event_access_tool(TOOL_QUIZ);
  135. $logInfo = [
  136. 'tool' => TOOL_QUIZ,
  137. 'tool_id' => (int) $exerciseId,
  138. 'tool_id_detail' => 0,
  139. 'action' => isset($_REQUEST['learnpath_id']) ? 'learnpath_id' : '',
  140. 'action_details' => isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : '',
  141. ];
  142. Event::registerLog($logInfo);
  143. // Tool introduction
  144. Display::display_introduction_section(TOOL_QUIZ);
  145. if (!empty($errorXmlExport)) {
  146. echo $errorXmlExport;
  147. }
  148. HotPotGCt($documentPath, 1, $userId);
  149. // Only for administrator
  150. if ($is_allowedToEdit) {
  151. if (!empty($choice)) {
  152. // All test choice, clean all test's results
  153. if ($choice === 'clean_all_test') {
  154. $check = Security::check_token('get');
  155. if ($check) {
  156. if ($limitTeacherAccess && !api_is_platform_admin()) {
  157. api_not_allowed(true);
  158. }
  159. // list of exercises in a course/session
  160. // we got variable $courseId $courseInfo session api_get_session_id()
  161. $exerciseList = ExerciseLib::get_all_exercises_for_course_id(
  162. $courseInfo,
  163. $sessionId,
  164. $courseId,
  165. false
  166. );
  167. $quantity_results_deleted = 0;
  168. foreach ($exerciseList as $exeItem) {
  169. // delete result for test, if not in a gradebook
  170. $exercise_action_locked = api_resource_is_locked_by_gradebook($exeItem['id'], LINK_EXERCISE);
  171. if ($exercise_action_locked == false) {
  172. $objExerciseTmp = new Exercise();
  173. if ($objExerciseTmp->read($exeItem['id'])) {
  174. $quantity_results_deleted += $objExerciseTmp->cleanResults(true);
  175. }
  176. }
  177. }
  178. echo Display::return_message(
  179. sprintf(
  180. get_lang('XResultsCleaned'),
  181. $quantity_results_deleted
  182. ),
  183. 'confirm'
  184. );
  185. }
  186. }
  187. // single exercise choice
  188. // construction of Exercise
  189. $objExerciseTmp = new Exercise();
  190. $check = Security::check_token('get');
  191. $exercise_action_locked = api_resource_is_locked_by_gradebook(
  192. $exerciseId,
  193. LINK_EXERCISE
  194. );
  195. if ($objExerciseTmp->read($exerciseId)) {
  196. if ($check) {
  197. switch ($choice) {
  198. case 'enable_launch':
  199. $objExerciseTmp->cleanCourseLaunchSettings();
  200. $objExerciseTmp->enableAutoLaunch();
  201. Display::addFlash(Display::return_message(get_lang('Updated')));
  202. break;
  203. case 'disable_launch':
  204. $objExerciseTmp->cleanCourseLaunchSettings();
  205. break;
  206. case 'delete':
  207. // deletes an exercise
  208. if ($exercise_action_locked == false) {
  209. if ($limitTeacherAccess && !api_is_platform_admin()) {
  210. // Teacher cannot delete an exercise
  211. break;
  212. }
  213. $objExerciseTmp->delete();
  214. $link_info = GradebookUtils::isResourceInCourseGradebook(
  215. api_get_course_id(),
  216. 1,
  217. $exerciseId,
  218. api_get_session_id()
  219. );
  220. if ($link_info !== false) {
  221. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  222. }
  223. echo Display::return_message(get_lang('ExerciseDeleted'), 'confirmation');
  224. }
  225. break;
  226. case 'enable':
  227. if ($limitTeacherAccess && !api_is_platform_admin()) {
  228. // Teacher change exercise
  229. break;
  230. }
  231. // enables an exercise
  232. if (empty($sessionId)) {
  233. $objExerciseTmp->enable();
  234. $objExerciseTmp->save();
  235. } else {
  236. if (!empty($objExerciseTmp->sessionId)) {
  237. $objExerciseTmp->enable();
  238. $objExerciseTmp->save();
  239. }
  240. }
  241. api_item_property_update(
  242. $courseInfo,
  243. TOOL_QUIZ,
  244. $objExerciseTmp->id,
  245. 'visible',
  246. $userId
  247. );
  248. // "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
  249. echo Display::return_message(get_lang('VisibilityChanged'), 'confirmation');
  250. break;
  251. case 'disable':
  252. if ($limitTeacherAccess && !api_is_platform_admin()) {
  253. // Teacher change exercise
  254. break;
  255. }
  256. // disables an exercise
  257. if (empty($sessionId)) {
  258. $objExerciseTmp->disable();
  259. $objExerciseTmp->save();
  260. } else {
  261. // Only change active if it belongs to a session
  262. if (!empty($objExerciseTmp->sessionId)) {
  263. $objExerciseTmp->disable();
  264. $objExerciseTmp->save();
  265. }
  266. }
  267. api_item_property_update(
  268. $courseInfo,
  269. TOOL_QUIZ,
  270. $objExerciseTmp->id,
  271. 'invisible',
  272. $userId
  273. );
  274. echo Display::return_message(
  275. get_lang('VisibilityChanged'),
  276. 'confirmation'
  277. );
  278. break;
  279. case 'disable_results':
  280. //disable the results for the learners
  281. $objExerciseTmp->disable_results();
  282. $objExerciseTmp->save();
  283. echo Display::return_message(
  284. get_lang('ResultsDisabled'),
  285. 'confirmation'
  286. );
  287. break;
  288. case 'enable_results':
  289. //disable the results for the learners
  290. $objExerciseTmp->enable_results();
  291. $objExerciseTmp->save();
  292. echo Display::return_message(
  293. get_lang('ResultsEnabled'),
  294. 'confirmation'
  295. );
  296. break;
  297. case 'clean_results':
  298. if ($limitTeacherAccess && !api_is_platform_admin()) {
  299. // Teacher change exercise
  300. break;
  301. }
  302. // Clean student results
  303. if ($exercise_action_locked == false) {
  304. $quantity_results_deleted = $objExerciseTmp->cleanResults(true);
  305. $title = $objExerciseTmp->selectTitle();
  306. echo Display::return_message(
  307. $title.': '.sprintf(
  308. get_lang('XResultsCleaned'),
  309. $quantity_results_deleted
  310. ),
  311. 'confirmation'
  312. );
  313. }
  314. break;
  315. case 'copy_exercise': //copy an exercise
  316. api_set_more_memory_and_time_limits();
  317. $objExerciseTmp->copyExercise();
  318. echo Display::return_message(
  319. get_lang('ExerciseCopied'),
  320. 'confirmation'
  321. );
  322. break;
  323. }
  324. }
  325. }
  326. // destruction of Exercise
  327. unset($objExerciseTmp);
  328. Security::clear_token();
  329. }
  330. if (!empty($hpchoice)) {
  331. switch ($hpchoice) {
  332. case 'delete':
  333. if ($limitTeacherAccess && !api_is_platform_admin()) {
  334. // Teacher change exercise
  335. break;
  336. }
  337. // deletes an exercise
  338. $imgparams = [];
  339. $imgcount = 0;
  340. GetImgParams($file, $documentPath, $imgparams, $imgcount);
  341. $fld = GetFolderName($file);
  342. for ($i = 0; $i < $imgcount; $i++) {
  343. my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
  344. DocumentManager::updateDbInfo("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
  345. }
  346. if (!is_dir($documentPath.$uploadPath."/".$fld."/")) {
  347. my_delete($documentPath.$file);
  348. DocumentManager::updateDbInfo("delete", $file);
  349. } else {
  350. if (my_delete($documentPath.$file)) {
  351. DocumentManager::updateDbInfo("delete", $file);
  352. }
  353. }
  354. /* hotpotatoes folder may contains several tests so
  355. don't delete folder if not empty :
  356. http://support.chamilo.org/issues/2165
  357. */
  358. if (!(strstr($uploadPath, DIR_HOTPOTATOES) && !folder_is_empty($documentPath.$uploadPath."/".$fld."/"))) {
  359. my_delete($documentPath.$uploadPath."/".$fld."/");
  360. }
  361. break;
  362. case 'enable': // enables an exercise
  363. if ($limitTeacherAccess && !api_is_platform_admin()) {
  364. // Teacher change exercise
  365. break;
  366. }
  367. $newVisibilityStatus = '1'; //"visible"
  368. $query = "SELECT id FROM $TBL_DOCUMENT
  369. WHERE c_id = $courseId AND path='".Database::escape_string($file)."'";
  370. $res = Database::query($query);
  371. $row = Database :: fetch_array($res, 'ASSOC');
  372. api_item_property_update(
  373. $courseInfo,
  374. TOOL_DOCUMENT,
  375. $row['id'],
  376. 'visible',
  377. $userId
  378. );
  379. //$dialogBox = get_lang('ViMod');
  380. break;
  381. case 'disable': // disables an exercise
  382. if ($limitTeacherAccess && !api_is_platform_admin()) {
  383. // Teacher change exercise
  384. break;
  385. }
  386. $newVisibilityStatus = '0'; //"invisible"
  387. $query = "SELECT id FROM $TBL_DOCUMENT
  388. WHERE c_id = $courseId AND path='".Database::escape_string($file)."'";
  389. $res = Database::query($query);
  390. $row = Database :: fetch_array($res, 'ASSOC');
  391. api_item_property_update(
  392. $courseInfo,
  393. TOOL_DOCUMENT,
  394. $row['id'],
  395. 'invisible',
  396. $userId
  397. );
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. }
  404. // Actions div bar
  405. if ($is_allowedToEdit) {
  406. echo '<div class="actions">';
  407. }
  408. // Selects $limit exercises at the same time
  409. // maximum number of exercises on a same page
  410. $limit = 50;
  411. // Display the next and previous link if needed
  412. $from = $page * $limit;
  413. HotPotGCt($documentPath, 1, $userId);
  414. //condition for the session
  415. $course_code = api_get_course_id();
  416. $condition_session = api_get_session_condition($sessionId, true, true);
  417. // Only for administrators
  418. if ($is_allowedToEdit) {
  419. $total_sql = "SELECT count(iid) as count
  420. FROM $TBL_EXERCISES
  421. WHERE c_id = $courseId AND active<>'-1' $condition_session ";
  422. $sql = "SELECT * FROM $TBL_EXERCISES
  423. WHERE c_id = $courseId AND active<>'-1' $condition_session
  424. ORDER BY title
  425. LIMIT ".$from.",".$limit;
  426. } else {
  427. // Only for students
  428. $total_sql = "SELECT count(iid) as count
  429. FROM $TBL_EXERCISES
  430. WHERE c_id = $courseId AND active = '1' $condition_session ";
  431. $sql = "SELECT * FROM $TBL_EXERCISES
  432. WHERE c_id = $courseId AND
  433. active='1' $condition_session
  434. ORDER BY title LIMIT ".$from.",".$limit;
  435. }
  436. $result = Database::query($sql);
  437. $result_total = Database::query($total_sql);
  438. $total_exercises = 0;
  439. if (Database :: num_rows($result_total)) {
  440. $result_total = Database::fetch_array($result_total);
  441. $total_exercises = $result_total['count'];
  442. }
  443. //get HotPotatoes files (active and inactive)
  444. if ($is_allowedToEdit) {
  445. $sql = "SELECT * FROM $TBL_DOCUMENT
  446. WHERE
  447. c_id = $courseId AND
  448. path LIKE '".Database::escape_string($uploadPath.'/%/%')."'";
  449. $res = Database::query($sql);
  450. $hp_count = Database :: num_rows($res);
  451. } else {
  452. $sql = "SELECT * FROM $TBL_DOCUMENT d
  453. INNER JOIN $TBL_ITEM_PROPERTY ip
  454. ON (d.id = ip.ref AND d.c_id = ip.c_id)
  455. WHERE
  456. ip.tool = '".TOOL_DOCUMENT."' AND
  457. d.path LIKE '".Database::escape_string($uploadPath.'/%/%')."' AND
  458. ip.visibility ='1' AND
  459. d.c_id = $courseId AND
  460. ip.c_id = $courseId";
  461. $res = Database::query($sql);
  462. $hp_count = Database::num_rows($res);
  463. }
  464. $total = $total_exercises + $hp_count;
  465. $token = Security::get_token();
  466. if ($is_allowedToEdit && $origin != 'learnpath') {
  467. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise_admin.php?'.api_get_cidreq().'">'.
  468. Display::return_icon('new_exercice.png', get_lang('NewEx'), '', ICON_SIZE_MEDIUM).'</a>';
  469. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_create.php?'.api_get_cidreq().'">'.
  470. Display::return_icon('new_question.png', get_lang('AddQ'), '', ICON_SIZE_MEDIUM).'</a>';
  471. // Question category
  472. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/tests_category.php?'.api_get_cidreq().'">';
  473. echo Display::return_icon('green_open.png', get_lang('QuestionCategory'), '', ICON_SIZE_MEDIUM);
  474. echo '</a>';
  475. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/question_pool.php?'.api_get_cidreq().'">';
  476. echo Display::return_icon('database.png', get_lang('QuestionPool'), '', ICON_SIZE_MEDIUM);
  477. echo '</a>';
  478. //echo Display::url(Display::return_icon('looknfeel.png', get_lang('Media')), 'media.php?' . api_get_cidreq());
  479. // end question category
  480. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/hotpotatoes.php?'.api_get_cidreq().'">'.Display::return_icon('import_hotpotatoes.png', get_lang('ImportHotPotatoesQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  481. // link to import qti2 ...
  482. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/qti2.php?'.api_get_cidreq().'">'.Display::return_icon('import_qti2.png', get_lang('ImportQtiQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  483. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/aiken.php?'.api_get_cidreq().'">'.Display::return_icon('import_aiken.png', get_lang('ImportAikenQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  484. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/upload_exercise.php?'.api_get_cidreq().'">'.Display::return_icon('import_excel.png', get_lang('ImportExcelQuiz'), '', ICON_SIZE_MEDIUM).'</a>';
  485. $cleanAll = Display::url(
  486. Display::return_icon(
  487. 'clean_all.png',
  488. get_lang('CleanAllStudentsResultsForAllTests'),
  489. '',
  490. ICON_SIZE_MEDIUM
  491. ),
  492. '#',
  493. [
  494. 'data-item-question' => addslashes(get_lang('AreYouSureToEmptyAllTestResults')),
  495. 'data-href' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'&choice=clean_all_test&sec_token='.$token,
  496. 'data-toggle' => 'modal',
  497. 'data-target' => '#confirm-delete',
  498. ]
  499. );
  500. if ($limitTeacherAccess) {
  501. if (api_is_platform_admin()) {
  502. echo $cleanAll;
  503. }
  504. } else {
  505. echo $cleanAll;
  506. }
  507. }
  508. if ($is_allowedToEdit) {
  509. echo '</div>'; // closing the actions div
  510. }
  511. if ($total > $limit) {
  512. echo '<div style="float:right;height:20px;">';
  513. //show pages navigation link for previous page
  514. if ($page) {
  515. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&page=".($page - 1)."\">".Display::return_icon('action_prev.png', get_lang('PreviousPage'))."</a>";
  516. } elseif ($total_exercises + $hp_count > $limit) {
  517. echo Display::return_icon('action_prev_na.png', get_lang('PreviousPage'));
  518. }
  519. //show pages navigation link for previous page
  520. if ($total_exercises > $from + $limit || $hp_count > $from + $limit) {
  521. echo ' '."<a href=\"".api_get_self()."?".api_get_cidreq()."&page=".($page + 1)."\">".Display::return_icon('action_next.png', get_lang('NextPage'))."</a>";
  522. } elseif ($page) {
  523. echo ' '.Display::return_icon('action_next_na.png', get_lang('NextPage'));
  524. }
  525. echo '</div>';
  526. }
  527. $i = 1;
  528. $online_icon = Display::return_icon(
  529. 'online.png',
  530. get_lang('Visible'),
  531. ['width' => '12px']
  532. );
  533. $offline_icon = Display::return_icon(
  534. 'offline.png',
  535. get_lang('Invisible'),
  536. ['width' => '12px']
  537. );
  538. $exerciseList = [];
  539. $list_ordered = null;
  540. while ($row = Database::fetch_array($result, 'ASSOC')) {
  541. $exerciseList[$row['iid']] = $row;
  542. }
  543. if (!empty($exerciseList) &&
  544. api_get_setting('exercise_invisible_in_session') === 'true'
  545. ) {
  546. if (!empty($sessionId)) {
  547. $changeDefaultVisibility = true;
  548. if (api_get_setting('configure_exercise_visibility_in_course') === 'true') {
  549. $changeDefaultVisibility = false;
  550. if (api_get_course_setting('exercise_invisible_in_session') == 1) {
  551. $changeDefaultVisibility = true;
  552. }
  553. }
  554. if ($changeDefaultVisibility) {
  555. // Check exercise
  556. foreach ($exerciseList as $exercise) {
  557. if ($exercise['session_id'] == 0) {
  558. $visibilityInfo = api_get_item_property_info(
  559. $courseId,
  560. TOOL_QUIZ,
  561. $exercise['iid'],
  562. $sessionId
  563. );
  564. if (empty($visibilityInfo)) {
  565. // Create a record for this
  566. api_item_property_update(
  567. $courseInfo,
  568. TOOL_QUIZ,
  569. $exercise['iid'],
  570. 'invisible',
  571. api_get_user_id(),
  572. 0,
  573. null,
  574. '',
  575. '',
  576. $sessionId
  577. );
  578. }
  579. }
  580. }
  581. }
  582. }
  583. }
  584. if (isset($list_ordered) && !empty($list_ordered)) {
  585. $new_question_list = [];
  586. foreach ($list_ordered as $exercise_id) {
  587. if (isset($exerciseList[$exercise_id])) {
  588. $new_question_list[] = $exerciseList[$exercise_id];
  589. }
  590. }
  591. $exerciseList = $new_question_list;
  592. }
  593. $tableRows = [];
  594. /* Listing exercises */
  595. if (!empty($exerciseList)) {
  596. if ($origin != 'learnpath') {
  597. //avoid sending empty parameters
  598. $mylpid = (empty($learnpath_id) ? '' : '&learnpath_id='.$learnpath_id);
  599. $mylpitemid = (empty($learnpath_item_id) ? '' : '&learnpath_item_id='.$learnpath_item_id);
  600. $i = 1;
  601. foreach ($exerciseList as $row) {
  602. $my_exercise_id = $row['id'];
  603. $exercise = new Exercise();
  604. $exercise->read($my_exercise_id, false);
  605. if (empty($exercise->id)) {
  606. continue;
  607. }
  608. $locked = $exercise->is_gradebook_locked;
  609. $i++;
  610. // Validation when belongs to a session
  611. $session_img = api_get_session_image($row['session_id'], $userInfo['status']);
  612. $time_limits = false;
  613. if (!empty($row['start_time']) || !empty($row['end_time'])) {
  614. $time_limits = true;
  615. }
  616. $is_actived_time = false;
  617. if ($time_limits) {
  618. // check if start time
  619. $start_time = false;
  620. if (!empty($row['start_time'])) {
  621. $start_time = api_strtotime($row['start_time'], 'UTC');
  622. }
  623. $end_time = false;
  624. if (!empty($row['end_time'])) {
  625. $end_time = api_strtotime($row['end_time'], 'UTC');
  626. }
  627. $now = time();
  628. //If both "clocks" are enable
  629. if ($start_time && $end_time) {
  630. if ($now > $start_time && $end_time > $now) {
  631. $is_actived_time = true;
  632. }
  633. } else {
  634. //we check the start and end
  635. if ($start_time) {
  636. if ($now > $start_time) {
  637. $is_actived_time = true;
  638. }
  639. }
  640. if ($end_time) {
  641. if ($end_time > $now) {
  642. $is_actived_time = true;
  643. }
  644. }
  645. }
  646. }
  647. // Blocking empty start times see BT#2800
  648. global $_custom;
  649. if (isset($_custom['exercises_hidden_when_no_start_date']) &&
  650. $_custom['exercises_hidden_when_no_start_date']
  651. ) {
  652. if (empty($row['start_time'])) {
  653. $time_limits = true;
  654. $is_actived_time = false;
  655. }
  656. }
  657. $cut_title = $exercise->getCutTitle();
  658. $alt_title = '';
  659. if ($cut_title != $row['title']) {
  660. $alt_title = ' title = "'.$exercise->getUnformattedTitle().'" ';
  661. }
  662. // Teacher only
  663. if ($is_allowedToEdit) {
  664. $lp_blocked = null;
  665. if ($exercise->exercise_was_added_in_lp == true) {
  666. $lp_blocked = Display::div(
  667. get_lang('AddedToLPCannotBeAccessed'),
  668. ['class' => 'lp_content_type_label']
  669. );
  670. }
  671. $visibility = api_get_item_visibility(
  672. $courseInfo,
  673. TOOL_QUIZ,
  674. $my_exercise_id,
  675. 0
  676. );
  677. if (!empty($sessionId)) {
  678. $setting = api_get_configuration_value('show_hidden_exercise_added_to_lp');
  679. if ($setting) {
  680. if ($exercise->exercise_was_added_in_lp == false) {
  681. if ($visibility == 0) {
  682. continue;
  683. }
  684. }
  685. } else {
  686. if ($visibility == 0) {
  687. continue;
  688. }
  689. }
  690. $visibility = api_get_item_visibility(
  691. $courseInfo,
  692. TOOL_QUIZ,
  693. $my_exercise_id,
  694. $sessionId
  695. );
  696. }
  697. if ($row['active'] == 0 || $visibility == 0) {
  698. $title = Display::tag('font', $cut_title, ['style' => 'color:grey']);
  699. } else {
  700. $title = $cut_title;
  701. }
  702. $count_exercise_not_validated = (int) Event::count_exercise_result_not_validated(
  703. $my_exercise_id,
  704. $courseId,
  705. $sessionId
  706. );
  707. $move = Display::return_icon(
  708. 'all_directions.png',
  709. get_lang('Move'),
  710. ['class' => 'moved', 'style' => 'margin-bottom:-0.5em;']
  711. );
  712. $move = null;
  713. $class_tip = '';
  714. if (!empty($count_exercise_not_validated)) {
  715. $results_text = $count_exercise_not_validated == 1 ? get_lang('ResultNotRevised') : get_lang('ResultsNotRevised');
  716. $title .= '<span class="exercise_tooltip" style="display: none;">'.$count_exercise_not_validated.' '.$results_text.' </span>';
  717. $class_tip = 'link_tooltip';
  718. }
  719. $url = $move.'<a '.$alt_title.' class="'.$class_tip.'" id="tooltip_'.$row['id'].'" href="overview.php?'.api_get_cidreq().$mylpid.$mylpitemid.'&exerciseId='.$row['id'].'">
  720. '.Display::return_icon('quiz.png', $row['title']).'
  721. '.$title.' </a>'.PHP_EOL;
  722. if (ExerciseLib::isQuizEmbeddable($row)) {
  723. $embeddableIcon = Display::return_icon('om_integration.png', get_lang('ThisQuizCanBeEmbeddable'));
  724. $url .= Display::div($embeddableIcon, ['class' => 'pull-right']);
  725. }
  726. $item = Display::tag('td', $url.' '.$session_img.$lp_blocked);
  727. // Count number exercise - teacher
  728. $sql = "SELECT count(*) count FROM $TBL_EXERCISE_QUESTION
  729. WHERE c_id = $courseId AND exercice_id = $my_exercise_id";
  730. $sqlresult = Database::query($sql);
  731. $rowi = (int) Database::result($sqlresult, 0, 0);
  732. if ($sessionId == $row['session_id']) {
  733. $actions = '';
  734. // Questions list
  735. $actions .= Display::url(
  736. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL),
  737. 'admin.php?'.api_get_cidreq().'&exerciseId='.$row['id']
  738. );
  739. // Test settings
  740. $settings = Display::url(
  741. Display::return_icon('settings.png', get_lang('Configure'), '', ICON_SIZE_SMALL),
  742. 'exercise_admin.php?'.api_get_cidreq().'&exerciseId='.$row['id']
  743. );
  744. if ($limitTeacherAccess && !api_is_platform_admin()) {
  745. $settings = '';
  746. }
  747. $actions .= $settings;
  748. // Exercise results
  749. $resultsLink = '<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'.
  750. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  751. if ($limitTeacherAccess) {
  752. if (api_is_platform_admin()) {
  753. $actions .= $resultsLink;
  754. }
  755. } else {
  756. // Exercise results
  757. $actions .= $resultsLink;
  758. }
  759. // Auto launch
  760. if ($autoLaunchAvailable) {
  761. $autoLaunch = $exercise->getAutoLaunch();
  762. if (empty($autoLaunch)) {
  763. $actions .= Display::url(
  764. Display::return_icon(
  765. 'launch_na.png',
  766. get_lang('Enable'),
  767. '',
  768. ICON_SIZE_SMALL
  769. ),
  770. 'exercise.php?'.api_get_cidreq().'&choice=enable_launch&sec_token='.$token.'&page='.$page.'&exerciseId='.$row['id']
  771. );
  772. } else {
  773. $actions .= Display::url(
  774. Display::return_icon(
  775. 'launch.png',
  776. get_lang('Disable'),
  777. '',
  778. ICON_SIZE_SMALL
  779. ),
  780. 'exercise.php?'.api_get_cidreq().'&choice=disable_launch&sec_token='.$token.'&page='.$page.'&exerciseId='.$row['id']
  781. );
  782. }
  783. }
  784. // Export
  785. $actions .= Display::url(
  786. Display::return_icon('cd.png', get_lang('CopyExercise')),
  787. '',
  788. [
  789. 'onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToCopy'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;",
  790. 'href' => 'exercise.php?'.api_get_cidreq().'&choice=copy_exercise&sec_token='.$token.'&exerciseId='.$row['id'],
  791. ]
  792. );
  793. // Clean exercise
  794. if ($locked == false) {
  795. $clean = Display::url(
  796. Display::return_icon(
  797. 'clean.png',
  798. get_lang('CleanStudentResults'),
  799. '',
  800. ICON_SIZE_SMALL
  801. ),
  802. '',
  803. [
  804. 'onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToDeleteResults'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;",
  805. 'href' => 'exercise.php?'.api_get_cidreq().'&choice=clean_results&sec_token='.$token.'&exerciseId='.$row['id'],
  806. ]
  807. );
  808. } else {
  809. $clean = Display::return_icon(
  810. 'clean_na.png',
  811. get_lang('ResourceLockedByGradebook'),
  812. '',
  813. ICON_SIZE_SMALL
  814. );
  815. }
  816. if ($limitTeacherAccess && !api_is_platform_admin()) {
  817. $clean = '';
  818. }
  819. $actions .= $clean;
  820. // Visible / invisible
  821. // Check if this exercise was added in a LP
  822. if ($exercise->exercise_was_added_in_lp == true) {
  823. $visibility = Display::return_icon(
  824. 'invisible.png',
  825. get_lang('AddedToLPCannotBeAccessed'),
  826. '',
  827. ICON_SIZE_SMALL
  828. );
  829. } else {
  830. if ($row['active'] == 0 || $visibility == 0) {
  831. $visibility = Display::url(
  832. Display::return_icon(
  833. 'invisible.png',
  834. get_lang('Activate'),
  835. '',
  836. ICON_SIZE_SMALL
  837. ),
  838. 'exercise.php?'.api_get_cidreq().'&choice=enable&sec_token='.$token.'&page='.$page.'&exerciseId='.$row['id']
  839. );
  840. } else {
  841. // else if not active
  842. $visibility = Display::url(
  843. Display::return_icon(
  844. 'visible.png',
  845. get_lang('Deactivate'),
  846. '',
  847. ICON_SIZE_SMALL
  848. ),
  849. 'exercise.php?'.api_get_cidreq().'&choice=disable&sec_token='.$token.'&page='.$page.'&exerciseId='.$row['id']
  850. );
  851. }
  852. }
  853. if ($limitTeacherAccess && !api_is_platform_admin()) {
  854. $visibility = '';
  855. }
  856. $actions .= $visibility;
  857. // Export qti ...
  858. $export = Display::url(
  859. Display::return_icon(
  860. 'export_qti2.png',
  861. 'IMS/QTI',
  862. '',
  863. ICON_SIZE_SMALL
  864. ),
  865. 'exercise.php?choice=exportqti2&exerciseId='.$row['id'].'&'.api_get_cidreq()
  866. );
  867. if ($limitTeacherAccess && !api_is_platform_admin()) {
  868. $export = '';
  869. }
  870. $actions .= $export;
  871. } else {
  872. // not session
  873. $actions = Display::return_icon(
  874. 'edit_na.png',
  875. get_lang('ExerciseEditionNotAvailableInSession')
  876. );
  877. // Check if this exercise was added in a LP
  878. if ($exercise->exercise_was_added_in_lp == true) {
  879. $visibility = Display::return_icon(
  880. 'invisible.png',
  881. get_lang('AddedToLPCannotBeAccessed'),
  882. '',
  883. ICON_SIZE_SMALL
  884. );
  885. } else {
  886. if ($row['active'] == 0 || $visibility == 0) {
  887. $visibility = Display::url(
  888. Display::return_icon(
  889. 'invisible.png',
  890. get_lang('Activate'),
  891. '',
  892. ICON_SIZE_SMALL
  893. ),
  894. 'exercise.php?'.api_get_cidreq().'&choice=enable&sec_token='.$token.'&page='.$page.'&exerciseId='.$row['id']
  895. );
  896. } else {
  897. // else if not active
  898. $visibility = Display::url(
  899. Display::return_icon(
  900. 'visible.png',
  901. get_lang('Deactivate'),
  902. '',
  903. ICON_SIZE_SMALL
  904. ),
  905. 'exercise.php?'.api_get_cidreq().'&choice=disable&sec_token='.$token.'&page='.$page.'&exerciseId='.$row['id']
  906. );
  907. }
  908. }
  909. if ($limitTeacherAccess && !api_is_platform_admin()) {
  910. $visibility = '';
  911. }
  912. $actions .= $visibility;
  913. $actions .= '<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'.
  914. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  915. $actions .= Display::url(
  916. Display::return_icon('cd.gif', get_lang('CopyExercise')),
  917. '',
  918. [
  919. 'onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToCopy'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;",
  920. 'href' => 'exercise.php?'.api_get_cidreq().'&choice=copy_exercise&sec_token='.$token.'&exerciseId='.$row['id'],
  921. ]
  922. );
  923. }
  924. // Delete
  925. $delete = '';
  926. if ($sessionId == $row['session_id']) {
  927. if ($locked == false) {
  928. $delete = Display::url(
  929. Display::return_icon(
  930. 'delete.png',
  931. get_lang('Delete'),
  932. '',
  933. ICON_SIZE_SMALL
  934. ),
  935. '',
  936. [
  937. 'onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToDeleteJS'), ENT_QUOTES, $charset))." ".addslashes($exercise->getUnformattedTitle())."?"."')) return false;",
  938. 'href' => 'exercise.php?'.api_get_cidreq().'&choice=delete&sec_token='.$token.'&exerciseId='.$row['id'],
  939. ]
  940. );
  941. } else {
  942. $delete = Display::return_icon(
  943. 'delete_na.png',
  944. get_lang('ResourceLockedByGradebook'),
  945. '',
  946. ICON_SIZE_SMALL
  947. );
  948. }
  949. }
  950. if ($limitTeacherAccess && !api_is_platform_admin()) {
  951. $delete = '';
  952. }
  953. $actions .= $delete;
  954. // Number of questions
  955. $random_label = null;
  956. if ($row['random'] > 0 || $row['random'] == -1) {
  957. // if random == -1 means use random questions with all questions
  958. $random_number_of_question = $row['random'];
  959. if ($random_number_of_question == -1) {
  960. $random_number_of_question = $rowi;
  961. }
  962. if ($row['random_by_category'] > 0) {
  963. $nbQuestionsTotal = TestCategory::getNumberOfQuestionRandomByCategory(
  964. $my_exercise_id,
  965. $random_number_of_question
  966. );
  967. $number_of_questions = $nbQuestionsTotal.' ';
  968. $number_of_questions .= ($nbQuestionsTotal > 1) ? get_lang('QuestionsLowerCase') : get_lang('QuestionLowerCase');
  969. $number_of_questions .= ' - ';
  970. $number_of_questions .= min(TestCategory::getNumberMaxQuestionByCat($my_exercise_id), $random_number_of_question).' '.get_lang('QuestionByCategory');
  971. } else {
  972. $random_label = ' ('.get_lang('Random').') ';
  973. $number_of_questions = $random_number_of_question.' '.$random_label.' / '.$rowi;
  974. // Bug if we set a random value bigger than the real number of questions
  975. if ($random_number_of_question > $rowi) {
  976. $number_of_questions = $rowi.' '.$random_label;
  977. }
  978. }
  979. } else {
  980. $number_of_questions = $rowi;
  981. }
  982. $item .= Display::tag('td', $number_of_questions);
  983. } else {
  984. // Student only.
  985. $visibility = api_get_item_visibility(
  986. $courseInfo,
  987. TOOL_QUIZ,
  988. $my_exercise_id,
  989. $sessionId
  990. );
  991. if ($visibility == 0) {
  992. continue;
  993. }
  994. $url = '<a '.$alt_title.' href="overview.php?'.api_get_cidreq().$mylpid.$mylpitemid.'&exerciseId='.$row['id'].'">'.
  995. $cut_title.'</a>';
  996. // Link of the exercise.
  997. $item = Display::tag('td', $url.' '.$session_img);
  998. // Count number exercise questions.
  999. /*$sql = "SELECT count(*) FROM $TBL_EXERCISE_QUESTION
  1000. WHERE c_id = $courseId AND exercice_id = ".$row['id'];
  1001. $sqlresult = Database::query($sql);
  1002. $rowi = Database::result($sqlresult, 0);
  1003. if ($row['random'] > 0) {
  1004. $row['random'].' '.api_strtolower(get_lang(($row['random'] > 1 ? 'Questions' : 'Question')));
  1005. } else {
  1006. //show results student
  1007. $rowi.' '.api_strtolower(get_lang(($rowi > 1 ? 'Questions' : 'Question')));
  1008. }*/
  1009. // This query might be improved later on by ordering by the new "tms" field rather than by exe_id
  1010. // Don't remove this marker: note-query-exe-results
  1011. $sql = "SELECT * FROM $TBL_TRACK_EXERCISES
  1012. WHERE
  1013. exe_exo_id = ".$row['id']." AND
  1014. exe_user_id = $userId AND
  1015. c_id = ".api_get_course_int_id()." AND
  1016. status <> 'incomplete' AND
  1017. orig_lp_id = 0 AND
  1018. orig_lp_item_id = 0 AND
  1019. session_id = '".api_get_session_id()."'
  1020. ORDER BY exe_id DESC";
  1021. $qryres = Database::query($sql);
  1022. $num = Database :: num_rows($qryres);
  1023. // Hide the results.
  1024. $my_result_disabled = $row['results_disabled'];
  1025. // Time limits are on
  1026. if ($time_limits) {
  1027. // Exam is ready to be taken
  1028. if ($is_actived_time) {
  1029. // Show results
  1030. if (
  1031. in_array(
  1032. $my_result_disabled,
  1033. [
  1034. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  1035. RESULT_DISABLE_SHOW_SCORE_ONLY,
  1036. RESULT_DISABLE_RANKING,
  1037. ]
  1038. )
  1039. ) {
  1040. // More than one attempt
  1041. if ($num > 0) {
  1042. $row_track = Database :: fetch_array($qryres);
  1043. $attempt_text = get_lang('LatestAttempt').' : ';
  1044. $attempt_text .= ExerciseLib::show_score(
  1045. $row_track['exe_result'],
  1046. $row_track['exe_weighting']
  1047. );
  1048. } else {
  1049. //No attempts
  1050. $attempt_text = get_lang('NotAttempted');
  1051. }
  1052. } else {
  1053. $attempt_text = '-';
  1054. }
  1055. } else {
  1056. // Quiz not ready due to time limits
  1057. //@todo use the is_visible function
  1058. if (!empty($row['start_time']) && !empty($row['end_time'])) {
  1059. $today = time();
  1060. $start_time = api_strtotime($row['start_time'], 'UTC');
  1061. $end_time = api_strtotime($row['end_time'], 'UTC');
  1062. if ($today < $start_time) {
  1063. $attempt_text = sprintf(
  1064. get_lang('ExerciseWillBeActivatedFromXToY'),
  1065. api_convert_and_format_date($row['start_time']),
  1066. api_convert_and_format_date($row['end_time'])
  1067. );
  1068. } else {
  1069. if ($today > $end_time) {
  1070. $attempt_text = sprintf(
  1071. get_lang('ExerciseWasActivatedFromXToY'),
  1072. api_convert_and_format_date($row['start_time']),
  1073. api_convert_and_format_date($row['end_time'])
  1074. );
  1075. }
  1076. }
  1077. } else {
  1078. //$attempt_text = get_lang('ExamNotAvailableAtThisTime');
  1079. if (!empty($row['start_time'])) {
  1080. $attempt_text = sprintf(
  1081. get_lang('ExerciseAvailableFromX'),
  1082. api_convert_and_format_date($row['start_time'])
  1083. );
  1084. }
  1085. if (!empty($row['end_time'])) {
  1086. $attempt_text = sprintf(
  1087. get_lang('ExerciseAvailableUntilX'),
  1088. api_convert_and_format_date($row['end_time'])
  1089. );
  1090. }
  1091. }
  1092. }
  1093. } else {
  1094. // Normal behaviour.
  1095. // Show results.
  1096. if (
  1097. in_array(
  1098. $my_result_disabled,
  1099. [
  1100. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  1101. RESULT_DISABLE_SHOW_SCORE_ONLY,
  1102. RESULT_DISABLE_RANKING,
  1103. ]
  1104. )
  1105. ) {
  1106. if ($num > 0) {
  1107. $row_track = Database :: fetch_array($qryres);
  1108. $attempt_text = get_lang('LatestAttempt').' : ';
  1109. $attempt_text .= ExerciseLib::show_score(
  1110. $row_track['exe_result'],
  1111. $row_track['exe_weighting']
  1112. );
  1113. } else {
  1114. $attempt_text = get_lang('NotAttempted');
  1115. }
  1116. } else {
  1117. $attempt_text = '-';
  1118. }
  1119. }
  1120. $class_tip = '';
  1121. if (empty($num)) {
  1122. $num = '';
  1123. } else {
  1124. $class_tip = 'link_tooltip';
  1125. //@todo use sprintf and show the results validated by the teacher
  1126. if ($num == 1) {
  1127. $num = $num.' '.get_lang('Result');
  1128. } else {
  1129. $num = $num.' '.get_lang('Results');
  1130. }
  1131. $num = '<span class="tooltip" style="display: none;">'.$num.'</span>';
  1132. }
  1133. $item .= Display::tag('td', $attempt_text);
  1134. }
  1135. if ($is_allowedToEdit) {
  1136. $additionalActions = ExerciseLib::getAdditionalTeacherActions($row['id']);
  1137. if (!empty($additionalActions)) {
  1138. $actions .= $additionalActions.PHP_EOL;
  1139. }
  1140. $item .= Display::tag('td', $actions, ['class' => 'td_actions']);
  1141. } else {
  1142. if ($isDrhOfCourse) {
  1143. $actions = '<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'.
  1144. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  1145. $item .= Display::tag('td', $actions, ['class' => 'td_actions']);
  1146. }
  1147. }
  1148. $tableRows[] = Display::tag(
  1149. 'tr',
  1150. $item,
  1151. [
  1152. 'id' => 'exercise_list_'.$my_exercise_id,
  1153. ]
  1154. );
  1155. }
  1156. }
  1157. }
  1158. // end exercise list
  1159. // Hotpotatoes results
  1160. $hotpotatoes_exist = false;
  1161. if ($is_allowedToEdit) {
  1162. $sql = "SELECT d.iid, d.path as path, d.comment as comment
  1163. FROM $TBL_DOCUMENT d
  1164. WHERE
  1165. d.c_id = $courseId AND
  1166. (d.path LIKE '%htm%') AND
  1167. d.path LIKE '".Database :: escape_string($uploadPath.'/%/%')."'
  1168. LIMIT ".$from.",".$limit; // only .htm or .html files listed
  1169. } else {
  1170. $sql = "SELECT d.iid, d.path as path, d.comment as comment
  1171. FROM $TBL_DOCUMENT d
  1172. WHERE
  1173. d.c_id = $courseId AND
  1174. (d.path LIKE '%htm%') AND
  1175. d.path LIKE '".Database :: escape_string($uploadPath.'/%/%')."'
  1176. LIMIT ".$from.",".$limit;
  1177. }
  1178. $result = Database::query($sql);
  1179. $attribute = [];
  1180. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  1181. $attribute['id'][] = $row['iid'];
  1182. $attribute['path'][] = $row['path'];
  1183. $attribute['comment'][] = $row['comment'];
  1184. }
  1185. $nbrActiveTests = 0;
  1186. if (isset($attribute['path']) && is_array($attribute['path'])) {
  1187. $hotpotatoes_exist = true;
  1188. foreach ($attribute['path'] as $key => $path) {
  1189. $item = '';
  1190. $title = GetQuizName($path, $documentPath);
  1191. if ($title == '') {
  1192. $title = basename($path);
  1193. }
  1194. // prof only
  1195. if ($is_allowedToEdit) {
  1196. $visibility = api_get_item_visibility(
  1197. ['real_id' => $courseId],
  1198. TOOL_DOCUMENT,
  1199. $attribute['id'][$key],
  1200. 0
  1201. );
  1202. if (!empty($sessionId)) {
  1203. if (0 == $visibility) {
  1204. continue;
  1205. }
  1206. $visibility = api_get_item_visibility(
  1207. ['real_id' => $courseId],
  1208. TOOL_DOCUMENT,
  1209. $attribute['id'][$key],
  1210. $sessionId
  1211. );
  1212. }
  1213. $item = Display::tag(
  1214. 'td',
  1215. implode(PHP_EOL, [
  1216. Display::return_icon('hotpotatoes_s.png', "HotPotatoes"),
  1217. Display::url(
  1218. $title,
  1219. 'showinframes.php?'.api_get_cidreq().'&'.http_build_query([
  1220. 'file' => $path,
  1221. 'uid' => $userId,
  1222. ]),
  1223. ['class' => $visibility == 0 ? 'text-muted' : null]
  1224. ),
  1225. ])
  1226. );
  1227. $item .= Display::tag('td', '-');
  1228. $actions = Display::url(
  1229. Display::return_icon(
  1230. 'edit.png',
  1231. get_lang('Edit'),
  1232. '',
  1233. ICON_SIZE_SMALL
  1234. ),
  1235. 'adminhp.php?'.api_get_cidreq().'&hotpotatoesName='.$path
  1236. );
  1237. $actions .= '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq().'&path='.$path.'">'.
  1238. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  1239. // if active
  1240. if ($visibility != 0) {
  1241. $nbrActiveTests = $nbrActiveTests + 1;
  1242. $actions .= ' <a href="'.$exercisePath.'?'.api_get_cidreq().'&hpchoice=disable&page='.$page.'&file='.$path.'">'.
  1243. Display::return_icon('visible.png', get_lang('Deactivate'), '', ICON_SIZE_SMALL).'</a>';
  1244. } else { // else if not active
  1245. $actions .= ' <a href="'.$exercisePath.'?'.api_get_cidreq().'&hpchoice=enable&page='.$page.'&file='.$path.'">'.
  1246. Display::return_icon('invisible.png', get_lang('Activate'), '', ICON_SIZE_SMALL).'</a>';
  1247. }
  1248. $actions .= '<a href="'.$exercisePath.'?'.api_get_cidreq().'&hpchoice=delete&file='.$path.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('AreYouSureToDeleteJS'), ENT_QUOTES, $charset).' '.$title."?").'\')) return false;">'.
  1249. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  1250. $item .= Display::tag('td', $actions);
  1251. $tableRows[] = Display::tag('tr', $item);
  1252. } else {
  1253. $visibility = api_get_item_visibility(
  1254. ['real_id' => $courseId],
  1255. TOOL_DOCUMENT,
  1256. $attribute['id'][$key],
  1257. $sessionId
  1258. );
  1259. if (0 == $visibility) {
  1260. continue;
  1261. }
  1262. // Student only
  1263. $attempt = ExerciseLib::getLatestHotPotatoResult(
  1264. $path,
  1265. $userId,
  1266. api_get_course_int_id(),
  1267. api_get_session_id()
  1268. );
  1269. $nbrActiveTests = $nbrActiveTests + 1;
  1270. $item .= Display::tag(
  1271. 'td',
  1272. Display::url(
  1273. $title,
  1274. 'showinframes.php?'.api_get_cidreq().'&'.http_build_query([
  1275. 'file' => $path,
  1276. 'cid' => api_get_course_id(),
  1277. 'uid' => $userId,
  1278. ])
  1279. )
  1280. );
  1281. if (!empty($attempt)) {
  1282. $actions = '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq().'&path='.$path.'&filter_by_user='.$userId.'">'.Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  1283. $attemptText = get_lang('LatestAttempt').' : ';
  1284. $attemptText .= ExerciseLib::show_score(
  1285. $attempt['exe_result'],
  1286. $attempt['exe_weighting']
  1287. ).' ';
  1288. $attemptText .= $actions;
  1289. } else {
  1290. // No attempts.
  1291. $attemptText = get_lang('NotAttempted').' ';
  1292. }
  1293. $item .= Display::tag('td', $attemptText);
  1294. if ($isDrhOfCourse) {
  1295. $actions = '<a href="hotpotatoes_exercise_report.php?'.api_get_cidreq().'&path='.$path.'">'.
  1296. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
  1297. $item .= Display::tag(
  1298. 'td',
  1299. $actions,
  1300. ['class' => 'td_actions']
  1301. );
  1302. }
  1303. $tableRows[] = Display::tag('tr', $item);
  1304. }
  1305. }
  1306. }
  1307. if (empty($exerciseList) && $hotpotatoes_exist == false) {
  1308. if ($is_allowedToEdit && $origin != 'learnpath') {
  1309. echo '<div id="no-data-view">';
  1310. echo '<h3>'.get_lang('Quiz').'</h3>';
  1311. echo Display::return_icon('quiz.png', '', [], 64);
  1312. echo '<div class="controls">';
  1313. echo Display::url(
  1314. '<em class="fa fa-plus"></em> '.get_lang('NewEx'),
  1315. 'exercise_admin.php?'.api_get_cidreq(),
  1316. ['class' => 'btn btn-primary']
  1317. );
  1318. echo '</div>';
  1319. echo '</div>';
  1320. }
  1321. } else {
  1322. if ($is_allowedToEdit) {
  1323. $headers = [
  1324. get_lang('ExerciseName'),
  1325. get_lang('QuantityQuestions'),
  1326. get_lang('Actions'),
  1327. ];
  1328. } else {
  1329. $headers = [
  1330. get_lang('ExerciseName'),
  1331. get_lang('Status'),
  1332. ];
  1333. if ($isDrhOfCourse) {
  1334. $headers[] = get_lang('Actions');
  1335. }
  1336. }
  1337. $headerList = '';
  1338. foreach ($headers as $header) {
  1339. $headerList .= Display::tag('th', $header);
  1340. }
  1341. echo '<div class="table-responsive">';
  1342. echo '<table class="table table-striped table-hover">';
  1343. echo Display::tag(
  1344. 'thead',
  1345. Display::tag('tr', $headerList)
  1346. );
  1347. echo '<tbody>';
  1348. foreach ($tableRows as $row) {
  1349. echo $row;
  1350. }
  1351. echo '</tbody>';
  1352. echo '</table>';
  1353. echo '</div>';
  1354. }
  1355. if ($origin != 'learnpath') { //so we are not in learnpath tool
  1356. Display::display_footer();
  1357. }