md_link.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * md_link.php for Chamilo metadata/*.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: class mdobject for Link-type objects
  10. * @package chamilo.metadata
  11. */
  12. class mdobject {
  13. var $mdo_course;
  14. var $mdo_type;
  15. var $mdo_id;
  16. var $mdo_eid;
  17. var $mdo_dcmap_e;
  18. var $mdo_dcmap_v;
  19. var $mdo_url;
  20. var $mdo_title;
  21. var $mdo_description;
  22. var $mdo_category;
  23. var $mdo_category_title;
  24. function mdo_define_htt() { return new xhtdoc(<<<EOD
  25. <!-- {-INDEXABLETEXT-} -->
  26. Title: {-V metadata/lom/general/title/string-} txt-sep
  27. Keyword(s): {-R metadata/lom/general/keyword C KWTEXT-} txt-sep
  28. Category: {-V metadata/lom/general/coverage/string-} txt-sep
  29. {-V metadata/lom/general/description[1]/string-} txt-end
  30. link-type
  31. <!-- {-KWTEXT-} -->
  32. {-V string-}-kw
  33. <!-- {--} -->
  34. EOD
  35. );
  36. }
  37. function mdo_generate_default_xml_metadata()
  38. {
  39. global $iso639_2_code, $ieee_xml;
  40. $xhtDoc = new xhtdoc($ieee_xml); $_user = api_get_user_info();
  41. if ($xhtDoc->htt_error)
  42. give_up('IEEE XML (metadata/md_funcs): ' . $xhtDoc->htt_error);
  43. $xhtDoc->xht_get_lang = 'get_lang'; $xhtDoc->xht_xmldoc = new xmddoc('');
  44. if ($xhtDoc->xht_xmldoc->error) give_up($xhtDoc->xht_xmldoc->error);
  45. $xhtDoc->xht_param['siteUri'] = make_uri();
  46. $xhtDoc->xht_param['entry'] = $this->mdo_course['sysCode'] .
  47. '.Link.' . $this->mdo_id; // 2005-05-30: path->sysCode
  48. $xhtDoc->xht_param['location'] = $this->mdo_url . '';
  49. $xhtDoc->xht_param['mdlang'] = strtolower($iso639_2_code);
  50. $xhtDoc->xht_param['lang'] = strtolower($iso639_2_code);
  51. $xhtDoc->xht_param['title'] =
  52. $this->mdo_title ? $this->mdo_title : get_lang('MdTitle');
  53. if (($d = $this->mdo_description))
  54. {
  55. if ($keywords = $this->_find_keywords($d)) $d = array_pop($keywords);
  56. $xhtDoc->xht_param['description'] = $d;
  57. }
  58. else
  59. $xhtDoc->xht_param['description'] = get_lang('MdDescription');
  60. $xhtDoc->xht_param['coverage'] = $this->mdo_category_title ?
  61. $this->mdo_category_title : get_lang('MdCoverage');
  62. if (isset($_user))
  63. {
  64. $xhtDoc->xht_param['author'] = "BEGIN:VCARD\\nFN:" .
  65. api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS) .
  66. "\\nEMAIL:".$_user['mail'] . "\\nEND:VCARD\\n";
  67. }
  68. $xhtDoc->xht_param['dateTime'] = date('Y-m-d');
  69. $xhtDoc->xht_param['format'] = ''; $xhtDoc->xht_param['size'] = '0';
  70. if (count($keywords))
  71. {
  72. $xd = new xmddoc(explode("\n",
  73. $mdt = $xhtDoc->xht_fill_template('XML')));
  74. if ($xd->error) return $mdt; // and worry later
  75. $this->_add_keywords($xd, $keywords);
  76. return $xd->xmd_xml();
  77. }
  78. return $xhtDoc->xht_fill_template('XML');
  79. }
  80. function mdo_override(&$xmlDoc) // by ref!
  81. {
  82. if ($this->mdo_url)
  83. {
  84. $xmlDoc->xmd_update('metadata/lom/technical/location', $this->mdo_url);
  85. $ge = $xmlDoc->xmd_select_single_element("metadata/lom/general");
  86. $xmlDoc->xmd_update('title[1]/string', $this->mdo_title, $ge);
  87. $xmlDoc->xmd_update('coverage[1]/string', $this->mdo_category_title, $ge);
  88. if (($d = $this->mdo_description))
  89. if ($keywords = $this->_find_keywords($d)) $d = array_pop($keywords);
  90. $xmlDoc->xmd_update('description[1]/string', $d, $ge);
  91. $xmlDoc->xmd_remove_nodes($xmlDoc->xmd_select_elements('keyword', $ge), $ge);
  92. if (count($keywords)) $this->_add_keywords($xmlDoc, $keywords);
  93. }
  94. return $xmlDoc->xmd_xml();
  95. }
  96. function mdo_storeback(&$xmlDoc) // by ref!
  97. {
  98. $course_id = api_get_course_int_id();
  99. if (!$this->mdo_url) return; // no record in link table, most probably
  100. if (!($v = $xmlDoc->xmd_value('metadata/lom/technical/location'))) return;
  101. if ($v != $this->mdo_url)
  102. { $this->mdo_url = $v; $u .= ", url = '" . addslashes($v) . "'"; }
  103. $ge = $xmlDoc->xmd_select_single_element("metadata/lom/general");
  104. $v = $xmlDoc->xmd_value('title[1]/string', $ge);
  105. if ($v != $this->mdo_title)
  106. { $this->mdo_title = $v; $u .= ", title = '" . addslashes($v) . "'"; }
  107. $vd = $xmlDoc->xmd_value('description[1]/string', $ge);
  108. $vk = $xmlDoc->xmd_value('keyword/string', $ge, array('in' => ', '));
  109. $v = $vk ? '<i kw="' . htmlspecialchars($vk) . '">' .
  110. ereg_replace('\[((/?(b|big|i|small|sub|sup|u))|br/)\]', '<\\1>',
  111. htmlspecialchars($vd)) . '</i>' : $vd;
  112. if ($v != $this->mdo_description)
  113. {
  114. $this->mdo_description = $v;
  115. $u .= ", description = '" . addslashes($v) . "'";
  116. }
  117. // do not store back a modified coverage as category...
  118. $link_table = Database::get_course_table(TABLE_LINK);
  119. if ($u) Database::query("UPDATE $link_table SET " . substr($u, 2) .
  120. " WHERE c_id = $course_id AND id='" . addslashes($this->mdo_id) . "'");
  121. }
  122. function mdo_add_breadcrump_nav()
  123. {
  124. global $interbreadcrumb;
  125. $regs = array(); // for use with ereg()
  126. $docurl = api_get_self(); // should be .../main/xxx/yyy.php
  127. if (ereg('^(.+[^/\.]+)/[^/\.]+/[^/\.]+.[^/\.]+$', $docurl, $regs))
  128. $docurl = $regs[1] . '/link/link.php';
  129. $interbreadcrumb[]= array ('url' => $docurl,
  130. "name"=> get_lang('MdCallingTool'));
  131. }
  132. function mdobject($_course, $id)
  133. {
  134. global $ieee_dcmap_e, $ieee_dcmap_v; // md_funcs
  135. $course_id = api_get_course_int_id();
  136. $this->mdo_course = $_course; $this->mdo_type = 'Link';
  137. $this->mdo_id = $id; $this->mdo_eid = $this->mdo_type . '.' . $id;
  138. $this->mdo_dcmap_e = $ieee_dcmap_e; $this->mdo_dcmap_v = $ieee_dcmap_v;
  139. $link_table = Database::get_course_table(TABLE_LINK);
  140. if (($linkinfo = @Database::fetch_array(Database::query(
  141. "SELECT url,title,description,category_id FROM $link_table WHERE c_id = $course_id AND id='" .intval($id) . "'"))))
  142. {
  143. $this->mdo_url = $linkinfo['url'];
  144. $this->mdo_title = $linkinfo['title'];
  145. $this->mdo_description = $linkinfo['description'];
  146. $this->mdo_category = ($lci = $linkinfo['category_id']);
  147. $linkcat_table = Database::get_course_table(TABLE_LINK_CATEGORY);
  148. if (($catinfo = @Database::fetch_array(Database::query(
  149. "SELECT category_title FROM $linkcat_table WHERE c_id = $course_id AND id='" .
  150. addslashes($lci) . "'"))))
  151. $this->mdo_category_title = $catinfo['category_title'];
  152. }
  153. }
  154. function _find_keywords($d)
  155. {
  156. $dd = new xmddoc($d); if ($dd->error) return NULL;
  157. $regs = array(); // for use with ereg()
  158. foreach ($dd->attributes[0] as $name => $value)
  159. if ($name == 'kw' && ereg('^<?([^>]+)>?$', $value, $regs))
  160. {
  161. $kwa = array_map('trim', explode(',', $regs[1]));
  162. if (ereg('^<' . ($tag = $dd->name[0]) . '[^>]*>(.*)</'.$tag.'>$',
  163. $d, $regs)) // e.g. <i kw="...">A &amp; <b>B</b>!</i>
  164. {
  165. $htdc = array_flip(get_html_translation_table(HTML_ENTITIES));
  166. $d = strtr(ereg_replace( // first <b> -> [b] etc.
  167. '<((/?(b|big|i|small|sub|sup|u))|br/)>', '[\\1]',
  168. ($regs[1])), $htdc); // then &amp; -> & etc.
  169. $d = strtr(str_replace("\r\n", " ", $d), "\r\n", " ");
  170. }
  171. else $d = $dd->xmd_text();
  172. array_push($kwa, $d); return $kwa;
  173. }
  174. return NULL;
  175. }
  176. function _add_keywords(&$xmlDoc, $keywords) // by ref!
  177. {
  178. $ge = $xmlDoc->xmd_select_single_element("metadata/lom/general");
  179. $dl = array("language" =>
  180. $xmlDoc->xmd_value("description/string/@language", $ge));
  181. foreach ($keywords as $kw)
  182. $xmlDoc->xmd_add_text_element("string", $kw,
  183. $xmlDoc->xmd_add_element("keyword", $ge), $dl);
  184. }
  185. }
  186. ?>