From 223d6cb3bf8a8ac99679c8d6403d555f94b11cd0 Mon Sep 17 00:00:00 2001 From: Nivedita Setru Date: Sun, 29 Mar 2015 18:56:40 +0100 Subject: [PATCH] MDL-26501 mod_glossary: Allow to specify visible tabs --- mod/glossary/db/install.xml | 3 +- mod/glossary/db/upgrade.php | 15 +++++ mod/glossary/formats.php | 37 ++++++++++++ mod/glossary/lang/en/glossary.php | 2 + mod/glossary/lib.php | 93 +++++++++++++++++++++++++++++-- mod/glossary/tabs.php | 34 +++++++---- mod/glossary/version.php | 2 +- mod/glossary/view.php | 40 +++++++++++++ 8 files changed, 207 insertions(+), 19 deletions(-) diff --git a/mod/glossary/db/install.xml b/mod/glossary/db/install.xml index 03774bdc5a4..5f86bcf6bb3 100644 --- a/mod/glossary/db/install.xml +++ b/mod/glossary/db/install.xml @@ -1,5 +1,5 @@ - @@ -112,6 +112,7 @@ + diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index f5b517dfdf0..a4aebf99f46 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -68,6 +68,21 @@ function xmldb_glossary_upgrade($oldversion) { // Moodle v2.9.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2015060200) { + + // Define field showtabs to be added to glossary_formats. + $table = new xmldb_table('glossary_formats'); + $field = new xmldb_field('showtabs', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'showgroup'); + + // Conditionally launch add field showtabs. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Glossary savepoint reached. + upgrade_mod_savepoint(true, 2015060200, 'glossary'); + } + return true; } diff --git a/mod/glossary/formats.php b/mod/glossary/formats.php index c1cd49829a2..d40cf89f213 100644 --- a/mod/glossary/formats.php +++ b/mod/glossary/formats.php @@ -42,6 +42,12 @@ if ( $mode == 'visible' and confirm_sesskey()) { $displayformat->sortkey = $form->sortkey; $displayformat->sortorder = $form->sortorder; + // Extract visible tabs from array into comma separated list. + $visibletabs = implode(',', $form->visibletabs); + // Include 'standard' tab by default along with other tabs. + // This way we don't run into the risk of users not selecting any tab for displayformat. + $displayformat->showtabs = GLOSSARY_STANDARD.','.$visibletabs; + $DB->update_record("glossary_formats",$displayformat); redirect("$CFG->wwwroot/$CFG->admin/settings.php?section=modsettingglossary#glossary_formats_header"); die; @@ -248,6 +254,37 @@ echo '';

+ + + + + diff --git a/mod/glossary/lang/en/glossary.php b/mod/glossary/lang/en/glossary.php index 88ff8d91a69..ef92d5379c1 100644 --- a/mod/glossary/lang/en/glossary.php +++ b/mod/glossary/lang/en/glossary.php @@ -80,6 +80,7 @@ $string['cnfshowgroup'] = 'Specify if the group break should be shown or not.'; $string['cnfsortkey'] = 'Select the sorting key by default.'; $string['cnfsortorder'] = 'Select the sorting order by default.'; $string['cnfstudentcanpost'] = 'Define if the students can or cannot post entries by default'; +$string['cnftabs'] = 'Select visible tabs for this glossary format'; $string['comment'] = 'Comment'; $string['commentdeleted'] = 'The comment has been deleted.'; $string['comments'] = 'Comments'; @@ -290,6 +291,7 @@ $string['studentcanpost'] = 'Students can add entries'; $string['totalentries'] = 'Total entries'; $string['usedynalink'] = 'Automatically link glossary entries'; $string['usedynalink_help'] = 'If site-wide glossary auto-linking has been enabled by an administrator and this setting is enabled, the "Add a new entry" form includes the option to automatically link the entry wherever the concept words and phrases appear throughout the rest of the course.'; +$string['visibletabs'] = 'Visible tabs'; $string['waitingapproval'] = 'Waiting approval'; $string['warningstudentcapost'] = '(Applies only if the glossary is not the main one)'; $string['withauthor'] = 'Concepts with author'; diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 3db07919500..4c21d15ba69 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -38,6 +38,17 @@ define("GLOSSARY_IMPORT_VIEW", 5); define("GLOSSARY_EXPORT_VIEW", 6); define("GLOSSARY_APPROVAL_VIEW", 7); +// Glossary tabs. +define('GLOSSARY_STANDARD', 'standard'); +define('GLOSSARY_AUTHOR', 'author'); +define('GLOSSARY_CATEGORY', 'category'); +define('GLOSSARY_DATE', 'date'); + +// Glossary displayformats. +define('GLOSSARY_CONTINUOUS', 'continuous'); +define('GLOSSARY_DICTIONARY', 'dictionary'); +define('GLOSSARY_FULLWITHOUTAUTHOR', 'fullwithoutauthor'); + /// STANDARD FUNCTIONS /////////////////////////////////////////////////////////// /** * @global object @@ -908,6 +919,10 @@ function glossary_get_available_formats() { $gf->visible = 1; $DB->insert_record("glossary_formats",$gf); } + + if(is_null($rec->showtabs)) { + glossary_set_default_visible_tabs($rec); + } } } } @@ -3044,11 +3059,29 @@ function glossary_get_completion_state($course,$cm,$userid,$type) { } function glossary_extend_navigation($navigation, $course, $module, $cm) { - global $CFG; - $navigation->add(get_string('standardview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id, 'mode'=>'letter'))); - $navigation->add(get_string('categoryview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id, 'mode'=>'cat'))); - $navigation->add(get_string('dateview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id, 'mode'=>'date'))); - $navigation->add(get_string('authorview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id, 'mode'=>'author'))); + global $CFG, $DB; + + $glossarytabs = $DB->get_field('glossary_formats', 'showtabs', array('name' => $module->displayformat)); + // Get visible tabs for the format and check if the menu needs to be displayed. + $showtabs = glossary_get_visible_tabs($glossarytabs); + + foreach ($showtabs as $showtabkey => $showtabvalue) { + + switch($showtabvalue) { + case GLOSSARY_STANDARD : + $navigation->add(get_string('standardview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id' => $cm->id, 'mode' => 'letter'))); + break; + case GLOSSARY_CATEGORY : + $navigation->add(get_string('categoryview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id' => $cm->id, 'mode' => 'cat'))); + break; + case GLOSSARY_DATE : + $navigation->add(get_string('dateview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id' => $cm->id, 'mode' => 'date'))); + break; + case GLOSSARY_AUTHOR : + $navigation->add(get_string('authorview', 'glossary'), new moodle_url('/mod/glossary/view.php', array('id' => $cm->id, 'mode' => 'author'))); + break; + } + } } /** @@ -3194,3 +3227,53 @@ function glossary_page_type_list($pagetype, $parentcontext, $currentcontext) { 'mod-glossary-edit'=>get_string('page-mod-glossary-edit', 'glossary')); return $module_pagetype; } + +/** + * Return list of all glossary tabs. + * @return array + * @throws coding_exception + */ +function glossary_get_all_tabs() { + + return array ( + GLOSSARY_AUTHOR => get_string('authorview', 'glossary'), + GLOSSARY_CATEGORY => get_string('categoryview', 'glossary'), + GLOSSARY_DATE => get_string('dateview', 'glossary') + ); +} + +/** + * Set 'showtabs' value for glossary formats + * @param $glossaryformat + */ +function glossary_set_default_visible_tabs($glossaryformat) { + global $DB; + + switch($glossaryformat->name) { + case GLOSSARY_CONTINUOUS: + $showtabs = 'standard,category,date'; + break; + case GLOSSARY_DICTIONARY: + $showtabs = 'standard'; + break; + case GLOSSARY_FULLWITHOUTAUTHOR: + $showtabs = 'standard,category,date'; + break; + default: + $showtabs = 'standard,author,category,date'; + break; + } + + $DB->set_field('glossary_formats', 'showtabs', $showtabs, array('id' => $glossaryformat->id)); +} + +/** + * Convert 'showtabs' string to array + * @param $glossarytabs + * @return array + */ +function glossary_get_visible_tabs($glossarytabs) { + $showtabs = preg_split('/,/', $glossarytabs, -1, PREG_SPLIT_NO_EMPTY); + + return $showtabs; +} diff --git a/mod/glossary/tabs.php b/mod/glossary/tabs.php index bafd4d1c700..d7013764d80 100644 --- a/mod/glossary/tabs.php +++ b/mod/glossary/tabs.php @@ -20,22 +20,32 @@ $tab = $defaulttab; } + // Get visible tabs for the format and check tab needs to be displayed. + $dt = glossary_get_visible_tabs($dp->showtabs); - $browserow[] = new tabobject(GLOSSARY_STANDARD_VIEW, - $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=letter', - get_string('standardview', 'glossary')); + if (in_array(GLOSSARY_STANDARD, $dt)) { + $browserow[] = new tabobject(GLOSSARY_STANDARD_VIEW, + $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=letter', + get_string('standardview', 'glossary')); + } - $browserow[] = new tabobject(GLOSSARY_CATEGORY_VIEW, - $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=cat', - get_string('categoryview', 'glossary')); + if (in_array(GLOSSARY_CATEGORY, $dt)) { + $browserow[] = new tabobject(GLOSSARY_CATEGORY_VIEW, + $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=cat', + get_string('categoryview', 'glossary')); + } - $browserow[] = new tabobject(GLOSSARY_DATE_VIEW, - $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=date', - get_string('dateview', 'glossary')); + if (in_array(GLOSSARY_DATE, $dt)) { + $browserow[] = new tabobject(GLOSSARY_DATE_VIEW, + $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=date', + get_string('dateview', 'glossary')); + } - $browserow[] = new tabobject(GLOSSARY_AUTHOR_VIEW, - $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=author', - get_string('authorview', 'glossary')); + if (in_array(GLOSSARY_AUTHOR, $dt)) { + $browserow[] = new tabobject(GLOSSARY_AUTHOR_VIEW, + $CFG->wwwroot.'/mod/glossary/view.php?id='.$id.'&mode=author', + get_string('authorview', 'glossary')); + } if ($tab < GLOSSARY_STANDARD_VIEW || $tab > GLOSSARY_AUTHOR_VIEW) { // We are on second row $inactive = array('edit'); diff --git a/mod/glossary/version.php b/mod/glossary/version.php index 0160218ecf5..a9f934dff5a 100644 --- a/mod/glossary/version.php +++ b/mod/glossary/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015051100; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2015060200; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2015050500; // Requires this Moodle version $plugin->component = 'mod_glossary'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; diff --git a/mod/glossary/view.php b/mod/glossary/view.php index 43a7ba89de3..c7b83e1ad6c 100644 --- a/mod/glossary/view.php +++ b/mod/glossary/view.php @@ -73,6 +73,10 @@ if ($page != 0 && $offset == 0) { $offset = $page * $entriesbypage; } +// Get visible tabs for this glossary format. +$glossarytabs = $DB->get_field('glossary_formats', 'showtabs', array('name' => $glossary->displayformat)); +$showtabs = glossary_get_visible_tabs($glossarytabs); + /// setting the default values for the display mode of the current glossary /// only if the glossary is viewed by the first time if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) { @@ -80,12 +84,30 @@ if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayf switch ($dp->defaultmode) { case 'cat': $defaulttab = GLOSSARY_CATEGORY_VIEW; + + // Handle defaultmode if 'category' tab is disabled. Fallback to 'standard' tab. + if (!in_array(GLOSSARY_CATEGORY, $showtabs)) { + $defaulttab = GLOSSARY_STANDARD_VIEW; + } + break; case 'date': $defaulttab = GLOSSARY_DATE_VIEW; + + // Handle defaultmode if 'date' tab is disabled. Fallback to 'standard' tab. + if (!in_array(GLOSSARY_DATE, $showtabs)) { + $defaulttab = GLOSSARY_STANDARD_VIEW; + } + break; case 'author': $defaulttab = GLOSSARY_AUTHOR_VIEW; + + // Handle defaultmode if 'author' tab is disabled. Fallback to 'standard' tab. + if (!in_array(GLOSSARY_AUTHOR, $showtabs)) { + $defaulttab = GLOSSARY_STANDARD_VIEW; + } + break; default: $defaulttab = GLOSSARY_STANDARD_VIEW; @@ -159,6 +181,12 @@ break; case 'cat': /// Looking for a certain cat $tab = GLOSSARY_CATEGORY_VIEW; + + // Validation - we don't want to display 'category' tab if it is disabled. + if (!in_array(GLOSSARY_CATEGORY, $showtabs)) { + $tab = GLOSSARY_STANDARD_VIEW; + } + if ( $hook > 0 ) { $category = $DB->get_record("glossary_categories", array("id"=>$hook)); } @@ -182,6 +210,12 @@ break; case 'date': $tab = GLOSSARY_DATE_VIEW; + + // Validation - we dont want to display 'date' tab if it is disabled. + if (!in_array(GLOSSARY_DATE, $showtabs)) { + $tab = GLOSSARY_STANDARD_VIEW; + } + if ( !$sortkey ) { $sortkey = 'UPDATE'; } @@ -192,6 +226,12 @@ break; case 'author': /// Looking for entries, browsed by author $tab = GLOSSARY_AUTHOR_VIEW; + + // Validation - we dont want to display 'author' tab if it is disabled. + if (!in_array(GLOSSARY_AUTHOR, $showtabs)) { + $tab = GLOSSARY_STANDARD_VIEW; + } + if ( !$hook ) { $hook = 'ALL'; }
+ showtabs); + $size = min(10, count($glossarytabs)); + ?> + + +

+
" />