Skip to content

Commit 2e3498a

Browse files
authored
Merge pull request #1 from learnweb/improve/workflows
Improve/workflows
2 parents 2932c1e + 30077b5 commit 2e3498a

File tree

5 files changed

+314
-13
lines changed

5 files changed

+314
-13
lines changed

.github/workflows/moodle-ci.yml

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
name: Moodle Plugin CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
static:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
matrix:
11+
php: ['8.3']
12+
moodle-branch: ['MOODLE_404_STABLE']
13+
database: ['pgsql']
14+
15+
steps:
16+
- name: Start PostgreSQL
17+
run: docker run -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres:14
18+
19+
- name: Check out repository code
20+
uses: actions/checkout@v3
21+
with:
22+
path: plugin
23+
24+
- name: Setup PHP ${{ matrix.php }}
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: ${{ matrix.php }}
28+
ini-values: max_input_vars=5000
29+
coverage: none
30+
31+
- name: Get composer cache directory
32+
id: composer-cache
33+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
34+
35+
- name: Composer cache
36+
uses: actions/cache@v3
37+
with:
38+
path: ${{ steps.composer-cache.outputs.dir }}
39+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
40+
restore-keys: |
41+
${{ runner.os }}-composer-
42+
43+
- name: npm cache
44+
uses: actions/cache@v3
45+
with:
46+
path: ~/.npm
47+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
48+
restore-keys: |
49+
${{ runner.os }}-node-
50+
51+
- name: Initialise moodle-plugin-ci
52+
run: |
53+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
54+
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
55+
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
56+
sudo locale-gen en_AU.UTF-8
57+
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
58+
59+
- name: Install mod plugin
60+
run: moodle-plugin-ci add-plugin learnweb/moodle-mod_moodleoverflow
61+
62+
- name: Install block plugin
63+
run: moodle-plugin-ci add-plugin learnweb/moodle-block_townsquare
64+
65+
- name: Install local plugin
66+
run: moodle-plugin-ci add-plugin learnweb/moodle-local_townsquaresupport
67+
68+
- name: Install moodle-plugin-ci
69+
run: |
70+
moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1 --no-init
71+
env:
72+
DB: ${{ matrix.database }}
73+
MOODLE_BRANCH: ${{ matrix.moodle-branch }}
74+
75+
- name: PHP Lint
76+
if: ${{ always() }}
77+
run: moodle-plugin-ci phplint
78+
79+
- name: PHP Copy/Paste Detector
80+
if: ${{ always() }}
81+
run: moodle-plugin-ci phpcpd
82+
continue-on-error: true
83+
84+
- name: PHP Mess Detector
85+
if: ${{ always() }}
86+
run: moodle-plugin-ci phpmd
87+
88+
- name: Moodle Code Checker
89+
if: ${{ always() }}
90+
run: moodle-plugin-ci codechecker
91+
92+
- name: Moodle PHPDoc Checker
93+
if: ${{ always() }}
94+
run: moodle-plugin-ci phpdoc
95+
continue-on-error: true
96+
97+
- name: Validating
98+
if: ${{ always() }}
99+
run: moodle-plugin-ci validate
100+
101+
- name: Check upgrade savepoints
102+
if: ${{ always() }}
103+
run: moodle-plugin-ci savepoints
104+
105+
- name: Mustache Lint
106+
if: ${{ always() }}
107+
run: moodle-plugin-ci mustache
108+
continue-on-error: true
109+
110+
- name: Grunt
111+
if: ${{ always() }}
112+
run: moodle-plugin-ci grunt
113+
continue-on-error: true
114+
115+
test:
116+
runs-on: ubuntu-latest
117+
needs: static
118+
119+
strategy:
120+
fail-fast: false
121+
matrix:
122+
php: ['8.0', '8.1', '8.2', '8.3']
123+
moodle-branch: ['MOODLE_401_STABLE', 'MOODLE_402_STABLE', 'MOODLE_403_STABLE', 'MOODLE_404_STABLE']
124+
database: ['mariadb', 'pgsql']
125+
exclude:
126+
- php: '8.0'
127+
moodle-branch: 'MOODLE_404_STABLE'
128+
- php: '8.2'
129+
moodle-branch: 'MOODLE_401_STABLE'
130+
- php: '8.3'
131+
moodle-branch: 'MOODLE_401_STABLE'
132+
- php: '8.3'
133+
moodle-branch: 'MOODLE_402_STABLE'
134+
- php: '8.3'
135+
moodle-branch: 'MOODLE_403_STABLE'
136+
include:
137+
- php: '7.4'
138+
moodle-branch: 'MOODLE_401_STABLE'
139+
database: 'pgsql'
140+
- php: '7.4'
141+
moodle-branch: 'MOODLE_401_STABLE'
142+
database: 'mariadb'
143+
144+
steps:
145+
- name: Start MariaDB
146+
if: matrix.database == 'mariadb'
147+
run: docker run -p 3306:3306 -e MYSQL_USER=root -e MYSQL_ALLOW_EMPTY_PASSWORD=true -d mariadb:10
148+
149+
- name: Start PostgreSQL
150+
if: matrix.database == 'pgsql'
151+
run: docker run -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres:14
152+
153+
- name: Check out repository code
154+
uses: actions/checkout@v3
155+
with:
156+
path: plugin
157+
158+
- name: Setup PHP ${{ matrix.php }}
159+
uses: shivammathur/setup-php@v2
160+
with:
161+
php-version: ${{ matrix.php }}
162+
ini-values: max_input_vars=5000
163+
coverage: none
164+
165+
- name: Get composer cache directory
166+
id: composer-cache
167+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
168+
- name: Composer cache
169+
uses: actions/cache@v3
170+
with:
171+
path: ${{ steps.composer-cache.outputs.dir }}
172+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
173+
restore-keys: |
174+
${{ runner.os }}-composer-
175+
- name: npm cache
176+
uses: actions/cache@v3
177+
with:
178+
path: ~/.npm
179+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
180+
restore-keys: |
181+
${{ runner.os }}-node-
182+
183+
- name: Initialise moodle-plugin-ci
184+
run: |
185+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
186+
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
187+
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
188+
sudo locale-gen en_AU.UTF-8
189+
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV
190+
191+
- name: Install moodle-plugin-ci
192+
run: |
193+
moodle-plugin-ci add-plugin learnweb/moodle-local_townsquaresupport
194+
moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
195+
env:
196+
DB: ${{ matrix.database }}
197+
MOODLE_BRANCH: ${{ matrix.moodle-branch }}
198+
199+
- name: PHPUnit tests
200+
if: ${{ always() }}
201+
run: moodle-plugin-ci phpunit
202+
203+
- name: Behat features
204+
if: ${{ always() }}
205+
run: moodle-plugin-ci behat --auto-rerun 0

.github/workflows/moodle-release.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#
2+
# Whenever a new tag starting with "v" is pushed, add the tagged version
3+
# to the Moodle Plugins directory at https://moodle.org/plugins
4+
#
5+
# revision: 2021070201
6+
# Changed to be released on Github release with the release notes.
7+
#
8+
name: Releasing in the Plugins directory
9+
10+
on:
11+
release:
12+
types: [published]
13+
14+
defaults:
15+
run:
16+
shell: bash
17+
18+
jobs:
19+
release-at-moodle-org:
20+
runs-on: ubuntu-latest
21+
env:
22+
PLUGIN: townsquareexpansion_moodleoverflow
23+
CURL: curl -s
24+
ENDPOINT: https://moodle.org/webservice/rest/server.php
25+
TOKEN: ${{ secrets.MOODLE_ORG_TOKEN }}
26+
FUNCTION: local_plugins_add_version
27+
28+
steps:
29+
- name: Call the service function
30+
id: add-version
31+
run: |
32+
TAGNAME="${{ github.event.release.tag_name }}"
33+
BODY="${{ github.event.release.body }}"
34+
ZIPURL="${{ github.event.release.zipball_url }}"
35+
RESPONSE=$(${CURL} ${ENDPOINT} --data-urlencode "wstoken=${TOKEN}" \
36+
--data-urlencode "wsfunction=${FUNCTION}" \
37+
--data-urlencode "moodlewsrestformat=json" \
38+
--data-urlencode "frankenstyle=${PLUGIN}" \
39+
--data-urlencode "zipurl=${ZIPURL}" \
40+
--data-urlencode "vcssystem=git" \
41+
--data-urlencode "vcsrepositoryurl=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}" \
42+
--data-urlencode "vcstag=${TAGNAME}" \
43+
--data-urlencode "changelogurl=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/commits/${TAGNAME}" \
44+
--data-urlencode "altdownloadurl=${ZIPURL}" \
45+
--data-urlencode "releasenotes=${BODY}" \
46+
--data-urlencode "releasenotesformat=4")
47+
echo "response=${RESPONSE}" >> $GITHUB_OUTPUT
48+
- name: Evaluate the response
49+
id: evaluate-response
50+
env:
51+
RESPONSE: ${{ steps.add-version.outputs.response }}
52+
run: |
53+
jq <<< ${RESPONSE}
54+
jq --exit-status ".id" <<< ${RESPONSE} > /dev/null

classes/moodleoverflow.php

+52-10
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
defined('MOODLE_INTERNAL') || die;
2727

2828
use local_townsquaresupport\townsquaresupportinterface;
29+
use mod_moodleoverflow\anonymous;
30+
use moodle_url;
2931

3032
global $CFG;
31-
require_once($CFG->dirroot . '/blocks/townsquare/locallib.php');
33+
require_once($CFG->dirroot . '/blocks/townsquare/lib.php');
3234

3335
/**
3436
* Class that implements the townsquaresupportinterface with the function to get the events from the plugin.
@@ -66,18 +68,35 @@ public static function get_events(): array {
6668
if ( (townsquare_filter_availability($event)) ||
6769
($event->eventtype == 'expectcompletionon' && townsquare_filter_activitycompletions($event))) {
6870
unset($moodleoverflowevents[$key]);
69-
continue;
7071
}
7172

72-
// Add the name of the instance to the event.
73-
if ($event->eventtype != 'post') {
74-
$event->instancename = $DB->get_field($event->modulename, 'name', ['id' => $event->instance]);
73+
if ($event->eventtype == 'post') {
74+
// Add an anonymous attribute.
75+
$event->anonymous = self::is_post_anonymous($event);
76+
77+
// If the post is anonymous, make the author anonymous.
78+
if ($event->anonymous) {
79+
$event->postuserfirstname = 'anonymous';
80+
$event->postuserlastname = '';
81+
}
82+
83+
// Add links.
84+
$event->linktopost = new moodle_url('/mod/moodleoverflow/discussion.php',
85+
['d' => $event->postdiscussion], 'p' . $event->postid);
86+
$event->linktoauthor = $event->anonymous ? new moodle_url('') :
87+
new moodle_url('/user/view.php', ['id' => $event->postuserid]);
7588
}
7689
}
77-
7890
return $moodleoverflowevents;
7991
}
8092

93+
/**
94+
* Builds the sql statement to get all moodleoverflow posts from the database.
95+
*
96+
* @param $courses
97+
* @param $timestart
98+
* @return array
99+
*/
81100
private static function get_moodleoverflowposts_from_db($courses, $timestart): array {
82101
global $DB;
83102
// Prepare params for sql statement.
@@ -102,7 +121,8 @@ private static function get_moodleoverflowposts_from_db($courses, $timestart): a
102121
posts.parent AS postparentid,
103122
posts.userid AS postuserid,
104123
posts.created AS timestart,
105-
posts.message AS postmessage
124+
posts.message AS content,
125+
posts.messageformat AS postmessageformat
106126
FROM {moodleoverflow_posts} posts
107127
JOIN {moodleoverflow_discussions} discuss ON discuss.id = posts.discussion
108128
JOIN {moodleoverflow} module ON module.id = discuss.moodleoverflow
@@ -118,19 +138,28 @@ private static function get_moodleoverflowposts_from_db($courses, $timestart): a
118138
return $DB->get_records_sql($sql, $params);
119139
}
120140

141+
/**
142+
* Build the sql statement to get all other events related to moodleoverflow from the database.
143+
* @param $courses
144+
* @param $timestart
145+
* @param $timeend
146+
* @return array
147+
*/
121148
private static function get_other_events_from_db($courses, $timestart, $timeend): array {
122149
global $DB;
123150
// Prepare params for sql statement.
124151
list($insqlcourses, $inparamscourses) = $DB->get_in_or_equal($courses, SQL_PARAMS_NAMED);
125152

126153
$params = ['timestart' => $timestart, 'timeduration' => $timestart,
127-
'timeend' => $timeend, 'courses' => $courses] + $inparamscourses;
154+
'timeend' => $timeend, 'courses' => $courses, ] + $inparamscourses;
128155
// Set the sql statement.
129-
$sql = "SELECT e.id, e.name, e.courseid, cm.id AS coursemoduleid, cm.availability AS availability, e.groupid, e.userid,
130-
e.modulename, e.instance, e.eventtype, e.timestart, e.timemodified, e.visible
156+
$sql = "SELECT e.id, e.name AS content, mo.name AS instancename, e.courseid, cm.id AS coursemoduleid,
157+
cm.availability AS availability, e.groupid, e.userid, e.modulename, e.instance, e.eventtype, e.timestart,
158+
e.timemodified, e.visible
131159
FROM {event} e
132160
JOIN {modules} m ON e.modulename = m.name
133161
JOIN {course_modules} cm ON (cm.course = e.courseid AND cm.module = m.id AND cm.instance = e.instance)
162+
JOIN {moodleoverflow} mo ON mo.id = e.instance
134163
WHERE (e.timestart >= :timestart OR e.timestart+e.timeduration > :timeduration)
135164
AND e.timestart <= :timeend
136165
AND e.courseid $insqlcourses
@@ -144,4 +173,17 @@ private static function get_other_events_from_db($courses, $timestart, $timeend)
144173
return $DB->get_records_sql($sql, $params);
145174
}
146175

176+
/**
177+
* Helper function to check if a post is anonymous. Helps to reduce the cyclomatic complexity.
178+
* @param $event
179+
* @return bool
180+
*/
181+
private static function is_post_anonymous($event) {
182+
if ($event->anonymoussetting == anonymous::EVERYTHING_ANONYMOUS) {
183+
return true;
184+
} else if ($event->anonymoussetting == anonymous::QUESTION_ANONYMOUS) {
185+
return $event->postuserid == $event->discussionuserid;
186+
}
187+
return false;
188+
}
147189
}

lang/en/moodleoverflow.php renamed to lang/en/townsquareexpansion_moodleoverflow.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2424
*/
2525
$string['pluginname'] = 'Townsquaresubplugin for mod_moodleoverflow';
26-
$string['pluginname'] = 'Moodleoverflow support for townsquare block';
26+
$string['pluginname_help'] = 'This subplugin allows the townsquare block to show posts and events from moodleoverflow.';
2727
$string['pluginnameadding'] = "Adding a Moodleoverflow support subplugin";
2828
$string['pluginnameediting'] = "Editing a Moodleoverflow support subplugin";
2929
$string['pluginnamesummary'] = "This subplugin allows the townsquare block to show posts and events from moodleoverflow.";
30-
$string['pluginname_help'] = 'This subplugin allows the townsquare block to show posts and events from moodleoverflow.';
30+
$string['plugintitle'] = 'Moodleoverflow support for townsquare block';

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
defined('MOODLE_INTERNAL') || die();
2626
$plugin->component = 'townsquareexpansion_moodleoverflow';
27-
$plugin->dependencies = ['local_townsquaresupport' => ANY_VERSION];
27+
$plugin->dependencies = ['local_townsquaresupport' => ANY_VERSION, 'mod_moodleoverflow' => ANY_VERSION];
2828
$plugin->release = '0.1.0';
2929
$plugin->version = 2024050900;
3030
$plugin->requires = 2022041900;

0 commit comments

Comments
 (0)