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