/**
  * Recursively delete category including all subcategories and courses
  *
  * Function {@link coursecat::can_delete_full()} MUST be called prior
  * to calling this function because there is no capability check
  * inside this function
  *
  * @param boolean $showfeedback display some notices
  * @return array return deleted courses
  * @throws moodle_exception
  */
 public function delete_full($showfeedback = true)
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/gradelib.php';
     require_once $CFG->libdir . '/questionlib.php';
     require_once $CFG->dirroot . '/cohort/lib.php';
     $deletedcourses = array();
     // Get children. Note, we don't want to use cache here because it would be rebuilt too often.
     $children = $DB->get_records('course_categories', array('parent' => $this->id), 'sortorder ASC');
     foreach ($children as $record) {
         $coursecat = new coursecat($record);
         $deletedcourses += $coursecat->delete_full($showfeedback);
     }
     if ($courses = $DB->get_records('course', array('category' => $this->id), 'sortorder ASC')) {
         foreach ($courses as $course) {
             if (!delete_course($course, false)) {
                 throw new moodle_exception('cannotdeletecategorycourse', '', '', $course->shortname);
             }
             $deletedcourses[] = $course;
         }
     }
     // Move or delete cohorts in this context.
     cohort_delete_category($this);
     // Now delete anything that may depend on course category context.
     grade_course_category_delete($this->id, 0, $showfeedback);
     if (!question_delete_course_category($this, 0, $showfeedback)) {
         throw new moodle_exception('cannotdeletecategoryquestions', '', '', $this->get_formatted_name());
     }
     // Finally delete the category and it's context.
     $DB->delete_records('course_categories', array('id' => $this->id));
     $coursecatcontext = context_coursecat::instance($this->id);
     $coursecatcontext->delete();
     cache_helper::purge_by_event('changesincoursecat');
     // Trigger a course category deleted event.
     /* @var \core\event\course_category_deleted $event */
     $event = \core\event\course_category_deleted::create(array('objectid' => $this->id, 'context' => $coursecatcontext, 'other' => array('name' => $this->name)));
     $event->set_coursecat($this);
     $event->trigger();
     // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
     if ($this->id == $CFG->defaultrequestcategory) {
         set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0)));
     }
     return $deletedcourses;
 }
Exemplo n.º 2
0
 /**
  * Recursively delete category including all subcategories and courses
  *
  * Function {@link coursecat::can_delete_full()} MUST be called prior
  * to calling this function because there is no capability check
  * inside this function
  *
  * @param boolean $showfeedback display some notices
  * @return array return deleted courses
  */
 public function delete_full($showfeedback = true)
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/gradelib.php';
     require_once $CFG->libdir . '/questionlib.php';
     require_once $CFG->dirroot . '/cohort/lib.php';
     $deletedcourses = array();
     // Get children. Note, we don't want to use cache here because
     // it would be rebuilt too often
     $children = $DB->get_records('course_categories', array('parent' => $this->id), 'sortorder ASC');
     foreach ($children as $record) {
         $coursecat = new coursecat($record);
         $deletedcourses += $coursecat->delete_full($showfeedback);
     }
     if ($courses = $DB->get_records('course', array('category' => $this->id), 'sortorder ASC')) {
         foreach ($courses as $course) {
             if (!delete_course($course, false)) {
                 throw new moodle_exception('cannotdeletecategorycourse', '', '', $course->shortname);
             }
             $deletedcourses[] = $course;
         }
     }
     // move or delete cohorts in this context
     cohort_delete_category($this);
     // now delete anything that may depend on course category context
     grade_course_category_delete($this->id, 0, $showfeedback);
     if (!question_delete_course_category($this, 0, $showfeedback)) {
         throw new moodle_exception('cannotdeletecategoryquestions', '', '', $this->get_formatted_name());
     }
     // finally delete the category and it's context
     $DB->delete_records('course_categories', array('id' => $this->id));
     delete_context(CONTEXT_COURSECAT, $this->id);
     add_to_log(SITEID, "category", "delete", "index.php", "{$this->name} (ID {$this->id})");
     cache_helper::purge_by_event('changesincoursecat');
     events_trigger('course_category_deleted', $this);
     // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
     if ($this->id == $CFG->defaultrequestcategory) {
         set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0)));
     }
     return $deletedcourses;
 }