2023-11-20 10:53:48 +01:00
|
|
|
<?php
|
|
|
|
// This file is part of the Studyplan plugin for Moodle
|
|
|
|
//
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
|
|
* Webservice class for handling associations of cohorts and users to a studyplan
|
|
|
|
* @package filter_bibleversesnwt
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace filter_bibleversesnwt;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webservice class for handling associations of cohorts and users to a studyplan
|
|
|
|
*/
|
|
|
|
class bibleservice extends \external_api {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function list_cohort
|
|
|
|
*/
|
|
|
|
public static function get_biblequote_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
'lang' => new \external_value(PARAM_TEXT, 'language code'),
|
|
|
|
'book' => new \external_value(PARAM_INT, 'bible book nr'),
|
|
|
|
'ch' => new \external_value(PARAM_INT, 'chapter number'),
|
|
|
|
'verse' => new \external_value(PARAM_INT, 'verse number'),
|
|
|
|
'book_end' => new \external_value(PARAM_INT, 'end bible book nr', VALUE_OPTIONAL),
|
|
|
|
'ch_end' => new \external_value(PARAM_INT, 'end chapter number', VALUE_OPTIONAL),
|
|
|
|
'verse_end' => new \external_value(PARAM_INT, 'end verse number', VALUE_OPTIONAL),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function list_cohort
|
|
|
|
*/
|
|
|
|
public static function get_biblequote_returns() : \external_description {
|
|
|
|
return new \external_single_structure([
|
|
|
|
"heading" => new \external_value(PARAM_RAW, 'heading'),
|
|
|
|
"quote" => new \external_value(PARAM_RAW, 'quote'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search cohorts for matching string
|
|
|
|
* @param string $like String to match cohorts with
|
|
|
|
* @param null $excludeid Do not include these cohorts
|
|
|
|
* @param int $contextid Context to search (default system)
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_biblequote($lang,$book,$ch,$verse,$book_end = null,$ch_end = null,$verse_end = null) {
|
2024-03-08 09:16:19 +01:00
|
|
|
global $DB;
|
|
|
|
$TABLE = "filter_bibleversesnwt";
|
2023-11-20 10:53:48 +01:00
|
|
|
|
2024-03-08 09:16:19 +01:00
|
|
|
$search = [
|
|
|
|
"lang" => $lang,
|
|
|
|
"book" => $book,
|
|
|
|
"ch"=> $ch,
|
|
|
|
"verse" => $verse,
|
|
|
|
"book_end" => $book_end,
|
|
|
|
"ch_end" => $ch_end,
|
|
|
|
"verse_end" => $verse_end,
|
|
|
|
];
|
2023-11-20 10:53:48 +01:00
|
|
|
|
2024-03-08 09:16:19 +01:00
|
|
|
$cache = $DB->get_record($TABLE,$search);
|
|
|
|
$now = time();
|
2023-11-20 10:53:48 +01:00
|
|
|
|
2024-03-08 09:16:19 +01:00
|
|
|
if($cache && ($now - $cache->time_retrieved) < (24*3600)) {
|
|
|
|
// Return from cache.
|
|
|
|
$quote = json_decode($cache->content,true);
|
|
|
|
return $quote;
|
2023-11-20 10:53:48 +01:00
|
|
|
} else {
|
2024-03-08 09:16:19 +01:00
|
|
|
|
|
|
|
$range = $book .str_pad($ch,3,'0',STR_PAD_LEFT) . str_pad($verse,3,'0',STR_PAD_LEFT);
|
|
|
|
|
|
|
|
if(!empty($book_end) && !empty($ch_end) && !empty($verse_end)) {
|
|
|
|
$range .= "-" . $book_end .str_pad($ch_end,3,'0',STR_PAD_LEFT) . str_pad($verse_end,3,'0',STR_PAD_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lang == "nl") {
|
|
|
|
$url = "https://www.jw.org/nl/bibliotheek/bijbel/nwt/boeken/json/html/{$range}";
|
|
|
|
} else { // if $lang == 'en'
|
|
|
|
$url = "https://www.jw.org/en/library/bible/nwt/books/json/html/{$range}";
|
|
|
|
}
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_SSH_COMPRESSION, true);
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
|
|
|
|
$o = json_decode($result,true);
|
|
|
|
if(isset($o["ranges"]) && isset($o["ranges"][$range])) {
|
|
|
|
$r = $o["ranges"][$range];
|
|
|
|
|
|
|
|
$quote = [
|
|
|
|
"heading" => $r["citation"],
|
|
|
|
"quote" => preg_replace('#<a.*?>(.*?)</a>#i', '\1', $r["html"]),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Store bible verse into cache.
|
|
|
|
$quotejson = json_encode($quote);
|
|
|
|
if($cache) {
|
|
|
|
$cache->time_retrieved = time();
|
|
|
|
$cache->content = $quotejson;
|
|
|
|
$DB->update_record($TABLE,$cache);
|
|
|
|
} else {
|
|
|
|
$cache = (object)$search;
|
|
|
|
$cache->time_retrieved = time();
|
|
|
|
$cache->content = $quotejson;
|
|
|
|
$DB->insert_record($TABLE,$cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $quote;
|
|
|
|
} else {
|
|
|
|
throw new \moodle_exception("Unexpected JSON code",'','',null,$result);
|
|
|
|
}
|
2023-11-20 10:53:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|