diff --git a/lib.php b/lib.php index 1bf541d..d98eb48 100644 --- a/lib.php +++ b/lib.php @@ -13,3 +13,26 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . + +namespace local_townsquaresupport; + +defined('MOODLE_INTERNAL') || die(); + +use context_module; +use dml_exception; + +global $CFG; +require_once($CFG->dirroot . '/calendar/lib.php'); +require_once($CFG->dirroot . '/blocks/townsquare/locallib.php'); +/** + * + */ +function townsquaresupport_get_subplugin_events() { + // Get all available subplugins. + $events = []; + $subplugins = \core_plugin_manager::instance()->get_plugins_of_type('supportedmodules'); + var_dump($subplugins); + foreach ($subplugins as $subplugin) { + $events += $subplugin->get_events(); + } +} diff --git a/locallib.php b/locallib.php new file mode 100644 index 0000000..59c13e7 --- /dev/null +++ b/locallib.php @@ -0,0 +1,118 @@ +. + +/** + * Internal library of functions for the townsquare block + * + * @package block_townsquare + * @copyright 2024 Tamaro Walter + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Gets the id of all courses where the current user is enrolled + * @return array + */ +function townsquaresupport_get_courses(): array { + global $USER; + + $enrolledcourses = enrol_get_all_users_courses($USER->id, true); + $courses = []; + foreach ($enrolledcourses as $enrolledcourse) { + $courses[] = $enrolledcourse->id; + } + + return $courses; +} + +/** + * Function for subplugins to get the start time of the search. + * @return int + */ +function townsquaresupport_get_timestart(): int { + return time() - 15768000; +} + +/** + * Function for subplugins to get the end time of the search. + * @return int + */ +function townsquaresupport_get_timeend(): int { + return time() + 15768000; +} + +/** + * Merge sort function for townsquare events. + * @param $events + * @return array + */ +function townsquaresupport_mergesort($events): array { + $length = count($events); + if ($length <= 1) { + return $events; + } + $mid = (int) ($length / 2); + $left = townsquare_mergesort(array_slice($events, 0, $mid)); + $right = townsquare_mergesort(array_slice($events, $mid)); + return townsquare_merge($left, $right); +} + +/** + * Function that sorts events in descending order by time created (newest event first) + * @param array $left + * @param array $right + * @return array + */ +function townsquaresupport_merge(array $left, array $right): array { + $result = []; + reset($left); + reset($right); + $numberofelements = count($left) + count($right); + for ($i = 0; $i < $numberofelements; $i++) { + if (current($left) && current($right)) { + if (current($left)->timestart > current($right)->timestart) { + $result[$i] = current($left); + next($left); + } else { + $result[$i] = current($right); + next($right); + } + } else if (current($left)) { + $result[$i] = current($left); + next($left); + } else { + $result[$i] = current($right); + next($right); + } + } + return $result; +} + +function townsquaresupport_filter_availability($event): bool { + // If there is no restriction defined, the event is available. + if ($event->availability == null) { + return false; + } + + // If there is a restriction, check if it applies to the user. + $modinfo = get_fast_modinfo($event->courseid); + $moduleinfo = $modinfo->get_cm($event->coursemoduleid); + if ($moduleinfo->uservisible) { + return false; + } + + return true; +} diff --git a/plugininfo/supportedmodules.php b/plugininfo/supportedmodules.php new file mode 100644 index 0000000..1b56820 --- /dev/null +++ b/plugininfo/supportedmodules.php @@ -0,0 +1,22 @@ +. + + +namespace local_townsquaresupport\plugininfo; + +class supportedmodules extends \core\plugininfo\base { + +} \ No newline at end of file diff --git a/supportedmodules/tsmoodleoverflow/lang/en/tsmoodleoverflow.php b/supportedmodules/tsmoodleoverflow/lang/en/tsmoodleoverflow.php new file mode 100644 index 0000000..511feac --- /dev/null +++ b/supportedmodules/tsmoodleoverflow/lang/en/tsmoodleoverflow.php @@ -0,0 +1,29 @@ +. + +/** + * Plugin strings for the townsquare subplugin for moodleoverflow support. + * + * @package block_ts_moodleoverflow + * @category string + * @copyright 2024 Tamaro Walter + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +$string['pluginname'] = 'Moodleoverflow support for townsquare block'; +$string['pluginnameadding'] = "Adding a Moodleoverflow support subplugin"; +$string['pluginnameediting'] = "Editing a Moodleoverflow support subplugin"; +$string['pluginnamesummary'] = "This subplugin allows the townsquare block to show new posts from moodleoverflow."; +$string['pluginname_help'] = 'This subplugin allows the townsquare block to show posts from moodleoverflow.'; diff --git a/supportedmodules/tsmoodleoverflow/lib.php b/supportedmodules/tsmoodleoverflow/lib.php new file mode 100644 index 0000000..75d7ed6 --- /dev/null +++ b/supportedmodules/tsmoodleoverflow/lib.php @@ -0,0 +1,96 @@ +. + +/** + * Internal library of functions for the ts_moodleoverflow subplugin + * + * @package block_townsquare + * @copyright 2024 Tamaro Walter + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +global $CFG; +require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php'); + +/** + * Function to get the newest post from the moodleoverflow module. + * @param array $courses The courses that will searched. + * @param int $timestart The start time of the search. + * @param int $timeend The end time of the search. + * @return array + */ +function ts_moodleoverflow_get_events($courses, $timestart): array { + global $DB; + + // If moodleoverflow is not installed or not activated, return empty array. + if (!$DB->get_record('modules', ['name' => 'forum', 'visible' => 1])) { + return []; + } + + // Get posts from the database. + $posts = ts_moodleoverflow_get_post_from_db($courses, $timestart); + + // Filter posts by availability. + foreach ($posts as $post) { + if (townsquaresupport_filter_availability($post)) { + unset($posts[$post->row_num]); + } + } + return $posts; +} + +function ts_moodleoverflow_get_post_from_db($courses, $timestart): array { + global $DB; + // Prepare params for sql statement. + list($insqlcourses, $inparamscourses) = $DB->get_in_or_equal($courses, SQL_PARAMS_NAMED); + $params = ['courses' => $courses, 'timestart' => $timestart] + $inparamscourses; + + $sql = "SELECT (ROW_NUMBER() OVER (ORDER BY posts.id)) AS row_num, + 'moodleoverflow' AS modulename, + module.id AS instanceid, + module.anonymous AS anonymoussetting, + 'post' AS eventtype, + cm.id AS coursemoduleid, + cm.availability AS availability, + module.name AS instancename, + discuss.course AS courseid, + discuss.userid AS discussionuserid, + discuss.name AS discussionsubject, + u.firstname AS postuserfirstname, + u.lastname AS postuserlastname, + posts.id AS postid, + posts.discussion AS postdiscussion, + posts.parent AS postparentid, + posts.userid AS postuserid, + posts.created AS timestart, + posts.message AS postmessage + FROM {moodleoverflow_posts} posts + JOIN {moodleoverflow_discussions} discuss ON discuss.id = posts.discussion + JOIN {moodleoverflow} module ON module.id = discuss.moodleoverflow + JOIN {modules} modules ON modules.name = 'moodleoverflow' + JOIN {user} u ON u.id = posts.userid + JOIN {course_modules} cm ON (cm.course = module.course AND cm.module = modules.id AND cm.instance = module.id) + WHERE discuss.course $insqlcourses + AND posts.created > :timestart + AND cm.visible = 1 + AND modules.visible = 1 + ORDER BY posts.created DESC;"; + + return $DB->get_records_sql($sql, $params); +} + diff --git a/supportedmodules/tsmoodleoverflow/tsmoodleoverflow.php b/supportedmodules/tsmoodleoverflow/tsmoodleoverflow.php new file mode 100644 index 0000000..3f45983 --- /dev/null +++ b/supportedmodules/tsmoodleoverflow/tsmoodleoverflow.php @@ -0,0 +1,43 @@ +. + +defined('MOODLE_INTERNAL') || die; + +use local_townsquaresupport\townsquaresupportinterface; + +global $CFG; +require_once($CFG->dirroot . '/blocks/townsquare/supportedmodules/block_ts_moodleoverflow/locallib.php'); + +/** + * Plugin strings are defined here. + * + * @package block_townsquare + * @copyright 2023 Tamaro Walter + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tsmoodleoverflow implements townsquaresupportinterface { + + /** + * Function from the interface. + * @return array + */ + public function get_events(): array { + $courses = townsquaresupport_get_courses(); + $timestart = townsquaresupport_get_timestart(); + return ts_moodleoverflow_get_events($courses, $timestart); + } + +} diff --git a/supportedmodules/tsmoodleoverflow/version.php b/supportedmodules/tsmoodleoverflow/version.php new file mode 100644 index 0000000..b82b83e --- /dev/null +++ b/supportedmodules/tsmoodleoverflow/version.php @@ -0,0 +1,31 @@ +. + +/** + * Plugin version and other meta-data are defined here. + * + * @package supportedmodules_tsmoodleoverflow + * @copyright 2023 Tamaro Walter + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); +$plugin->component = 'supportedmodules_tsmoodleoverflow'; +$plugin->dependencies = ['block_townsquare' => ANY_VERSION]; +$plugin->release = '0.1.0'; +$plugin->version = 2024011503; +$plugin->requires = 2022041900; +$plugin->maturity = MATURITY_ALPHA;