Merge branch 'MDL-27193_glossaryDB' of https://github.com/andyjdavis/moodle

This commit is contained in:
Jake Dallimore 2021-05-06 09:49:14 +08:00
commit c7c186681a
4 changed files with 189 additions and 55 deletions

View File

@ -0,0 +1,153 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
/**
* Class for glossary display formats management.
*
* @package mod_glossary
* @copyright 2021 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_glossary_admin_setting_display_formats extends admin_setting {
/**
* Calls parent::__construct with specific arguments
*/
public function __construct() {
$this->nosave = true;
parent::__construct('glossarydisplayformats', get_string('displayformatssetup', 'glossary'), '', '');
}
/**
* Always returns true, does nothing
*
* @return true
*/
public function get_setting() {
return true;
}
/**
* Always returns true, does nothing
*
* @return true
*/
public function get_defaultsetting() {
return true;
}
/**
* Always returns '', does not write anything
*
* @param string $data Unused
* @return string Always returns ''
*/
public function write_setting($data) {
// Do not write any setting.
return '';
}
/**
* Checks if $query is one of the available display formats
*
* @param string $query The string to search for
* @return bool Returns true if found, false if not
*/
public function is_related($query) {
global $DB;
if (parent::is_related($query)) {
return true;
}
$query = core_text::strtolower($query);
$formats = $DB->get_records("glossary_formats");
foreach ($formats as $format) {
if (strpos(core_text::strtolower($format->name), $query) !== false) {
return true;
}
$localised = get_string("displayformat$format->name", "glossary");
if (strpos(core_text::strtolower($localised), $query) !== false) {
return true;
}
}
return false;
}
/**
* Builds the XHTML to display the control
*
* @param string $data Unused
* @param string $query
* @return string
*/
public function output_html($data, $query='') {
global $CFG, $OUTPUT, $DB;
$stredit = get_string("edit");
$strhide = get_string("hide");
$strshow = get_string("show");
$str = $OUTPUT->heading(get_string('displayformatssetup', 'glossary'), 3, 'main', true);
$recformats = $DB->get_records("glossary_formats");
$formats = array();
// Build alphabetized list of formats.
foreach ($recformats as $format) {
$formats[get_string("displayformat$format->name", "glossary")] = $format;
}
ksort($formats);
$table = new html_table();
$table->align = array('left', 'center');
foreach ($formats as $formatname => $format) {
$editicon = html_writer::link(
new moodle_url(
'/mod/glossary/formats.php',
array('id' => $format->id, 'mode' => 'edit')
),
$OUTPUT->pix_icon('t/edit', $stredit),
array('title' => $stredit));
if ( $format->visible ) {
$vtitle = $strhide;
$vicon = "t/hide";
} else {
$vtitle = $strshow;
$vicon = "t/show";
}
$visibleicon = html_writer::link(
new moodle_url(
'/mod/glossary/formats.php',
array('id' => $format->id, 'mode' => 'visible', 'sesskey' => sesskey())
),
$OUTPUT->pix_icon($vicon, $vtitle),
array('title' => $vtitle)
);
$table->data[] = array(
$formatname,
$editicon . '&nbsp;&nbsp;' . $visibleicon
);
}
$str .= html_writer::table($table);
return highlight($query, $str);
}
}

View File

@ -78,8 +78,8 @@ echo '<table width="90%" align="center" class="generalbox">';
<td align="right" width="20%"><?php echo html_writer::label(get_string('popupformat','glossary'), 'menupopupformatname'); ?></td>
<td>
<?php
//get and update available formats
$recformats = glossary_get_available_formats();
// Get available formats.
$recformats = $DB->get_records("glossary_formats");
$formats = array();

View File

@ -906,26 +906,38 @@ function glossary_scale_used_anywhere($scaleid) {
function glossary_get_available_formats() {
global $CFG, $DB;
//Get available formats (plugin) and insert (if necessary) them into glossary_formats
// Get available formats (plugin) and insert them (if necessary) into glossary_formats.
$formats = get_list_of_plugins('mod/glossary/formats', 'TEMPLATE');
$pluginformats = array();
$formatrecords = $DB->get_records("glossary_formats");
foreach ($formats as $format) {
//If the format file exists
// If the format file exists.
if (file_exists($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php')) {
include_once($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php');
//If the function exists
if (function_exists('glossary_show_entry_'.$format)) {
//Acummulate it as a valid format
// Acummulate it as a valid format.
$pluginformats[] = $format;
//If the format doesn't exist in the table
if (!$rec = $DB->get_record('glossary_formats', array('name'=>$format))) {
//Insert the record in glossary_formats
// Check if the format exists in the table.
$rec = null;
foreach ($formatrecords as $record) {
if ($record->name == $format) {
$rec = $record;
break;
}
}
if (!$rec) {
// Insert the record in glossary_formats.
$gf = new stdClass();
$gf->name = $format;
$gf->popupformatname = $format;
$gf->visible = 1;
$id = $DB->insert_record('glossary_formats', $gf);
$rec = $DB->get_record('glossary_formats', array('id' => $id));
array_push($formatrecords, $rec);
}
if (empty($rec->showtabs)) {
@ -935,31 +947,29 @@ function glossary_get_available_formats() {
}
}
//Delete non_existent formats from glossary_formats table
$formats = $DB->get_records("glossary_formats");
foreach ($formats as $format) {
// Delete non_existent formats from glossary_formats table.
foreach ($formatrecords as $record) {
$todelete = false;
//If the format in DB isn't a valid previously detected format then delete the record
if (!in_array($format->name,$pluginformats)) {
// If the format in DB isn't a valid previously detected format then delete the record.
if (!in_array($record->name, $pluginformats)) {
$todelete = true;
}
if ($todelete) {
//Delete the format
$DB->delete_records('glossary_formats', array('name'=>$format->name));
//Reasign existing glossaries to default (dictionary) format
if ($glossaries = $DB->get_records('glossary', array('displayformat'=>$format->name))) {
// Delete the format.
$DB->delete_records('glossary_formats', array('id' => $record->id));
unset($formatrecords[$record->id]);
// Reassign existing glossaries to default (dictionary) format.
if ($glossaries = $DB->get_records('glossary', array('displayformat' => $record->name))) {
foreach($glossaries as $glossary) {
$DB->set_field('glossary','displayformat','dictionary', array('id'=>$glossary->id));
$DB->set_field('glossary', 'displayformat', 'dictionary', array('id' => $glossary->id));
}
}
}
}
//Now everything is ready in glossary_formats table
$formats = $DB->get_records("glossary_formats");
return $formats;
return $formatrecords;
}
/**

View File

@ -47,38 +47,9 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configcheckbox('glossary_fullmatch', get_string('fullmatch', 'glossary'),
get_string('cnffullmatch', 'glossary'), 0));
// This is unfortunately necessary to ensure that the glossary_formats table is populated and up to date.
// Ensure the table is in sync with what display formats are available in code.
glossary_get_available_formats();
//Update and get available formats
$recformats = glossary_get_available_formats();
$formats = array();
//Take names
foreach ($recformats as $format) {
$formats[$format->id] = get_string("displayformat$format->name", "glossary");
}
asort($formats);
$str = '<table>';
foreach ($formats as $formatid=>$formatname) {
$recformat = $DB->get_record('glossary_formats', array('id'=>$formatid));
$str .= '<tr>';
$str .= '<td>' . $formatname . '</td>';
$eicon = "<a title=\"".get_string("edit")."\" href=\"$CFG->wwwroot/mod/glossary/formats.php?id=$formatid&amp;mode=edit\">";
$eicon .= $OUTPUT->pix_icon('t/edit', get_string('edit')). "</a>";
if ( $recformat->visible ) {
$vtitle = get_string("hide");
$vicon = "t/hide";
} else {
$vtitle = get_string("show");
$vicon = "t/show";
}
$url = "$CFG->wwwroot/mod/glossary/formats.php?id=$formatid&amp;mode=visible&amp;sesskey=".sesskey();
$viconlink = "<a title=\"$vtitle\" href=\"$url\">";
$viconlink .= $OUTPUT->pix_icon($vicon, $vtitle) . "</a>";
$str .= '<td align="center">' . $eicon . '&nbsp;&nbsp;' . $viconlink . '</td>';
$str .= '</tr>';
}
$str .= '</table>';
$settings->add(new admin_setting_heading('glossary_formats_header', get_string('displayformatssetup', 'glossary'), $str));
$settings->add(new mod_glossary_admin_setting_display_formats());
}