Skip to content

Commit

Permalink
Bug fixes - DES-903.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasanthlmsace committed Nov 27, 2024
1 parent b57ea89 commit c93f4a2
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 201 deletions.
129 changes: 129 additions & 0 deletions classes/cache/loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?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.
* @param mixed $courseid
* @return void
*/
public function delete_vaild_section_completed_cache($courseid) {
$store = $this->get_store();
$prefix = "v_s_c_c_{$courseid}";
if ($list = $store->find_by_prefix($prefix)) {
$keys = array_map(function($key) {
$key = current(explode('-', $key));
return $key;
}, $list);
$this->delete_many($keys);
}
}

/**
* Delete user section completed cache.
* @param mixed $courseid
* @return void
*/
public function delete_user_section_completed_cache($courseid) {
$prefix = "s_c_c_{$courseid}";
$this->delete_prefix_cache($prefix);
}

/**
* Delete due overdue activities count.
* @param mixed $courseid
* @param mixed $userid
* @return void
*/
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);
}

/**
* Deleted course uncompletion criteria.
* @param mixed $courseid
* @param mixed $userid
* @return void
*/
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);
}

/**
* Deleted criteria progress,
* @param mixed $courseid
* @param mixed $userid
* @return void
*/
public function delete_criteria_progress($courseid, $userid = 0) {
$prefix = "c_p_c{$courseid}";
if ($userid) {
$prefix .= "_u_{$userid}";
}
$this->delete_prefix_cache($prefix);
}

/**
* Delete prerequisites courses.
* @return void
*/
public function delete_prerequisites_courses() {
$prefix = "data_prereq_main_c";
$this->delete_prefix_cache($prefix);
}

/**
* Delete the prefix cache.
* @param mixed $prefix
* @return void
*/
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);
}
}
}
214 changes: 207 additions & 7 deletions classes/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

namespace format_designer;

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

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

/**
* Designer format event observer.
*/
Expand All @@ -42,14 +46,17 @@ 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 @@ -64,6 +71,92 @@ public static function course_section_created($event) {
}
}


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

/**
* Event handle course completion update.
* @param mixed $event
* @return bool
*/
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);
}

/**
* Event handle course update.
* @param mixed $event
* @return bool
*/
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);
}

/**
* Event handle course complete.
* @param mixed $event
* @return bool
*/
public static function course_completed($event) {
global $DB;
$userid = $event->relateduserid;
$courseid = $event->courseid;
if (course_get_format($courseid)->get_course()->format !== 'designer') {
return true;
}
self::course_user_cache_updated($courseid, $userid);
// Related the course data cache deleted.
$records = $DB->get_records('course_completion_criteria', ['courseinstance' => $courseid]);
foreach ($records as $record) {
self::course_user_cache_updated($record->course, $userid);
}
}

/**
* Event course completion updated.
* @param mixed $event
* @return bool
*/
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);
}

/**
* Event Course module created
* @param mixed $event
* @return void
*/
public static function course_module_created($event) {
self::course_section_module_cache_updated($event->courseid, $event->objectid);
}

/**
* After course module deleted, deleted the format_designer_options data related to the format_designer options.
*
Expand All @@ -73,19 +166,126 @@ public static function course_section_created($event) {
public static function course_module_deleted($event) {
global $DB;
$courseid = $event->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);
}

/**
* After course deleted, deleted the format_designer_options data related to the format_designer options.
*
* @param object $event
* Event course module update.
* @param mixed $event
* @return void
*/
public static function course_deleted($event) {
public static function course_module_updated($event) {
self::course_section_module_cache_updated($event->courseid, $event->objectid);
}

/**
* Event course section deleted.
* @param mixed $event
* @return void
*/
public static function course_section_deleted($event) {
$data = $event->get_data();
$sectionid = $data['objectid'];
$courseid = $data['courseid'];
self::course_section_cache_updated($courseid, $sectionid);
}

/**
* Event course section updated.
* @param mixed $event
* @return void
*/
public static function course_section_updated($event) {
$data = $event->get_data();
$sectionid = $data['objectid'];
$courseid = $data['courseid'];
self::course_section_cache_updated($courseid, $sectionid);
}

/**
* Course cache updated.
* @param mixed $courseid
* @return void
*/
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}");
}

/**
* Course user cache updated.
* @param mixed $courseid
* @param mixed $userid
* @return void
*/
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}");
}

/**
* Handled the Course section module cache updated.
* @param mixed $courseid
* @param mixed $cmid
* @return bool
*/
public static function course_section_module_cache_updated($courseid, $cmid) {
global $DB;
$courseid = $event->courseid;
$DB->delete_records('format_designer_options', ['courseid' => $courseid]);

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}");
}

/**
* Course section cache updated.
* @param mixed $courseid
* @param mixed $sectionid
* @return bool
*/
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}");
}
}
Loading

0 comments on commit c93f4a2

Please sign in to comment.