commit da9ac095a090efb1482c5bc762ebe72aef0289cd Author: David Bogner Date: Thu Apr 11 23:15:49 2013 +0200 MDL-38910 autosubscribe users on frontpage forums diff --git a/mod/forum/db/events.php b/mod/forum/db/events.php index ded6583..ee3e825 100644 --- a/mod/forum/db/events.php +++ b/mod/forum/db/events.php @@ -38,4 +38,11 @@ $handlers = array ( 'schedule' => 'instant', 'internal' => 1, ), + + 'user_created' => array ( + 'handlerfile' => '/mod/forum/lib.php', + 'handlerfunction' => 'forum_user_created', + 'schedule' => 'instant', + 'internal' => 1, + ), ); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 14a7427..fc53809 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3065,6 +3065,7 @@ function forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfro // format the post body $options = new stdClass(); $options->para = true; + $options->noclean = true; $formattedtext = format_text($post->message, $post->messageformat, $options, $course->id); $output = ''; @@ -6274,6 +6275,42 @@ function forum_user_unenrolled($cp) { } } +/** + * This function is called whenever a user is created. + * Users are automatically enrolled in a forum + * on the frontpage according to the forum settings + * + * @param stdClass $user complete user data as object + * @return void + */ +function forum_user_created($user) { + global $DB; + //check if at least one forum exists on frontpage if not don't go further in order to improve performance + if(!$DB->record_exists('forum', array('course' => 1))){ + return; + } + + $sql = "SELECT f.id, cm.id AS cmid + FROM {forum} f + JOIN {course_modules} cm ON (cm.instance = f.id) + JOIN {modules} m ON (m.id = cm.module) + LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid) + WHERE f.course = :courseid + AND f.forcesubscribe = :initial + AND m.name = 'forum' + AND fs.id IS NULL"; + $params = array('courseid'=>1, 'userid'=>$user->id, 'initial'=>NEWSLETTERTEMP_INITIALSUBSCRIBE); + + $forums = $DB->get_records_sql($sql, $params); + + foreach ($forums as $forum) { + // If user doesn't have allowforcesubscribe capability then don't subscribe. + if (has_capability('mod/forum:allowforcesubscribe', context_module::instance($forum->cmid), $user->id)) { + forum_subscribe($user->id, $forum->id); + } + } +} + // Functions to do with read tracking. /** diff --git a/mod/forum/version.php b/mod/forum/version.php index d43c1fa..4a7bd7f 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$module->version = 2012112901; // The current module version (Date: YYYYMMDDXX) +$module->version = 2013041100; // The current module version (Date: YYYYMMDDXX) $module->requires = 2012112900; // Requires this Moodle version $module->component = 'mod_forum'; // Full name of the plugin (used for diagnostics) $module->cron = 60;