示例#1
0
文件: lib.php 项目: numbas/moodle
/**
 * Delete category, but move contents to another category.
 * @param object $ccategory
 * @param int $newparentid category id
 * @return bool status
 */
function category_delete_move($category, $newparentid, $showfeedback = true)
{
    global $CFG, $DB, $OUTPUT;
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->dirroot . '/cohort/lib.php';
    if (!($newparentcat = $DB->get_record('course_categories', array('id' => $newparentid)))) {
        return false;
    }
    if ($children = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) {
        foreach ($children as $childcat) {
            move_category($childcat, $newparentcat);
        }
    }
    if ($courses = $DB->get_records('course', array('category' => $category->id), 'sortorder ASC', 'id')) {
        if (!move_courses(array_keys($courses), $newparentid)) {
            echo $OUTPUT->notification("Error moving courses");
            return false;
        }
        echo $OUTPUT->notification(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess');
    }
    // move or delete cohorts in this context
    cohort_delete_category($category);
    // now delete anything that may depend on course category context
    grade_course_category_delete($category->id, $newparentid, $showfeedback);
    if (!question_delete_course_category($category, $newparentcat, $showfeedback)) {
        echo $OUTPUT->notification(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess');
        return false;
    }
    // finally delete the category and it's context
    $DB->delete_records('course_categories', array('id' => $category->id));
    delete_context(CONTEXT_COURSECAT, $category->id);
    events_trigger('course_category_deleted', $category);
    echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess');
    return true;
}
示例#2
0
 public function test_cohort_delete_category()
 {
     global $DB;
     $this->resetAfterTest();
     $category = $this->getDataGenerator()->create_category();
     $cohort = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category->id)->id));
     cohort_delete_category($category);
     $this->assertTrue($DB->record_exists('cohort', array('id' => $cohort->id)));
     $newcohort = $DB->get_record('cohort', array('id' => $cohort->id));
     $this->assertEquals(context_system::instance()->id, $newcohort->contextid);
 }
 /**
  * Deletes a category and moves all content (children, courses and questions) to the new parent
  *
  * Note that this function does not check capabilities, {@link coursecat::can_move_content_to()}
  * must be called prior
  *
  * @param int $newparentid
  * @param bool $showfeedback
  * @return bool
  */
 public function delete_move($newparentid, $showfeedback = false)
 {
     global $CFG, $DB, $OUTPUT;
     require_once $CFG->libdir . '/gradelib.php';
     require_once $CFG->libdir . '/questionlib.php';
     require_once $CFG->dirroot . '/cohort/lib.php';
     // Get all objects and lists because later the caches will be reset so.
     // We don't need to make extra queries.
     $newparentcat = self::get($newparentid, MUST_EXIST, true);
     $catname = $this->get_formatted_name();
     $children = $this->get_children();
     $params = array('category' => $this->id);
     $coursesids = $DB->get_fieldset_select('course', 'id', 'category = :category ORDER BY sortorder ASC', $params);
     $context = $this->get_context();
     if ($children) {
         foreach ($children as $childcat) {
             $childcat->change_parent_raw($newparentcat);
             // Log action.
             $event = \core\event\course_category_updated::create(array('objectid' => $childcat->id, 'context' => $childcat->get_context()));
             $event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id));
             $event->trigger();
         }
         fix_course_sortorder();
     }
     if ($coursesids) {
         if (!move_courses($coursesids, $newparentid)) {
             if ($showfeedback) {
                 echo $OUTPUT->notification("Error moving courses");
             }
             return false;
         }
         if ($showfeedback) {
             echo $OUTPUT->notification(get_string('coursesmovedout', '', $catname), 'notifysuccess');
         }
     }
     // 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, $newparentid, $showfeedback);
     if (!question_delete_course_category($this, $newparentcat, $showfeedback)) {
         if ($showfeedback) {
             echo $OUTPUT->notification(get_string('errordeletingquestionsfromcategory', 'question', $catname), 'notifysuccess');
         }
         return false;
     }
     // Finally delete the category and it's context.
     $DB->delete_records('course_categories', array('id' => $this->id));
     $context->delete();
     // 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' => $context, 'other' => array('name' => $this->name)));
     $event->set_coursecat($this);
     $event->trigger();
     cache_helper::purge_by_event('changesincoursecat');
     if ($showfeedback) {
         echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $catname), 'notifysuccess');
     }
     // 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 true;
 }
示例#4
0
 /**
  * Deletes a category and moves all content (children, courses and questions) to the new parent
  *
  * Note that this function does not check capabilities, {@link coursecat::can_move_content_to()}
  * must be called prior
  *
  * @param int $newparentid
  * @param bool $showfeedback
  * @return bool
  */
 public function delete_move($newparentid, $showfeedback = false)
 {
     global $CFG, $DB, $OUTPUT;
     require_once $CFG->libdir . '/gradelib.php';
     require_once $CFG->libdir . '/questionlib.php';
     require_once $CFG->dirroot . '/cohort/lib.php';
     // get all objects and lists because later the caches will be reset so
     // we don't need to make extra queries
     $newparentcat = self::get($newparentid, MUST_EXIST, true);
     $catname = $this->get_formatted_name();
     $children = $this->get_children();
     $coursesids = $DB->get_fieldset_select('course', 'id', 'category = :category ORDER BY sortorder ASC', array('category' => $this->id));
     $context = context_coursecat::instance($this->id);
     if ($children) {
         foreach ($children as $childcat) {
             $childcat->change_parent_raw($newparentcat);
             // Log action.
             add_to_log(SITEID, "category", "move", "editcategory.php?id={$childcat->id}", $childcat->id);
         }
         fix_course_sortorder();
     }
     if ($coursesids) {
         if (!move_courses($coursesids, $newparentid)) {
             if ($showfeedback) {
                 echo $OUTPUT->notification("Error moving courses");
             }
             return false;
         }
         if ($showfeedback) {
             echo $OUTPUT->notification(get_string('coursesmovedout', '', $catname), 'notifysuccess');
         }
     }
     // 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, $newparentid, $showfeedback);
     if (!question_delete_course_category($this, $newparentcat, $showfeedback)) {
         if ($showfeedback) {
             echo $OUTPUT->notification(get_string('errordeletingquestionsfromcategory', 'question', $catname), 'notifysuccess');
         }
         return false;
     }
     // finally delete the category and it's context
     $DB->delete_records('course_categories', array('id' => $this->id));
     $context->delete();
     add_to_log(SITEID, "category", "delete", "index.php", "{$this->name} (ID {$this->id})");
     events_trigger('course_category_deleted', $this);
     cache_helper::purge_by_event('changesincoursecat');
     if ($showfeedback) {
         echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $catname), 'notifysuccess');
     }
     // 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 true;
 }