From 9a730b7a9a970004eaa3c3a69a0f8dc43a67e488 Mon Sep 17 00:00:00 2001 From: prasanna-lmsace <57126778+prasanna-lmsace@users.noreply.github.com> Date: Tue, 15 Oct 2024 20:41:05 +0530 Subject: [PATCH 1/4] Improvement: Add support for Moodle 4.5 --- classes/text_filter.php | 248 ++++++++++++++++++++++++++++++++++++++++ filter.php | 219 +---------------------------------- version.php | 4 +- 3 files changed, 252 insertions(+), 219 deletions(-) create mode 100644 classes/text_filter.php diff --git a/classes/text_filter.php b/classes/text_filter.php new file mode 100644 index 0000000..dcc7a38 --- /dev/null +++ b/classes/text_filter.php @@ -0,0 +1,248 @@ +. + +/** + * Siyavula filter base. + */ + +namespace filter_siyavula; + +require_once($CFG->dirroot . '/filter/siyavula/lib.php'); + +use filter_siyavula\renderables\practice_activity_renderable; +use filter_siyavula\renderables\standalone_activity_renderable; +use filter_siyavula\renderables\standalone_list_activity_renderable; +use filter_siyavula\renderables\assignment_activity_renderable; + +if (class_exists('\core_filters\text_filter')) { + class_alias('\core_filters\text_filter', 'siyavula_moodle_text_filter'); +} else { + class_alias('\moodle_text_filter', 'siyavula_moodle_text_filter'); +} + +/** + * Siyavula text filter. + */ +class text_filter extends \siyavula_moodle_text_filter { + + public function get_activity_type($text) { + if (strpos($text, '[[syp') !== false) { + $activitytype = 'practice'; + } else if (strpos($text, '[[sya') !== false) { + $activitytype = 'assignment'; + } else if (strpos($text, '[[sy') !== false) { + if (strpos($text, ',') == true) { + $activitytype = 'standaloneList'; + } else { + $activitytype = 'standalone'; + } + } else { + $activitytype = null; + } + return $activitytype; + } + + public function parse_filter_text($text) { + // Get the text + if (preg_match('/\[\[(.*?)\]\]/', $text, $matches)) { + $text = $matches[1]; + } else { + $text = ""; + } + // Strip whitespace. + $text = preg_replace("/\s+/", "", $text); + // Strip "sy-" and "syp-" identifiers. + $text = str_replace("sy-", "", $text); + $text = str_replace("syp-", "", $text); + $text = str_replace("sya-", "", $text); + // Convert filter string to array. + $textarray = explode(",", $text); + + // Parse the text into an array with the structure + // [[template_id,random_seed(optional)]] + // i.e: [[1220, 458724], [1221]]. + $templatelist = []; + foreach ($textarray as $key => $item) { + if (strpos($text, '|') == true) { + $item = explode("|", $item); + // Strip all non-numeric characters. + $item[0] = preg_replace('/[^0-9]/', '', $item[0]); + $item[1] = preg_replace('/[^0-9]/', '', $item[1]); + // Convert to integer. + $item[0] = (int)$item[0]; + $item[1] = (int)$item[1]; + } else { + // Strip all non-numeric characters. + $item = preg_replace('/[^0-9]/', '', $item); + // Convert to integer. + $item = [(int)$item]; + } + + array_push($templatelist, $item); + } + + return $templatelist; + } + + public function get_standalone_activity_data($text) { + $templatelist = $this->parse_filter_text($text)[0]; + $templateid = $templatelist[0]; + $randomseed = (isset($templatelist[1]) ? $templatelist[1] : rand(1, 99999)); + + return array($templateid, $randomseed); + } + + public function get_standalone_list_activity_data($text) { + return $this->parse_filter_text($text); + } + + public function get_practice_activity_data($text) { + $templatelist = $this->parse_filter_text($text)[0]; + $sectionid = $templatelist[0]; + + return $sectionid; + } + + public function get_assignment_activity_data($text) { + $templatelist = $this->parse_filter_text($text)[0]; + $assignmentid = $templatelist[0]; + + return $assignmentid; + } + + + public function filter($text, array $options = array()) { + + global $OUTPUT, $USER, $PAGE, $CFG, $DB; + + // Verify if user not authenticated. + $userauth = false; + if (isguestuser() || $USER == null) { + $userauth = true; + header('Location: ' . $CFG->wwwroot . '/login/index.php'); + exit(); + } + + $activitytype = $this->get_activity_type($text); + if (!$activitytype) { + return $text; + } + + $clientip = $_SERVER['REMOTE_ADDR']; + $siyavulaconfig = get_config('filter_siyavula'); + $token = siyavula_get_user_token($siyavulaconfig, $clientip); + $usertoken = siyavula_get_external_user_token($siyavulaconfig, $clientip, $token); + $showbtnretry = $siyavulaconfig->showretry; + $showlivepreview = $siyavulaconfig->showlivepreview; + $baseurl = $siyavulaconfig->url_base; + + + $result = ''; + + if ($activitytype == 'standalone') { + list($templateid, $randomseed) = $this->get_standalone_activity_data($text); + + $renderer = $PAGE->get_renderer('filter_siyavula'); + $activityrenderable = new standalone_activity_renderable(); + $activityrenderable->wwwroot = $CFG->wwwroot; + $activityrenderable->baseurl = $baseurl; + $activityrenderable->showlivepreview = $showlivepreview; + $activityrenderable->token = $token; + $activityrenderable->usertoken = $usertoken->token; + $activityrenderable->activitytype = $activitytype; + $activityrenderable->templateid = $templateid; + $activityrenderable->randomseed = $randomseed; + + $result .= $renderer->render_standalone_activity($activityrenderable); + } else if ($activitytype == 'standaloneList') { + $templatelist = $this->get_standalone_list_activity_data($text); + + $renderer = $PAGE->get_renderer('filter_siyavula'); + $activityrenderable = new standalone_activity_renderable(); + $activityrenderable->wwwroot = $CFG->wwwroot; + $activityrenderable->baseurl = $baseurl; + $activityrenderable->showlivepreview = $showlivepreview; + $activityrenderable->token = $token; + $activityrenderable->usertoken = $usertoken->token; + $activityrenderable->activitytype = $activitytype; + $activityrenderable->templatelist = json_encode($templatelist); + + $result .= $renderer->render_standalone_activity($activityrenderable); + } else if ($activitytype == 'practice') { + $sectionid = $this->get_practice_activity_data($text); + + $renderer = $PAGE->get_renderer('filter_siyavula'); + $activityrenderable = new practice_activity_renderable(); + $activityrenderable->wwwroot = $CFG->wwwroot; + $activityrenderable->baseurl = $baseurl; + $activityrenderable->showlivepreview = $showlivepreview; + $activityrenderable->token = $token; + $activityrenderable->usertoken = $usertoken->token; + $activityrenderable->activitytype = $activitytype; + $activityrenderable->sectionid = $sectionid; + + $result .= $renderer->render_practice_activity($activityrenderable); + } else if ($activitytype == 'assignment') { + $assignmentid = $this->get_assignment_activity_data($text); + + $renderer = $PAGE->get_renderer('filter_siyavula'); + $activityrenderable = new assignment_activity_renderable(); + $activityrenderable->wwwroot = $CFG->wwwroot; + $activityrenderable->baseurl = $baseurl; + $activityrenderable->showlivepreview = $showlivepreview; + $activityrenderable->token = $token; + $activityrenderable->usertoken = $usertoken->token; + $activityrenderable->activitytype = $activitytype; + $activityrenderable->assignmentid = $assignmentid; + + $result .= $renderer->render_assignment_activity($activityrenderable); + } + + // TODO: Refactor this (LC) + // Strip HTML + $newtext = strip_tags($text); + if ($activitytype == 'practice') { + $re = '/\[{2}[syp\-\d{1,},?|]*\]{2}/m'; + } else if ($activitytype == 'assignment') { + $re = '/\[{2}[sya\-\d{1,},?|]*\]{2}/m'; + } else { + $re = '/\[{2}[sy\-\d{1,},?|]*\]{2}/m'; + } + // Find the [[sy{p}-]] filter + preg_match_all($re, $newtext, $matches); + // Define the correct match + $text_to_replace_render = $matches[0][0]; + // Replace the raw filter text with the question's HTML + $result = str_replace($text_to_replace_render, $result, $text); + + // Render questions not apply format siyavula. + if (!empty($result)) { + + // Current version is Moodle 3.9 or higher use the event types. Otherwise use the older versions. + if ($CFG->version >= 2022041904) { + $PAGE->requires->js_call_amd('filter_siyavula/initmathjax', 'init'); + } else { + $PAGE->requires->js_call_amd('filter_siyavula/initmathjax-backward', 'init'); + } + + return $result; + + } else { + return $text; + } + + } +} diff --git a/filter.php b/filter.php index 474abd4..d6494d0 100644 --- a/filter.php +++ b/filter.php @@ -14,220 +14,5 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -require_once($CFG->dirroot . '/filter/siyavula/lib.php'); - -use filter_siyavula\renderables\practice_activity_renderable; -use filter_siyavula\renderables\standalone_activity_renderable; -use filter_siyavula\renderables\standalone_list_activity_renderable; -use filter_siyavula\renderables\assignment_activity_renderable; - -class filter_siyavula extends moodle_text_filter { - - public function get_activity_type($text) { - if (strpos($text, '[[syp') !== false) { - $activitytype = 'practice'; - } else if (strpos($text, '[[sya') !== false) { - $activitytype = 'assignment'; - } else if (strpos($text, '[[sy') !== false) { - if (strpos($text, ',') == true) { - $activitytype = 'standaloneList'; - } else { - $activitytype = 'standalone'; - } - } else { - $activitytype = null; - } - return $activitytype; - } - - public function parse_filter_text($text) { - // Get the text - if (preg_match('/\[\[(.*?)\]\]/', $text, $matches)) { - $text = $matches[1]; - } else { - $text = ""; - } - // Strip whitespace. - $text = preg_replace("/\s+/", "", $text); - // Strip "sy-" and "syp-" identifiers. - $text = str_replace("sy-", "", $text); - $text = str_replace("syp-", "", $text); - $text = str_replace("sya-", "", $text); - // Convert filter string to array. - $textarray = explode(",", $text); - - // Parse the text into an array with the structure - // [[template_id,random_seed(optional)]] - // i.e: [[1220, 458724], [1221]]. - $templatelist = []; - foreach ($textarray as $key => $item) { - if (strpos($text, '|') == true) { - $item = explode("|", $item); - // Strip all non-numeric characters. - $item[0] = preg_replace('/[^0-9]/', '', $item[0]); - $item[1] = preg_replace('/[^0-9]/', '', $item[1]); - // Convert to integer. - $item[0] = (int)$item[0]; - $item[1] = (int)$item[1]; - } else { - // Strip all non-numeric characters. - $item = preg_replace('/[^0-9]/', '', $item); - // Convert to integer. - $item = [(int)$item]; - } - - array_push($templatelist, $item); - } - - return $templatelist; - } - - public function get_standalone_activity_data($text) { - $templatelist = $this->parse_filter_text($text)[0]; - $templateid = $templatelist[0]; - $randomseed = (isset($templatelist[1]) ? $templatelist[1] : rand(1, 99999)); - - return array($templateid, $randomseed); - } - - public function get_standalone_list_activity_data($text) { - return $this->parse_filter_text($text); - } - - public function get_practice_activity_data($text) { - $templatelist = $this->parse_filter_text($text)[0]; - $sectionid = $templatelist[0]; - - return $sectionid; - } - - public function get_assignment_activity_data($text) { - $templatelist = $this->parse_filter_text($text)[0]; - $assignmentid = $templatelist[0]; - - return $assignmentid; - } - - - public function filter($text, array $options = array()) { - - global $OUTPUT, $USER, $PAGE, $CFG, $DB; - - // Verify if user not authenticated. - $userauth = false; - if (isguestuser() || $USER == null) { - $userauth = true; - header('Location: ' . $CFG->wwwroot . '/login/index.php'); - exit(); - } - - $activitytype = $this->get_activity_type($text); - if (!$activitytype) { - return $text; - } - - $clientip = $_SERVER['REMOTE_ADDR']; - $siyavulaconfig = get_config('filter_siyavula'); - $token = siyavula_get_user_token($siyavulaconfig, $clientip); - $usertoken = siyavula_get_external_user_token($siyavulaconfig, $clientip, $token); - $showbtnretry = $siyavulaconfig->showretry; - $showlivepreview = $siyavulaconfig->showlivepreview; - $baseurl = $siyavulaconfig->url_base; - - - $result = ''; - - if ($activitytype == 'standalone') { - list($templateid, $randomseed) = $this->get_standalone_activity_data($text); - - $renderer = $PAGE->get_renderer('filter_siyavula'); - $activityrenderable = new standalone_activity_renderable(); - $activityrenderable->wwwroot = $CFG->wwwroot; - $activityrenderable->baseurl = $baseurl; - $activityrenderable->showlivepreview = $showlivepreview; - $activityrenderable->token = $token; - $activityrenderable->usertoken = $usertoken->token; - $activityrenderable->activitytype = $activitytype; - $activityrenderable->templateid = $templateid; - $activityrenderable->randomseed = $randomseed; - - $result .= $renderer->render_standalone_activity($activityrenderable); - } else if ($activitytype == 'standaloneList') { - $templatelist = $this->get_standalone_list_activity_data($text); - - $renderer = $PAGE->get_renderer('filter_siyavula'); - $activityrenderable = new standalone_activity_renderable(); - $activityrenderable->wwwroot = $CFG->wwwroot; - $activityrenderable->baseurl = $baseurl; - $activityrenderable->showlivepreview = $showlivepreview; - $activityrenderable->token = $token; - $activityrenderable->usertoken = $usertoken->token; - $activityrenderable->activitytype = $activitytype; - $activityrenderable->templatelist = json_encode($templatelist); - - $result .= $renderer->render_standalone_activity($activityrenderable); - } else if ($activitytype == 'practice') { - $sectionid = $this->get_practice_activity_data($text); - - $renderer = $PAGE->get_renderer('filter_siyavula'); - $activityrenderable = new practice_activity_renderable(); - $activityrenderable->wwwroot = $CFG->wwwroot; - $activityrenderable->baseurl = $baseurl; - $activityrenderable->showlivepreview = $showlivepreview; - $activityrenderable->token = $token; - $activityrenderable->usertoken = $usertoken->token; - $activityrenderable->activitytype = $activitytype; - $activityrenderable->sectionid = $sectionid; - - $result .= $renderer->render_practice_activity($activityrenderable); - } else if ($activitytype == 'assignment') { - $assignmentid = $this->get_assignment_activity_data($text); - - $renderer = $PAGE->get_renderer('filter_siyavula'); - $activityrenderable = new assignment_activity_renderable(); - $activityrenderable->wwwroot = $CFG->wwwroot; - $activityrenderable->baseurl = $baseurl; - $activityrenderable->showlivepreview = $showlivepreview; - $activityrenderable->token = $token; - $activityrenderable->usertoken = $usertoken->token; - $activityrenderable->activitytype = $activitytype; - $activityrenderable->assignmentid = $assignmentid; - - $result .= $renderer->render_assignment_activity($activityrenderable); - } - - // TODO: Refactor this (LC) - // Strip HTML - $newtext = strip_tags($text); - if ($activitytype == 'practice') { - $re = '/\[{2}[syp\-\d{1,},?|]*\]{2}/m'; - } else if ($activitytype == 'assignment') { - $re = '/\[{2}[sya\-\d{1,},?|]*\]{2}/m'; - } else { - $re = '/\[{2}[sy\-\d{1,},?|]*\]{2}/m'; - } - // Find the [[sy{p}-]] filter - preg_match_all($re, $newtext, $matches); - // Define the correct match - $text_to_replace_render = $matches[0][0]; - // Replace the raw filter text with the question's HTML - $result = str_replace($text_to_replace_render, $result, $text); - - // Render questions not apply format siyavula. - if (!empty($result)) { - - // Current version is Moodle 3.9 or higher use the event types. Otherwise use the older versions. - if ($CFG->version >= 2022041904) { - $PAGE->requires->js_call_amd('filter_siyavula/initmathjax', 'init'); - } else { - $PAGE->requires->js_call_amd('filter_siyavula/initmathjax-backward', 'init'); - } - - return $result; - - } else { - return $text; - } - - } -} +// Make backwards compatibility with Moodle 4.4 and below. +class_alias(\filter_siyavula\text_filter::class, \filter_siyavula::class); diff --git a/version.php b/version.php index cd6ca9a..4f04fdf 100644 --- a/version.php +++ b/version.php @@ -1,9 +1,9 @@ version = 2023121301; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2023121302; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2019111809; // Requires this Moodle version $plugin->component = 'filter_siyavula'; // Full name of the plugin (used for diagnostics) $plugin->maturity = MATURITY_STABLE; -$plugin->supported = [38, 444]; +$plugin->supported = [38, 405]; $plugin->release = '1.3.0'; From f625c1359f8f242f13c7b569e4c7830432c3ca6e Mon Sep 17 00:00:00 2001 From: prasanna-lmsace <57126778+prasanna-lmsace@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:27:29 +0530 Subject: [PATCH 2/4] Improve backward support for Moodle 3.11. --- classes/text_filter.php | 5 ++--- version.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/classes/text_filter.php b/classes/text_filter.php index dcc7a38..fe7d04d 100644 --- a/classes/text_filter.php +++ b/classes/text_filter.php @@ -230,9 +230,8 @@ public function filter($text, array $options = array()) { // Render questions not apply format siyavula. if (!empty($result)) { - - // Current version is Moodle 3.9 or higher use the event types. Otherwise use the older versions. - if ($CFG->version >= 2022041904) { + // Current version is Moodle 4.0 or higher use the event types. Otherwise use the older versions. + if ($CFG->version >= 2022041912) { $PAGE->requires->js_call_amd('filter_siyavula/initmathjax', 'init'); } else { $PAGE->requires->js_call_amd('filter_siyavula/initmathjax-backward', 'init'); diff --git a/version.php b/version.php index 4f04fdf..84983a5 100644 --- a/version.php +++ b/version.php @@ -1,7 +1,7 @@ version = 2023121302; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2023121303; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2019111809; // Requires this Moodle version $plugin->component = 'filter_siyavula'; // Full name of the plugin (used for diagnostics) $plugin->maturity = MATURITY_STABLE; From 80472bfa8b8347679579fc52e45bd92aea7c5a99 Mon Sep 17 00:00:00 2001 From: prasanna-lmsace <57126778+prasanna-lmsace@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:39:11 +0530 Subject: [PATCH 3/4] Update version to align with current year --- version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.php b/version.php index 84983a5..54c7856 100644 --- a/version.php +++ b/version.php @@ -1,7 +1,7 @@ version = 2023121303; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2024102200; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2019111809; // Requires this Moodle version $plugin->component = 'filter_siyavula'; // Full name of the plugin (used for diagnostics) $plugin->maturity = MATURITY_STABLE; From 248927782ddcc5184cd6ed45d8b4f935aa215659 Mon Sep 17 00:00:00 2001 From: Lehan Coetzee Date: Wed, 23 Oct 2024 09:18:42 +0200 Subject: [PATCH 4/4] Update supported Moodle versions and plugin release --- version.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version.php b/version.php index 54c7856..4342487 100644 --- a/version.php +++ b/version.php @@ -5,5 +5,5 @@ $plugin->requires = 2019111809; // Requires this Moodle version $plugin->component = 'filter_siyavula'; // Full name of the plugin (used for diagnostics) $plugin->maturity = MATURITY_STABLE; -$plugin->supported = [38, 405]; -$plugin->release = '1.3.0'; +$plugin->supported = [311, 405]; +$plugin->release = '1.4.0';