link.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Main script for the links tool.
  5. *
  6. * Features:
  7. * - Organize links into categories;
  8. * - favorites/bookmarks-like interface;
  9. * - move links up/down within a category;
  10. * - move categories up/down;
  11. * - expand/collapse all categories (except the main "non"-category);
  12. * - add link to 'root' category => category-less link is always visible.
  13. *
  14. * @author Patrick Cool, main author, completely rewritten
  15. * @author René Haentjens, added CSV file import (October 2004)
  16. * @package chamilo.link
  17. * @todo improve organisation, tables should come from database library, use formvalidator
  18. * @todo Needs serious rewriting here. This doesn't make sense
  19. */
  20. /* INIT SECTION */
  21. // Language files that need to be included
  22. $language_file = array('link', 'admin');
  23. // Including libraries
  24. require_once '../inc/global.inc.php';
  25. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  26. $this_section = SECTION_COURSES;
  27. api_protect_course_script();
  28. $htmlHeadXtra[] = '<script type="text/javascript">
  29. $(document).ready( function() {
  30. for (i=0;i<$(".actions").length;i++) {
  31. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
  32. $(".actions:eq("+i+")").hide();
  33. }
  34. }
  35. });
  36. function check_url(id, url) {
  37. var url = "'.api_get_path(WEB_AJAX_PATH).'link.ajax.php?a=check_url&url=" +url;
  38. var loading = " '.addslashes(Display::return_icon('loading1.gif')).'";
  39. $("#url_id_"+id).html(loading);
  40. $("#url_id_"+id).load(url);
  41. }
  42. </script>';
  43. // @todo change the $_REQUEST into $_POST or $_GET
  44. // @todo remove this code
  45. $link_submitted = isset($_POST['submitLink']);
  46. $category_submitted = isset($_POST['submitCategory']);
  47. $urlview = !empty($_GET['urlview']) ? $_GET['urlview'] : '';
  48. $submit_import = !empty($_POST['submitImport']) ? $_POST['submitImport'] : '';
  49. $down = !empty($_GET['down']) ? $_GET['down'] : '';
  50. $up = !empty($_GET['up']) ? $_GET['up'] : '';
  51. $catmove = !empty($_GET['catmove']) ? $_GET['catmove'] : '';
  52. $editlink = !empty($_REQUEST['editlink']) ? $_REQUEST['editlink'] : '';
  53. $id = !empty($_REQUEST['id']) ? $_REQUEST['id'] : '';
  54. $urllink = !empty($_REQUEST['urllink']) ? $_REQUEST['urllink'] : '';
  55. $title = !empty($_REQUEST['title']) ? $_REQUEST['title'] : '';
  56. $description = !empty($_REQUEST['description']) ? $_REQUEST['description'] : '';
  57. $selectcategory = !empty($_REQUEST['selectcategory']) ? $_REQUEST['selectcategory'] : '';
  58. $submit_link = isset($_REQUEST['submitLink']);
  59. $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
  60. $category_title = !empty($_REQUEST['category_title']) ? $_REQUEST['category_title'] : '';
  61. $submit_category = isset($_POST['submitCategory']);
  62. $target_link = !empty($_REQUEST['target_link']) ? $_REQUEST['target_link'] : '_self';
  63. $nameTools = get_lang('Links');
  64. // Condition for the session
  65. $session_id = api_get_session_id();
  66. $condition_session = api_get_session_condition($session_id, true, true);
  67. if (isset($_GET['action']) && $_GET['action'] == 'addlink') {
  68. $nameTools = '';
  69. $interbreadcrumb[] = array('url' => 'link.php', 'name' => get_lang('Links'));
  70. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddLink'));
  71. }
  72. if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
  73. $nameTools = '';
  74. $interbreadcrumb[] = array('url' => 'link.php', 'name' => get_lang('Links'));
  75. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddCategory'));
  76. }
  77. if (isset($_GET['action']) && $_GET['action'] == 'editlink') {
  78. $nameTools = '';
  79. $interbreadcrumb[] = array('url' => 'link.php', 'name' => get_lang('Links'));
  80. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditLink'));
  81. }
  82. // Database Table definitions
  83. $tbl_link = Database::get_course_table(TABLE_LINK);
  84. $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
  85. $course_id = api_get_course_int_id();
  86. // Statistics
  87. event_access_tool(TOOL_LINK);
  88. Display::display_header($nameTools, 'Links');
  89. ?>
  90. <script type="text/javascript">
  91. /* <![CDATA[ */
  92. function MM_popupMsg(msg) { //v1.0
  93. confirm(msg);
  94. }
  95. /* ]]> */
  96. </script>
  97. <?php
  98. /* Action Handling */
  99. $nameTools = get_lang('Links');
  100. if (isset($_GET['action'])) {
  101. $check_token = Security::check_token('request');
  102. if ($check_token) {
  103. switch ($_GET['action']) {
  104. case 'addlink':
  105. if ($link_submitted) {
  106. if (!addlinkcategory("link")) { // Here we add a link
  107. unset($submit_link);
  108. }
  109. }
  110. break;
  111. case 'addcategory':
  112. if ($category_submitted) {
  113. if (!addlinkcategory('category')) { // Here we add a category
  114. unset($submit_category);
  115. }
  116. }
  117. break;
  118. case 'importcsv':
  119. if ($_POST['submitImport']) {
  120. import_csvfile();
  121. }
  122. break;
  123. case 'deletelink':
  124. deletelinkcategory('link'); // Here we delete a link
  125. break;
  126. case 'deletecategory':
  127. deletelinkcategory('category'); // Here we delete a category
  128. break;
  129. case 'editlink':
  130. editlinkcategory('link'); // Here we edit a link
  131. break;
  132. case 'editcategory':
  133. editlinkcategory('category'); // Here we edit a category
  134. break;
  135. case 'visible':
  136. change_visibility($_GET['id'], $_GET['scope']); // Here we edit a category
  137. break;
  138. case 'invisible':
  139. change_visibility($_GET['id'], $_GET['scope']); // Here we edit a category
  140. break;
  141. }
  142. Security::clear_token();
  143. }
  144. }
  145. $token = Security::get_token();
  146. /* Introduction section */
  147. Display::display_introduction_section(TOOL_LINK);
  148. if (api_is_allowed_to_edit(null, true) && isset($_GET['action'])) {
  149. echo '<div class="actions">';
  150. if (!empty($_GET['lp_id']) || !empty($_POST['lp_id'])){
  151. if (!empty($_POST['lp_id'])){
  152. $lp_id = Security::remove_XSS($_POST['lp_id']);
  153. } else {
  154. $lp_id = Security::remove_XSS($_GET['lp_id']);
  155. }
  156. echo "<a href=\"../newscorm/lp_controller.php?".api_get_cidreq()."&gradebook=&action=add_item&type=step&lp_id=".$lp_id."#resource_tab-3\">".Display::return_icon('back.png', get_lang("BackTo").' '.get_lang("LearningPaths"),'',ICON_SIZE_MEDIUM)."</a>";
  157. } else {
  158. //echo '<a href="link.php?cidReq='.Security::remove_XSS($_GET['cidReq']).'&amp;urlview='.Security::remove_XSS($_GET['urlview']).'">'.Display::return_icon('back.png', get_lang('BackToLinksOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  159. }
  160. echo '</div>';
  161. // Displaying the correct title and the form for adding a category or link. This is only shown when nothing
  162. // has been submitted yet, hence !isset($submit_link)
  163. if (($_GET['action'] == 'addlink' || $_GET['action'] == 'editlink') && empty($_POST['submitLink'])) {
  164. if ($category == '') {
  165. $category = 0;
  166. }
  167. echo '<form class="form-horizontal" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&amp;urlview='.Security::remove_XSS($urlview).'">';
  168. if ($_GET['action'] == 'addlink') {
  169. echo '<legend>'.get_lang('LinkAdd').'</legend>';
  170. } else {
  171. echo '<legend>'.get_lang('LinkMod').'</legend>';
  172. }
  173. echo '<input type="hidden" name="sec_token" value="'.$token.'" />';
  174. if ($_GET['action'] == 'editlink') {
  175. $clean_link_id = intval($_GET['id']);
  176. $link_info = get_link_info($_GET['id']);
  177. if ($link_info) {
  178. $urllink = $link_info['url'];
  179. $title = $link_info['title'];
  180. $description = $link_info['description'];
  181. $category = $link_info['category_id'];
  182. $onhomepage = '';
  183. if ($link_info['on_homepage'] != 0) {
  184. $onhomepage = 'checked';
  185. }
  186. $target_link = $link_info['target'];
  187. }
  188. echo '<input type="hidden" name="id" value="'.$clean_link_id.'" />';
  189. }
  190. echo ' <div class="control-group">
  191. <label class="control-label">
  192. <span class="form_required">*</span> URL
  193. </label>
  194. <div class="controls">
  195. <input type="text" name="urllink" size="50" value="' . (empty($urllink) ? 'http://' : Security::remove_XSS($urllink)) . '" />
  196. </div>
  197. </div>';
  198. echo ' <div class="control-group">
  199. <label class="control-label">
  200. '.get_lang('LinkName').'
  201. </label>
  202. <div class="controls">
  203. <input type="text" name="title" size="50" value="' . Security::remove_XSS($title) . '" />
  204. </div>
  205. </div>';
  206. echo ' <div class="control-group">
  207. <label class="control-label">
  208. '.get_lang('Metadata').'
  209. </label>
  210. <div class="controls">
  211. <a href="../metadata/index.php?eid='.urlencode('Link.'.$clean_link_id).'">'.get_lang('AddMetadata').'</a>
  212. </div>
  213. </div>';
  214. echo ' <div class="control-group">
  215. <label class="control-label">
  216. '.get_lang('Description').'
  217. </label>
  218. <div class="controls">
  219. <textarea rows="3" cols="50" name="description">' . Security::remove_XSS($description) . '</textarea>
  220. </div>
  221. </div>';
  222. $sqlcategories = "SELECT * FROM ".$tbl_categories." WHERE c_id = $course_id $condition_session ORDER BY display_order DESC";
  223. $resultcategories = Database::query($sqlcategories);
  224. if (Database::num_rows($resultcategories)) {
  225. echo ' <div class="control-group">
  226. <label class="control-label">
  227. '.get_lang('Category').'
  228. </label>
  229. <div class="controls">';
  230. echo ' <select name="selectcategory">';
  231. echo ' <option value="0">--</option>';
  232. while ($myrow = Database::fetch_array($resultcategories)) {
  233. echo ' <option value="'.$myrow['id'].'"';
  234. if ($myrow['id'] == $category) {
  235. echo ' selected';
  236. }
  237. echo '>'.$myrow['category_title'].'</option>';
  238. }
  239. echo ' </select>';
  240. echo ' </div>
  241. </div>';
  242. }
  243. echo ' <div class="control-group">
  244. <label class="control-label">
  245. </label>
  246. <div class="controls">
  247. <input class="checkbox" type="checkbox" name="onhomepage" id="onhomepage" value="1"'.$onhomepage.'><label for="onhomepage"> '.get_lang('OnHomepage').'?</label>
  248. </div>
  249. </div>';
  250. echo ' <div class="control-group" id="div_target">
  251. <label class="control-label">
  252. '.get_lang('LinkTarget').'
  253. </label>
  254. <div class="controls">
  255. <select name="target_link" id="target_link">';
  256. $targets = array('_self'=>get_lang('LinkOpenSelf'),'_blank'=>get_lang('LinkOpenBlank'),'_parent'=>get_lang('LinkOpenParent'),'_top'=>get_lang('LinkOpenTop'));
  257. foreach ($targets as $target_id => $target) {
  258. $selected = '';
  259. if ($target_id == $target_link) {
  260. $selected = ' selected="selected"';
  261. }
  262. echo ' <option value="'.$target_id.'"'.$selected.'>'.$target.'</option> ';
  263. }
  264. echo ' </select>
  265. <span class="help-block">
  266. '.get_lang('AddTargetOfLinkOnHomepage').'
  267. </span>
  268. </div>
  269. </div>';
  270. if (api_get_setting('search_enabled') == 'true') {
  271. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  272. $specific_fields = get_specific_field_list();
  273. echo ' <div class="control-group">
  274. <label class="control-label">
  275. '.get_lang('SearchFeatureDoIndexLink').'
  276. </label>
  277. <div class="controls">
  278. <label for="index_document">
  279. <input class="checkbox" type="checkbox" name="index_document" id="index_document" checked="checked">
  280. '.get_lang('Yes').'
  281. </label>
  282. </div>
  283. </div>';
  284. foreach ($specific_fields as $specific_field) {
  285. $default_values = '';
  286. if ($_GET['action'] == 'editlink') {
  287. $filter = array('c_id'=> "'". api_get_course_int_id() ."'", 'field_id' => $specific_field['id'], 'ref_id' => Security::remove_XSS($_GET['id']), 'tool_id' => '\''. TOOL_LINK .'\'');
  288. $values = get_specific_field_values_list($filter, array('value'));
  289. if (!empty($values)) {
  290. $arr_str_values = array();
  291. foreach ($values as $value) {
  292. $arr_str_values[] = $value['value'];
  293. }
  294. $default_values = implode(', ', $arr_str_values);
  295. }
  296. }
  297. $sf_textbox = '
  298. <div class="control-group">
  299. <label class="control-label">%s</label>
  300. <div class="controls">
  301. <input name="%s" type="text" value="%s"/>
  302. </div>
  303. </div>';
  304. echo sprintf($sf_textbox, $specific_field['name'], $specific_field['code'], $default_values);
  305. }
  306. }
  307. //echo '<input type="hidden" name="origin" value="' . Security::remove_XSS($_GET['origin']) . '" />';
  308. echo '<input type="hidden" name="lp_id" value="' . Security::remove_XSS($_GET['lp_id']) . '" />';
  309. echo '<div class="control-group">
  310. <label class="control-label">
  311. </label>
  312. <div class="controls">
  313. <button class="save" type="Submit" name="submitLink" value="OK">'.get_lang('SaveLink').'</button>
  314. </div>
  315. </div>';
  316. echo '</form>';
  317. } elseif(($_GET['action'] == 'addcategory' || $_GET['action'] == 'editcategory') && !$submit_category) {
  318. echo '<form class="form-horizontal" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&amp;urlview='.Security::remove_XSS($urlview).'">';
  319. if ($_GET['action'] == 'addcategory') {
  320. echo '<legend>'.get_lang('CategoryAdd').'</legend>';
  321. $my_cat_title = get_lang('CategoryAdd');
  322. } else {
  323. echo '<legend>'.get_lang('CategoryMod').'</legend>';
  324. $my_cat_title = get_lang('CategoryMod');
  325. }
  326. echo '<input type="hidden" name="sec_token" value="'.$token.'" />';
  327. if ($_GET['action'] == 'editcategory') {
  328. echo '<input type="hidden" name="id" value="'.$id.'" />';
  329. }
  330. echo ' <div class="control-group">
  331. <label class="control-label">
  332. <span class="form_required">*</span> '.get_lang('CategoryName').'
  333. </label>
  334. <div class="controls">
  335. <input type="text" name="category_title" size="50" value="'.Security::remove_XSS($category_title).'" />
  336. </div>
  337. </div>';
  338. echo ' <div class="control-group">
  339. <label class="control-label">
  340. '.get_lang('Description').'
  341. </label>
  342. <div class="controls">
  343. <textarea rows="3" cols="50" name="description">'.Security::remove_XSS($description).'</textarea>
  344. </div>
  345. </div>';
  346. echo ' <div class="control-group">
  347. <label class="control-label">
  348. </label>
  349. <div class="controls">
  350. <button class="save" type="submit" name="submitCategory">'.$my_cat_title.' </button>
  351. </div>
  352. </div>';
  353. echo "</form>";
  354. }
  355. }
  356. if (!empty($down)) {
  357. movecatlink($down);
  358. }
  359. if (!empty($up)) {
  360. movecatlink($up);
  361. }
  362. if (empty($_GET['action']) || ($_GET['action'] != 'editlink' && $_GET['action'] != 'addcategory' && $_GET['action'] != 'addlink') || $link_submitted || $category_submitted) {
  363. /* Action Links */
  364. echo '<div class="actions">';
  365. if (api_is_allowed_to_edit(null, true)) {
  366. $urlview = Security::remove_XSS($urlview);
  367. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=addlink&amp;category='.(!empty($category) ? $category : '').'&amp;urlview='.$urlview.'">'.Display::return_icon('new_link.png', get_lang('LinkAdd'),'',ICON_SIZE_MEDIUM).'</a>';
  368. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=addcategory&amp;urlview='.$urlview.'">'.Display::return_icon('new_folder.png', get_lang('CategoryAdd'),'',ICON_SIZE_MEDIUM).'</a>';
  369. /* "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=importcsv&amp;urlview=".$urlview."\">".get_lang('CsvImport')."</a>\n", // RH*/
  370. }
  371. // Making the show none / show all links. Show none means urlview=0000 (number of zeros depending on the
  372. // number of categories). Show all means urlview=1111 (number of 1 depending on teh number of categories).
  373. $sqlcategories = "SELECT * FROM ".$tbl_categories." WHERE c_id = $course_id $condition_session ORDER BY display_order DESC";
  374. $resultcategories = Database::query($sqlcategories);
  375. $aantalcategories = Database::num_rows($resultcategories);
  376. if ($aantalcategories > 0) {
  377. $resultcategories = Database::query($sqlcategories);
  378. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview=';
  379. for ($j = 1; $j <= $aantalcategories; $j++) {
  380. echo '0';
  381. }
  382. echo '">'.Display::return_icon('view_remove.png', $shownone,'',ICON_SIZE_MEDIUM).'</a>';
  383. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview=';
  384. for ($j = 1; $j <= $aantalcategories; $j++) {
  385. echo '1';
  386. }
  387. echo '">'.Display::return_icon('view_tree.png', $showall,'',ICON_SIZE_MEDIUM).'</a>';
  388. }
  389. echo '</div>';
  390. // Displaying the links which have no category (thus category = 0 or NULL), if none present this will not be displayed
  391. $sqlLinks = "SELECT * FROM ".$tbl_link." WHERE c_id = $course_id AND category_id=0 OR category_id IS NULL";
  392. $result = Database::query($sqlLinks);
  393. $numberofzerocategory = Database::num_rows($result);
  394. if ($numberofzerocategory !== 0) {
  395. echo '<table class="data_table">';
  396. echo '<tr><th style="font-weight: bold; text-align:left;padding-left: 10px;">'.get_lang('General').'</th></tr>';
  397. echo '</table>';
  398. showlinksofcategory(0);
  399. }
  400. $i = 0;
  401. $catcounter = 1;
  402. $view = '0';
  403. while ($myrow = Database::fetch_array($resultcategories)) {
  404. // Validacion when belongs to a session
  405. $session_img = api_get_session_image($myrow['session_id'], $_user['status']);
  406. //if (!isset($urlview)) {
  407. if ($urlview == '') {
  408. // No $view set in the url, thus for each category link it should be all zeros except it's own
  409. makedefaultviewcode($i);
  410. } else {
  411. $view = $urlview;
  412. $view[$i] = '1';
  413. }
  414. // If the $urlview has a 1 for this categorie, this means it is expanded and should be desplayed as a
  415. // - instead of a +, the category is no longer clickable and all the links of this category are displayed
  416. $myrow['description'] = text_filter($myrow['description']);
  417. if (isset($urlview[$i]) && $urlview[$i] == '1') {
  418. $newurlview = $urlview;
  419. $newurlview[$i] = '0';
  420. echo '<tr>';
  421. echo '<table class="data_table">';
  422. echo '<tr>';
  423. echo '<th width="81%" style="font-weight: bold; text-align:left;padding-left: 5px;">';
  424. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;urlview='.Security::remove_XSS($newurlview).'">';
  425. echo '<img src="../img/icons/22/view_remove.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).'</a><br />&nbsp;&nbsp;&nbsp;'.$myrow['description'];
  426. echo '</th>';
  427. if (api_is_allowed_to_edit(null, true)) {
  428. if ($session_id == $myrow['session_id']) {
  429. echo '<th>';
  430. showcategoryadmintools($myrow['id']);
  431. echo '</th>';
  432. } else {
  433. echo '<th>'.get_lang('EditionNotAvailableFromSession');
  434. }
  435. }
  436. echo '</tr>';
  437. echo '</table>';
  438. echo showlinksofcategory($myrow['id']);
  439. echo '</tr>';
  440. } else {
  441. echo '<tr>';
  442. echo '<table class="data_table">';
  443. echo '<tr>';
  444. echo '<th width="81%" style="font-weight: bold; text-align:left;padding-left: 5px;"><a href="'.api_get_self().'?'.api_get_cidreq().'&amp;urlview=';
  445. echo is_array($view) ? implode('', $view) : $view;
  446. echo '"><img src="../img/icons/22/view_tree.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).$session_img;
  447. echo'</a><br />&nbsp;&nbsp;&nbsp;';
  448. echo $myrow['description'];
  449. if (api_is_allowed_to_edit(null, true)) {
  450. echo '<th style="text-align:center;">';
  451. showcategoryadmintools($myrow['id']);
  452. echo '</th>';
  453. }
  454. echo '</th>';
  455. echo '</tr>';
  456. echo '</table>';
  457. echo '</tr>';
  458. }
  459. // Displaying the link of the category
  460. $i++;
  461. }
  462. echo '</table>';
  463. }
  464. Display::display_footer();