Skip to content

Commit

Permalink
Only interested if a CT course, fixes 'Activity::metaused should chec…
Browse files Browse the repository at this point in the history
…k if the course uses topcoll before ->get_settings to avoid error' - #108.
  • Loading branch information
gjb2048 committed Aug 20, 2021
1 parent 62febfe commit cd44254
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
32 changes: 15 additions & 17 deletions classes/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
use \cm_info;

require_once($CFG->dirroot.'/mod/assign/locallib.php');
require_once($CFG->dirroot.'/course/format/lib.php'); // For course_get_format.

/**
* Activity functions.
Expand Down Expand Up @@ -963,8 +962,8 @@ public static function invalidatestudentscache() {
* @param int $userid User id.
* @param int $courseid Course id.
*/
public static function userenrolmentcreated($userid, $courseid) {
if (self::activitymetaused($courseid)) {
public static function userenrolmentcreated($userid, $courseid, $courseformat) {
if (self::activitymetaused($courseformat)) {
self::clearcoursemodulecount($courseid);
}
}
Expand All @@ -975,8 +974,8 @@ public static function userenrolmentcreated($userid, $courseid) {
* @param int $userid User id.
* @param int $courseid Course id.
*/
public static function userenrolmentupdated($userid, $courseid) {
if (self::activitymetaused($courseid)) {
public static function userenrolmentupdated($userid, $courseid, $courseformat) {
if (self::activitymetaused($courseformat)) {
self::clearcoursemodulecount($courseid);
}
}
Expand All @@ -987,8 +986,8 @@ public static function userenrolmentupdated($userid, $courseid) {
* @param int $userid User id.
* @param int $courseid Course id.
*/
public static function userenrolmentdeleted($userid, $courseid) {
if (self::activitymetaused($courseid)) {
public static function userenrolmentdeleted($userid, $courseid, $courseformat) {
if (self::activitymetaused($courseformat)) {
self::clearcoursemodulecount($courseid);
}
}
Expand All @@ -999,8 +998,8 @@ public static function userenrolmentdeleted($userid, $courseid) {
* @param int $modid Module id.
* @param int $courseid Course id.
*/
public static function modulecreated($modid, $courseid) {
self::modulechanged($modid, $courseid);
public static function modulecreated($modid, $courseid, $courseformat) {
self::modulechanged($modid, $courseid, $courseformat);
}

/**
Expand All @@ -1009,8 +1008,8 @@ public static function modulecreated($modid, $courseid) {
* @param int $modid Module id.
* @param int $courseid Course id.
*/
public static function moduleupdated($modid, $courseid) {
self::modulechanged($modid, $courseid);
public static function moduleupdated($modid, $courseid, $courseformat) {
self::modulechanged($modid, $courseid, $courseformat);
}

/**
Expand All @@ -1019,8 +1018,8 @@ public static function moduleupdated($modid, $courseid) {
* @param int $modid Module id.
* @param int $courseid Course id.
*/
private static function modulechanged($modid, $courseid) {
if (self::activitymetaused($courseid)) {
private static function modulechanged($modid, $courseid, $courseformat) {
if (self::activitymetaused($courseformat)) {
$lock = self::lockmodulecountcache($courseid);
$modulecountcache = \cache::make('format_topcoll', 'activitymodulecountcache');
$modulecountcourse = $modulecountcache->get($courseid);
Expand All @@ -1041,8 +1040,8 @@ private static function modulechanged($modid, $courseid) {
* @param int $modid Module id.
* @param int $courseid Course id.
*/
public static function moduledeleted($modid, $courseid) {
if (self::activitymetaused($courseid)) {
public static function moduledeleted($modid, $courseid, $courseformat) {
if (self::activitymetaused($courseformat)) {
$lock = self::lockmodulecountcache($courseid);
$modulecountcache = \cache::make('format_topcoll', 'activitymodulecountcache');
$modulecountcourse = $modulecountcache->get($courseid);
Expand Down Expand Up @@ -1132,8 +1131,7 @@ private static function lockmodulecountcache($courseid) {
*
* @return boolean True or False.
*/
private static function activitymetaused($courseid) {
$courseformat = course_get_format($courseid);
private static function activitymetaused($courseformat) {
$tcsettings = $courseformat->get_settings();
if ((!empty($tcsettings['showadditionalmoddata'])) && ($tcsettings['showadditionalmoddata'] == 2)) {
return true; // Could in theory test the module but then this method wouldn't work for user events.
Expand Down
47 changes: 41 additions & 6 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/course/format/lib.php'); // For course_get_format.

/**
* Event observers supported by this format.
*/
Expand All @@ -52,6 +54,12 @@ public static function course_content_deleted(\core\event\course_content_deleted
$DB->delete_records("user_preferences", array("name" => 'topcoll_toggle_'.$event->objectid)); // This is the $courseid.
}

/* Events observed for the purpose of the activty functionality.
TODO: Do need to monitor when a course is changed to CT and clear the cache for
that course? i.e. scenario of using CT, then changing to Topics then changing
back again -> data would be invalid.
*/

/**
* Observer for the role_allow_view_updated event.
*/
Expand Down Expand Up @@ -88,7 +96,9 @@ public static function role_deleted() {
* @param \core\event\user_enrolment_created $event
*/
public static function user_enrolment_created(\core\event\user_enrolment_created $event) {
\format_topcoll\activity::userenrolmentcreated($event->relateduserid, $event->courseid);
if ($courseformat = self::istopcoll($event->courseid)) {
\format_topcoll\activity::userenrolmentcreated($event->relateduserid, $event->courseid, $courseformat);
}
}

/**
Expand All @@ -97,7 +107,9 @@ public static function user_enrolment_created(\core\event\user_enrolment_created
* @param \core\event\user_enrolment_updated $event
*/
public static function user_enrolment_updated(\core\event\user_enrolment_updated $event) {
\format_topcoll\activity::userenrolmentupdated($event->relateduserid, $event->courseid);
if ($courseformat = self::istopcoll($event->courseid)) {
\format_topcoll\activity::userenrolmentupdated($event->relateduserid, $event->courseid, $courseformat);
}
}

/**
Expand All @@ -106,7 +118,9 @@ public static function user_enrolment_updated(\core\event\user_enrolment_updated
* @param \core\event\user_enrolment_deleted $event
*/
public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) {
\format_topcoll\activity::userenrolmentdeleted($event->relateduserid, $event->courseid);
if ($courseformat = self::istopcoll($event->courseid)) {
\format_topcoll\activity::userenrolmentdeleted($event->relateduserid, $event->courseid, $courseformat);
}
}

/**
Expand All @@ -115,7 +129,9 @@ public static function user_enrolment_deleted(\core\event\user_enrolment_deleted
* @param \core\event\course_module_created $event
*/
public static function course_module_created(\core\event\course_module_created $event) {
\format_topcoll\activity::modulecreated($event->objectid, $event->courseid);
if ($courseformat = self::istopcoll($event->courseid)) {
\format_topcoll\activity::modulecreated($event->objectid, $event->courseid, $courseformat);
}
}

/**
Expand All @@ -124,7 +140,9 @@ public static function course_module_created(\core\event\course_module_created $
* @param \core\event\course_module_updated $event
*/
public static function course_module_updated(\core\event\course_module_updated $event) {
\format_topcoll\activity::moduleupdated($event->objectid, $event->courseid);
if ($courseformat = self::istopcoll($event->courseid)) {
\format_topcoll\activity::moduleupdated($event->objectid, $event->courseid, $courseformat);
}
}

/**
Expand All @@ -133,6 +151,23 @@ public static function course_module_updated(\core\event\course_module_updated $
* @param \core\event\course_module_deleted $event
*/
public static function course_module_deleted(\core\event\course_module_deleted $event) {
\format_topcoll\activity::moduledeleted($event->objectid, $event->courseid);
if ($courseformat = self::istopcoll($event->courseid)) {
\format_topcoll\activity::moduledeleted($event->objectid, $event->courseid, $courseformat);
}
}

/**
* Is the course using the Collapsed Topics course format?
*
* @param int $courseid Course id.
*
* @return object | bool format_topcoll object or false if not a Collapsed Topics course.
*/
private static function istopcoll($courseid) {
$courseformat = course_get_format($courseid);
if ($courseformat instanceof format_topcoll) {
return $courseformat;
}
return false;
}
}

0 comments on commit cd44254

Please sign in to comment.