1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Event observers used in forum. 19 * 20 * @package mod_forum 21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 /** 28 * Event observer for mod_forum. 29 */ 30 class mod_forum_observer { 31 32 /** 33 * Triggered via user_enrolment_deleted event. 34 * 35 * @param \core\event\user_enrolment_deleted $event 36 */ 37 public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) { 38 global $DB; 39 40 // NOTE: this has to be as fast as possible. 41 // Get user enrolment info from event. 42 $cp = (object)$event->other['userenrolment']; 43 if ($cp->lastenrol) { 44 if (!$forums = $DB->get_records('forum', array('course' => $cp->courseid), '', 'id')) { 45 return; 46 } 47 list($forumselect, $params) = $DB->get_in_or_equal(array_keys($forums), SQL_PARAMS_NAMED); 48 $params['userid'] = $cp->userid; 49 50 $DB->delete_records_select('forum_digests', 'userid = :userid AND forum '.$forumselect, $params); 51 $DB->delete_records_select('forum_subscriptions', 'userid = :userid AND forum '.$forumselect, $params); 52 $DB->delete_records_select('forum_track_prefs', 'userid = :userid AND forumid '.$forumselect, $params); 53 $DB->delete_records_select('forum_read', 'userid = :userid AND forumid '.$forumselect, $params); 54 } 55 } 56 57 /** 58 * Observer for role_assigned event. 59 * 60 * @param \core\event\role_assigned $event 61 * @return void 62 */ 63 public static function role_assigned(\core\event\role_assigned $event) { 64 global $CFG, $DB; 65 66 $context = context::instance_by_id($event->contextid, MUST_EXIST); 67 68 // If contextlevel is course then only subscribe user. Role assignment 69 // at course level means user is enroled in course and can subscribe to forum. 70 if ($context->contextlevel != CONTEXT_COURSE) { 71 return; 72 } 73 74 // Forum lib required for the constant used below. 75 require_once($CFG->dirroot . '/mod/forum/lib.php'); 76 77 $userid = $event->relateduserid; 78 $sql = "SELECT f.id, f.course as course, cm.id AS cmid, f.forcesubscribe 79 FROM {forum} f 80 JOIN {course_modules} cm ON (cm.instance = f.id) 81 JOIN {modules} m ON (m.id = cm.module) 82 LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid) 83 WHERE f.course = :courseid 84 AND f.forcesubscribe = :initial 85 AND m.name = 'forum' 86 AND fs.id IS NULL"; 87 $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => FORUM_INITIALSUBSCRIBE); 88 89 $forums = $DB->get_records_sql($sql, $params); 90 foreach ($forums as $forum) { 91 // If user doesn't have allowforcesubscribe capability then don't subscribe. 92 $modcontext = context_module::instance($forum->cmid); 93 if (has_capability('mod/forum:allowforcesubscribe', $modcontext, $userid)) { 94 \mod_forum\subscriptions::subscribe_user($userid, $forum, $modcontext); 95 } 96 } 97 } 98 99 /** 100 * Observer for \core\event\course_module_created event. 101 * 102 * @param \core\event\course_module_created $event 103 * @return void 104 */ 105 public static function course_module_created(\core\event\course_module_created $event) { 106 global $CFG; 107 108 if ($event->other['modulename'] === 'forum') { 109 // Include the forum library to make use of the forum_instance_created function. 110 require_once($CFG->dirroot . '/mod/forum/lib.php'); 111 112 $forum = $event->get_record_snapshot('forum', $event->other['instanceid']); 113 forum_instance_created($event->get_context(), $forum); 114 } 115 } 116 117 /** 118 * Observer for \core\event\course_created event. 119 * 120 * @param \core\event\course_created $event 121 * @return void 122 */ 123 public static function course_created(\core\event\course_created $event) { 124 global $CFG; 125 126 $course = $event->get_record_snapshot('course', $event->objectid); 127 $format = course_get_format($course); 128 if ($format->supports_news() && !empty($course->newsitems)) { 129 require_once($CFG->dirroot . '/mod/forum/lib.php'); 130 // Auto create the announcements forum. 131 forum_get_course_forum($event->objectid, 'news'); 132 } 133 } 134 135 /** 136 * Observer for \core\event\course_updated event. 137 * 138 * @param \core\event\course_updated $event 139 * @return void 140 */ 141 public static function course_updated(\core\event\course_updated $event) { 142 global $CFG; 143 144 $course = $event->get_record_snapshot('course', $event->objectid); 145 $format = course_get_format($course); 146 if ($format->supports_news() && !empty($course->newsitems)) { 147 require_once($CFG->dirroot . '/mod/forum/lib.php'); 148 // Auto create the announcements forum. 149 forum_get_course_forum($event->objectid, 'news'); 150 } 151 } 152 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body