Skip to content

Commit

Permalink
add plugininfo and first subplugin
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Apr 17, 2024
1 parent 6763353 commit a6f418f
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,26 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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();
}
}
118 changes: 118 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.

/**
* 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;
}
22 changes: 22 additions & 0 deletions plugininfo/supportedmodules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// 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/>.


namespace local_townsquaresupport\plugininfo;

class supportedmodules extends \core\plugininfo\base {

}
29 changes: 29 additions & 0 deletions supportedmodules/tsmoodleoverflow/lang/en/tsmoodleoverflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.

/**
* 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.';
96 changes: 96 additions & 0 deletions supportedmodules/tsmoodleoverflow/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.

/**
* 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);
}

43 changes: 43 additions & 0 deletions supportedmodules/tsmoodleoverflow/tsmoodleoverflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// 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/>.

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);
}

}
31 changes: 31 additions & 0 deletions supportedmodules/tsmoodleoverflow/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// 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/>.

/**
* 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;

0 comments on commit a6f418f

Please sign in to comment.