importlinks.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * Chamilo metadata/importlinks.php
  4. * 2006/12/15
  5. * Copyright (C) 2006 rene.haentjens@UGent.be - see metadata/md_funcs.php
  6. * @package chamilo.metadata
  7. */
  8. /**
  9. * Chamilo Metadata: create table entries for a category of Link-type items
  10. */
  11. // PRELIMS -------------------------------------------------------------------->
  12. $getpostvars = array('lcn', 'slo'); require('md_funcs.php');
  13. define('EID_TYPE', 'Link');
  14. define('OF_EID_TYPE', "eid LIKE '" . EID_TYPE . ".%'");
  15. require('md_' . strtolower(EID_TYPE) . '.php');
  16. // name of the language file that needs to be included
  17. $language_file = 'md_' . strtolower(EID_TYPE);
  18. include('../inc/global.inc.php');
  19. $nameTools = get_lang('Tool');
  20. ($nameTools && get_lang('Sorry')) or give_up(
  21. 'Language file ' . $language_file . " doesn't define 'Tool' and 'Sorry'");
  22. $_course = api_get_course_info(); isset($_course) or give_up(get_lang('Sorry'));
  23. $is_allowed_to_edit = isset($_user['user_id']) && $is_courseMember && api_is_allowed_to_edit();
  24. if (!$is_allowed_to_edit) give_up(get_lang('Denied'));
  25. $mdStore = new mdstore($is_allowed_to_edit); // create table if needed
  26. require(api_get_path(LIBRARY_PATH) . 'xmd.lib.php');
  27. require(api_get_path(LIBRARY_PATH) . 'xht.lib.php');
  28. require('md_phpdig.php');
  29. $mdObj = new mdobject($_course, 0);
  30. $mdCat = $mdObj->mdo_dcmap_v['Coverage'];
  31. $mdUrl = 'metadata/lom/technical/location[1]';
  32. $mdObj->mdo_add_breadcrump_nav(); // see 'md_' . EID_TYPE . '.php'
  33. $htmldecode = array_flip(get_html_translation_table(HTML_SPECIALCHARS));
  34. function check_andor_get($row, $get = '', $check = '', $tobe = '')
  35. {
  36. global $mdCat, $htmldecode;
  37. if (!$check && !$get) return FALSE;
  38. $regs = array(); // for use with ereg()
  39. if ($get == $mdCat && !$check) // cheat to be quicker
  40. if (ereg('<coverage>[^<]*<string language="..">([^<]+)<\/string>',
  41. $row['mdxmltext'], $regs)) return strtr($regs[1], $htmldecode);
  42. if ($check == $mdCat && !$get) // cheat to be quicker
  43. if (ereg('<coverage>[^<]*<string language="..">([^<]+)<\/string>',
  44. $row['mdxmltext'], $regs))
  45. return (strtr($regs[1], $htmldecode) == $tobe);
  46. $xmlDoc = new xmddoc(explode("\n", $row['mdxmltext']));
  47. if ($xmlDoc->error) return FALSE;
  48. if (!$check) return $xmlDoc->xmd_value($get);
  49. if ($xmlDoc->xmd_value($check) == $tobe)
  50. return $get ? $xmlDoc->xmd_value($get) : TRUE;
  51. return FALSE;
  52. }
  53. function get_cat($catname)
  54. {
  55. global $_course; $cateq = "category_title='". addslashes($catname) . "'";
  56. $linkcat_table = Database::get_course_table(TABLE_LINK_CATEGORY);
  57. $result = Database::query("SELECT id FROM $linkcat_table WHERE " . $cateq);
  58. if (Database::num_rows($result) >= 1 && ($row = Database::fetch_array($result)))
  59. return $row['id']; // several categories with same name: take first
  60. return FALSE;
  61. }
  62. // SET CURRENT LINKS CATEGORY - HEADER ---------------------------------------->
  63. unset($lci); // category-id
  64. if (isset($lcn)) // category_title
  65. {
  66. $lcn = substr(ereg_replace("[^\x20-\x7E\xA1-\xFF]", "", $lcn), 0, 255);
  67. $uceids = array(); $mceids = array();
  68. $result = $mdStore->mds_get_many('eid,mdxmltext', OF_EID_TYPE);
  69. while ($row = Database::fetch_array($result))
  70. if (check_andor_get($row, '', $mdCat, $lcn)) $uceids[] = $row['eid'];
  71. if (($lci = get_cat($lcn)) !== FALSE)
  72. {
  73. $link_table = Database::get_course_table(TABLE_LINK);
  74. $result = Database::query("SELECT id FROM $link_table WHERE category_id=" . $lci);
  75. while ($row = Database::fetch_array($result))
  76. {
  77. $lceids[$id = (int) $row['id']] = ($eid = EID_TYPE . '.' . $id);
  78. if (in_array($eid, $uceids)) $mceids[] = $eid;
  79. }
  80. $hdrInfo = ' ' . get_lang('WorkOn') . ' ' . htmlspecialchars($lcn, ENT_QUOTES, $charset) .
  81. ', LC-id=&nbsp;' . htmlspecialchars($lci, ENT_QUOTES, $charset);
  82. }
  83. elseif ($lcn)
  84. {
  85. $hdrInfo = ' (' . htmlspecialchars($lcn, ENT_QUOTES, $charset) .
  86. ': ' . get_lang('NotInDB') . ')';
  87. }
  88. else
  89. unset($lcn);
  90. $uceids = array_diff($uceids, $mceids); // old entries with no link
  91. if (count($lceids) && count($uceids))
  92. {
  93. $mdStore->mds_delete_many($uceids); $ufos = Database::affected_rows();
  94. }
  95. $interbreadcrumb[]= array(
  96. 'url' => api_get_self() . '?lcn=' . urlencode($lcn),
  97. 'name'=> get_lang('Continue') . ' ' . htmlspecialchars($lcn, ENT_QUOTES, $charset));
  98. }
  99. $htmlHeadXtra[] = '
  100. <link rel="stylesheet" type="text/css" href="md_styles.css">
  101. <script type="text/javascript" src="md_script.js"></script>
  102. ';
  103. Display::display_header($nameTools);
  104. // OPERATIONS ----------------------------------------------------------------->
  105. if ($ufos) echo '<h3>', $ufos, ' ', get_lang('RemainingFor'), ' ',
  106. htmlspecialchars($lcn, ENT_QUOTES, $charset), '</h3>', "\n";
  107. if (isset($slo)) echo '<h3>', $slo, '</h3>', "\n"; // selected links op
  108. if (isset($slo))
  109. if ($slo == get_lang('Create') && count($lceids))
  110. {
  111. foreach ($lceids as $id => $eid)
  112. {
  113. $mdObj = new mdobject($_course, $id); $xht = $mdObj->mdo_define_htt();
  114. $mdStore->mds_put($eid, $mdt = $mdObj->mdo_generate_default_xml_metadata(),
  115. 'mdxmltext', '?');
  116. $xht->xht_xmldoc = new xmddoc(explode("\n", $mdt));
  117. $mdStore->mds_put($eid, $xht->xht_fill_template('INDEXABLETEXT'),
  118. 'indexabletext');
  119. echo '<span class="lbs" onClick="', "javascript: makeWindow('index.php?eid=",
  120. urlencode($eid), "', '', '')\">", htmlspecialchars($eid, ENT_QUOTES, $charset), '</span> ';
  121. }
  122. echo '<br>';
  123. }
  124. elseif ($slo == get_lang('Remove') && count($lceids))
  125. {
  126. $mdStore->mds_delete_many($mceids); $aff = Database::affected_rows();
  127. echo $aff, ' MDEs/ ', count($lceids), ' ', get_lang('MdCallingTool'),
  128. '<br><br><b>', get_lang('AllRemovedFor'),
  129. ' ', htmlspecialchars($lcn, ENT_QUOTES, $charset), '</b><br />';
  130. }
  131. elseif ($slo == get_lang('Remove') && count($mceids)) // obsolete category
  132. {
  133. $mdStore->mds_delete_many($mceids);
  134. echo get_lang('AllRemovedFor'), ' ', htmlspecialchars($lcn, ENT_QUOTES, $charset), '<br />';
  135. }
  136. elseif ($slo == get_lang('Index') && file_exists($phpDigIncCn) && count($mceids))
  137. {
  138. $result = $mdStore->mds_get_many('eid,mdxmltext,indexabletext',
  139. OF_EID_TYPE . " AND eid IN ('" .
  140. implode("','", array_map('addslashes', $mceids)) . "')");
  141. while ($row = Database::fetch_array($result)) // load indexabletexts in memory
  142. $idt[check_andor_get($row, $mdUrl)] = $row['indexabletext'];
  143. require($phpDigIncCn); // switch to PhpDig DB
  144. foreach ($idt as $url => $text)
  145. {
  146. $pu = parse_url($url);
  147. if (!isset($pu['scheme'])) $pu['scheme'] = "http";
  148. if (isset($pu['host']))
  149. {
  150. $url = $pu['scheme'] . "://" . $pu['host'] . "/"; $file = '';
  151. if (($path = $pu['path']))
  152. if (substr($path, -1) == '/') $path = substr($path, 1);
  153. else
  154. {
  155. $pi = pathinfo($path); $path = $pi['dirname'];
  156. if ($path{0} == '\\') $path = substr($path, 1);
  157. if ($path{0} == '/') $path = substr($path, 1) . '/';
  158. $file = $pi['basename'];
  159. }
  160. $file .= ($pu['query'] ? '?'.$pu['query'] : '') .
  161. ($pu['fragment'] ? '#'.$pu['fragment'] : '');
  162. if ($site_id = remove_engine_entries($url, $path, $file))
  163. {
  164. echo '<table>', "\n";
  165. index_words($site_id, $path, $file,
  166. get_first_words($text, $url . $path, $file),
  167. get_keywords($text));
  168. echo '</table>', "\n";
  169. }
  170. else
  171. {
  172. echo '<table>', "\n";
  173. echo '<tr><td>', htmlspecialchars($url, ENT_QUOTES, $charset),
  174. '</td><td>', htmlspecialchars($path, ENT_QUOTES, $charset),
  175. '</td><td>', htmlspecialchars($file, ENT_QUOTES, $charset), '</td></tr>';
  176. echo '</table>', "\n";
  177. }
  178. }
  179. elseif (isset($pu['scheme']) && $pu['scheme'] == 'mailto' && isset($pu['path']))
  180. {
  181. if ($site_id = remove_engine_entries($url = 'mailto:' . $pu['path'], ''))
  182. {
  183. echo '<table>', "\n";
  184. index_words($site_id, '', '',
  185. get_first_words($text, $url, ''),
  186. get_keywords($text));
  187. echo '</table>', "\n";
  188. }
  189. else
  190. {
  191. echo '<table>', "\n";
  192. echo '<tr><td>', htmlspecialchars($url, ENT_QUOTES, $charset),
  193. '</td><td>', htmlspecialchars($path, ENT_QUOTES, $charset),
  194. '</td><td>', htmlspecialchars($file, ENT_QUOTES, $charset), '</td></tr>';
  195. echo '</table>', "\n";
  196. }
  197. }
  198. }
  199. if(isset($db))
  200. {
  201. //mysql_select_db($_configuration['main_database'], $db);
  202. Database::select_db($_configuration['main_database'], $db);
  203. }
  204. }
  205. elseif ($slo == get_lang('Index'))
  206. {
  207. echo 'Problem! PhpDig connect.php has gone ...';
  208. }
  209. // STATISTICS ----------------------------------------------------------------->
  210. echo '<h3>', get_lang('Statistics'), '</h3>', "\n";
  211. $result = $mdStore->mds_get_many('eid,mdxmltext', OF_EID_TYPE);
  212. echo get_lang('TotalMDEs'), Database::num_rows($result), "\n";
  213. while ($row = Database::fetch_array($result))
  214. {
  215. $cat = check_andor_get($row, $mdCat);
  216. $perCat[$cat] = ($pc = $perCat[$cat]) ? $pc + 1 : 1;
  217. }
  218. if (count($perCat))
  219. {
  220. echo '<table>', "\n";
  221. foreach ($perCat as $cat => $number)
  222. {
  223. echo '<tr><td>', $cat == $lcn ? '' : '(', htmlspecialchars($cat, ENT_QUOTES, $charset),
  224. $cat == $lcn ? '' : ')', ':</td><td align="right">',
  225. $number, '</td></tr>', "\n";
  226. }
  227. echo '</table>', "\n";
  228. }
  229. if (isset($lci))
  230. {
  231. echo '<br><br>', htmlspecialchars($lcn, ENT_QUOTES, $charset), ' ', get_lang('MdCallingTool'),
  232. ': ', count($lceids), '<br />', "\n";
  233. }
  234. // SELECT & FOOTER ------------------------------------------------------------>
  235. echo '<h3>', $nameTools, $hdrInfo, '</h3>', "\n";
  236. echo '<form action="' .api_get_self(). '?lcn=' . urlencode($lcn) .
  237. '" method="post">', "\n";
  238. if (count($lceids)) echo
  239. '<input type="submit" name="slo" value="', get_lang('Create'), '">', "\n";
  240. if ($perCat[$lcn]) echo
  241. '<input type="submit" name="slo" value="', get_lang('Remove'), '">', "\n";
  242. if ($perCat[$lcn] && file_exists($phpDigIncCn)) echo
  243. '<input type="submit" name="slo" value="', get_lang('Index'), '">', "\n";
  244. echo '</form>', "\n";
  245. if (count($perCat)) foreach ($perCat as $cat => $number)
  246. $perCat[$cat] = '(' . htmlspecialchars($cat, ENT_QUOTES, $charset) . ')';
  247. $linkcat_table = Database::get_course_table(TABLE_LINK_CATEGORY);
  248. $result = Database::query("SELECT category_title FROM $linkcat_table");
  249. while ($row = Database::fetch_array($result))
  250. {
  251. $cat = $row['category_title']; $hcat = htmlspecialchars($cat, ENT_QUOTES, $charset);
  252. if ($perCat[$cat] == $hcat) $dups[] = $cat;
  253. else $perCat[$cat] = $hcat;
  254. }
  255. if (count($dups))
  256. {
  257. $warning = get_lang('WarningDups');;
  258. foreach ($dups as $cat) unset($perCat[$cat]);
  259. }
  260. echo '<h3>', get_lang('OrElse'), $warning, '</h3>', "\n", // select new target
  261. '<table><tr><td align="right" class="alternativeBgDark">', "\n",
  262. '<form action="'.api_get_self().'" method="post">', "\n",
  263. get_lang('SLC'), ' :', "\n", '<select name="lcn">', "\n",
  264. '<option value=""></option>', "\n";
  265. foreach ($perCat as $cat => $text) echo '<option value="' .
  266. htmlspecialchars($cat, ENT_QUOTES, $charset) . '"' .
  267. ($cat == $lcn ? ' selected' : '') . '>' . $text . '</option>', "\n";
  268. echo '</select><input type="submit" value="', ' '.get_lang('Ok').' ', '">', "\n",
  269. '</form>', "\n", '</td></tr></table>', "\n";
  270. Display::display_footer();
  271. ?>