link.php 19 KB

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