<commit_before><?php  // $Id$
/// This page prints a particular instance of glossary
    require_once("../../config.php");
    require_once("lib.php");
    require_once("$CFG->dirroot/rss/rsslib.php");

    global $CFG, $THEME, $USER;
    $debug = 0;
    $CFG->startpagetime = microtime();            
    
    optional_variable($id);           // Course Module ID
    optional_variable($g);            // Glossary ID

    optional_variable($tab,GLOSSARY_NO_VIEW); // browsing entries by categories?

    optional_variable($mode,"");  // [ "term"   | "entry"  | "cat"     | "date" | 
                                        //   "letter" | "search" | "author"  | "approval" ]
    optional_variable($hook,"");  // the term, entry, cat, etc... to look for based on mode

    optional_variable($fullsearch,0); // full search (concept and definition) when searching?

    optional_variable($sortkey,"");    // Sorted view: 
                                       //    [ CREATION | UPDATE | FIRSTNAME | LASTNAME |
                                       //      concept | timecreated | ... ]
    optional_variable($sortorder,"");  // it defines the order of the sorting (ASC or DESC)

    optional_variable($offset,0);      // entries to bypass (for paging purpouses)

    optional_variable($show,"");       // [ concept | alias ] => mode=term hook=$show
    optional_variable($displayformat,-1);  // override of the glossary display format

    if (!empty($id)) {
        if (! $cm = get_record("course_modules", "id", $id)) {
            error("Course Module ID was incorrect");
        }     
        if (! $course = get_record("course", "id", $cm->course)) {
            error("Course is misconfigured");
        }     
        if (! $glossary = get_record("glossary", "id", $cm->instance)) {
            error("Course module is incorrect");
        } 
    } else if (!empty($g)) {
        if (! $glossary = get_record("glossary", "id", $g)) {
            error("Course module is incorrect");
        } 
        if (! $course = get_record("course", "id", $glossary->course)) {
            error("Could not determine which course this belonged to!");
        }     
        if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
            error("Could not determine which course module this belonged to!");
        }
    } else {
        error("Must specify glossary ID or course module ID");
    }

    if ($CFG->forcelogin) {
        require_login();
    }
    
/// redirecting if adding a new entry
    if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
        redirect("edit.php?id=$cm->id&mode=$mode");
    }

/// setting the defaut number of entries per page if not set
    
    if ( !$entriesbypage = $glossary->entbypage ) {
        $entriesbypage = $CFG->glossary_entbypage;
    }

/// setting the default values for the display mode of the current glossary
/// only if the glossary is viewed by the first time
    if ( $dp = get_record("glossary_displayformats","fid", $glossary->displayformat) ) {
        $printpivot = $dp->showgroup;
        if ( $mode == '' and $hook == '' and $show == '') {
            $mode      = $dp->defaultmode;
            $hook      = $dp->defaulthook;
            $sortkey   = $dp->sortkey;
            $sortorder = $dp->sortorder;            
        }
    } else {
        $printpivot = 1;
        if ( $mode == '' and $hook == '' and $show == '') {
            $mode = 'letter';
            $hook = 'ALL';
        }
    }

    if ( $displayformat == -1 ) {
         $displayformat = $glossary->displayformat;
    } 
    if ( $displayformat == GLOSSARY_FORMAT_CONTINUOUS ) { 
        $mode = 'date';
    }

    if ( $show ) {
        $mode = 'term';
        $hook = $show;
        $show = '';
    }
/// Processing standard security processes
    $navigation = "";
    if ($course->category) {
        $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
        require_login($course->id);
    }
    if (!$cm->visible and !isteacher($course->id)) {
        notice(get_string("activityiscurrentlyhidden"));
    } 
    add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&tab=$tab", $glossary->id, $cm->id);

/// stablishing flag variables
    if ( $sortorder = strtolower($sortorder) ) {
        if ($sortorder != 'asc' and $sortorder != 'desc') {
            $sortorder = '';
        }
    }
    if ( $sortkey = strtoupper($sortkey) ) {
        if ($sortkey != 'CREATION' and 
            $sortkey != 'UPDATE' and 
            $sortkey != 'FIRSTNAME' and 
            $sortkey != 'LASTNAME'
            ) {
            $sortkey = '';
        }
    }

    switch ( $mode = strtolower($mode) ) {
    case 'search': /// looking for terms containing certain word(s)
        $tab = GLOSSARY_STANDARD_VIEW;

        $searchterms = explode(' ', $hook); // Search for words independently
        foreach ($searchterms as $key => $searchterm) {
            if (strlen($searchterm) < 2) {
                unset($searchterms[$key]);
            } 
        } 
        $hook = trim(implode(' ', $searchterms));
    break;
    
    case 'entry':  /// Looking for a certain entry id
        $tab = GLOSSARY_STANDARD_VIEW;
        if ( $dp = get_record("glossary_displayformats","fid", $glossary->displayformat) ) {
            if ( $dp->relatedview >= 0 ) {
                $displayformat = $dp->relatedview;
            }
        }
    break;
    
    case 'cat':    /// Looking for a certain cat
        $tab = GLOSSARY_CATEGORY_VIEW;
        if ( $hook > 0 ) {
            $category = get_record("glossary_categories","id",$hook);
        }
    break;

    case 'approval':    /// Looking for entries waiting for approval
        $tab = GLOSSARY_APPROVAL_VIEW;
        if ( !$hook and !$sortkey and !$sortorder) {
            $hook = 'ALL';
        }
    break;

    case 'term':   /// Looking for entries that include certain term in its concept, definition or aliases
        $tab = GLOSSARY_STANDARD_VIEW;
    break;

    case 'date':
        $tab = GLOSSARY_DATE_VIEW;
        if ( !$sortkey ) {
            $sortkey = 'UPDATE';
        } 
        if ( !$sortorder ) {
            $sortorder = 'desc';
        }
    break;
    
    case 'author':  /// Looking for entries, browsed by author
        $tab = GLOSSARY_AUTHOR_VIEW;
        if ( !$hook ) {
            $hook = 'ALL';
        } 
        if ( !$sortkey ) {
            $sortkey = 'FIRSTNAME';
        } 
        if ( !$sortorder ) {
            $sortorder = 'asc';
        }
    break;

    case 'letter':  /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
    default:
        $tab = GLOSSARY_STANDARD_VIEW;
        if ( !$hook ) {
            $hook = 'ALL';  
        } 
    break;
    }  

    switch ( $tab ) {
    case GLOSSARY_IMPORT_VIEW: 
    case GLOSSARY_EXPORT_VIEW: 
    case GLOSSARY_APPROVAL_VIEW:
        $isuserframe = 0;
    break;
    
    default:
        $isuserframe = 1;
    break;
    }

/// Printing the heading
    $strglossaries = get_string("modulenameplural", "glossary");
    $strglossary = get_string("modulename", "glossary");
    $strallcategories = get_string("allcategories", "glossary");
    $straddentry = get_string("addentry", "glossary");
    $strnoentries = get_string("noentries", "glossary");
    $strsearchconcept = get_string("searchconcept", "glossary");
    $strsearchindefinition = get_string("searchindefinition", "glossary");
    $strsearch = get_string("search");
    
    print_header(strip_tags("$course->shortname: $glossary->name"), "$course->fullname",
        "$navigation <A HREF=index.php?id=$course->id>$strglossaries</A> -> $glossary->name",
        "", "", true, update_module_button($cm->id, $course->id, $strglossary),
        navmenu($course, $cm));

    //If rss are activated at site and glossary level and this glossary has rss defined, show link
        if ($CFG->enablerssfeeds && $CFG->glossary_enablerssfeeds && $glossary->rsstype and $glossary->rssarticles) {
            echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top"><td align="right">';
            $tooltiptext = get_string("rsssubscriberss","glossary",$glossary->name);
            rss_print_link($course->id, $USER->id, "glossary", $glossary->id, $tooltiptext);
            echo '</td></tr></table>';
        }
    
    echo '<p align="center"><font size="3"><b>' . stripslashes_safe($glossary->name);
    if ( $isuserframe and $mode != 'search') {
    /// the "Print" icon
        echo " <a title =\"". get_string("printerfriendly","glossary") . "\" target=\"_blank\" href=\"print.php?id=$cm->id&mode=$mode&hook=$hook&sortkey=$sortkey&sortorder=$sortorder&offset=$offset\">";
        echo '<img border=0 src="print.gif"/></a>';
    }
    echo '</b></font></p>';

/// Info box
    if ( $glossary->intro ) {
        echo '<table align="center" width="70%" bgcolor="#FFFFFF" class="generaltab"><tr><td>';
        echo format_text($glossary->intro);
        print_simple_box_end();
    }

/// Search box
//    echo '<p>';
    echo '<table align="center" width="70%" bgcolor="' . $THEME->cellheading .'" class="generalbox"><tr><td align=center>';
    
    echo '<p align="center">';
    echo '<form method="POST" action="view.php">';
    echo '<input type="submit" value="'.$strsearch.'" name="searchbutton"> ';
    if ($mode == 'search') {
        echo '<input type="text" name="hook" size="20" value="'.$hook.'"> ';
    } else {
        echo '<input type="text" name="hook" size="20" value=""> ';
    }
    if ($fullsearch) {
        $fullsearchchecked = 'checked="checked"';
    } else {
        $fullsearchchecked = '';
    }
    echo '<input type="checkbox" name="fullsearch" value="1" '.$fullsearchchecked.'>';
    echo '<input type="hidden" name="mode" value="search">';
    echo '<input type="hidden" name="id" value="'.$cm->id.'">';
    echo $strsearchindefinition;
    echo '</form>';
    echo '</p>';
    print_simple_box_end();    

    include("tabs.html");

    include_once("sql.php");

/// printing the entries
    $entriesshown = 0;
    $currentpivot = '';
    $ratingsmenuused = NULL;
    $paging = NULL;
    $tableisopen = 0;
    if ( $hook == 'SPECIAL' ) {
        $alphabet = explode(",", get_string("alphabet"));
    }
    if ($allentries) {
        /// printing the paging links

        $paging = get_string("allentries","glossary");
        if ( $offset < 0 ) {
            $paging = '<strong>' . $paging . '</strong>';
        } else {
            $paging = "<a href=\"view.php?id=$id&mode=$mode&hook=$hook&offset=-1&sortkey=$sortkey&sortorder=$sortorder\">" . $paging . '</a>';
        }
        if ($count > $entriesbypage ) {            
            for ($i = 0; ($i*$entriesbypage) < $count  ; $i++   ) {
                if ( $paging != '' ) {
                    if ($i % 20 == 0 and $i) {
                        $paging .= '<br>';
                    } else {
                        $paging .= ' | ';
                    }
                }
                $pagenumber = (string) ($i + 1 );
                if ($offset / $entriesbypage == $i) {
                    $paging .= '<strong>' . $pagenumber . '</strong>';
                } else {
                    $paging .= "<a href=\"view.php?id=$id&mode=$mode&hook=$hook&offset=" . ($i*$entriesbypage) . "&sortkey=$sortkey&sortorder=$sortorder\">" . $pagenumber . '</a>';
                }
            }
            $paging  = "<font size=1><center>" . get_string ("jumpto") . " $paging</center></font>";
        } else {
            $paging = '';<commit_msg>Now paging will mantain the "fullsearch" option. Bug 1438. (http://moodle.org/bugs/bug.php?op=show&bugid=1438)<commit_after><?php  // $Id$
/// This page prints a particular instance of glossary
    require_once("../../config.php");
    require_once("lib.php");
    require_once("$CFG->dirroot/rss/rsslib.php");

    global $CFG, $THEME, $USER;
    $debug = 0;
    $CFG->startpagetime = microtime();            
    
    optional_variable($id);           // Course Module ID
    optional_variable($g);            // Glossary ID

    optional_variable($tab,GLOSSARY_NO_VIEW); // browsing entries by categories?

    optional_variable($mode,"");  // [ "term"   | "entry"  | "cat"     | "date" | 
                                        //   "letter" | "search" | "author"  | "approval" ]
    optional_variable($hook,"");  // the term, entry, cat, etc... to look for based on mode

    optional_variable($fullsearch,0); // full search (concept and definition) when searching?

    optional_variable($sortkey,"");    // Sorted view: 
                                       //    [ CREATION | UPDATE | FIRSTNAME | LASTNAME |
                                       //      concept | timecreated | ... ]
    optional_variable($sortorder,"");  // it defines the order of the sorting (ASC or DESC)

    optional_variable($offset,0);      // entries to bypass (for paging purpouses)

    optional_variable($show,"");       // [ concept | alias ] => mode=term hook=$show
    optional_variable($displayformat,-1);  // override of the glossary display format

    if (!empty($id)) {
        if (! $cm = get_record("course_modules", "id", $id)) {
            error("Course Module ID was incorrect");
        }     
        if (! $course = get_record("course", "id", $cm->course)) {
            error("Course is misconfigured");
        }     
        if (! $glossary = get_record("glossary", "id", $cm->instance)) {
            error("Course module is incorrect");
        } 
    } else if (!empty($g)) {
        if (! $glossary = get_record("glossary", "id", $g)) {
            error("Course module is incorrect");
        } 
        if (! $course = get_record("course", "id", $glossary->course)) {
            error("Could not determine which course this belonged to!");
        }     
        if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
            error("Could not determine which course module this belonged to!");
        }
    } else {
        error("Must specify glossary ID or course module ID");
    }

    if ($CFG->forcelogin) {
        require_login();
    }
    
/// redirecting if adding a new entry
    if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
        redirect("edit.php?id=$cm->id&mode=$mode");
    }

/// setting the defaut number of entries per page if not set
    
    if ( !$entriesbypage = $glossary->entbypage ) {
        $entriesbypage = $CFG->glossary_entbypage;
    }

/// setting the default values for the display mode of the current glossary
/// only if the glossary is viewed by the first time
    if ( $dp = get_record("glossary_displayformats","fid", $glossary->displayformat) ) {
        $printpivot = $dp->showgroup;
        if ( $mode == '' and $hook == '' and $show == '') {
            $mode      = $dp->defaultmode;
            $hook      = $dp->defaulthook;
            $sortkey   = $dp->sortkey;
            $sortorder = $dp->sortorder;            
        }
    } else {
        $printpivot = 1;
        if ( $mode == '' and $hook == '' and $show == '') {
            $mode = 'letter';
            $hook = 'ALL';
        }
    }

    if ( $displayformat == -1 ) {
         $displayformat = $glossary->displayformat;
    } 
    if ( $displayformat == GLOSSARY_FORMAT_CONTINUOUS ) { 
        $mode = 'date';
    }

    if ( $show ) {
        $mode = 'term';
        $hook = $show;
        $show = '';
    }
/// Processing standard security processes
    $navigation = "";
    if ($course->category) {
        $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
        require_login($course->id);
    }
    if (!$cm->visible and !isteacher($course->id)) {
        notice(get_string("activityiscurrentlyhidden"));
    } 
    add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&tab=$tab", $glossary->id, $cm->id);

/// stablishing flag variables
    if ( $sortorder = strtolower($sortorder) ) {
        if ($sortorder != 'asc' and $sortorder != 'desc') {
            $sortorder = '';
        }
    }
    if ( $sortkey = strtoupper($sortkey) ) {
        if ($sortkey != 'CREATION' and 
            $sortkey != 'UPDATE' and 
            $sortkey != 'FIRSTNAME' and 
            $sortkey != 'LASTNAME'
            ) {
            $sortkey = '';
        }
    }

    switch ( $mode = strtolower($mode) ) {
    case 'search': /// looking for terms containing certain word(s)
        $tab = GLOSSARY_STANDARD_VIEW;

        $searchterms = explode(' ', $hook); // Search for words independently
        foreach ($searchterms as $key => $searchterm) {
            if (strlen($searchterm) < 2) {
                unset($searchterms[$key]);
            } 
        } 
        $hook = trim(implode(' ', $searchterms));
    break;
    
    case 'entry':  /// Looking for a certain entry id
        $tab = GLOSSARY_STANDARD_VIEW;
        if ( $dp = get_record("glossary_displayformats","fid", $glossary->displayformat) ) {
            if ( $dp->relatedview >= 0 ) {
                $displayformat = $dp->relatedview;
            }
        }
    break;
    
    case 'cat':    /// Looking for a certain cat
        $tab = GLOSSARY_CATEGORY_VIEW;
        if ( $hook > 0 ) {
            $category = get_record("glossary_categories","id",$hook);
        }
    break;

    case 'approval':    /// Looking for entries waiting for approval
        $tab = GLOSSARY_APPROVAL_VIEW;
        if ( !$hook and !$sortkey and !$sortorder) {
            $hook = 'ALL';
        }
    break;

    case 'term':   /// Looking for entries that include certain term in its concept, definition or aliases
        $tab = GLOSSARY_STANDARD_VIEW;
    break;

    case 'date':
        $tab = GLOSSARY_DATE_VIEW;
        if ( !$sortkey ) {
            $sortkey = 'UPDATE';
        } 
        if ( !$sortorder ) {
            $sortorder = 'desc';
        }
    break;
    
    case 'author':  /// Looking for entries, browsed by author
        $tab = GLOSSARY_AUTHOR_VIEW;
        if ( !$hook ) {
            $hook = 'ALL';
        } 
        if ( !$sortkey ) {
            $sortkey = 'FIRSTNAME';
        } 
        if ( !$sortorder ) {
            $sortorder = 'asc';
        }
    break;

    case 'letter':  /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
    default:
        $tab = GLOSSARY_STANDARD_VIEW;
        if ( !$hook ) {
            $hook = 'ALL';  
        } 
    break;
    }  

    switch ( $tab ) {
    case GLOSSARY_IMPORT_VIEW: 
    case GLOSSARY_EXPORT_VIEW: 
    case GLOSSARY_APPROVAL_VIEW:
        $isuserframe = 0;
    break;
    
    default:
        $isuserframe = 1;
    break;
    }

/// Printing the heading
    $strglossaries = get_string("modulenameplural", "glossary");
    $strglossary = get_string("modulename", "glossary");
    $strallcategories = get_string("allcategories", "glossary");
    $straddentry = get_string("addentry", "glossary");
    $strnoentries = get_string("noentries", "glossary");
    $strsearchconcept = get_string("searchconcept", "glossary");
    $strsearchindefinition = get_string("searchindefinition", "glossary");
    $strsearch = get_string("search");
    
    print_header(strip_tags("$course->shortname: $glossary->name"), "$course->fullname",
        "$navigation <A HREF=index.php?id=$course->id>$strglossaries</A> -> $glossary->name",
        "", "", true, update_module_button($cm->id, $course->id, $strglossary),
        navmenu($course, $cm));

    //If rss are activated at site and glossary level and this glossary has rss defined, show link
        if ($CFG->enablerssfeeds && $CFG->glossary_enablerssfeeds && $glossary->rsstype and $glossary->rssarticles) {
            echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top"><td align="right">';
            $tooltiptext = get_string("rsssubscriberss","glossary",$glossary->name);
            rss_print_link($course->id, $USER->id, "glossary", $glossary->id, $tooltiptext);
            echo '</td></tr></table>';
        }
    
    echo '<p align="center"><font size="3"><b>' . stripslashes_safe($glossary->name);
    if ( $isuserframe and $mode != 'search') {
    /// the "Print" icon
        echo " <a title =\"". get_string("printerfriendly","glossary") . "\" target=\"_blank\" href=\"print.php?id=$cm->id&mode=$mode&hook=$hook&sortkey=$sortkey&sortorder=$sortorder&offset=$offset\">";
        echo '<img border=0 src="print.gif"/></a>';
    }
    echo '</b></font></p>';

/// Info box
    if ( $glossary->intro ) {
        echo '<table align="center" width="70%" bgcolor="#FFFFFF" class="generaltab"><tr><td>';
        echo format_text($glossary->intro);
        print_simple_box_end();
    }

/// Search box
//    echo '<p>';
    echo '<table align="center" width="70%" bgcolor="' . $THEME->cellheading .'" class="generalbox"><tr><td align=center>';
    
    echo '<p align="center">';
    echo '<form method="POST" action="view.php">';
    echo '<input type="submit" value="'.$strsearch.'" name="searchbutton"> ';
    if ($mode == 'search') {
        echo '<input type="text" name="hook" size="20" value="'.$hook.'"> ';
    } else {
        echo '<input type="text" name="hook" size="20" value=""> ';
    }
    if ($fullsearch) {
        $fullsearchchecked = 'checked="checked"';
    } else {
        $fullsearchchecked = '';
    }
    echo '<input type="checkbox" name="fullsearch" value="1" '.$fullsearchchecked.'>';
    echo '<input type="hidden" name="mode" value="search">';
    echo '<input type="hidden" name="id" value="'.$cm->id.'">';
    echo $strsearchindefinition;
    echo '</form>';
    echo '</p>';
    print_simple_box_end();    

    include("tabs.html");

    include_once("sql.php");

/// printing the entries
    $entriesshown = 0;
    $currentpivot = '';
    $ratingsmenuused = NULL;
    $paging = NULL;
    $tableisopen = 0;
    if ( $hook == 'SPECIAL' ) {
        $alphabet = explode(",", get_string("alphabet"));
    }
    if ($allentries) {
        /// printing the paging links

        $paging = get_string("allentries","glossary");
        if ( $offset < 0 ) {
            $paging = '<strong>' . $paging . '</strong>';
        } else {
            $paging = "<a href=\"view.php?id=$id&mode=$mode&hook=$hook&offset=-1&sortkey=$sortkey&sortorder=$sortorder&fullsearch=$fullsearch\">" . $paging . '</a>';
        }
        if ($count > $entriesbypage ) {            
            for ($i = 0; ($i*$entriesbypage) < $count  ; $i++   ) {
                if ( $paging != '' ) {
                    if ($i % 20 == 0 and $i) {
                        $paging .= '<br>';
                    } else {
                        $paging .= ' | ';
                    }
                }
                $pagenumber = (string) ($i + 1 );
                if ($offset / $entriesbypage == $i) {
                    $paging .= '<strong>' . $pagenumber . '</strong>';
                } else {
                    $paging .= "<a href=\"view.php?id=$id&mode=$mode&hook=$hook&offset=" . ($i*$entriesbypage) . "&sortkey=$sortkey&sortorder=$sortorder&fullsearch=$fullsearch\">" . $pagenumber . '</a>';
                }
            }
            $paging  = "<font size=1><center>" . get_string ("jumpto") . " $paging</center></font>";
        } else {
            $paging = '';
{'fraction_non_alphanumeric': 0.1690146876204342, 'fraction_numerical': 0.003596968269601336, 'mean_word_length': 2.135606874328679, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 219, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6775831', 'n_tokens_mistral': 7384, 'n_tokens_neox': 7130, 'n_words': 1579}