moodle-filter_bibleversesnwt/classes/bibleservice.php

98 lines
3.8 KiB
PHP

<?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) {
$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];
return [
"heading" => $r["citation"],
"quote" => preg_replace('#<a.*?>(.*?)</a>#i', '\1', $r["html"]),
];
} else {
throw new \moodle_exception("Unexpected JSON code",'','',null,$result);
}
}
}