course_home.lib.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CTool;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. /**
  6. * Class CourseHome
  7. */
  8. class CourseHome
  9. {
  10. /**
  11. * Gets the html content to show in the 3 column view
  12. * @param string $cat
  13. * @param int $userId
  14. * @return string
  15. */
  16. public static function show_tool_3column($cat, $userId = null)
  17. {
  18. $_user = api_get_user_info($userId);
  19. $TBL_ACCUEIL = Database::get_course_table(TABLE_TOOL_LIST);
  20. $TABLE_TOOLS = Database::get_main_table(TABLE_MAIN_COURSE_MODULE);
  21. $numcols = 3;
  22. $table = new HTML_Table('width="100%"');
  23. $all_tools = [];
  24. $course_id = api_get_course_int_id();
  25. switch ($cat) {
  26. case 'Basic':
  27. $condition_display_tools = ' WHERE a.c_id = '.$course_id.' AND a.link=t.link AND t.position="basic" ';
  28. if ((api_is_coach() || api_is_course_tutor()) && $_SESSION['studentview'] != 'studentview') {
  29. $condition_display_tools = ' WHERE
  30. a.c_id = '.$course_id.' AND
  31. a.link=t.link AND
  32. (t.position="basic" OR a.name = "'.TOOL_TRACKING.'")
  33. ';
  34. }
  35. $sql = "SELECT a.*, t.image img, t.row, t.column
  36. FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
  37. $condition_display_tools ORDER BY t.row, t.column";
  38. break;
  39. case 'External':
  40. if (api_is_allowed_to_edit()) {
  41. $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
  42. WHERE
  43. a.c_id = $course_id AND
  44. ((a.link=t.link AND t.position='external') OR
  45. (a.visibility <= 1 AND
  46. (a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND
  47. a.image=t.image))
  48. ORDER BY a.id";
  49. } else {
  50. $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
  51. WHERE
  52. a.c_id = $course_id AND
  53. (a.visibility = 1 AND ((a.link=t.link AND t.position='external') OR
  54. ((a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND
  55. a.image=t.image)))
  56. ORDER BY a.id";
  57. }
  58. break;
  59. case 'courseAdmin':
  60. $sql = "SELECT a.*, t.image img, t.row, t.column
  61. FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
  62. WHERE a.c_id = $course_id AND admin=1 AND a.link=t.link
  63. ORDER BY t.row, t.column";
  64. break;
  65. case 'platformAdmin':
  66. $sql = "SELECT *, image img FROM $TBL_ACCUEIL
  67. WHERE c_id = $course_id AND visibility = 2
  68. ORDER BY id";
  69. }
  70. $result = Database::query($sql);
  71. // Grabbing all the tools from $course_tool_table
  72. while ($tool = Database::fetch_array($result)) {
  73. $all_tools[] = $tool;
  74. }
  75. $course_id = api_get_course_int_id();
  76. // Grabbing all the links that have the property on_homepage set to 1
  77. if ($cat == 'External') {
  78. $tbl_link = Database::get_course_table(TABLE_LINK);
  79. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  80. if (api_is_allowed_to_edit(null, true)) {
  81. $sql_links = "SELECT tl.*, tip.visibility
  82. FROM $tbl_link tl
  83. LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id
  84. WHERE
  85. tl.c_id = $course_id AND
  86. tip.c_id = $course_id AND
  87. tl.on_homepage='1' AND
  88. tip.visibility != 2";
  89. } else {
  90. $sql_links = "SELECT tl.*, tip.visibility
  91. FROM $tbl_link tl
  92. LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id
  93. WHERE
  94. tl.c_id = $course_id AND
  95. tip.c_id = $course_id AND
  96. tl.on_homepage='1' AND
  97. tip.visibility = 1";
  98. }
  99. $result_links = Database::query($sql_links);
  100. while ($links_row = Database::fetch_array($result_links)) {
  101. $properties = [];
  102. $properties['name'] = $links_row['title'];
  103. $properties['link'] = $links_row['url'];
  104. $properties['visibility'] = $links_row['visibility'];
  105. $properties['img'] = 'external.gif';
  106. $properties['adminlink'] = api_get_path(WEB_CODE_PATH).'link/link.php?action=editlink&amp;id='.$links_row['id'];
  107. $all_tools[] = $properties;
  108. }
  109. }
  110. $cell_number = 0;
  111. // Draw line between basic and external, only if there are entries in External
  112. if ($cat == 'External' && count($all_tools)) {
  113. $table->setCellContents(0, 0, '<hr noshade="noshade" size="1"/>');
  114. $table->updateCellAttributes(0, 0, 'colspan="3"');
  115. $cell_number += $numcols;
  116. }
  117. foreach ($all_tools as & $tool) {
  118. if ($tool['image'] == 'scormbuilder.gif') {
  119. // check if the published learnpath is visible for student
  120. $published_lp_id = self::get_published_lp_id_from_link($tool['link']);
  121. if (!api_is_allowed_to_edit(null, true) &&
  122. !learnpath::is_lp_visible_for_student(
  123. $published_lp_id,
  124. api_get_user_id(),
  125. api_get_course_id(),
  126. api_get_session_id()
  127. )
  128. ) {
  129. continue;
  130. }
  131. }
  132. if (api_get_session_id() != 0 &&
  133. in_array($tool['name'], ['course_maintenance', 'course_setting'])
  134. ) {
  135. continue;
  136. }
  137. $cell_content = '';
  138. // The name of the tool
  139. $tool_name = self::translate_tool_name($tool);
  140. $link_annex = '';
  141. // The url of the tool
  142. if ($tool['img'] != 'external.gif') {
  143. $tool['link'] = api_get_path(WEB_CODE_PATH).$tool['link'];
  144. $qm_or_amp = strpos($tool['link'], '?') === false ? '?' : '&amp;';
  145. $link_annex = $qm_or_amp.api_get_cidreq();
  146. } else {
  147. // If an external link ends with 'login=', add the actual login...
  148. $pos = strpos($tool['link'], '?login=');
  149. $pos2 = strpos($tool['link'], '&amp;login=');
  150. if ($pos !== false or $pos2 !== false) {
  151. $link_annex = $_user['username'];
  152. }
  153. }
  154. // Setting the actual image url
  155. $tool['img'] = Display::returnIconPath($tool['img']);
  156. // VISIBLE
  157. if (($tool['visibility'] ||
  158. ((api_is_coach() || api_is_course_tutor()) && $tool['name'] == TOOL_TRACKING)) ||
  159. $cat == 'courseAdmin' || $cat == 'platformAdmin'
  160. ) {
  161. if (strpos($tool['name'], 'visio_') !== false) {
  162. $cell_content .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.$tool['link'].$link_annex.'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'"><img src="'.$tool['img'].'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
  163. } elseif (strpos($tool['name'], 'chat') !== false &&
  164. api_get_course_setting('allow_open_chat_window')
  165. ) {
  166. // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
  167. $cell_content .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.$tool['link'].$link_annex.'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'"><img src="'.$tool['img'].'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
  168. } else {
  169. // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
  170. $cell_content .= '<a href="'.$tool['link'].$link_annex.'" target="'.$tool['target'].'"><img src="'.$tool['img'].'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
  171. }
  172. } else {
  173. // INVISIBLE
  174. if (api_is_allowed_to_edit(null, true)) {
  175. if (strpos($tool['name'], 'visio_') !== false) {
  176. $cell_content .= '<a href="javascript: void(0);" onclick="window.open(\''.$tool['link'].$link_annex.'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'"><img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
  177. } elseif (strpos($tool['name'], 'chat') !== false && api_get_course_setting('allow_open_chat_window')) {
  178. // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
  179. $cell_content .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.$tool['link'].$link_annex.'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'" class="text-muted"><img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
  180. } else {
  181. // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
  182. $cell_content .= '<a href="'.$tool['link'].$link_annex.'" target="'.$tool['target'].'" class="text-muted">
  183. <img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
  184. }
  185. } else {
  186. // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
  187. $cell_content .= '<img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">';
  188. $cell_content .= '<span class="text-muted">'.$tool_name.'</span>';
  189. }
  190. }
  191. $lnk = [];
  192. if (api_is_allowed_to_edit(null, true) &&
  193. $cat != "courseAdmin" &&
  194. !strpos($tool['link'], 'learnpath_handler.php?learnpath_id') &&
  195. !api_is_coach()
  196. ) {
  197. if ($tool['visibility']) {
  198. $link['name'] = Display::return_icon(
  199. 'remove.gif',
  200. get_lang('Deactivate'),
  201. ['style' => 'vertical-align: middle;']
  202. );
  203. $link['cmd'] = "hide=yes";
  204. $lnk[] = $link;
  205. } else {
  206. $link['name'] = Display::return_icon(
  207. 'add.gif',
  208. get_lang('Activate'),
  209. ['style' => 'vertical-align: middle;']
  210. );
  211. $link['cmd'] = "restore=yes";
  212. $lnk[] = $link;
  213. }
  214. if (is_array($lnk)) {
  215. foreach ($lnk as & $this_lnk) {
  216. if (isset($tool['adminlink']) && $tool['adminlink']) {
  217. $cell_content .= '<a href="'.$properties['adminlink'].'">'.
  218. Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  219. } else {
  220. $cell_content .= '<a href="'.api_get_self().'?id='.$tool['id'].'&amp;'.$this_lnk['cmd'].'">'.$this_lnk['name'].'</a>';
  221. }
  222. }
  223. }
  224. }
  225. $table->setCellContents($cell_number / $numcols, ($cell_number) % $numcols, $cell_content);
  226. $table->updateCellAttributes($cell_number / $numcols, ($cell_number) % $numcols, 'width="32%" height="42"');
  227. $cell_number++;
  228. }
  229. return $table->toHtml();
  230. }
  231. /**
  232. * Displays the tools of a certain category.
  233. *
  234. * @param string $course_tool_category contains the category of tools to display:
  235. * "Public", "PublicButHide", "courseAdmin", "claroAdmin"
  236. * @return string
  237. */
  238. public static function show_tool_2column($course_tool_category)
  239. {
  240. $html = '';
  241. $web_code_path = api_get_path(WEB_CODE_PATH);
  242. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  243. $course_id = api_get_course_int_id();
  244. switch ($course_tool_category) {
  245. case TOOL_PUBLIC:
  246. $condition_display_tools = ' WHERE c_id = '.$course_id.' AND visibility = 1 ';
  247. if ((api_is_coach() || api_is_course_tutor()) && $_SESSION['studentview'] != 'studentview') {
  248. $condition_display_tools = ' WHERE
  249. c_id = '.$course_id.' AND
  250. (visibility = 1 OR (visibility = 0 AND name = "'.TOOL_TRACKING.'")) ';
  251. }
  252. $result = Database::query("SELECT * FROM $course_tool_table $condition_display_tools ORDER BY id");
  253. $col_link = "##003399";
  254. break;
  255. case TOOL_PUBLIC_BUT_HIDDEN:
  256. $result = Database::query("SELECT * FROM $course_tool_table WHERE c_id = $course_id AND visibility=0 AND admin=0 ORDER BY id");
  257. $col_link = "##808080";
  258. break;
  259. case TOOL_COURSE_ADMIN:
  260. $result = Database::query("SELECT * FROM $course_tool_table WHERE c_id = $course_id AND admin=1 AND visibility != 2 ORDER BY id");
  261. $col_link = "##003399";
  262. break;
  263. case TOOL_PLATFORM_ADMIN:
  264. $result = Database::query("SELECT * FROM $course_tool_table WHERE c_id = $course_id AND visibility = 2 ORDER BY id");
  265. $col_link = "##003399";
  266. }
  267. $i = 0;
  268. // Grabbing all the tools from $course_tool_table
  269. while ($temp_row = Database::fetch_array($result)) {
  270. if ($course_tool_category == TOOL_PUBLIC_BUT_HIDDEN && $temp_row['image'] != 'scormbuilder.gif') {
  271. $temp_row['image'] = str_replace('.gif', '_na.gif', $temp_row['image']);
  272. }
  273. $all_tools_list[] = $temp_row;
  274. }
  275. // Grabbing all the links that have the property on_homepage set to 1
  276. $course_link_table = Database::get_course_table(TABLE_LINK);
  277. $course_item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  278. switch ($course_tool_category) {
  279. case TOOL_PUBLIC:
  280. $sql_links = "SELECT tl.*, tip.visibility
  281. FROM $course_link_table tl
  282. LEFT JOIN $course_item_property_table tip ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id
  283. WHERE tl.on_homepage='1' AND tip.visibility = 1";
  284. break;
  285. case TOOL_PUBLIC_BUT_HIDDEN:
  286. $sql_links = "SELECT tl.*, tip.visibility
  287. FROM $course_link_table tl
  288. LEFT JOIN $course_item_property_table tip ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id
  289. WHERE tl.on_homepage='1' AND tip.visibility = 0";
  290. break;
  291. default:
  292. $sql_links = null;
  293. break;
  294. }
  295. if ($sql_links != null) {
  296. $properties = [];
  297. $result_links = Database::query($sql_links);
  298. while ($links_row = Database::fetch_array($result_links)) {
  299. unset($properties);
  300. $properties['name'] = $links_row['title'];
  301. $properties['link'] = $links_row['url'];
  302. $properties['visibility'] = $links_row['visibility'];
  303. $properties['image'] = $course_tool_category == TOOL_PUBLIC_BUT_HIDDEN ? 'external_na.gif' : 'external.gif';
  304. $properties['adminlink'] = api_get_path(WEB_CODE_PATH).'link/link.php?action=editlink&id='.$links_row['id'];
  305. $all_tools_list[] = $properties;
  306. }
  307. }
  308. if (isset($all_tools_list)) {
  309. $lnk = [];
  310. foreach ($all_tools_list as & $tool) {
  311. if ($tool['image'] == 'scormbuilder.gif') {
  312. // check if the published learnpath is visible for student
  313. $published_lp_id = self::get_published_lp_id_from_link($tool['link']);
  314. if (!api_is_allowed_to_edit(null, true) &&
  315. !learnpath::is_lp_visible_for_student(
  316. $published_lp_id,
  317. api_get_user_id(),
  318. api_get_course_id(),
  319. api_get_session_id()
  320. )
  321. ) {
  322. continue;
  323. }
  324. }
  325. if (api_get_session_id() != 0 &&
  326. in_array($tool['name'], ['course_maintenance', 'course_setting'])
  327. ) {
  328. continue;
  329. }
  330. if (!($i % 2)) {
  331. $html .= "<tr valign=\"top\">";
  332. }
  333. // NOTE : Table contains only the image file name, not full path
  334. if (stripos($tool['link'], 'http://') === false &&
  335. stripos($tool['link'], 'https://') === false &&
  336. stripos($tool['link'], 'ftp://') === false
  337. ) {
  338. $tool['link'] = $web_code_path.$tool['link'];
  339. }
  340. $class = '';
  341. if ($course_tool_category == TOOL_PUBLIC_BUT_HIDDEN) {
  342. $class = 'class="text-muted"';
  343. }
  344. $qm_or_amp = strpos($tool['link'], '?') === false ? '?' : '&amp;';
  345. $tool['link'] = $tool['link'];
  346. $html .= '<td width="50%" height="30">';
  347. if (strpos($tool['name'], 'visio_') !== false) {
  348. $html .= '<a '.$class.' href="javascript: void(0);" onclick="javascript: window.open(\''.htmlspecialchars($tool['link']).(($tool['image'] == 'external.gif' || $tool['image'] == 'external_na.gif') ? '' : $qm_or_amp.api_get_cidreq()).'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'">';
  349. } elseif (strpos($tool['name'], 'chat') !== false && api_get_course_setting('allow_open_chat_window')) {
  350. $html .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.htmlspecialchars($tool['link']).$qm_or_amp.api_get_cidreq().'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'" '.$class.'>';
  351. } else {
  352. $html .= '<a href="'.htmlspecialchars($tool['link']).(($tool['image'] == 'external.gif' || $tool['image'] == 'external_na.gif') ? '' : $qm_or_amp.api_get_cidreq()).'" target="'.$tool['target'].'" '.$class.'>';
  353. }
  354. $tool_name = self::translate_tool_name($tool);
  355. $html .= Display::return_icon(
  356. $tool['image'],
  357. $tool_name,
  358. [],
  359. null,
  360. ICON_SIZE_MEDIUM
  361. ).'&nbsp;'.$tool_name.
  362. '</a>';
  363. // This part displays the links to hide or remove a tool.
  364. // These links are only visible by the course manager.
  365. unset($lnk);
  366. if (api_is_allowed_to_edit(null, true) && !api_is_coach()) {
  367. if ($tool['visibility'] == '1' || $tool['name'] == TOOL_TRACKING) {
  368. $link['name'] = Display::returnFontAwesomeIcon('minus');
  369. $link['title'] = get_lang('Deactivate');
  370. $link['cmd'] = 'hide=yes';
  371. $lnk[] = $link;
  372. }
  373. if ($course_tool_category == TOOL_PUBLIC_BUT_HIDDEN) {
  374. //$link['name'] = Display::return_icon('add.gif', get_lang('Activate'));
  375. $link['name'] = Display::returnFontAwesomeIcon('plus');
  376. $link['title'] = get_lang('Activate');
  377. $link['cmd'] = 'restore=yes';
  378. $lnk[] = $link;
  379. if ($tool['added_tool'] == 1) {
  380. //$link['name'] = Display::return_icon('delete.gif', get_lang('Remove'));
  381. $link['name'] = Display::returnFontAwesomeIcon('trash');
  382. $link['title'] = get_lang('Remove');
  383. $link['cmd'] = 'remove=yes';
  384. $lnk[] = $link;
  385. }
  386. }
  387. if (isset($tool['adminlink'])) {
  388. $html .= '<a href="'.$tool['adminlink'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  389. }
  390. }
  391. if (api_is_platform_admin() && !api_is_coach()) {
  392. if ($tool['visibility'] == 2) {
  393. $link['name'] = Display::returnFontAwesomeIcon('undo');
  394. $link['title'] = get_lang('Activate');
  395. $link['cmd'] = 'hide=yes';
  396. $lnk[] = $link;
  397. if ($tool['added_tool'] == 1) {
  398. $link['name'] = get_lang('Delete');
  399. $link['cmd'] = 'askDelete=yes';
  400. $lnk[] = $link;
  401. }
  402. }
  403. if ($tool['visibility'] == 0 && $tool['added_tool'] == 0) {
  404. $link['name'] = Display::returnFontAwesomeIcon('trash');
  405. $link['title'] = get_lang('Remove');
  406. $link['cmd'] = 'remove=yes';
  407. $lnk[] = $link;
  408. }
  409. }
  410. if (is_array($lnk)) {
  411. $html .= '<div class="pull-right">';
  412. $html .= '<div class="btn-options">';
  413. $html .= '<div class="btn-group btn-group-sm" role="group">';
  414. foreach ($lnk as & $this_link) {
  415. if (!isset($tool['adminlink'])) {
  416. $html .= '<a class="btn btn-default" title='.$this_link['title'].' href="'.api_get_self().'?'.api_get_cidreq().'&amp;id='.$tool['id'].'&amp;'.$this_link['cmd'].'">'.$this_link['name'].'</a>';
  417. }
  418. }
  419. $html .= '</div>';
  420. $html .= '</div>';
  421. $html .= '</div>';
  422. }
  423. $html .= "</td>";
  424. if ($i % 2) {
  425. $html .= "</tr>";
  426. }
  427. $i++;
  428. }
  429. }
  430. if ($i % 2) {
  431. $html .= "<td width=\"50%\">&nbsp;</td></tr>";
  432. }
  433. return $html;
  434. }
  435. /**
  436. * Gets the tools of a certain category. Returns an array expected
  437. * by show_tools_category()
  438. * @param string $course_tool_category contains the category of tools to
  439. * display: "toolauthoring", "toolinteraction", "tooladmin", "tooladminplatform", "toolplugin"
  440. * @param int $courseId Optional
  441. * @param int $sessionId Optional
  442. * @return array
  443. */
  444. public static function get_tools_category(
  445. $course_tool_category,
  446. $courseId = 0,
  447. $sessionId = 0
  448. ) {
  449. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  450. $is_platform_admin = api_is_platform_admin();
  451. $all_tools_list = [];
  452. // Condition for the session
  453. $sessionId = $sessionId ?: api_get_session_id();
  454. $course_id = $courseId ?: api_get_course_int_id();
  455. $userId = api_get_user_id();
  456. $user = api_get_user_entity($userId);
  457. $condition_session = api_get_session_condition(
  458. $sessionId,
  459. true,
  460. true,
  461. 't.session_id'
  462. );
  463. switch ($course_tool_category) {
  464. case TOOL_STUDENT_VIEW:
  465. $conditions = ' WHERE visibility = 1 AND (category = "authoring" OR category = "interaction" OR category = "plugin") ';
  466. if ((api_is_coach() || api_is_course_tutor()) && $_SESSION['studentview'] != 'studentview') {
  467. $conditions = ' WHERE (
  468. visibility = 1 AND (
  469. category = "authoring" OR
  470. category = "interaction" OR
  471. category = "plugin"
  472. ) OR (name = "'.TOOL_TRACKING.'")
  473. )';
  474. }
  475. $sql = "SELECT *
  476. FROM $course_tool_table t
  477. $conditions AND
  478. c_id = $course_id $condition_session
  479. ";
  480. break;
  481. case TOOL_AUTHORING:
  482. $sql = "SELECT * FROM $course_tool_table t
  483. WHERE category = 'authoring' AND c_id = $course_id $condition_session
  484. ";
  485. break;
  486. case TOOL_INTERACTION:
  487. $sql = "SELECT * FROM $course_tool_table t
  488. WHERE category = 'interaction' AND c_id = $course_id $condition_session
  489. ";
  490. break;
  491. case TOOL_ADMIN_VISIBLE:
  492. $sql = "SELECT * FROM $course_tool_table t
  493. WHERE category = 'admin' AND visibility ='1' AND c_id = $course_id $condition_session
  494. ";
  495. break;
  496. case TOOL_ADMIN_PLATFORM:
  497. $sql = "SELECT * FROM $course_tool_table t
  498. WHERE category = 'admin' AND c_id = $course_id $condition_session
  499. ";
  500. break;
  501. case TOOL_DRH:
  502. $sql = "SELECT * FROM $course_tool_table t
  503. WHERE name IN ('tracking') AND c_id = $course_id $condition_session
  504. ";
  505. break;
  506. case TOOL_COURSE_PLUGIN:
  507. //Other queries recover id, name, link, image, visibility, admin, address, added_tool, target, category and session_id
  508. // but plugins are not present in the tool table, only globally and inside the course_settings table once configured
  509. $sql = "SELECT * FROM $course_tool_table t
  510. WHERE category = 'plugin' AND name <> 'courseblock' AND c_id = $course_id $condition_session
  511. ";
  512. break;
  513. }
  514. $sql .= " ORDER BY id ";
  515. $result = Database::query($sql);
  516. $tools = [];
  517. while ($row = Database::fetch_assoc($result)) {
  518. $tools[] = $row;
  519. }
  520. // Get the list of hidden tools - this might imply performance slowdowns
  521. // if the course homepage is loaded many times, so the list of hidden
  522. // tools might benefit from a shared memory storage later on
  523. $list = api_get_settings('Tools', 'list', api_get_current_access_url_id());
  524. $hide_list = [];
  525. $check = false;
  526. foreach ($list as $line) {
  527. // Admin can see all tools even if the course_hide_tools configuration is set
  528. if ($is_platform_admin) {
  529. continue;
  530. }
  531. if ($line['variable'] == 'course_hide_tools' && $line['selected_value'] == 'true') {
  532. $hide_list[] = $line['subkey'];
  533. $check = true;
  534. }
  535. }
  536. $allowEditionInSession = api_get_configuration_value('allow_edit_tool_visibility_in_session');
  537. $toolWithSessionValue = [];
  538. foreach ($tools as $row) {
  539. if (!empty($row['session_id'])) {
  540. $toolWithSessionValue[$row['name']] = $row;
  541. }
  542. }
  543. foreach ($tools as $temp_row) {
  544. $add = false;
  545. if ($check) {
  546. if (!in_array($temp_row['name'], $hide_list)) {
  547. $add = true;
  548. }
  549. } else {
  550. $add = true;
  551. }
  552. if (isset($toolWithSessionValue[$temp_row['name']])) {
  553. if (!empty($temp_row['session_id'])) {
  554. continue;
  555. }
  556. }
  557. if ($allowEditionInSession && !empty($sessionId)) {
  558. // Checking if exist row in session
  559. $criteria = [
  560. 'cId' => $course_id,
  561. 'name' => $temp_row['name'],
  562. 'sessionId' => $sessionId,
  563. ];
  564. /** @var CTool $toolObj */
  565. $toolObj = Database::getManager()->getRepository('ChamiloCourseBundle:CTool')->findOneBy($criteria);
  566. if ($toolObj) {
  567. if (api_is_allowed_to_edit() == false && $toolObj->getVisibility() == false) {
  568. continue;
  569. }
  570. }
  571. }
  572. if ($temp_row['image'] == 'scormbuilder.gif') {
  573. $lp_id = self::get_published_lp_id_from_link($temp_row['link']);
  574. $lp = new learnpath(
  575. api_get_course_id(),
  576. $lp_id,
  577. $userId
  578. );
  579. $path = $lp->get_preview_image_path(ICON_SIZE_BIG);
  580. $add = learnpath::is_lp_visible_for_student(
  581. $lp_id,
  582. $userId,
  583. api_get_course_id(),
  584. api_get_session_id()
  585. );
  586. if ($path) {
  587. $temp_row['custom_image'] = $path;
  588. }
  589. }
  590. if ($temp_row['image'] === 'lp_category.gif') {
  591. $lpCategory = self::getPublishedLpCategoryFromLink(
  592. $temp_row['link']
  593. );
  594. $add = learnpath::categoryIsVisibleForStudent(
  595. $lpCategory,
  596. $user
  597. );
  598. }
  599. if ($add) {
  600. $all_tools_list[] = $temp_row;
  601. }
  602. }
  603. // Grabbing all the links that have the property on_homepage set to 1
  604. $course_link_table = Database::get_course_table(TABLE_LINK);
  605. $course_item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  606. $condition_session = api_get_session_condition(
  607. $sessionId,
  608. true,
  609. true,
  610. 'tip.session_id'
  611. );
  612. switch ($course_tool_category) {
  613. case TOOL_AUTHORING:
  614. $sql_links = "SELECT tl.*, tip.visibility
  615. FROM $course_link_table tl
  616. LEFT JOIN $course_item_property_table tip
  617. ON tip.tool='link' AND tip.ref=tl.id
  618. WHERE
  619. tl.c_id = $course_id AND
  620. tip.c_id = $course_id AND
  621. tl.on_homepage='1' $condition_session";
  622. break;
  623. case TOOL_INTERACTION:
  624. $sql_links = null;
  625. /*
  626. $sql_links = "SELECT tl.*, tip.visibility
  627. FROM $course_link_table tl
  628. LEFT JOIN $course_item_property_table tip ON tip.tool='link' AND tip.ref=tl.id
  629. WHERE tl.on_homepage='1' ";
  630. */
  631. break;
  632. case TOOL_STUDENT_VIEW:
  633. $sql_links = "SELECT tl.*, tip.visibility
  634. FROM $course_link_table tl
  635. LEFT JOIN $course_item_property_table tip
  636. ON tip.tool='link' AND tip.ref=tl.id
  637. WHERE
  638. tl.c_id = $course_id AND
  639. tip.c_id = $course_id AND
  640. tl.on_homepage ='1' $condition_session";
  641. break;
  642. case TOOL_ADMIN:
  643. $sql_links = "SELECT tl.*, tip.visibility
  644. FROM $course_link_table tl
  645. LEFT JOIN $course_item_property_table tip
  646. ON tip.tool='link' AND tip.ref=tl.id
  647. WHERE
  648. tl.c_id = $course_id AND
  649. tip.c_id = $course_id AND
  650. tl.on_homepage='1' $condition_session";
  651. break;
  652. default:
  653. $sql_links = null;
  654. break;
  655. }
  656. // Edited by Kevin Van Den Haute (kevin@develop-it.be) for integrating Smartblogs
  657. if ($sql_links != null) {
  658. $result_links = Database::query($sql_links);
  659. if (Database::num_rows($result_links) > 0) {
  660. while ($links_row = Database::fetch_array($result_links, 'ASSOC')) {
  661. $properties = [];
  662. $properties['name'] = $links_row['title'];
  663. $properties['session_id'] = $links_row['session_id'];
  664. $properties['link'] = $links_row['url'];
  665. $properties['visibility'] = $links_row['visibility'];
  666. $properties['image'] = $links_row['visibility'] == '0' ? 'file_html.png' : 'file_html.png';
  667. $properties['adminlink'] = api_get_path(WEB_CODE_PATH).'link/link.php?action=editlink&id='.$links_row['id'];
  668. $properties['target'] = $links_row['target'];
  669. $tmp_all_tools_list[] = $properties;
  670. }
  671. }
  672. }
  673. if (isset($tmp_all_tools_list)) {
  674. foreach ($tmp_all_tools_list as $tool) {
  675. if ($tool['image'] == 'blog.gif') {
  676. // Init
  677. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  678. // Get blog id
  679. $blog_id = substr($tool['link'], strrpos($tool['link'], '=') + 1, strlen($tool['link']));
  680. // Get blog members
  681. if ($is_platform_admin) {
  682. $sql = "SELECT * FROM $tbl_blogs_rel_user blogs_rel_user
  683. WHERE blog_id = ".$blog_id;
  684. } else {
  685. $sql = "SELECT * FROM $tbl_blogs_rel_user blogs_rel_user
  686. WHERE blog_id = ".$blog_id." AND user_id = ".$userId;
  687. }
  688. $result = Database::query($sql);
  689. if (Database::num_rows($result) > 0) {
  690. $all_tools_list[] = $tool;
  691. }
  692. } else {
  693. $all_tools_list[] = $tool;
  694. }
  695. }
  696. }
  697. $all_tools_list = CourseHome::filterPluginTools($all_tools_list, $course_tool_category);
  698. return $all_tools_list;
  699. }
  700. /**
  701. * Filter tool icons. Only show if $patronKey is = :teacher
  702. * Example dataIcons[i]['name']: parameter titleIcons1:teacher || titleIcons2 || titleIcons3:teacher
  703. * @param array $dataIcons array Reference to icons
  704. * @param string $courseToolCategory Current tools category
  705. * @return array
  706. */
  707. private static function filterPluginTools($dataIcons, $courseToolCategory)
  708. {
  709. $patronKey = ':teacher';
  710. if ($courseToolCategory == TOOL_STUDENT_VIEW) {
  711. //Fix only coach can see external pages - see #8236 - icpna
  712. if (api_is_coach()) {
  713. foreach ($dataIcons as $index => $array) {
  714. if (isset($array['name'])) {
  715. $dataIcons[$index]['name'] = str_replace($patronKey, '', $array['name']);
  716. }
  717. }
  718. return $dataIcons;
  719. }
  720. $flagOrder = false;
  721. foreach ($dataIcons as $index => $array) {
  722. if (!isset($array['name'])) {
  723. continue;
  724. }
  725. $pos = strpos($array['name'], $patronKey);
  726. if ($pos !== false) {
  727. unset($dataIcons[$index]);
  728. $flagOrder = true;
  729. }
  730. }
  731. if ($flagOrder) {
  732. return array_values($dataIcons);
  733. }
  734. return $dataIcons;
  735. }
  736. // clean patronKey of name icons
  737. foreach ($dataIcons as $index => $array) {
  738. if (isset($array['name'])) {
  739. $dataIcons[$index]['name'] = str_replace($patronKey, '', $array['name']);
  740. }
  741. }
  742. return $dataIcons;
  743. }
  744. /**
  745. * Displays the tools of a certain category.
  746. * @param array $all_tools_list List of tools as returned by get_tools_category()
  747. * @param bool $rows
  748. *
  749. * @return array
  750. */
  751. public static function show_tools_category($all_tools_list, $rows = false)
  752. {
  753. $_user = api_get_user_info();
  754. $theme = api_get_setting('homepage_view');
  755. if ($theme === 'vertical_activity') {
  756. //ordering by get_lang name
  757. $order_tool_list = [];
  758. if (is_array($all_tools_list) && count($all_tools_list) > 0) {
  759. foreach ($all_tools_list as $key => $new_tool) {
  760. $tool_name = self::translate_tool_name($new_tool);
  761. $order_tool_list [$key] = $tool_name;
  762. }
  763. natsort($order_tool_list);
  764. $my_temp_tool_array = [];
  765. foreach ($order_tool_list as $key => $new_tool) {
  766. $my_temp_tool_array[] = $all_tools_list[$key];
  767. }
  768. $all_tools_list = $my_temp_tool_array;
  769. } else {
  770. $all_tools_list = [];
  771. }
  772. }
  773. $web_code_path = api_get_path(WEB_CODE_PATH);
  774. $session_id = api_get_session_id();
  775. $is_platform_admin = api_is_platform_admin();
  776. $allowEditionInSession = api_get_configuration_value('allow_edit_tool_visibility_in_session');
  777. if ($session_id == 0) {
  778. $is_allowed_to_edit = api_is_allowed_to_edit(null, true) && api_is_course_admin();
  779. } else {
  780. $is_allowed_to_edit = api_is_allowed_to_edit(null, true) && !api_is_coach();
  781. if ($allowEditionInSession) {
  782. $is_allowed_to_edit = api_is_allowed_to_edit(null, true) && api_is_coach($session_id, api_get_course_int_id());
  783. }
  784. }
  785. $i = 0;
  786. $items = [];
  787. $app_plugin = new AppPlugin();
  788. if (isset($all_tools_list)) {
  789. $lnk = '';
  790. foreach ($all_tools_list as & $tool) {
  791. $item = [];
  792. $studentview = false;
  793. $tool['original_link'] = $tool['link'];
  794. if ($tool['image'] == 'scormbuilder.gif') {
  795. // check if the published learnpath is visible for student
  796. $published_lp_id = self::get_published_lp_id_from_link($tool['link']);
  797. if (api_is_allowed_to_edit(null, true)) {
  798. $studentview = true;
  799. }
  800. if (!api_is_allowed_to_edit(null, true) &&
  801. !learnpath::is_lp_visible_for_student(
  802. $published_lp_id,
  803. api_get_user_id(),
  804. api_get_course_id(),
  805. api_get_session_id()
  806. )
  807. ) {
  808. continue;
  809. }
  810. }
  811. if ($session_id != 0 && in_array($tool['name'], ['course_setting'])) {
  812. continue;
  813. }
  814. // This part displays the links to hide or remove a tool.
  815. // These links are only visible by the course manager.
  816. unset($lnk);
  817. $item['extra'] = null;
  818. $toolAdmin = isset($tool['admin']) ? $tool['admin'] : '';
  819. if ($is_allowed_to_edit) {
  820. if (empty($session_id)) {
  821. if (isset($tool['id'])) {
  822. if ($tool['visibility'] == '1' && $toolAdmin != '1') {
  823. $link['name'] = Display::return_icon(
  824. 'visible.png',
  825. get_lang('Deactivate'),
  826. ['id' => 'linktool_'.$tool['iid']],
  827. ICON_SIZE_SMALL,
  828. false
  829. );
  830. $link['cmd'] = 'hide=yes';
  831. $lnk[] = $link;
  832. }
  833. if ($tool['visibility'] == '0' && $toolAdmin != '1') {
  834. $link['name'] = Display::return_icon(
  835. 'invisible.png',
  836. get_lang('Activate'),
  837. ['id' => 'linktool_'.$tool['iid']],
  838. ICON_SIZE_SMALL,
  839. false
  840. );
  841. $link['cmd'] = 'restore=yes';
  842. $lnk[] = $link;
  843. }
  844. }
  845. } elseif ($allowEditionInSession) {
  846. $criteria = [
  847. 'cId' => api_get_course_int_id(),
  848. 'name' => $tool['name'],
  849. 'sessionId' => $session_id
  850. ];
  851. /** @var CTool $tool */
  852. $toolObj = Database::getManager()->getRepository('ChamiloCourseBundle:CTool')->findOneBy($criteria);
  853. if ($toolObj) {
  854. $visibility = (int) $toolObj->getVisibility();
  855. switch ($visibility) {
  856. case '0':
  857. $info = pathinfo($tool['image']);
  858. $basename = basename($tool['image'], '.'.$info['extension']); // $file is set to "index"
  859. $tool['image'] = $basename.'_na.'.$info['extension'];
  860. $link['name'] = Display::return_icon(
  861. 'invisible.png',
  862. get_lang('Activate'),
  863. ['id' => 'linktool_'.$tool['iid']],
  864. ICON_SIZE_SMALL,
  865. false
  866. );
  867. $link['cmd'] = 'restore=yes';
  868. $lnk[] = $link;
  869. break;
  870. case '1':
  871. $link['name'] = Display::return_icon(
  872. 'visible.png',
  873. get_lang('Deactivate'),
  874. ['id' => 'linktool_'.$tool['iid']],
  875. ICON_SIZE_SMALL,
  876. false
  877. );
  878. $link['cmd'] = 'hide=yes';
  879. $lnk[] = $link;
  880. break;
  881. }
  882. } else {
  883. $link['name'] = Display::return_icon(
  884. 'visible.png',
  885. get_lang('Deactivate'),
  886. ['id' => 'linktool_'.$tool['iid']],
  887. ICON_SIZE_SMALL,
  888. false
  889. );
  890. $link['cmd'] = 'hide=yes';
  891. $lnk[] = $link;
  892. }
  893. }
  894. if (!empty($tool['adminlink'])) {
  895. $item['extra'] = '<a href="'.$tool['adminlink'].'">'.
  896. Display::return_icon('edit.gif', get_lang('Edit')).
  897. '</a>';
  898. }
  899. }
  900. // Both checks are necessary as is_platform_admin doesn't take student view into account
  901. if ($is_platform_admin && $is_allowed_to_edit) {
  902. if ($toolAdmin != '1') {
  903. $link['cmd'] = 'hide=yes';
  904. }
  905. }
  906. $item['visibility'] = null;
  907. if (isset($lnk) && is_array($lnk)) {
  908. foreach ($lnk as $this_link) {
  909. if (empty($tool['adminlink'])) {
  910. $item['visibility'] .=
  911. '<a class="make_visible_and_invisible" href="'.api_get_self().'?'.api_get_cidreq().'&id='.$tool['iid'].'&'.$this_link['cmd'].'">'.
  912. $this_link['name'].'</a>';
  913. }
  914. }
  915. } else {
  916. $item['visibility'] .= '';
  917. }
  918. // NOTE : Table contains only the image file name, not full path
  919. if (stripos($tool['link'], 'http://') === false &&
  920. stripos($tool['link'], 'https://') === false &&
  921. stripos($tool['link'], 'ftp://') === false
  922. ) {
  923. $tool['link'] = $web_code_path.$tool['link'];
  924. }
  925. if ($tool['visibility'] == '0' && $toolAdmin != '1') {
  926. $class = 'text-muted';
  927. $info = pathinfo($tool['image']);
  928. $basename = basename($tool['image'], '.'.$info['extension']); // $file is set to "index"
  929. $tool['image'] = $basename.'_na.'.$info['extension'];
  930. } else {
  931. $class = '';
  932. }
  933. $qm_or_amp = strpos($tool['link'], '?') === false ? '?' : '&';
  934. // If it's a link, we don't add the cidReq
  935. if ($tool['image'] == 'file_html.png' || $tool['image'] == 'file_html_na.png') {
  936. $tool['link'] = $tool['link'].$qm_or_amp;
  937. } else {
  938. $tool['link'] = $tool['link'].$qm_or_amp.api_get_cidreq();
  939. }
  940. $tool_link_params = [];
  941. $toolIid = isset($tool["iid"]) ? $tool["iid"] : null;
  942. //@todo this visio stuff should be removed
  943. if (strpos($tool['name'], 'visio_') !== false) {
  944. $tool_link_params = [
  945. 'id' => 'tooldesc_'.$toolIid,
  946. 'href' => '"javascript: void(0);"',
  947. 'class' => $class,
  948. 'onclick' => 'javascript: window.open(\''.$tool['link'].'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')',
  949. 'target' => $tool['target']
  950. ];
  951. } elseif (strpos($tool['name'], 'chat') !== false &&
  952. api_get_course_setting('allow_open_chat_window')
  953. ) {
  954. $tool_link_params = [
  955. 'id' => 'tooldesc_'.$toolIid,
  956. 'class' => $class,
  957. 'href' => 'javascript: void(0);',
  958. 'onclick' => 'javascript: window.open(\''.$tool['link'].'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')', //Chat Open Windows
  959. 'target' => $tool['target']
  960. ];
  961. } else {
  962. $tool_link_params = [
  963. 'id' => 'tooldesc_'.$toolIid,
  964. 'href' => $tool['link'],
  965. 'class' => $class,
  966. 'target' => $tool['target']
  967. ];
  968. }
  969. $tool_name = self::translate_tool_name($tool);
  970. // Including Courses Plugins
  971. // Creating title and the link
  972. if (isset($tool['category']) && $tool['category'] == 'plugin') {
  973. $plugin_info = $app_plugin->getPluginInfo($tool['name']);
  974. if (isset($plugin_info) && isset($plugin_info['title'])) {
  975. $tool_name = $plugin_info['title'];
  976. }
  977. if (!file_exists(api_get_path(SYS_CODE_PATH).'img/'.$tool['image']) &&
  978. !file_exists(api_get_path(SYS_CODE_PATH).'img/icons/64/'.$tool['image'])) {
  979. $tool['image'] = 'plugins.png';
  980. }
  981. $tool_link_params['href'] = api_get_path(WEB_PLUGIN_PATH)
  982. .$tool['original_link'].$qm_or_amp.api_get_cidreq();
  983. }
  984. $icon = Display::return_icon(
  985. $tool['image'],
  986. $tool_name,
  987. ['class' => 'tool-icon', 'id' => 'toolimage_'.$toolIid],
  988. ICON_SIZE_BIG,
  989. false
  990. );
  991. /*if (!empty($tool['custom_icon'])) {
  992. $image = self::getCustomWebIconPath().$tool['custom_icon'];
  993. $icon = Display::img(
  994. $image,
  995. $tool['description'],
  996. array(
  997. 'class' => 'tool-icon',
  998. 'id' => 'toolimage_'.$tool['id']
  999. )
  1000. );
  1001. }*/
  1002. // Validation when belongs to a session
  1003. $session_img = api_get_session_image(
  1004. $tool['session_id'],
  1005. !empty($_user['status']) ? $_user['status'] : ''
  1006. );
  1007. if ($studentview) {
  1008. $tool_link_params['href'] .= '&isStudentView=true';
  1009. }
  1010. $item['url_params'] = $tool_link_params;
  1011. $item['icon'] = Display::url($icon, $tool_link_params['href'], $tool_link_params);
  1012. $item['tool'] = $tool;
  1013. $item['name'] = $tool_name;
  1014. $tool_link_params['id'] = 'is'.$tool_link_params['id'];
  1015. $item['link'] = Display::url(
  1016. $tool_name.$session_img,
  1017. $tool_link_params['href'],
  1018. $tool_link_params
  1019. );
  1020. $items[] = $item;
  1021. $i++;
  1022. } // end of foreach
  1023. }
  1024. if (api_get_setting('homepage_view') != 'activity_big') {
  1025. return $items;
  1026. }
  1027. foreach ($items as &$item) {
  1028. $originalImage = self::getToolIcon($item);
  1029. $item['tool']['image'] = Display::url(
  1030. $originalImage,
  1031. $item['url_params']['href'],
  1032. $item['url_params']
  1033. );
  1034. }
  1035. return $items;
  1036. }
  1037. /**
  1038. * Find the tool icon when homepage_view is activity_big
  1039. * @param array $item
  1040. * @return string
  1041. */
  1042. private static function getToolIcon(array $item)
  1043. {
  1044. $image = str_replace('.gif', '.png', $item['tool']['image']);
  1045. $toolIid = isset($item['tool']['iid']) ? $item['tool']['iid'] : null;
  1046. if (isset($item['tool']['custom_image'])) {
  1047. return Display::img(
  1048. $item['tool']['custom_image'],
  1049. $item['name'],
  1050. ['id' => 'toolimage_'.$toolIid]
  1051. );
  1052. }
  1053. if (isset($item['tool']['custom_icon']) && !empty($item['tool']['custom_icon'])) {
  1054. $customIcon = $item['tool']['custom_icon'];
  1055. if ($item['tool']['visibility'] == '0') {
  1056. $customIcon = self::getDisableIcon($item['tool']['custom_icon']);
  1057. }
  1058. return Display::img(
  1059. self::getCustomWebIconPath().$customIcon,
  1060. $item['name'],
  1061. ['id' => 'toolimage_'.$toolIid]
  1062. );
  1063. }
  1064. return Display::return_icon(
  1065. $image,
  1066. $item['name'],
  1067. ['id' => 'toolimage_'.$toolIid],
  1068. ICON_SIZE_BIG,
  1069. false
  1070. );
  1071. }
  1072. /**
  1073. * Shows the general data for a particular meeting
  1074. *
  1075. * @param int $id_session
  1076. * @return string session data
  1077. */
  1078. public static function show_session_data($id_session)
  1079. {
  1080. $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1081. $sessionInfo = api_get_session_info($id_session);
  1082. if (empty($sessionInfo)) {
  1083. return '';
  1084. }
  1085. $sql = 'SELECT name FROM '.$session_category_table.'
  1086. WHERE id = "'.intval($sessionInfo['session_category_id']).'"';
  1087. $rs_category = Database::query($sql);
  1088. $session_category = '';
  1089. if (Database::num_rows($rs_category) > 0) {
  1090. $rows_session_category = Database::store_result($rs_category);
  1091. $rows_session_category = $rows_session_category[0];
  1092. $session_category = $rows_session_category['name'];
  1093. }
  1094. $coachInfo = api_get_user_info($sessionInfo['id_coach']);
  1095. $output = '';
  1096. if (!empty($session_category)) {
  1097. $output .= '<tr><td>'.get_lang('SessionCategory').': '.'<b>'.$session_category.'</b></td></tr>';
  1098. }
  1099. $dateInfo = SessionManager::parseSessionDates($sessionInfo);
  1100. $msgDate = $dateInfo['access'];
  1101. $output .= '<tr>
  1102. <td style="width:50%">'.get_lang('SessionName').': '.'<b>'.$sessionInfo['name'].'</b></td>
  1103. <td>'.get_lang('GeneralCoach').': '.'<b>'.$coachInfo['complete_name'].'</b></td></tr>';
  1104. $output .= '<tr>
  1105. <td>'.get_lang('SessionIdentifier').': '.
  1106. Display::return_icon('star.png', ' ', ['align' => 'absmiddle']).'
  1107. </td>
  1108. <td>'.get_lang('Date').': '.'<b>'.$msgDate.'</b>
  1109. </td>
  1110. </tr>';
  1111. return $output;
  1112. }
  1113. /**
  1114. * Retrieves the name-field within a tool-record and translates it on necessity.
  1115. * @param array $tool The input record.
  1116. * @return string Returns the name of the corresponding tool.
  1117. */
  1118. public static function translate_tool_name(& $tool)
  1119. {
  1120. static $already_translated_icons = [
  1121. 'file_html.gif',
  1122. 'file_html_na.gif',
  1123. 'file_html.png',
  1124. 'file_html_na.png',
  1125. 'scormbuilder.gif',
  1126. 'scormbuilder_na.gif',
  1127. 'blog.gif',
  1128. 'blog_na.gif',
  1129. 'external.gif',
  1130. 'external_na.gif'
  1131. ];
  1132. $toolName = Security::remove_XSS(stripslashes($tool['name']));
  1133. if (in_array($tool['image'], $already_translated_icons)) {
  1134. return $toolName;
  1135. }
  1136. $toolName = api_underscore_to_camel_case($toolName);
  1137. if (isset($GLOBALS['Tool'.$toolName])) {
  1138. return get_lang('Tool'.$toolName);
  1139. }
  1140. return $toolName;
  1141. }
  1142. /**
  1143. * Get published learning path id from link inside course home
  1144. * @param string Link to published lp
  1145. * @return int Learning path id
  1146. */
  1147. public static function get_published_lp_id_from_link($published_lp_link)
  1148. {
  1149. $lp_id = 0;
  1150. $param_lp_id = strstr($published_lp_link, 'lp_id=');
  1151. if (!empty($param_lp_id)) {
  1152. $a_param_lp_id = explode('=', $param_lp_id);
  1153. if (isset($a_param_lp_id[1])) {
  1154. $lp_id = intval($a_param_lp_id[1]);
  1155. }
  1156. }
  1157. return $lp_id;
  1158. }
  1159. /**
  1160. * Get published learning path category from link inside course home
  1161. * @param string $link
  1162. * @return CLpCategory
  1163. */
  1164. public static function getPublishedLpCategoryFromLink($link)
  1165. {
  1166. $query = parse_url($link, PHP_URL_QUERY);
  1167. parse_str($query, $params);
  1168. $id = isset($params['id']) ? (int) $params['id'] : 0;
  1169. $em = Database::getManager();
  1170. /** @var CLpCategory $category */
  1171. $category = $em->find('ChamiloCourseBundle:CLpCategory', $id);
  1172. return $category;
  1173. }
  1174. /**
  1175. * @param bool $include_admin_tools
  1176. * @return array
  1177. */
  1178. public static function get_navigation_items($include_admin_tools = false)
  1179. {
  1180. $navigation_items = [];
  1181. $course_id = api_get_course_int_id();
  1182. $courseInfo = api_get_course_info();
  1183. $sessionId = api_get_session_id();
  1184. if (!empty($course_id)) {
  1185. $course_tools_table = Database::get_course_table(TABLE_TOOL_LIST);
  1186. /* Link to the Course homepage */
  1187. $navigation_items['home']['image'] = 'home.gif';
  1188. $navigation_items['home']['link'] = $courseInfo['course_public_url'];
  1189. $navigation_items['home']['name'] = get_lang('CourseHomepageLink');
  1190. $sql = "SELECT * FROM $course_tools_table
  1191. WHERE c_id = $course_id AND visibility='1' and admin='0'
  1192. ORDER BY id ASC";
  1193. $result = Database::query($sql);
  1194. $hideTools = [];
  1195. $hideToolsKeys = [];
  1196. if (!api_is_platform_admin()) {
  1197. $hideTools = api_get_setting('course_hide_tools');
  1198. $hideToolsKeys = array_keys($hideTools);
  1199. }
  1200. while ($row = Database::fetch_array($result)) {
  1201. if (!empty($hideTools)) {
  1202. if (in_array($row['name'], $hideToolsKeys)) {
  1203. // Tool is hidden
  1204. if ($hideTools[$row['name']] == 'true') {
  1205. continue;
  1206. }
  1207. }
  1208. }
  1209. $navigation_items[$row['id']] = $row;
  1210. if (stripos($row['link'], 'http://') === false &&
  1211. stripos($row['link'], 'https://') === false
  1212. ) {
  1213. $navigation_items[$row['id']]['link'] = api_get_path(WEB_CODE_PATH);
  1214. if ($row['category'] == 'plugin') {
  1215. $plugin = new AppPlugin();
  1216. $pluginInfo = $plugin->getPluginInfo($row['name']);
  1217. if (isset($pluginInfo['title'])) {
  1218. $navigation_items[$row['id']]['link'] = api_get_path(WEB_PLUGIN_PATH);
  1219. $navigation_items[$row['id']]['name'] = $pluginInfo['title'];
  1220. }
  1221. } else {
  1222. $navigation_items[$row['id']]['name'] = self::translate_tool_name($row);
  1223. }
  1224. $navigation_items[$row['id']]['link'] .= $row['link'];
  1225. }
  1226. }
  1227. /* Admin (edit rights) only links
  1228. - Course settings (course admin only)
  1229. - Course rights (roles & rights overview) */
  1230. if ($include_admin_tools) {
  1231. $sql = "SELECT name, image FROM $course_tools_table
  1232. WHERE c_id = $course_id AND link='course_info/infocours.php'";
  1233. $sql_result = Database::query($sql);
  1234. $course_setting_info = Database::fetch_array($sql_result);
  1235. $course_setting_visual_name = self::translate_tool_name($course_setting_info);
  1236. if ($sessionId == 0) {
  1237. // course settings item
  1238. $navigation_items['course_settings']['image'] = $course_setting_info['image'];
  1239. $navigation_items['course_settings']['link'] = api_get_path(WEB_CODE_PATH).'course_info/infocours.php';
  1240. $navigation_items['course_settings']['name'] = $course_setting_visual_name;
  1241. }
  1242. }
  1243. }
  1244. foreach ($navigation_items as $key => $navigation_item) {
  1245. if (strstr($navigation_item['link'], '?')) {
  1246. //link already contains a parameter, add course id parameter with &
  1247. $parameter_separator = '&amp;';
  1248. } else {
  1249. //link doesn't contain a parameter yet, add course id parameter with ?
  1250. $parameter_separator = '?';
  1251. }
  1252. //$navigation_items[$key]['link'] .= $parameter_separator.api_get_cidreq();
  1253. $navigation_items[$key]['link'] .= $parameter_separator.'cidReq='.api_get_course_id().'&gidReq=0&id_session='.$sessionId;
  1254. }
  1255. return $navigation_items;
  1256. }
  1257. /**
  1258. * Show a navigation menu
  1259. */
  1260. public static function show_navigation_menu()
  1261. {
  1262. $navigation_items = self::get_navigation_items(true);
  1263. $course_id = api_get_course_id();
  1264. $class = null;
  1265. $idLearn = null;
  1266. $item = null;
  1267. $marginLeft = 160;
  1268. $html = '<div id="toolnav">';
  1269. $html .= '<ul id="toolnavbox">';
  1270. $count = 0;
  1271. foreach ($navigation_items as $key => $navigation_item) {
  1272. //students can't see the course settings option
  1273. $count++;
  1274. if (!api_is_allowed_to_edit() && $key == 'course_settings') {
  1275. continue;
  1276. }
  1277. $html .= '<li>';
  1278. $url_item = parse_url($navigation_item['link']);
  1279. $url_current = parse_url($_SERVER['REQUEST_URI']);
  1280. if (api_get_setting('show_navigation_menu') == 'text') {
  1281. $class = 'text';
  1282. $marginLeft = 170;
  1283. $item = $navigation_item['name'];
  1284. } elseif (api_get_setting('show_navigation_menu') == 'icons') {
  1285. $class = 'icons';
  1286. $marginLeft = 25;
  1287. $item = Display::return_icon(
  1288. substr($navigation_item['image'], 0, -3)."png",
  1289. $navigation_item['name'],
  1290. ['class' => 'tool-img'],
  1291. ICON_SIZE_SMALL
  1292. );
  1293. } else {
  1294. $class = 'icons-text';
  1295. $item = $navigation_item['name'].
  1296. Display::return_icon(
  1297. substr($navigation_item['image'], 0, -3)."png",
  1298. $navigation_item['name'],
  1299. ['class' => 'tool-img'],
  1300. ICON_SIZE_SMALL
  1301. );
  1302. }
  1303. if (stristr($url_item['path'], $url_current['path'])) {
  1304. if (!isset($_GET['learnpath_id']) || strpos($url_item['query'], 'learnpath_id='.intval($_GET['learnpath_id'])) === 0) {
  1305. $idLearn = ' id="here"';
  1306. }
  1307. }
  1308. if (strpos($navigation_item['link'], 'chat') !== false &&
  1309. api_get_course_setting('allow_open_chat_window', $course_id)
  1310. ) {
  1311. $html .= '<a '.$idLearn.' class="btn btn-default text-left '.$class.' " href="javascript: void(0);" onclick="javascript: window.open(\''.$navigation_item['link'].'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$navigation_item['target'].'"';
  1312. $html .= ' title="'.$navigation_item['name'].'">';
  1313. $html .= $item;
  1314. $html .= '</a>';
  1315. } else {
  1316. $html .= '<a '.$idLearn.' class="btn btn-default text-left '.$class.'" href="'.$navigation_item['link'].'" target="_top" title="'.$navigation_item['name'].'">';
  1317. $html .= $item;
  1318. $html .= '</a>';
  1319. }
  1320. $html .= '</li>';
  1321. }
  1322. $html .= '</ul>';
  1323. $html .= '<script>$(function() {
  1324. $("#toolnavbox a").stop().animate({"margin-left":"-' . $marginLeft.'px"},1000);
  1325. $("#toolnavbox > li").hover(
  1326. function () {
  1327. $("a",$(this)).stop().animate({"margin-left":"-2px"},200);
  1328. $("span",$(this)).css("display","block");
  1329. },
  1330. function () {
  1331. $("a",$(this)).stop().animate({"margin-left":"-' . $marginLeft.'px"},200);
  1332. $("span",$(this)).css("display","initial");
  1333. }
  1334. );
  1335. });</script>';
  1336. $html .= '</div>';
  1337. return $html;
  1338. }
  1339. /**
  1340. * Show a toolbar with shortcuts to the course tool
  1341. * @param int $orientation
  1342. *
  1343. * @return string
  1344. */
  1345. public static function show_navigation_tool_shortcuts($orientation = SHORTCUTS_HORIZONTAL)
  1346. {
  1347. $origin = api_get_origin();
  1348. if ($origin === 'learnpath') {
  1349. return '';
  1350. }
  1351. $navigation_items = self::get_navigation_items(false);
  1352. $html = '';
  1353. if (!empty($navigation_items)) {
  1354. if ($orientation == SHORTCUTS_HORIZONTAL) {
  1355. $style_id = "toolshortcuts_horizontal";
  1356. } else {
  1357. $style_id = "toolshortcuts_vertical";
  1358. }
  1359. $html .= '<div id="'.$style_id.'">';
  1360. foreach ($navigation_items as $key => $navigation_item) {
  1361. if (strpos($navigation_item['link'], 'chat') !== false &&
  1362. api_get_course_setting('allow_open_chat_window')
  1363. ) {
  1364. $html .= '<a class="items-icon" href="javascript: void(0);" onclick="javascript: window.open(\''.$navigation_item['link'].'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$navigation_item['target'].'"';
  1365. } else {
  1366. $html .= '<a class="items-icon" href="'.$navigation_item['link'].'"';
  1367. }
  1368. if (strpos(api_get_self(), $navigation_item['link']) !== false) {
  1369. $html .= ' id="here"';
  1370. }
  1371. $html .= ' target="_top" title="'.$navigation_item['name'].'">';
  1372. if (isset($navigation_item['category']) && $navigation_item['category'] == 'plugin') {
  1373. /*$plugin_info = $app_plugin->getPluginInfo($navigation_item['name']);
  1374. if (isset($plugin_info) && isset($plugin_info['title'])) {
  1375. $tool_name = $plugin_info['title'];
  1376. }*/
  1377. if (!file_exists(api_get_path(SYS_CODE_PATH).'img/'.$navigation_item['image']) &&
  1378. !file_exists(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_MEDIUM.'/'.$navigation_item['image'])
  1379. ) {
  1380. $navigation_item['image'] = 'plugins.png';
  1381. }
  1382. }
  1383. $html .= Display::return_icon(
  1384. substr($navigation_item['image'], 0, -3).'png',
  1385. $navigation_item['name'],
  1386. [],
  1387. ICON_SIZE_MEDIUM
  1388. );
  1389. $html .= '</a> ';
  1390. if ($orientation == SHORTCUTS_VERTICAL) {
  1391. $html .= '<br />';
  1392. }
  1393. }
  1394. $html .= '</div>';
  1395. }
  1396. return $html;
  1397. }
  1398. /**
  1399. * List course homepage tools from authoring and interaction sections
  1400. * @param int $courseId The course ID (guessed from context if not provided)
  1401. * @param int $sessionId The session ID (guessed from context if not provided)
  1402. * @return array List of all tools data from the c_tools table
  1403. */
  1404. public static function toolsIconsAction($courseId = null, $sessionId = null)
  1405. {
  1406. if (empty($courseId)) {
  1407. $courseId = api_get_course_int_id();
  1408. } else {
  1409. $courseId = intval($courseId);
  1410. }
  1411. if (empty($sessionId)) {
  1412. $sessionId = api_get_session_id();
  1413. } else {
  1414. $sessionId = intval($sessionId);
  1415. }
  1416. if (empty($courseId)) {
  1417. // We shouldn't get here, but for some reason api_get_course_int_id()
  1418. // doesn't seem to get the course from the context, sometimes
  1419. return [];
  1420. }
  1421. $table = Database::get_course_table(TABLE_TOOL_LIST);
  1422. $sql = "SELECT * FROM $table
  1423. WHERE category in ('authoring','interaction')
  1424. AND c_id = $courseId
  1425. AND session_id = $sessionId
  1426. ORDER BY id";
  1427. $result = Database::query($sql);
  1428. $data = Database::store_result($result, 'ASSOC');
  1429. return $data;
  1430. }
  1431. /**
  1432. * @param int $editIcon
  1433. * @return array
  1434. */
  1435. public static function getTool($editIcon)
  1436. {
  1437. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  1438. $editIcon = intval($editIcon);
  1439. $sql = "SELECT * FROM $course_tool_table
  1440. WHERE iid = $editIcon";
  1441. $result = Database::query($sql);
  1442. $tool = Database::fetch_assoc($result, 'ASSOC');
  1443. return $tool;
  1444. }
  1445. /**
  1446. * @return string
  1447. */
  1448. public static function getCustomSysIconPath()
  1449. {
  1450. // Check if directory exists or create it if it doesn't
  1451. $dir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/';
  1452. if (!is_dir($dir)) {
  1453. mkdir($dir, api_get_permissions_for_new_directories(), true);
  1454. }
  1455. return $dir;
  1456. }
  1457. /**
  1458. * @return string
  1459. */
  1460. public static function getCustomWebIconPath()
  1461. {
  1462. // Check if directory exists or create it if it doesn't
  1463. $dir = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/';
  1464. return $dir;
  1465. }
  1466. /**
  1467. * @param string $icon
  1468. * @return string
  1469. */
  1470. public static function getDisableIcon($icon)
  1471. {
  1472. $fileInfo = pathinfo($icon);
  1473. return $fileInfo['filename'].'_na.'.$fileInfo['extension'];
  1474. }
  1475. /**
  1476. * @param int $id
  1477. * @param array $values
  1478. */
  1479. public static function updateTool($id, $values)
  1480. {
  1481. $table = Database::get_course_table(TABLE_TOOL_LIST);
  1482. $params = [
  1483. 'name' => $values['name'],
  1484. 'link' => $values['link'],
  1485. 'target' => $values['target'],
  1486. 'visibility' => $values['visibility'],
  1487. 'description' => $values['description'],
  1488. ];
  1489. if (isset($_FILES['icon']['size']) && $_FILES['icon']['size'] !== 0) {
  1490. $dir = self::getCustomSysIconPath();
  1491. // Resize image if it is larger than 64px
  1492. $temp = new Image($_FILES['icon']['tmp_name']);
  1493. $picture_infos = $temp->get_image_info();
  1494. if ($picture_infos['width'] > 64) {
  1495. $thumbwidth = 64;
  1496. } else {
  1497. $thumbwidth = $picture_infos['width'];
  1498. }
  1499. if ($picture_infos['height'] > 64) {
  1500. $new_height = 64;
  1501. } else {
  1502. $new_height = $picture_infos['height'];
  1503. }
  1504. $temp->resize($thumbwidth, $new_height, 0);
  1505. //copy the image to the course upload folder
  1506. $path = $dir.$_FILES['icon']['name'];
  1507. $result = $temp->send_image($path);
  1508. $temp = new Image($path);
  1509. $r = $temp->convert2bw();
  1510. $ext = pathinfo($path, PATHINFO_EXTENSION);
  1511. $bwPath = substr($path, 0, -(strlen($ext) + 1)).'_na.'.$ext;
  1512. if ($r === false) {
  1513. error_log('Conversion to B&W of '.$path.' failed in '.__FILE__.' at line '.__LINE__);
  1514. } else {
  1515. $temp->send_image($bwPath);
  1516. $iconName = $_FILES['icon']['name'];
  1517. $params['custom_icon'] = $iconName;
  1518. }
  1519. }
  1520. Database::update(
  1521. $table,
  1522. $params,
  1523. [' iid = ?' => [$id]]
  1524. );
  1525. }
  1526. /**
  1527. * @param int $id
  1528. */
  1529. public static function deleteIcon($id)
  1530. {
  1531. $table = Database::get_course_table(TABLE_TOOL_LIST);
  1532. $tool = self::getTool($id);
  1533. if ($tool && !empty($tool['custom_icon'])) {
  1534. $file = self::getCustomSysIconPath().$tool['custom_icon'];
  1535. $fileInfo = pathinfo($file);
  1536. $fileGray = $fileInfo['filename'].'_na.'.$fileInfo['extension'];
  1537. $fileGray = self::getCustomSysIconPath().$fileGray;
  1538. if (file_exists($file) && is_file($file)) {
  1539. if (Security::check_abs_path($file, self::getCustomSysIconPath())) {
  1540. unlink($file);
  1541. }
  1542. }
  1543. if (file_exists($fileGray) && is_file($fileGray)) {
  1544. if (Security::check_abs_path($fileGray, self::getCustomSysIconPath())) {
  1545. unlink($fileGray);
  1546. }
  1547. }
  1548. $params = [
  1549. 'custom_icon' => ''
  1550. ];
  1551. Database::update(
  1552. $table,
  1553. $params,
  1554. [' iid = ?' => [$id]]
  1555. );
  1556. }
  1557. }
  1558. }