Merge branch 'MDL-76716-m402' of https://github.com/stefangub/moodle into MOODLE_402_STABLE

This commit is contained in:
Huong Nguyen
2024-01-25 08:51:44 +07:00
3 changed files with 84 additions and 73 deletions
+41 -37
View File
@@ -294,59 +294,63 @@ class grade_category extends grade_object {
public function delete($source=null) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$grade_item = $this->load_grade_item();
try {
$transaction = $DB->start_delegated_transaction();
$grade_item = $this->load_grade_item();
if ($this->is_course_category()) {
if ($this->is_course_category()) {
if ($categories = grade_category::fetch_all(array('courseid'=>$this->courseid))) {
if ($categories = self::fetch_all(['courseid' => $this->courseid])) {
foreach ($categories as $category) {
foreach ($categories as $category) {
if ($category->id == $this->id) {
continue; // do not delete course category yet
if ($category->id == $this->id) {
continue; // Do not delete course category yet.
}
$category->delete($source);
}
$category->delete($source);
}
}
if ($items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
if ($items = grade_item::fetch_all(['courseid' => $this->courseid])) {
foreach ($items as $item) {
foreach ($items as $item) {
if ($item->id == $grade_item->id) {
continue; // do not delete course item yet
if ($item->id == $grade_item->id) {
continue; // Do not delete course item yet.
}
$item->delete($source);
}
}
} else {
$this->force_regrading();
$parent = $this->load_parent_category();
// Update children's categoryid/parent field first.
if ($children = grade_item::fetch_all(['categoryid' => $this->id])) {
foreach ($children as $child) {
$child->set_parent($parent->id);
}
}
if ($children = self::fetch_all(['parent' => $this->id])) {
foreach ($children as $child) {
$child->set_parent($parent->id);
}
$item->delete($source);
}
}
} else {
$this->force_regrading();
// First delete the attached grade item and grades.
$grade_item->delete($source);
$parent = $this->load_parent_category();
// Delete category itself.
$success = parent::delete($source);
// Update children's categoryid/parent field first
if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
foreach ($children as $child) {
$child->set_parent($parent->id);
}
}
if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
foreach ($children as $child) {
$child->set_parent($parent->id);
}
}
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
}
// first delete the attached grade item and grades
$grade_item->delete($source);
// delete category itself
$success = parent::delete($source);
$transaction->allow_commit();
return $success;
}
+11 -9
View File
@@ -1147,16 +1147,18 @@ class grade_grade extends grade_object {
*/
public function delete($source = null) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$success = parent::delete($source);
// If the grade was deleted successfully trigger a grade_deleted event.
if ($success && !empty($this->grade_item)) {
\core\event\grade_deleted::create_from_grade($this)->trigger();
try {
$transaction = $DB->start_delegated_transaction();
$success = parent::delete($source);
// If the grade was deleted successfully trigger a grade_deleted event.
if ($success && !empty($this->grade_item)) {
$this->load_grade_item();
\core\event\grade_deleted::create_from_grade($this)->trigger();
}
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
}
$transaction->allow_commit();
return $success;
}
+32 -27
View File
@@ -431,16 +431,18 @@ class grade_item extends grade_object {
public function delete($source=null) {
global $DB;
$transaction = $DB->start_delegated_transaction();
$this->delete_all_grades($source);
$success = parent::delete($source);
$transaction->allow_commit();
if ($success) {
$event = \core\event\grade_item_deleted::create_from_grade_item($this);
$event->trigger();
try {
$transaction = $DB->start_delegated_transaction();
$this->delete_all_grades($source);
$success = parent::delete($source);
if ($success) {
$event = \core\event\grade_item_deleted::create_from_grade_item($this);
$event->trigger();
}
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
}
return $success;
}
@@ -453,27 +455,30 @@ class grade_item extends grade_object {
public function delete_all_grades($source=null) {
global $DB;
$transaction = $DB->start_delegated_transaction();
try {
$transaction = $DB->start_delegated_transaction();
if (!$this->is_course_item()) {
$this->force_regrading();
}
if ($grades = grade_grade::fetch_all(array('itemid'=>$this->id))) {
foreach ($grades as $grade) {
$grade->delete($source);
if (!$this->is_course_item()) {
$this->force_regrading();
}
if ($grades = grade_grade::fetch_all(['itemid' => $this->id])) {
foreach ($grades as $grade) {
$grade->delete($source);
}
}
// Delete all the historical files.
// We only support feedback files for modules atm.
if ($this->is_external_item()) {
$fs = new file_storage();
$fs->delete_area_files($this->get_context()->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
}
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
}
// Delete all the historical files.
// We only support feedback files for modules atm.
if ($this->is_external_item()) {
$fs = new file_storage();
$fs->delete_area_files($this->get_context()->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
}
$transaction->allow_commit();
return true;
}