Skip to content

Commit

Permalink
Issue ModernLMS#22: Fixed course condition when duplicating an activity
Browse files Browse the repository at this point in the history
- Removed update_after_restore as its useless
- Added include_after_restore in the case of missing course
- Renamed $this->cmid to $this->courseid to avoid some confusion
  • Loading branch information
Yohan Velay committed Feb 2, 2024
1 parent cdc7c4f commit 69e5c26
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
require_once($CFG->libdir . '/completionlib.php');

class condition extends \core_availability\condition {
/** @var int ID of module that this depends on */
protected $cmid;
/** @var int ID of course that this depends on */
protected $courseid;

/** @var int Expected completion type (one of the COMPLETE_xx constants) */
protected $expectedcompletion;
Expand All @@ -44,9 +44,9 @@ class condition extends \core_availability\condition {
* @throws \coding_exception If invalid data structure.
*/
public function __construct($structure) {
// Get cmid.
// Get courseid.
if (isset($structure->cm) && is_number($structure->cm)) {
$this->cmid = (int)$structure->cm;
$this->courseid = (int)$structure->cm;
} else {
throw new \coding_exception('Missing or invalid ->cm for completion condition');
}
Expand All @@ -62,7 +62,7 @@ public function __construct($structure) {

public function save() {
return (object)array('type' => 'othercompleted',
'cm' => $this->cmid, 'e' => $this->expectedcompletion);
'course' => $this->courseid, 'e' => $this->expectedcompletion);
}

/**
Expand All @@ -71,19 +71,19 @@ public function save() {
* Intended for unit testing, as normally the JSON values are constructed
* by JavaScript code.
*
* @param int $cmid Course id of other activity
* @param int $courseid Course id of other activity
* @param int $expectedcompletion Expected completion value (COMPLETION_xx)
*/
public static function get_json($cmid, $expectedcompletion) {
return (object)array('type' => 'othercompleted', 'cm' => (int)$cmid,
public static function get_json($courseid, $expectedcompletion) {
return (object)array('type' => 'othercompleted', 'course' => (int)$courseid,
'e' => (int)$expectedcompletion);
}

public function is_available($not, \core_availability\info $info, $grabthelot, $userid) {

global $DB;

$course = $this->cmid;
$course = $this->courseid;
$sqlcoursecomplete = "SELECT * FROM {course_completions} as a WHERE a.course = $course AND a.userid = $userid";
$datacompletes = $DB->get_records_sql($sqlcoursecomplete);
$allow = false;
Expand Down Expand Up @@ -120,7 +120,7 @@ public function get_description($full, $not, \core_availability\info $info) {
$modc = get_courses();

foreach ($modc as $modcs) {
if($modcs->id == $this->cmid){
if($modcs->id == $this->courseid){
$modname = $modcs->fullname;
}
}
Expand Down Expand Up @@ -158,26 +158,14 @@ protected function get_debug_string() {
default:
throw new \coding_exception('Unexpected expected completion');
}
return 'cm' . $this->cmid . ' ' . $type;
return 'course' . $this->courseid . ' ' . $type;
}

public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name) {
public function include_after_restore($restoreid, $courseid, \base_logger $logger, $name, \base_task $task) {
global $DB;
$rec = \restore_dbops::get_backup_ids_record($restoreid, 'course_module', $this->cmid);
if (!$rec || !$rec->newitemid) {
// If we are on the same course (e.g. duplicate) then we can just
// use the existing one.
if ($DB->record_exists('course_modules',
array('id' => $this->cmid, 'course' => $courseid))) {
return false;
}
// Otherwise it's a warning.
$this->cmid = 0;
$logger->process('Restored item (' . $name .
') has availability condition on module that was not restored',
\backup::LOG_WARNING);
} else {
$this->cmid = (int)$rec->newitemid;

if (!$DB->record_exists('course', ['id' => $this->courseid])) {
return false;
}
return true;
}
Expand Down Expand Up @@ -207,7 +195,7 @@ public static function completion_value_used($course, $cmid) {
$ci = new \core_availability\info_module($othercm);
$tree = $ci->get_availability_tree();
foreach ($tree->get_all_children('availability_othercompleted\condition') as $cond) {
self::$modsusedincondition[$course->id][$cond->cmid] = true;
self::$modsusedincondition[$course->id][$cond->courseid] = true;
}
}

Expand All @@ -219,7 +207,7 @@ public static function completion_value_used($course, $cmid) {
$ci = new \core_availability\info_section($section);
$tree = $ci->get_availability_tree();
foreach ($tree->get_all_children('availability_othercompleted\condition') as $cond) {
self::$modsusedincondition[$course->id][$cond->cmid] = true;
self::$modsusedincondition[$course->id][$cond->courseid] = true;
}
}
}
Expand All @@ -234,8 +222,8 @@ public static function wipe_static_cache() {
}

public function update_dependency_id($table, $oldid, $newid) {
if ($table === 'course_modules' && (int)$this->cmid === (int)$oldid) {
$this->cmid = $newid;
if ($table === 'course_modules' && (int)$this->courseid === (int)$oldid) {
$this->courseid = $newid;
return true;
} else {
return false;
Expand Down

0 comments on commit 69e5c26

Please sign in to comment.