diff --git a/amd/build/filter_bibleversesnwt.min.js b/amd/build/filter_bibleversesnwt.min.js new file mode 100644 index 0000000..562b6c1 --- /dev/null +++ b/amd/build/filter_bibleversesnwt.min.js @@ -0,0 +1,3 @@ +define("filter_bibleversesnwt/filter_bibleversesnwt",["exports","core/ajax","core/modal","core/modal_factory","core/modal_events","core/notification"],(function(_exports,_ajax,_modal,_modal_factory,_modal_events,_notification){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function bible_link(event){const target=event.target,info=JSON.parse(target.getAttribute("data-verseinfo")),bibleblock_heading=document.querySelector("#block_bibleblock_heading"),bibleblock_quote=document.querySelector("#block_bibleblock_content");return bibleblock_quote&&(bibleblock_heading&&(bibleblock_heading.innerHTML=""),bibleblock_quote.innerHTML='
'),(0,_ajax.call)([{methodname:"filter_bibleversesnwt_get_biblequote",args:{lang:info.lang,book:info.book,ch:info.ch,verse:info.verse,book_end:info.book,ch_end:info.ch,verse_end:info.verse_end}}])[0].then((quote=>{var modalconfig;console.info("Got bible quote",quote),bibleblock_quote?(console.info("Attempting to place in verses block",bibleblock_heading,bibleblock_quote),bibleblock_heading&&(bibleblock_heading.innerHTML=quote.heading),bibleblock_quote.innerHTML=quote.quote):(console.info("Attempting to place in new modal"),(modalconfig={title:quote.heading,body:quote.quote},_modal.default.create?_modal.default.create(modalconfig):_modal_factory.default.create(modalconfig)).then((modal=>(modal.getRoot().on(_modal_events.default.hidden,(()=>{modal.destroy()})),modal.show(),modal))).catch(_notification.default.exception))})).catch((error=>{console.error("Error retrieving bible quote:",error),window.open(target.getAttribute("href"),"_bible_","width=500,height=800")})),event.preventDefault(),!1}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=function(){if(!initialized){initialized=!0;const elements=document.querySelectorAll("a.bible-link[data-verseinfo]");console.info("Found elements",elements),elements.forEach((el=>{console.info("Handling element",el),el.addEventListener("click",bible_link)}))}},_modal=_interopRequireDefault(_modal),_modal_factory=_interopRequireDefault(_modal_factory),_modal_events=_interopRequireDefault(_modal_events),_notification=_interopRequireDefault(_notification);let initialized=!1})); + +//# sourceMappingURL=filter_bibleversesnwt.min.js.map \ No newline at end of file diff --git a/amd/build/filter_bibleversesnwt.min.js.map b/amd/build/filter_bibleversesnwt.min.js.map new file mode 100644 index 0000000..7e64a2c --- /dev/null +++ b/amd/build/filter_bibleversesnwt.min.js.map @@ -0,0 +1 @@ +{"version":3,"file":"filter_bibleversesnwt.min.js","sources":["../src/filter_bibleversesnwt.js"],"sourcesContent":["/*eslint no-var: \"error\" */\n/*eslint no-unused-vars: \"off\" */\n/*eslint no-console: \"off\" */\n/*eslint linebreak-style: \"off\" */\n/*eslint no-trailing-spaces: \"off\" */\n/*eslint-env es6*/\n// Put this file in path/to/plugin/amd/src\n// You can call it anything you like\nimport {call} from 'core/ajax';\nimport Modal from 'core/modal';\nimport ModalFactory from 'core/modal_factory';\nimport ModalEvents from 'core/modal_events';\nimport Notification from 'core/notification';\n\n/**\n * \n * @param {*} modalconfig \n * @returns Promise of modal\n */\nfunction modalcreate(modalconfig) {\n if(Modal.create) {\n return Modal.create(modalconfig);\n } else {\n return ModalFactory.create(modalconfig);\n }\n}\n/**\n * \n * @param {*} lang \n * @param {*} book \n * @param {*} ch \n * @param {*} verse \n * @param {*} book_end \n * @param {*} ch_end \n * @param {*} verse_end \n */\nfunction getBibleQuote(lang,book,ch,verse,book_end, ch_end, verse_end) {\n return new Promise((resolve, reject) => {\n let range = \"00000000\";\n let url = \"\";\n\n range = book.toString() + ch.toString().padStart(3,'0') + verse.toString().padStart(3,'0');\n\n if(book_end && ch_end && verse_end) {\n range = range + \"-\" + book_end.toString() + ch_end.toString().padStart(3,'0') + verse_end.toString().padStart(3,'0');\n }\n\n if (lang == \"nl\") {\n url = `https://www.jw.org/nl/bibliotheek/bijbel/nwt/boeken/json/html/${range}`;\n } else { // if $lang == 'en'\n url = `https://www.jw.org/en/library/bible/nwt/books/json/html/${range}`;\n }\n\n window.fetch(url).then((response) => {\n try {\n const o = JSON.parse();\n resolve(o.ranges[range]);\n } catch (error) {\n reject(\"Error parsing json response\",error, response);\n }\n }).catch((error) => {\n reject(\"Error retrieving url\",error);\n });\n });\n}\n\n/**\n * \n * @param {*} event \n */\nfunction bible_link(event) {\n const target = event.target;\n const info = JSON.parse(target.getAttribute(\"data-verseinfo\"));\n const bibleblock_heading = document.querySelector(\"#block_bibleblock_heading\");\n const bibleblock_quote = document.querySelector(\"#block_bibleblock_content\");\n if( bibleblock_quote ) {\n if(bibleblock_heading) {\n bibleblock_heading.innerHTML = \"\";\n }\n bibleblock_quote.innerHTML = ``;\n }\n call([{\n methodname: 'filter_bibleversesnwt_get_biblequote',\n args: { \n lang: info.lang,\n book: info.book,\n ch: info.ch,\n verse: info.verse,\n book_end: info.book,\n ch_end: info.ch,\n verse_end: info.verse_end}\n }])[0].then((quote) => {\n console.info(\"Got bible quote\",quote);\n if( bibleblock_quote ) {\n console.info(\"Attempting to place in verses block\",bibleblock_heading,bibleblock_quote);\n if(bibleblock_heading) {\n bibleblock_heading.innerHTML = quote.heading;\n }\n bibleblock_quote.innerHTML = quote.quote;\n } else {\n console.info(\"Attempting to place in new modal\",);\n modalcreate({\n title: quote.heading,\n body: quote.quote,\n }).then(modal => {\n // Handle hidden event.\n modal.getRoot().on(ModalEvents.hidden, () => {\n // Destroy when hidden.\n modal.destroy();\n });\n // Show the modal.\n modal.show();\n return modal;\n })\n .catch(Notification.exception);\n\n }\n }).catch((error) => {\n console.error(\"Error retrieving bible quote:\", error);\n window.open(target.getAttribute(\"href\"),\"_bible_\",\"width=500,height=800\");\n });\n\n event.preventDefault();\n return false;\n}\n\n/**\n * \n */\nlet initialized = false;\n/**\n * Initialize the Page\n */\nexport function init() {\n // Make sure this function is only called once on the page. May be called multiple times because of filter weirdness\n if (!initialized) {\n initialized = true;\n\n const elements = document.querySelectorAll(\"a.bible-link[data-verseinfo]\");\n console.info(\"Found elements\",elements);\n elements.forEach((el) => {\n console.info(\"Handling element\",el);\n el.addEventListener(\"click\", bible_link);\n });\n }\n}\n\n\n"],"names":["bible_link","event","target","info","JSON","parse","getAttribute","bibleblock_heading","document","querySelector","bibleblock_quote","innerHTML","methodname","args","lang","book","ch","verse","book_end","ch_end","verse_end","then","quote","modalconfig","console","heading","title","body","Modal","create","ModalFactory","modal","getRoot","on","ModalEvents","hidden","destroy","show","catch","Notification","exception","error","window","open","preventDefault","initialized","elements","querySelectorAll","forEach","el","addEventListener"],"mappings":"8TAsESA,WAAWC,aACVC,OAASD,MAAMC,OACfC,KAAOC,KAAKC,MAAMH,OAAOI,aAAa,mBACtCC,mBAAqBC,SAASC,cAAc,6BAC5CC,iBAAmBF,SAASC,cAAc,oCAC5CC,mBACGH,qBACCA,mBAAmBI,UAAY,IAEnCD,iBAAiBC,UAAa,+DAE7B,CAAC,CACFC,WAAY,uCACZC,KAAM,CACFC,KAAMX,KAAKW,KACXC,KAAMZ,KAAKY,KACXC,GAAIb,KAAKa,GACTC,MAAOd,KAAKc,MACZC,SAAUf,KAAKY,KACfI,OAAQhB,KAAKa,GACbI,UAAWjB,KAAKiB,cACpB,GAAGC,MAAMC,YAxEIC,YAyEbC,QAAQrB,KAAK,kBAAkBmB,OAC3BZ,kBACAc,QAAQrB,KAAK,sCAAsCI,mBAAmBG,kBACnEH,qBACCA,mBAAmBI,UAAYW,MAAMG,SAEzCf,iBAAiBC,UAAYW,MAAMA,QAEnCE,QAAQrB,KAAK,qCAjFJoB,YAkFG,CACRG,MAAOJ,MAAMG,QACbE,KAAML,MAAMA,OAnFrBM,eAAMC,OACED,eAAMC,OAAON,aAEbO,uBAAaD,OAAON,cAiFpBF,MAAKU,QAEJA,MAAMC,UAAUC,GAAGC,sBAAYC,QAAQ,KAEnCJ,MAAMK,aAGVL,MAAMM,OACCN,SAEVO,MAAMC,sBAAaC,eAGzBF,OAAOG,QACNjB,QAAQiB,MAAM,gCAAiCA,OAC/CC,OAAOC,KAAKzC,OAAOI,aAAa,QAAQ,UAAU,2BAGtDL,MAAM2C,kBACC,uFAYFC,YAAa,CACdA,aAAc,QAERC,SAAWtC,SAASuC,iBAAiB,gCAC3CvB,QAAQrB,KAAK,iBAAiB2C,UAC9BA,SAASE,SAASC,KACdzB,QAAQrB,KAAK,mBAAmB8C,IAChCA,GAAGC,iBAAiB,QAASlD,yNAbrC6C,aAAc"} \ No newline at end of file diff --git a/amd/src/filter_bibleversesnwt.js b/amd/src/filter_bibleversesnwt.js new file mode 100644 index 0000000..55abb1e --- /dev/null +++ b/amd/src/filter_bibleversesnwt.js @@ -0,0 +1,148 @@ +/*eslint no-var: "error" */ +/*eslint no-unused-vars: "off" */ +/*eslint no-console: "off" */ +/*eslint linebreak-style: "off" */ +/*eslint no-trailing-spaces: "off" */ +/*eslint-env es6*/ +// Put this file in path/to/plugin/amd/src +// You can call it anything you like +import {call} from 'core/ajax'; +import Modal from 'core/modal'; +import ModalFactory from 'core/modal_factory'; +import ModalEvents from 'core/modal_events'; +import Notification from 'core/notification'; + +/** + * + * @param {*} modalconfig + * @returns Promise of modal + */ +function modalcreate(modalconfig) { + if(Modal.create) { + return Modal.create(modalconfig); + } else { + return ModalFactory.create(modalconfig); + } +} +/** + * + * @param {*} lang + * @param {*} book + * @param {*} ch + * @param {*} verse + * @param {*} book_end + * @param {*} ch_end + * @param {*} verse_end + */ +function getBibleQuote(lang,book,ch,verse,book_end, ch_end, verse_end) { + return new Promise((resolve, reject) => { + let range = "00000000"; + let url = ""; + + range = book.toString() + ch.toString().padStart(3,'0') + verse.toString().padStart(3,'0'); + + if(book_end && ch_end && verse_end) { + range = range + "-" + book_end.toString() + ch_end.toString().padStart(3,'0') + verse_end.toString().padStart(3,'0'); + } + + 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}`; + } + + window.fetch(url).then((response) => { + try { + const o = JSON.parse(); + resolve(o.ranges[range]); + } catch (error) { + reject("Error parsing json response",error, response); + } + }).catch((error) => { + reject("Error retrieving url",error); + }); + }); +} + +/** + * + * @param {*} event + */ +function bible_link(event) { + const target = event.target; + const info = JSON.parse(target.getAttribute("data-verseinfo")); + const bibleblock_heading = document.querySelector("#block_bibleblock_heading"); + const bibleblock_quote = document.querySelector("#block_bibleblock_content"); + if( bibleblock_quote ) { + if(bibleblock_heading) { + bibleblock_heading.innerHTML = ""; + } + bibleblock_quote.innerHTML = ``; + } + call([{ + methodname: 'filter_bibleversesnwt_get_biblequote', + args: { + lang: info.lang, + book: info.book, + ch: info.ch, + verse: info.verse, + book_end: info.book, + ch_end: info.ch, + verse_end: info.verse_end} + }])[0].then((quote) => { + console.info("Got bible quote",quote); + if( bibleblock_quote ) { + console.info("Attempting to place in verses block",bibleblock_heading,bibleblock_quote); + if(bibleblock_heading) { + bibleblock_heading.innerHTML = quote.heading; + } + bibleblock_quote.innerHTML = quote.quote; + } else { + console.info("Attempting to place in new modal",); + modalcreate({ + title: quote.heading, + body: quote.quote, + }).then(modal => { + // Handle hidden event. + modal.getRoot().on(ModalEvents.hidden, () => { + // Destroy when hidden. + modal.destroy(); + }); + // Show the modal. + modal.show(); + return modal; + }) + .catch(Notification.exception); + + } + }).catch((error) => { + console.error("Error retrieving bible quote:", error); + window.open(target.getAttribute("href"),"_bible_","width=500,height=800"); + }); + + event.preventDefault(); + return false; +} + +/** + * + */ +let initialized = false; +/** + * Initialize the Page + */ +export function init() { + // Make sure this function is only called once on the page. May be called multiple times because of filter weirdness + if (!initialized) { + initialized = true; + + const elements = document.querySelectorAll("a.bible-link[data-verseinfo]"); + console.info("Found elements",elements); + elements.forEach((el) => { + console.info("Handling element",el); + el.addEventListener("click", bible_link); + }); + } +} + + diff --git a/build.php b/build.php new file mode 100644 index 0000000..4d033ac --- /dev/null +++ b/build.php @@ -0,0 +1,76 @@ +. +/** + * Build script to properly create a distribution zip + * @package block_mytreestudyplan + * @copyright 2023 P.M. Kuipers + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +define('CLI_SCRIPT', true); +require_once("../../config.php"); +$plugin = new stdClass; +require_once('version.php'); + +$a = explode("_", $plugin->component, 2); +$plugin->type = $a[0]; +$plugin->name = $a[1]; + +$excludepaths = [ + "build", // Dir for build zip files. + "build/*", + "build.*", + "vuemode.sh", + "amd/src", + "amd/src/*", + ".git", + ".git/*", + ".gitignore", + "*.zip", +]; + +// Determine some paths. +$wd = realpath(dirname(__FILE__)); +$parent = dirname($wd); +$plugindirname = basename($wd); +$builddir = $wd."/"."build"; +$zipname = $builddir."/"."{$plugin->name}-{$plugin->version}.zip"; + +// Create the exclude line. +$exclude = "-x "; +foreach ($excludepaths as $x) { + $exclude .= "'{$plugindirname}/{$x}' "; +} + +if (!is_dir($builddir)) { + mkdir($builddir); + if (!is_dir($builddir)) { + print("Cannot access dir '{$builddir}' to store zip files\n"); + exit(1); + } +} + +if (file_exists($zipfile)) { + print("Zip file '{$zipfile}' already exists. Exiting...\n"); + exit(1); +} + +$cwd = getcwd(); +chdir($parent); + +$cmd = "zip -r '{$zipname}' '{$plugindirname}' {$exclude}"; +system($cmd); + +chdir($cwd); diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..7528487 --- /dev/null +++ b/build.sh @@ -0,0 +1,14 @@ +#!/bin/bash +SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +WD=`pwd` + +# run the grunt script in the scripts working directory +cd $SCRIPTDIR +grunt amd + +# run the build php script +php ${SCRIPTDIR}/build.php $@ + + +# return to the working directory +cd $WD \ No newline at end of file diff --git a/classes/bible.php b/classes/bible.php index 5e2c456..cd6c857 100644 --- a/classes/bible.php +++ b/classes/bible.php @@ -23,10 +23,10 @@ class bible { $verse_end = ($verse_end > $verse)?$verse_end:$verse; if($this->lang == "nl"){ - return "https://wol.jw.org/nl/wol/b/r18/lp-o/nwtsty/${book}/${chapter}#v=${book}:${chapter}:${verse}-${book}:${chapter}:${verse_end}"; + return "https://wol.jw.org/nl/wol/b/r18/lp-o/nwtsty/{$book}/{$chapter}#v={$book}:{$chapter}:{$verse}-{$book}:{$chapter}:{$verse_end}"; } else { // if $this->lang == 'en' - return "https://wol.jw.org/nl/wol/b/r1/lp-e/nwtsty/${book}/${chapter}#v=${book}:${chapter}:${verse}-${book}:${chapter}:${verse_end}"; + return "https://wol.jw.org/nl/wol/b/r1/lp-e/nwtsty/{$book}/{$chapter}#v={$book}:{$chapter}:{$verse}-{$book}:{$chapter}:{$verse_end}"; } } @@ -190,15 +190,17 @@ class bible { // First check the presence of the bookname before if( ($ix = stripos($content,$bookname)) !== false){ $matches = []; - $content = preg_replace_callback($re,function ($m) use ($booknum, $bookname){ + $lang = $this->lang; + $content = preg_replace_callback($re,function ($m) use ($lang,$booknum, $bookname){ $book = strtolower(trim($m[1])); $ch = $m[2]; $verse = $m[3]; $verse_to = (count($m)>4)?$m[4]:0; if($book == $bookname){ // Check if name matches - $href = $this->wolhref($booknum,$ch,$verse,$verse_to); - return "".$m[0].""; + $href = $this->wolhref($lang, $booknum, $ch, $verse, $verse_to); + $versedata = json_encode(["lang" => $lang, "book" => $booknum, "ch" => $ch, "verse" => $verse, "verse_end" => $verse_to, "wolhref" => $href]); + return "".$m[0].""; } else { return $m[0]; } diff --git a/classes/bibleservice.php b/classes/bibleservice.php new file mode 100644 index 0000000..5c6973a --- /dev/null +++ b/classes/bibleservice.php @@ -0,0 +1,97 @@ +. +/** + * 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('#