Skip to content

Commit 7dbcaa9

Browse files
committed
synchronize with master
2 parents 92edcb8 + c061592 commit 7dbcaa9

38 files changed

+1771
-2025
lines changed

.github/workflows/moodle-ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
5151
- name: Initialise moodle-plugin-ci
5252
run: |
53-
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
53+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
5454
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
5555
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
5656
sudo locale-gen en_AU.UTF-8
@@ -173,7 +173,7 @@ jobs:
173173
174174
- name: Initialise moodle-plugin-ci
175175
run: |
176-
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
176+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
177177
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
178178
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
179179
sudo locale-gen en_AU.UTF-8
@@ -192,4 +192,4 @@ jobs:
192192

193193
- name: Behat features
194194
if: ${{ always() }}
195-
run: moodle-plugin-ci behat --auto-rerun 0
195+
run: moodle-plugin-ci behat --auto-rerun 0

classes/capabilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public static function has(string $capability, context $context, $userid = null)
9494

9595
$key = "$userid:$context->id:$capability";
9696

97-
if (!isset($cache[$key])) {
98-
$cache[$key] = has_capability($capability, $context, $userid);
97+
if (!isset(self::$cache[$key])) {
98+
self::$cache[$key] = has_capability($capability, $context, $userid);
9999
}
100100

101-
return $cache[$key];
101+
return self::$cache[$key];
102102
}
103103

104104
}

classes/manager/mail_manager.php

Lines changed: 57 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class mail_manager {
6565
* @return bool
6666
*/
6767
public static function moodleoverflow_send_mails(): bool {
68-
global $DB, $CFG, $PAGE;
68+
global $CFG, $DB, $PAGE;
6969

7070
// Get the course object of the top level site.
7171
$site = get_site();
@@ -86,10 +86,8 @@ public static function moodleoverflow_send_mails(): bool {
8686
$moodleoverflows = [];
8787
$courses = [];
8888
$coursemodules = [];
89-
$subscribedusers = [];
9089

91-
// Posts older than x days will not be mailed.
92-
// This will avoid problems with the cron not ran for a long time.
90+
// Posts older than x days will not be mailed. This will avoid problems with the cron not ran for a long time.
9391
$timenow = time();
9492
$endtime = $timenow - get_config('moodleoverflow', 'maxeditingtime');
9593
$starttime = $endtime - (get_config('moodleoverflow', 'maxmailingtime') * 60 * 60);
@@ -102,7 +100,6 @@ public static function moodleoverflow_send_mails(): bool {
102100
mtrace('Errors occurred while trying to mark some posts as being mailed.');
103101
return false;
104102
}
105-
106103
// Loop through all posts to be mailed.
107104
foreach ($posts as $postid => $post) {
108105
self::check_post($post, $mailcount, $users, $discussions, $errorcount, $posts, $postid,
@@ -126,18 +123,17 @@ public static function moodleoverflow_send_mails(): bool {
126123
$userto->markposts = [];
127124

128125
// Cache the capabilities of the user.
129-
cron_setup_user($userto);
126+
$CFG->branch >= 402 ? \core\cron::setup_user($userto) : cron_setup_user($userto);
130127

131128
// Reset the caches.
132-
foreach ($coursemodules as $moodleoverflowid => $unused) {
129+
foreach ($coursemodules as $moodleoverflowid) {
133130
$coursemodules[$moodleoverflowid]->cache = new stdClass();
134131
$coursemodules[$moodleoverflowid]->cache->caps = [];
135132
unset($coursemodules[$moodleoverflowid]->uservisible);
136133
}
137134

138135
// Loop through all posts of this users.
139136
foreach ($posts as $post) {
140-
141137
self::send_post($userto, $post, $coursemodules, $errorcount,
142138
$discussions, $moodleoverflows, $courses, $mailcount, $users, $site, $textout, $htmlout);
143139
}
@@ -148,18 +144,13 @@ public static function moodleoverflow_send_mails(): bool {
148144
}
149145

150146
// Check for all posts whether errors occurred.
151-
if ($posts) {
152-
153-
// Loop through all posts.
154-
foreach ($posts as $post) {
155-
156-
// Tracing information.
157-
mtrace($mailcount[$post->id] . " users were sent post $post->id");
147+
foreach ($posts as $post) {
148+
// Tracing information.
149+
mtrace($mailcount[$post->id] . " users were sent post $post->id");
158150

159-
// Mark the posts with errors in the database.
160-
if ($errorcount[$post->id]) {
161-
$DB->set_field('moodleoverflow_posts', 'mailed', self::MOODLEOVERFLOW_MAILED_ERROR, ['id' => $post->id]);
162-
}
151+
// Mark the posts with errors in the database.
152+
if ($errorcount[$post->id]) {
153+
$DB->set_field('moodleoverflow_posts', 'mailed', self::MOODLEOVERFLOW_MAILED_ERROR, ['id' => $post->id]);
163154
}
164155
}
165156

@@ -232,7 +223,6 @@ public static function moodleoverflow_mark_old_posts_as_mailed($endtime) {
232223
* @param stdClass $user
233224
*/
234225
public static function moodleoverflow_minimise_user_record(stdClass $user) {
235-
236226
// Remove all information for the mail generation that are not needed.
237227
unset($user->institution);
238228
unset($user->department);
@@ -268,61 +258,31 @@ public static function moodleoverflow_minimise_user_record(stdClass $user) {
268258
private static function check_post($post, array &$mailcount, array &$users, array &$discussions, array &$errorcount,
269259
array &$posts, int $postid, array &$moodleoverflows, array &$courses,
270260
array &$coursemodules) {
271-
global $DB;
272261
// Check the cache if the discussion exists.
273262
$discussionid = $post->discussion;
274-
if (!isset($discussions[$discussionid])) {
275-
// Retrieve the discussion from the database.
276-
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion]);
277-
278-
// If there is a record, update the cache. Else ignore the post.
279-
if ($discussion) {
280-
$discussions[$discussionid] = $discussion;
281-
subscriptions::fill_subscription_cache($discussion->moodleoverflow);
282-
subscriptions::fill_discussion_subscription_cache($discussion->moodleoverflow);
283-
} else {
284-
mtrace('Could not find discussion ' . $discussionid);
285-
unset($posts[$postid]);
286-
return;
287-
}
263+
if (!self::cache_record('moodleoverflow_discussions', $discussionid, $discussions,
264+
'Could not find discussion ', $posts, $postid, true)) {
265+
return;
288266
}
289267

290268
// Retrieve the connected moodleoverflow instance from the database.
291269
$moodleoverflowid = $discussions[$discussionid]->moodleoverflow;
292-
if (!isset($moodleoverflows[$moodleoverflowid])) {
293-
294-
// Retrieve the record from the database and update the cache.
295-
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflowid]);
296-
if ($moodleoverflow) {
297-
$moodleoverflows[$moodleoverflowid] = $moodleoverflow;
298-
} else {
299-
mtrace('Could not find moodleoverflow ' . $moodleoverflowid);
300-
unset($posts[$postid]);
301-
return;
302-
}
270+
if (!self::cache_record('moodleoverflow', $moodleoverflowid, $moodleoverflows,
271+
'Could not find moodleoverflow ', $posts, $postid, false)) {
272+
return;
303273
}
304274

305275
// Retrieve the connected courses from the database.
306276
$courseid = $moodleoverflows[$moodleoverflowid]->course;
307-
if (!isset($courses[$courseid])) {
308-
309-
// Retrieve the record from the database and update the cache.
310-
$course = $DB->get_record('course', ['id' => $courseid]);
311-
if ($course) {
312-
$courses[$courseid] = $course;
313-
} else {
314-
mtrace('Could not find course ' . $courseid);
315-
unset($posts[$postid]);
316-
return;
317-
}
277+
if (!self::cache_record('course', $courseid, $courses,
278+
'Could not find course ', $posts, $postid, false)) {
279+
return;
318280
}
319281

320282
// Retrieve the connected course modules from the database.
321283
if (!isset($coursemodules[$moodleoverflowid])) {
322-
323284
// Retrieve the coursemodule and update the cache.
324-
$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid, $courseid);
325-
if ($cm) {
285+
if ($cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid, $courseid)) {
326286
$coursemodules[$moodleoverflowid] = $cm;
327287
} else {
328288
mtrace('Could not find course module for moodleoverflow ' . $moodleoverflowid);
@@ -333,14 +293,12 @@ private static function check_post($post, array &$mailcount, array &$users, arra
333293

334294
// Cache subscribed users of each moodleoverflow.
335295
if (!isset($subscribedusers[$moodleoverflowid])) {
336-
337296
// Retrieve the context module.
338297
$modulecontext = context_module::instance($coursemodules[$moodleoverflowid]->id);
339298

340299
// Retrieve all subscribed users.
341300
$mid = $moodleoverflows[$moodleoverflowid];
342-
$subusers = subscriptions::get_subscribed_users($mid, $modulecontext, 'u.*', true);
343-
if ($subusers) {
301+
if ($subusers = subscriptions::get_subscribed_users($mid, $modulecontext, 'u.*', true)) {
344302
// Loop through all subscribed users.
345303
foreach ($subusers as $postuser) {
346304
// Save the user into the cache.
@@ -361,6 +319,39 @@ private static function check_post($post, array &$mailcount, array &$users, arra
361319
$errorcount[$postid] = 0;
362320
}
363321

322+
/**
323+
* Helper function for check_post(). Caches the a record exists in the database and caches the record if needed.
324+
* @param string $table
325+
* @param int $id
326+
* @param array $cache
327+
* @param string $errorMessage
328+
* @param array $posts
329+
* @param int $postid
330+
* @param bool $fillsubscache If the subscription cache is being filled (only when checking discussion cache)
331+
* @return bool
332+
* @throws \dml_exception
333+
*/
334+
private static function cache_record($table, $id, &$cache, $errormessage, &$posts, $postid, $fillsubscache) {
335+
global $DB;
336+
// Check if cache if an record exists already in the cache.
337+
if (!isset($cache[$id])) {
338+
// If there is a record in the database, update the cache. Else ignore the post.
339+
if ($record = $DB->get_record($table, ['id' => $id])) {
340+
$cache[$id] = $record;
341+
if ($fillsubscache) {
342+
subscriptions::fill_subscription_cache($record->moodleoverflow);
343+
subscriptions::fill_discussion_subscription_cache($record->moodleoverflow);
344+
}
345+
} else {
346+
mtrace($errormessage . $id);
347+
unset($posts[$postid]);
348+
return false;
349+
}
350+
}
351+
return true;
352+
}
353+
354+
364355
/**
365356
* Send the Mail with information of the post depending on theinformation available.
366357
* E.g. anonymous post do not include names, users who want resumes do not get single mails.
@@ -427,11 +418,8 @@ private static function send_post($userto, $post, array &$coursemodules, array &
427418
}
428419

429420
// Check whether the user is subscribed to the discussion.
430-
$iscm = $coursemodules[$moodleoverflow->id];
431421
$uid = $userto->id;
432-
$did = $post->discussion;
433-
$issubscribed = subscriptions::is_subscribed($uid, $moodleoverflow, $modulecontext, $did);
434-
if (!$issubscribed) {
422+
if (!subscriptions::is_subscribed($uid, $moodleoverflow, $modulecontext, $post->discussion)) {
435423
return;
436424
}
437425

@@ -464,7 +452,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &
464452
}
465453

466454
// Setup roles and languages.
467-
cron_setup_user($userto, $course);
455+
$CFG->branch >= 402 ? \core\cron::setup_user($userto, $course) : cron_setup_user($userto, $course);
468456

469457
// Cache the users capability to view full names.
470458
if (!isset($userto->viewfullnames[$moodleoverflow->id])) {
@@ -522,16 +510,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &
522510
}
523511

524512
// Format the data.
525-
$data = new moodleoverflow_email(
526-
$course,
527-
$cm,
528-
$moodleoverflow,
529-
$discussion,
530-
$post,
531-
$userfrom,
532-
$userto,
533-
$canreply
534-
);
513+
$data = new moodleoverflow_email($course, $cm, $moodleoverflow, $discussion, $post, $userfrom, $userto, $canreply);
535514

536515
// Retrieve the unsubscribe-link.
537516
$userfrom->customheaders[] = sprintf('List-Unsubscribe: <%s>', $data->get_unsubscribediscussionlink());
@@ -557,7 +536,6 @@ private static function send_post($userto, $post, array &$coursemodules, array &
557536

558537
// Check whether the post is a reply.
559538
if ($post->parent) {
560-
561539
// Add a reply header.
562540
$parentid = generate_email_messageid(hash('sha256', $post->parent . 'to' . $userto->id));
563541
$userfrom->customheaders[] = "In-Reply-To: $parentid";

classes/post_form.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function definition() {
5858
$modform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
5959

6060
// The message.
61-
$modform->addElement('editor', 'message', get_string('message', 'moodleoverflow'), null);
61+
$modform->addElement('editor', 'message', get_string('message', 'moodleoverflow'), null,
62+
self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
6263
$modform->setType('message', PARAM_RAW);
6364
$modform->addRule('message', get_string('required'), 'required', null, 'client');
6465

@@ -141,6 +142,25 @@ public static function attachment_options($moodleoverflow) {
141142
'return_types' => FILE_INTERNAL | FILE_CONTROLLED_LINK,
142143
];
143144
}
145+
146+
/**
147+
* Returns the options array to use in forum text editor
148+
*
149+
* @param context_module $context
150+
* @param int $postid post id, use null when adding new post
151+
* @return array
152+
*/
153+
public static function editor_options(context_module $context, $postid) {
154+
global $COURSE, $PAGE, $CFG;
155+
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
156+
return [
157+
'maxfiles' => EDITOR_UNLIMITED_FILES,
158+
'maxbytes' => $maxbytes,
159+
'trusttext' => true,
160+
'return_types' => FILE_INTERNAL | FILE_EXTERNAL,
161+
'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid),
162+
];
163+
}
144164
}
145165

146166

classes/privacy/data_export_helper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
1617
/**
1718
* Privacy Subsystem implementation for mod_moodleoverflow.
1819
*

0 commit comments

Comments
 (0)