<?php /*                                 <!-- Dokeos metadata/importdocs.php -->
                                                             <!-- 2005/05/31 -->

<!-- Copyright (C) 2005 rene.haentjens@UGent.be -  see metadata/md_funcs.php -->

*/

/**
============================================================================== 
*	Dokeos Metadata: index all course documents with PhpDig
*
*	@package dokeos.metadata
============================================================================== 
*/


// PRELIMS -------------------------------------------------------------------->

$getpostvars = array('dmo'); require('md_funcs.php');

define('EID_TYPE', 'Document'); define('AFTER_DOT', strlen(EID_TYPE) + 1);
define('OF_EID_TYPE', "`eid` LIKE '" . EID_TYPE . ".%'");

require('md_' . strtolower(EID_TYPE) . '.php');

$langFile = 'md_' . strtolower(EID_TYPE);
include('../inc/claro_init_global.inc.php');
$nameTools = get_lang('Tool');

($nameTools && get_lang('Sorry')) or give_up( 
    'Language file ' . $langFile . " doesn't define 'Tool' and 'Sorry'");

$_course = api_get_course_info(); isset($_course) or give_up(get_lang('Sorry'));

$is_allowed_to_edit = isset($_uid) && $is_courseMember && is_allowed_to_edit();
if (!$is_allowed_to_edit) give_up(get_lang('Denied'));

$mdObj = new mdobject($_course, 0);
$mdStore = new mdstore($is_allowed_to_edit);  // create table if needed

require(api_get_library_path() . '/xmd.lib.php');
require(api_get_library_path() . '/xht.lib.php');

require('md_phpdig.php');

$mdObj->mdo_add_breadcrump_nav();  // see 'md_' . EID_TYPE . '.php'

$htmlHeadXtra[] = '
<link rel="stylesheet" type="text/css" href="md_styles.css">
<script language="JavaScript" src="md_script.js"></script>
';
Display::display_header($nameTools);


if (isset($dmo))  // for future use
{
    echo '<h3>', $dmo, '</h3>', "\n";  // document metadata op

    // if ($dmo == get_lang('Index')) $dmo = $dmo;
}

$result = $mdStore->mds_get_many('eid,indexabletext', OF_EID_TYPE);
echo get_lang('Tool'), ': ', mysql_num_rows($result), "<br><br>\n";

$idt = array(); $cidpar = '?cidReq=' . $_course['sysCode'];

while ($row = mysql_fetch_array($result))  // load indexabletexts in memory
{
    $mdObj = new mdobject($_course, substr($row['eid'], AFTER_DOT));
    $idt[$mdObj->mdo_url . $cidpar] = $row['indexabletext'];
}

if (count($idt) && file_exists($phpDigIncCn))
{
    require($phpDigIncCn);  // switch to PhpDig DB
    
    foreach ($idt as $url => $text)
    if (ereg('^http://([^/]+)/(.+)/([^/]+)\?cidReq=(.+)$', $url, $regs))
    {
        $path = $regs[2] .'/'; $file = $regs[3] . '?cidReq=' . $regs[4];
        if ($site_id = remove_engine_entries('http://' . $regs[1] .'/', 
                $path, $file))
        {
            echo '<table>', "\n";
            index_words($site_id, $path, $file, 
                get_first_words($text, $path, $file), 
                get_keywords($text));
            echo '</table>', "\n";
        }
    }
    
    if(isset($db)) mysql_select_db($mainDbName, $db);  // back to Dokeos
}
else echo 'No documents with metadata or no PhpDig in this course...<br>';

if (false && file_exists($phpDigIncCn))  // future: buttons for operations
{
    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">', "\n", 
        '<input type="submit" name="dmo" value="', get_lang('Import', 'noDLTT'), '">', "\n", 
        '<input type="submit" name="dmo" value="', get_lang('Remove', 'noDLTT'), '">', "\n", 
        '</form>', "\n";
}

Display::display_footer();
?>
