Project

General

Profile

Feature #1845 » link.php

Yoselyn Castillo, 06/06/2012 20:07

 
1
<?php
2
/* For licensing terms, see /license.txt */
3

    
4
/**
5
 * Main script for the links tool.
6
 *
7
 * Features:
8
 * - Organize links into categories;
9
 * - favorites/bookmarks-like interface;
10
 * - move links up/down within a category;
11
 * - move categories up/down;
12
 * - expand/collapse all categories (except the main "non"-category);
13
 * - add link to 'root' category => category-less link is always visible.
14
 *
15
 * @author Patrick Cool, main author, completely rewritten
16
 * @author René Haentjens, added CSV file import (October 2004)
17
 * @package chamilo.link
18
 * @todo improve organisation, tables should come from database library, use formvalidator
19
 * @todo Needs serious rewriting here. This doesn't make sense
20
 */
21

    
22
/*	INIT SECTION */
23

    
24
// Language files that need to be included
25
$language_file = array('link', 'admin');
26

    
27
// Including libraries
28
require_once '../inc/global.inc.php';
29
$current_course_tool  = TOOL_LINK;
30

    
31
require_once api_get_path(LIBRARY_PATH).'link.lib.php';
32

    
33
$this_section = SECTION_COURSES;
34
api_protect_course_script();
35

    
36
$htmlHeadXtra[] = '<script type="text/javascript">
37
    $(document).ready( function() {
38
    	for (i=0;i<$(".actions").length;i++) {
39
    		if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
40
    			$(".actions:eq("+i+")").hide();
41
    		}
42
    	}
43
     });
44

    
45
     function check_url(id, url) {
46
     	var url = "'.api_get_path(WEB_AJAX_PATH).'link.ajax.php?a=check_url&url=" +url;
47
     	var loading = " '.addslashes(Display::return_icon('loading1.gif')).'";
48
     	$("#url_id_"+id).html(loading);
49
    	$("#url_id_"+id).load(url);
50
     }
51
 </script>';
52

    
53
// @todo change the $_REQUEST into $_POST or $_GET
54
// @todo remove this code
55
$link_submitted = isset($_POST['submitLink']);
56
$category_submitted = isset($_POST['submitCategory']);
57
$urlview = !empty($_GET['urlview']) ? $_GET['urlview'] : '';
58
$submit_import = !empty($_POST['submitImport']) ? $_POST['submitImport'] : '';
59
$down = !empty($_GET['down']) ? $_GET['down'] : '';
60
$up = !empty($_GET['up']) ? $_GET['up'] : '';
61
$catmove = !empty($_GET['catmove']) ? $_GET['catmove'] : '';
62
$editlink = !empty($_REQUEST['editlink']) ? $_REQUEST['editlink'] : '';
63
$id = !empty($_REQUEST['id']) ? $_REQUEST['id'] : '';
64
$urllink = !empty($_REQUEST['urllink']) ? $_REQUEST['urllink'] : '';
65
$title = !empty($_REQUEST['title']) ? $_REQUEST['title'] : '';
66
$description = !empty($_REQUEST['description']) ? $_REQUEST['description'] : '';
67
$selectcategory = !empty($_REQUEST['selectcategory']) ? $_REQUEST['selectcategory'] : '';
68
$submit_link = isset($_REQUEST['submitLink']);
69
$action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
70
$category_title = !empty($_REQUEST['category_title']) ? $_REQUEST['category_title'] : '';
71
$submit_category = isset($_POST['submitCategory']);
72
$target_link = !empty($_REQUEST['target_link']) ? $_REQUEST['target_link'] : '_self';
73

    
74
$nameTools = get_lang('Links');
75

    
76
// Condition for the session
77
$session_id = api_get_session_id();
78
$condition_session = api_get_session_condition($session_id, true, true);
79

    
80
if (isset($_GET['action']) && $_GET['action'] == 'addlink') {
81
	$nameTools = '';
82
	$interbreadcrumb[] = array('url' => 'link.php', 'name' => get_lang('Links'));
83
	$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddLink'));
84
}
85

    
86
if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
87
	$nameTools = '';
88
	$interbreadcrumb[] = array('url' => 'link.php', 'name' => get_lang('Links'));
89
	$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddCategory'));
90
}
91

    
92
if (isset($_GET['action']) && $_GET['action'] == 'editlink') {
93
	$nameTools = '';
94
	$interbreadcrumb[] = array('url' => 'link.php', 'name' => get_lang('Links'));
95
	$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditLink'));
96
}
97

    
98
// Database Table definitions
99
$tbl_link       = Database::get_course_table(TABLE_LINK);
100
$tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
101

    
102
$course_id = api_get_course_int_id();
103

    
104
// Statistics
105
event_access_tool(TOOL_LINK);
106

    
107
Display::display_header($nameTools, 'Links');
108

    
109
?>
110
<script type="text/javascript">
111
/* <![CDATA[ */
112
function MM_popupMsg(msg) { //v1.0
113
  confirm(msg);
114
}
115
/* ]]> */
116
</script>
117

    
118
<?php
119

    
120
/*	Action Handling */
121

    
122
$nameTools = get_lang('Links');
123

    
124
if (isset($_GET['action'])) {
125
	$check_token = Security::check_token('request');
126
	if ($check_token) {
127
		switch ($_GET['action']) {
128
			case 'addlink':
129
				if ($link_submitted) {
130
					if (!addlinkcategory("link")) {	// Here we add a link
131
						unset($submit_link);
132
					}
133
				}
134
				break;
135
			case 'addcategory':
136
				if ($category_submitted) {
137
					if (!addlinkcategory('category')) {	// Here we add a category
138
						unset($submit_category);
139
					}
140
				}
141
				break;
142
			case 'importcsv':
143
				if ($_POST['submitImport']) {
144
					import_csvfile();
145
				}
146
				break;
147
			case 'deletelink':
148
				deletelinkcategory('link'); // Here we delete a link
149
				break;
150
			case 'deletecategory':
151
				deletelinkcategory('category'); // Here we delete a category
152
				break;
153
			case 'editlink':
154
				editlinkcategory('link'); // Here we edit a link
155
				break;
156
			case 'editcategory':
157
				editlinkcategory('category'); // Here we edit a category
158
				break;
159
			case 'visible':
160
				change_visibility($_GET['id'], $_GET['scope']); // Here we edit a category
161
				break;
162
			case 'invisible':
163
				change_visibility($_GET['id'], $_GET['scope']); // Here we edit a category
164
				break;
165
		}
166
		Security::clear_token();
167
	}
168
}
169
$token = Security::get_token();
170

    
171
/*	Introduction section */
172

    
173
Display::display_introduction_section(TOOL_LINK);
174

    
175
if (api_is_allowed_to_edit(null, true) && isset($_GET['action'])) {
176
	echo '<div class="actions">';
177
	if (!empty($_GET['lp_id']) || !empty($_POST['lp_id'])){
178
		if (!empty($_POST['lp_id'])){
179
			$lp_id = Security::remove_XSS($_POST['lp_id']);
180
		} else {
181
			$lp_id = Security::remove_XSS($_GET['lp_id']);
182
		}
183
		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>";
184
	} else {
185
		//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>';
186
	}
187
	echo '</div>';
188

    
189
	// Displaying the correct title and the form for adding a category or link. This is only shown when nothing
190
	// has been submitted yet, hence !isset($submit_link)
191
	if (($_GET['action'] == 'addlink' || $_GET['action'] == 'editlink') && empty($_POST['submitLink'])) {
192

    
193

    
194

    
195
		if ($category == '') {
196
			$category = 0;
197
		}
198
		echo '<form class="form-horizontal" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&amp;urlview='.Security::remove_XSS($urlview).'">';
199
        if ($_GET['action'] == 'addlink') {
200
			echo '<legend>'.get_lang('LinkAdd').'</legend>';
201
		} else {
202
			echo '<legend>'.get_lang('LinkMod').'</legend>';
203
		}
204
		echo '<input type="hidden" name="sec_token" value="'.$token.'" />';
205

    
206
		if ($_GET['action'] == 'editlink') {
207
		    $clean_link_id = intval($_GET['id']);
208
            $link_info = get_link_info($_GET['id']);
209
            if ($link_info) {
210
                $urllink        = $link_info['url'];
211
                $title          = $link_info['title'];
212
                $description    = $link_info['description'];
213
                $category       = $link_info['category_id'];
214
                $onhomepage     = '';
215
                if ($link_info['on_homepage'] != 0) {
216
                    $onhomepage = 'checked';
217
                }
218
                $target_link = $link_info['target'];
219
            }
220
			echo '<input type="hidden" name="id" value="'.$clean_link_id.'" />';
221
		} else {
222
            $target_link = "_self";
223
        }
224
        
225
		echo '	<div class="control-group">
226
					<label class="control-label">
227
						<span class="form_required">*</span> URL
228
					</label>
229
					<div class="controls">
230
						<input type="text" name="urllink" class="span6" value="' . (empty($urllink) ? 'http://' : Security::remove_XSS($urllink)) . '" />
231
					</div>
232
				</div>';
233
		echo '	<div class="control-group">
234
					<label class="control-label">
235
						'.get_lang('LinkName').'
236
					</label>
237
					<div class="controls">
238
						<input type="text" name="title" class="span6" value="' . Security::remove_XSS($title) . '" />
239
					</div>
240
				</div>';
241
		echo '	<div class="control-group">
242
					<label class="control-label">
243
						'.get_lang('Metadata').'
244
					</label>
245
					<div class="controls">
246
						<a href="../metadata/index.php?eid='.urlencode('Link.'.$clean_link_id).'">'.get_lang('AddMetadata').'</a>
247
					</div>
248
				</div>';
249
		echo '	<div class="control-group">
250
					<label class="control-label">
251
						'.get_lang('Description').'
252
					</label>
253
					<div class="controls">
254
						<textarea class="span3" cols="50" name="description">' .	Security::remove_XSS($description) . '</textarea>
255
					</div>
256
				</div>';
257

    
258
		$sqlcategories = "SELECT * FROM ".$tbl_categories." WHERE c_id = $course_id $condition_session ORDER BY display_order DESC";
259
		$resultcategories = Database::query($sqlcategories);
260

    
261
		if (Database::num_rows($resultcategories)) {
262
			echo '	<div class="control-group">
263
						<label class="control-label">
264
							'.get_lang('Category').'
265
						</label>
266
						<div class="controls">';
267
			echo '			<select name="selectcategory">';
268
			echo '			<option value="0">--</option>';
269
			while ($myrow = Database::fetch_array($resultcategories)) {
270
				echo '		<option value="'.$myrow['id'].'"';
271
				if ($myrow['id'] == $category) {
272
					echo ' selected';
273
				}
274
				echo '>'.$myrow['category_title'].'</option>';
275
			}
276
			echo '			</select>';
277
			echo '		</div>
278
					</div>';
279
		}
280
		echo '	<div class="control-group">
281
					<label class="control-label">
282
					</label>
283
					<div class="controls">
284
						<input class="checkbox" type="checkbox" name="onhomepage" id="onhomepage" value="1"'.$onhomepage.'><label for="onhomepage"> '.get_lang('OnHomepage').'?</label>
285
					</div>
286
				</div>';
287
		echo '	<div class="control-group" id="div_target">
288
					<label class="control-label">
289
						'.get_lang('LinkTarget').'
290
					</label>
291
					<div class="controls">
292
						<select  name="target_link" id="target_link">';
293
        $targets = array('_self'=>get_lang('LinkOpenSelf'),'_blank'=>get_lang('LinkOpenBlank'),'_parent'=>get_lang('LinkOpenParent'),'_top'=>get_lang('LinkOpenTop'));
294
		foreach ($targets as $target_id => $target) {
295
			$selected = '';
296
			if ($target_id == $target_link) {
297
				$selected = ' selected="selected"';
298
			}
299
			echo '    	<option value="'.$target_id.'"'.$selected.'>'.$target.'</option> ';
300
		}
301
		echo '        </select>
302
		<span class="help-block">
303
            '.get_lang('AddTargetOfLinkOnHomepage').'
304
            </span>
305
					</div>
306
				</div>';
307

    
308
		if (api_get_setting('search_enabled') == 'true') {
309
			require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
310
			$specific_fields = get_specific_field_list();
311

    
312
			echo '	<div class="control-group">
313
						<label class="control-label">
314
							'.get_lang('SearchFeatureDoIndexLink').'
315
						</label>
316
						<div class="controls">
317
                            <label for="index_document">
318
							<input class="checkbox" type="checkbox" name="index_document" id="index_document" checked="checked">
319
                            '.get_lang('Yes').'
320
                            </label>
321
						</div>
322
                    </div>';
323

    
324
			foreach ($specific_fields as $specific_field) {
325
				$default_values = '';
326
				if ($_GET['action'] == 'editlink') {
327
					$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 .'\'');
328
					$values = get_specific_field_values_list($filter, array('value'));
329
					if (!empty($values)) {
330
						$arr_str_values = array();
331
						foreach ($values as $value) {
332
							$arr_str_values[] = $value['value'];
333
						}
334
						$default_values = implode(', ', $arr_str_values);
335
					}
336
				}
337

    
338
				$sf_textbox = '
339
						<div class="control-group">
340
							<label class="control-label">%s</label>
341
							<div class="controls">
342
								<input name="%s" type="text" value="%s"/>
343
							</div>
344
						</div>';
345

    
346
				echo sprintf($sf_textbox, $specific_field['name'], $specific_field['code'], $default_values);
347
			}
348
		}
349
		//echo '<input type="hidden" name="origin"  value="' . Security::remove_XSS($_GET['origin']) . '" />';
350
		echo '<input type="hidden" name="lp_id"  value="' . Security::remove_XSS($_GET['lp_id']) . '" />';
351
		echo '<div class="control-group">
352
					<label class="control-label">
353
					</label>
354
					<div class="controls">
355
						<button class="btn save" type="submit" name="submitLink" value="OK">'.get_lang('SaveLink').'</button>
356
					</div>
357
				</div>';
358
		echo '</form>';
359
	} elseif(($_GET['action'] == 'addcategory' || $_GET['action'] == 'editcategory') && !$submit_category) {
360
		echo '<form class="form-horizontal" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&amp;urlview='.Security::remove_XSS($urlview).'">';
361

    
362
        if ($_GET['action'] == 'addcategory') {
363
			echo '<legend>'.get_lang('CategoryAdd').'</legend>';
364
			$my_cat_title = get_lang('CategoryAdd');
365
		} else {
366
			echo '<legend>'.get_lang('CategoryMod').'</legend>';
367
			$my_cat_title = get_lang('CategoryMod');
368
		}
369

    
370
		echo '<input type="hidden" name="sec_token" value="'.$token.'" />';
371
		if ($_GET['action'] == 'editcategory') {
372
			echo '<input type="hidden" name="id" value="'.$id.'" />';
373
		}
374
		echo '	<div class="control-group">
375
					<label class="control-label">
376
						<span class="form_required">*</span> '.get_lang('CategoryName').'
377
					</label>
378
					<div class="controls">
379
						<input type="text" name="category_title" size="50" value="'.Security::remove_XSS($category_title).'" />
380
					</div>
381
				</div>';
382

    
383
		echo '	<div class="control-group">
384
					<label class="control-label">
385
						'.get_lang('Description').'
386
					</label>
387
					<div class="controls">
388
						<textarea rows="3" cols="50" name="description">'.Security::remove_XSS($description).'</textarea>
389
					</div>
390
				</div>';
391

    
392
		echo '	<div class="control-group">
393
					<label class="control-label">
394
					</label>
395
					<div class="controls">
396
						<button class="btn save" type="submit" name="submitCategory">'.$my_cat_title.' </button>
397
					</div>
398
				</div>';
399
		echo "</form>";
400
	}
401
}
402

    
403
if (!empty($down)) {
404
	movecatlink($down);
405
}
406
if (!empty($up)) {
407
	movecatlink($up);
408
}
409

    
410
if (empty($_GET['action']) || ($_GET['action'] != 'editlink' && $_GET['action'] != 'addcategory' && $_GET['action'] != 'addlink') || $link_submitted || $category_submitted) {
411

    
412
	/*	Action Links */
413

    
414
	echo '<div class="actions">';
415
	if (api_is_allowed_to_edit(null, true)) {
416
		$urlview = Security::remove_XSS($urlview);
417
		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>';
418
		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>';
419

    
420
		   /* "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=importcsv&amp;urlview=".$urlview."\">".get_lang('CsvImport')."</a>\n", // RH*/
421
	}
422
	// Making the show none / show all links. Show none means urlview=0000 (number of zeros depending on the
423
	// number of categories). Show all means urlview=1111 (number of 1 depending on teh number of categories).
424
	$sqlcategories = "SELECT * FROM ".$tbl_categories." WHERE c_id = $course_id $condition_session ORDER BY display_order DESC";
425
	$resultcategories = Database::query($sqlcategories);
426
	$aantalcategories = Database::num_rows($resultcategories);
427
	if ($aantalcategories > 0) {
428
	    $resultcategories = Database::query($sqlcategories);
429
		echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview=';
430
		for ($j = 1; $j <= $aantalcategories; $j++) {
431
			echo '0';
432
		}
433
		echo '">'.Display::return_icon('view_remove.png', $shownone,'',ICON_SIZE_MEDIUM).'</a>';
434
		echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview=';
435
		for ($j = 1; $j <= $aantalcategories; $j++) {
436
			echo '1';
437
		}
438
		echo '">'.Display::return_icon('view_tree.png', $showall,'',ICON_SIZE_MEDIUM).'</a>';
439
	}
440
	echo '</div>';
441

    
442
	// Displaying the links which have no category (thus category = 0 or NULL), if none present this will not be displayed
443
	$sqlLinks = "SELECT * FROM ".$tbl_link." WHERE c_id = $course_id AND category_id=0 OR category_id IS NULL";
444
	$result = Database::query($sqlLinks);
445
	$numberofzerocategory = Database::num_rows($result);
446

    
447
	if ($numberofzerocategory !== 0) {
448
	    echo '<table class="data_table">';
449
		echo '<tr><th style="font-weight: bold; text-align:left;padding-left: 10px;">'.get_lang('General').'</th></tr>';
450
		echo '</table>';
451
		showlinksofcategory(0);
452
	}
453

    
454
	$i = 0;
455
	$catcounter = 1;
456
	$view = '0';
457

    
458
	while ($myrow = Database::fetch_array($resultcategories)) {
459
		// Validacion when belongs to a session
460
		$session_img = api_get_session_image($myrow['session_id'], $_user['status']);
461

    
462
		//if (!isset($urlview)) {
463
		if ($urlview == '') {
464
			// No $view set in the url, thus for each category link it should be all zeros except it's own
465
			makedefaultviewcode($i);
466
		} else {
467
			$view = $urlview;
468
			$view[$i] = '1';
469
		}
470
		// If the $urlview has a 1 for this categorie, this means it is expanded and should be desplayed as a
471
		// - instead of a +, the category is no longer clickable and all the links of this category are displayed
472
		$myrow['description'] = text_filter($myrow['description']);
473

    
474
		if (isset($urlview[$i]) && $urlview[$i] == '1') {
475
			$newurlview = $urlview;
476
			$newurlview[$i] = '0';
477

    
478
			echo '<tr>';
479
				echo '<table class="data_table">';
480
				echo '<tr>';
481
					echo '<th width="81%" style="font-weight: bold; text-align:left;padding-left: 5px;">';
482
					echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;urlview='.Security::remove_XSS($newurlview).'">';
483
					echo '<img src="../img/icons/22/view_remove.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).'</a><br />&nbsp;&nbsp;&nbsp;'.$myrow['description'];
484
					echo '</th>';
485
					if (api_is_allowed_to_edit(null, true)) {
486
						if ($session_id == $myrow['session_id']) {
487
							echo '<th>';
488
							showcategoryadmintools($myrow['id']);
489
							echo '</th>';
490
						} else {
491
							echo '<th>'.get_lang('EditionNotAvailableFromSession');
492
						}
493
					}
494
				echo '</tr>';
495
				echo '</table>';
496
				echo showlinksofcategory($myrow['id']);
497
			echo '</tr>';
498
		} else {
499
			echo '<tr>';
500
				echo '<table class="data_table">';
501
				echo '<tr>';
502
					echo '<th width="81%" style="font-weight: bold; text-align:left;padding-left: 5px;"><a href="'.api_get_self().'?'.api_get_cidreq().'&amp;urlview=';
503
					echo is_array($view) ? implode('', $view) : $view;
504
					echo '"><img src="../img/icons/22/view_tree.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).$session_img;
505
					echo'</a><br />&nbsp;&nbsp;&nbsp;';
506
					echo $myrow['description'];
507
					if (api_is_allowed_to_edit(null, true)) {
508
						echo '<th style="text-align:center;">';
509
						showcategoryadmintools($myrow['id']);
510
						echo '</th>';
511
					}
512
					echo '</th>';
513
				echo '</tr>';
514

    
515
				echo '</table>';
516
			echo '</tr>';
517
		}
518
		// Displaying the link of the category
519
		$i++;
520
	}
521
	echo '</table>';
522
}
523

    
524
Display::display_footer();
    (1-1/1)