mdApiTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * mdApiTest.php
  4. * 2004/09/30
  5. * @copyright 2004 rene.haentjens@UGent.be - see metadata/md_funcs.php
  6. * @package chamilo.metadata
  7. */
  8. /**
  9. * Chamilo Metadata: MD API test and demo
  10. * The API allows other Dokeos scripts to define & manipulate metadata
  11. * In this example, MD is defined for 'Document.1001', 1002, 1003
  12. */
  13. require("../md_funcs.php");
  14. define('EID_TYPE', 'Document');
  15. require('../md_' . strtolower(EID_TYPE) . '.php');
  16. // name of the language file that needs to be included
  17. /*
  18. $language_file = 'Whatever';
  19. */
  20. require("../../inc/global.inc.php");
  21. isset($_course) or give_up("Select a course first...");
  22. $is_allowed_to_edit = isset($_user['user_id']) && $is_courseMember && is_allowed_to_edit();
  23. if (!$is_allowed_to_edit) give_up("You're not allowed to edit...");
  24. $mdStore = new mdstore($is_allowed_to_edit); // create table if needed
  25. require(api_get_path(LIBRARY_PATH) . 'xmd.lib.php'); // mds_update_xml_and_mdt
  26. require(api_get_path(LIBRARY_PATH) . 'xht.lib.php'); // mdo_generate_default_xml_metadata
  27. $noPHP_SELF = TRUE;
  28. Display::display_header($nameTools); echo "\n";
  29. // if the language file in use is not 'md_' . EID_TYPE ...
  30. $langMdTitle = 'Default Title (if doc not in DB)';
  31. $langMdDescription = 'Default description (if doc has no comment)';
  32. $langMdCoverage = 'bachelor of engineering';
  33. $langMdCopyright = 'Ghent University';
  34. foreach(array(1001, 1002, 1003) as $eid_id)
  35. {
  36. $mdObj = new mdobject($_course, $eid_id); // see 'md_' . EID_TYPE . '.php'
  37. $eid = $mdObj->mdo_eid;
  38. $titlePath = $mdObj->mdo_dcmap_v['Title']; // no IEEE dependencies here...
  39. if (($mdt_rec = $mdStore->mds_get($eid)) === FALSE)
  40. {
  41. $mdt = $mdObj->mdo_generate_default_xml_metadata();
  42. $xmlDoc = new xmddoc(explode("\n", $mdt));
  43. if (!$xmlDoc->error)
  44. {
  45. echo htmlspecialchars($titlePath), ': ';
  46. $mdTitle = $xmlDoc->xmd_value($titlePath);
  47. if ($mdTitle == $langMdTitle)
  48. {
  49. $mdTitle = EID_TYPE . ' ' . $eid_id;
  50. $xmlDoc->xmd_update($titlePath, $mdTitle);
  51. $mdt = $xmlDoc->xmd_xml();
  52. }
  53. echo htmlspecialchars($mdTitle), ':';
  54. }
  55. $mdStore->mds_put($eid, $mdt, 'mdxmltext', FALSE);
  56. echo '<a href="../index.php?eid=', urlencode($eid), '">',
  57. htmlspecialchars($eid), '</a><br>';
  58. }
  59. }
  60. echo '<br>';
  61. $xmlDoc = new xmddoc(explode("\n", $mdStore->mds_get($eid = EID_TYPE . '.1002')));
  62. if ($xmlDoc->error) give_up($xmlDoc->error);
  63. $mdObj = new mdobject($_course, '1002');
  64. $mda = "~~"; // delete metadata of 'Document.1002'
  65. $mdt = $mdStore->mds_update_xml_and_mdt($mdObj, $xmlDoc, $mda, $eid, $trace);
  66. // note: $xmlDoc and $trace are passed by reference...
  67. $mdObj = new mdobject($_course, '1003');
  68. $xmlDoc = new xmddoc(explode("\n", $mdStore->mds_get($eid = EID_TYPE . '.1003')));
  69. if ($xmlDoc->error) give_up($xmlDoc->error);
  70. $map_lang = 'string/@language';
  71. $dcmap_e_kwplace = 'metadata/lom/general'; $dcmap_e_kwelem = 'keyword';
  72. $dcmap_e_keyword = $dcmap_e_kwplace . '/' . $dcmap_e_kwelem;
  73. $mda = $mdObj->mdo_dcmap_v['Description'] . '=Nouvelle description' .
  74. "\n" . $mdObj->mdo_dcmap_e['Coverage'] . "~" .
  75. "\n" . $dcmap_e_kwplace . '!' . $dcmap_e_kwelem .
  76. "\n" . $dcmap_e_keyword . "[-1]!string=afrique" .
  77. "\n" . $dcmap_e_keyword . "[-1]/" . $map_lang . "=en" .
  78. "\n" . $mdObj->mdo_dcmap_e['Title'] . ',' .
  79. $mdObj->mdo_dcmap_e['Description'] . ',' .
  80. $dcmap_e_keyword . ";" . $map_lang . "=fr" .
  81. ""; // update metadata of 'Document.1003' - see md_funcs
  82. // note we don't go far with IEEE independence...
  83. $mdt = $mdStore->mds_update_xml_and_mdt($mdObj, $xmlDoc, $mda, $eid, $trace);
  84. echo htmlspecialchars($trace), '<br><br>';
  85. // The simplest API calls: store and fetch DC metadata element values:
  86. $mdObj = new mdobject($_course, '1003');
  87. $mdStore->mds_put_dc_elements($mdObj, array('Coverage' => 'broad...', 'Type' => 'aggressive text'));
  88. // Coverage won't work, because that element has been removed above...
  89. $dcelem = $mdStore->mds_get_dc_elements($mdObj);
  90. foreach (array('Identifier', 'Title', 'Language', 'Description', 'Coverage',
  91. 'Type', 'Date', 'Creator', 'Format', 'Rights') as $dce)
  92. {
  93. echo $dce, '= ', htmlspecialchars($dcelem[$dce]), '<br>';
  94. }
  95. echo '<br>';
  96. $mdObj = new mdobject($_course, '1002');
  97. $mdStore->mds_put_dc_elements($mdObj, array('Coverage' => 'broad...'));
  98. $dcelem = $mdStore->mds_get_dc_elements($mdObj);
  99. foreach (array('Identifier', 'Title', 'Language', 'Description', 'Coverage',
  100. 'Type', 'Date', 'Creator', 'Format', 'Rights') as $dce)
  101. {
  102. echo $dce, '= ', htmlspecialchars($dcelem[$dce]), '<br>';
  103. }
  104. echo '<br>';
  105. $mdStore->mds_append(EID_TYPE . '.1001', ' search words');
  106. $mdStore->mds_append(EID_TYPE . '.1001', ' more findable terms');
  107. Display::display_footer();
  108. ?>