Skip to content

Commit

Permalink
Bug fixs - Designer Slow issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasanthlmsace committed Sep 21, 2024
1 parent c2143ac commit 6cf5068
Show file tree
Hide file tree
Showing 13 changed files with 594 additions and 217 deletions.
111 changes: 111 additions & 0 deletions classes/cache/loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Format Designer - Custom cache loader for the smart menus.
*
* @package format_designer
* @copyright 2023 bdecent GmbH <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace format_designer\cache;

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

require_once($CFG->dirroot.'/cache/classes/loaders.php');

/**
* Custom cache loader to handle the smart menus and items deletion.
*/
class loader extends \cache_application {

/**
* Delete the cached menus or menu items for all of its users.
*
* Fetch the cache store, generate the keys with menu or item id and keyword of user cache.
* Get the list of cached files by their filename, filenames are stored in the format of "menuid/itemid_u_ userid".
* Generate the key with menu/item id and the keyword of "_u" to get list of all users cache file for this menu/item.
*
* Delete all the files using delete_many method.
*
* @param int $id ID of the menu or item.
* @return void
*/
public function delete_vaild_section_completed_cache($courseid, $sectionid = 0) {
$store = $this->get_store();
$prefix = "v_s_c_c_{$courseid}";
if ($sectionid) {
$prefix .= "_s_{$sectionid}";
}
if ($list = $store->find_by_prefix($prefix)) {
$keys = array_map(function($key) {
$key = current(explode('-', $key));
return $key;
}, $list);
$this->delete_many($keys);
}
}


public function delete_user_section_completed_cache($courseid, $sectionid = 0, $userid = 0) {
$prefix = "s_c_c_{$courseid}";
$this->delete_prefix_cache($prefix);
}


public function delete_due_overdue_activities_count($courseid, $userid = 0) {
$prefix = "d_o_a_c_c{$courseid}";
if ($userid) {
$prefix .= "_u{$userid}";
}
$this->delete_prefix_cache($prefix);
}


public function delete_course_progress_uncompletion_criteria($courseid, $userid = 0) {
$prefix = "u_c_c_s{$courseid}";
if ($userid) {
$prefix .= "_u{$userid}";
}
$this->delete_prefix_cache($prefix);
}

public function delete_criteria_progress($courseid, $userid = 0) {
$prefix = "c_p_c{$courseid}";
if ($userid) {
$prefix .= "_u_{$userid}";
}
$this->delete_prefix_cache($prefix);
}


public function delete_prerequisites_courses() {
$prefix = "data_prereq_main_c";
$this->delete_prefix_cache($prefix);
}

public function delete_prefix_cache($prefix) {
$store = $this->get_store();
if ($list = $store->find_by_prefix($prefix)) {
$keys = array_map(function($key) {
$key = current(explode('-', $key));
return $key;
}, $list);
$this->delete_many($keys);
}
}
}
151 changes: 142 additions & 9 deletions classes/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace format_designer;

require_once($CFG->dirroot . "/course/format/designer/lib.php");


/**
* Designer format event observer.
*/
Expand All @@ -42,14 +45,18 @@ public static function course_section_created($event) {
$sectionid = $data['objectid'];
$sectionnum = $data['other']['sectionnum'];
$contextid = $data['contextid'];
$courseid = $data['courseid'];;
$courseid = $data['courseid'];
$filearea = 'sectiondesignbackground';
$option = get_config('format_designer', 'sectiondesignerbackgroundimage');
$coursecontext = \context_course::instance($courseid);

if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}

// course_section_cache_updated.
self::course_section_cache_updated($courseid, $sectionid);

$format = course_get_format($courseid);
$options = $format->section_format_options();
$sectiondata = ['id' => $sectionid];
Expand All @@ -63,29 +70,155 @@ public static function course_section_created($event) {
}
}


/**
* After course module deleted, deleted the format_designer_options data related to the format_designer options.
* After course deleted, deleted the format_designer_options data related to the format_designer options.
*
* @param object $event
* @return void
*/
public static function course_module_deleted($event) {
public static function course_deleted($event) {
global $DB;
$courseid = $event->courseid;
$cmid = $event->objectid;
$DB->delete_records('format_designer_options', ['courseid' => $courseid, 'cmid' => $cmid]);
$DB->delete_records('format_designer_options', ['courseid' => $courseid]);
$cache = format_designer_get_cache_object();
$cache->delete_prerequisites_courses();
self::course_cache_updated($courseid);
}

public static function course_completion_updated($event) {
$data = $event->get_data();
$courseid = $data['courseid'];
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
self::course_cache_updated($courseid);
}

public static function course_updated($event) {
$courseid = $event->courseid;
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
self::course_cache_updated($courseid);
}

public static function course_completed($event) {
$userid = $event->relateduserid;
$courseid = $event->courseid;
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
self::course_user_cache_updated($courseid, $userid);
}

public static function course_module_completion_updated($event) {
$userid = $event->relateduserid;
$courseid = $event->courseid;
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
self::course_user_cache_updated($courseid, $userid);
}


public static function course_module_created($event) {
self::course_section_module_cache_updated($event->courseid, $event->objectid);
}

/**
* After course deleted, deleted the format_designer_options data related to the format_designer options.
* After course module deleted, deleted the format_designer_options data related to the format_designer options.
*
* @param object $event
* @return void
*/
public static function course_deleted($event) {
public static function course_module_deleted($event) {
global $DB;
$courseid = $event->courseid;
$DB->delete_records('format_designer_options', ['courseid' => $courseid]);
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
$cmid = $event->objectid;
$DB->delete_records('format_designer_options', ['courseid' => $courseid, 'cmid' => $cmid]);

// Clear cache.
self::course_section_module_cache_updated($event->courseid, $event->objectid);
}

public static function course_module_updated($event) {
self::course_section_module_cache_updated($event->courseid, $event->objectid);
}


public static function course_section_deleted($event) {
$data = $event->get_data();
$sectionid = $data['objectid'];
$courseid = $data['courseid'];
self::course_section_cache_updated($courseid, $sectionid);
}

public static function course_section_updated($event) {
$data = $event->get_data();
$sectionid = $data['objectid'];
$courseid = $data['courseid'];
self::course_section_cache_updated($courseid, $sectionid);
}

public static function course_cache_updated($courseid) {
$cache = format_designer_get_cache_object();
$cache->delete_vaild_section_completed_cache($courseid);
$cache->delete_user_section_completed_cache($courseid);
$cache->delete_course_progress_uncompletion_criteria($courseid);
$cache->delete_due_overdue_activities_count($courseid);
$cache->delete_criteria_progress($courseid);
$cache->delete("g_c_a{$courseid}");
$cache->delete("g_c_s_ic{$courseid}");
}


public static function course_user_cache_updated($courseid , $userid) {
$cache = format_designer_get_cache_object();
$cache->delete_vaild_section_completed_cache($courseid);
$cache->delete_user_section_completed_cache($courseid);
$cache->delete_course_progress_uncompletion_criteria($courseid, $userid);
$cache->delete_due_overdue_activities_count($courseid, $userid);
$cache->delete_criteria_progress($courseid, $userid);
$cache->delete("g_c_a{$courseid}");
$cache->delete("g_c_s_ic{$courseid}");
}


public static function course_section_module_cache_updated($courseid, $cmid, $sectionid = 0) {
global $DB;

if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}

$cm = $DB->get_record("course_modules", ['id' => $cmid]);
// Clear cache.
$cache = format_designer_get_cache_object();
$cache->delete_vaild_section_completed_cache($courseid);
$cache->delete_user_section_completed_cache($courseid);
$cache->delete_course_progress_uncompletion_criteria($courseid);
$cache->delete_due_overdue_activities_count($courseid);
$cache->delete_criteria_progress($courseid);
$cache->delete("fdo_cm_j_{$courseid}");
$cache->delete("g_c_a{$courseid}");
$cache->delete("g_c_s_ic{$courseid}");
}

public static function course_section_cache_updated($courseid, $sectionid) {
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
// Clear cache.
$cache = format_designer_get_cache_object();
$cache->delete_vaild_section_completed_cache($courseid, $sectionid);
$cache->delete_user_section_completed_cache($courseid, $sectionid);
$cache->delete_due_overdue_activities_count($courseid);
$cache->delete_course_progress_uncompletion_criteria($courseid);
$cache->delete_criteria_progress($courseid);
$cache->delete("g_c_a{$courseid}");
$cache->delete("g_c_s_ic{$courseid}");
}
}
20 changes: 9 additions & 11 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ public function get_course_staff_users($course) {

// Course context.
$coursecontext = \context_course::instance($course->id);

// List of staff users ids for the course.
$staffids = $this->get_staffs_users($course);

// Staff users are not found.
if (empty($staffids)) {
return [];
Expand All @@ -108,6 +110,7 @@ public function get_course_staff_users($course) {
}
}
}

$roles = get_user_roles($coursecontext, $userid, false);
array_map(function($role) {
$role->name = role_get_name($role);
Expand Down Expand Up @@ -154,17 +157,12 @@ public function get_course_staff_users($course) {
*/
protected function get_staffs_users($course) {
$staffids = [];
$staffroleids = explode(",", $course->coursestaff);
$enrolusers = enrol_get_course_users_roles($course->id);
if (!empty($enrolusers)) {
foreach ($enrolusers as $userid => $roles) {
foreach ($staffroleids as $staffid) {
if (isset($roles[$staffid])) {
$staffids[] = $userid;
}
}
}
if (!empty($course->coursestaff)) {
$staffroleids = explode(",", $course->coursestaff);
$coursecontext = \context_course::instance($course->id);
$staffids = get_role_users($staffroleids, $coursecontext, false, 'ra.id, u.lastname, u.firstname, u.id');
}
return array_unique($staffids);
return array_keys($staffids);

}
}
Loading

0 comments on commit 6cf5068

Please sign in to comment.