Esempio n. 1
0
    /**
     * Delete courses
     *
     * @param array $courseids A list of course ids
     * @since Moodle 2.2
     */
    public static function delete_courses($courseids) {
        global $CFG, $DB;
        require_once($CFG->dirroot."/course/lib.php");

        // Parameter validation.
        $params = self::validate_parameters(self::delete_courses_parameters(), array('courseids'=>$courseids));

        $transaction = $DB->start_delegated_transaction();

        foreach ($params['courseids'] as $courseid) {
            $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);

            // Check if the context is valid.
            $coursecontext = context_course::instance($course->id);
            self::validate_context($coursecontext);

            // Check if the current user has enought permissions.
            if (!can_delete_course($courseid)) {
                throw new moodle_exception('cannotdeletecategorycourse', 'error',
                    '', format_string($course->fullname)." (id: $courseid)");
            }

            delete_course($course, false);
        }

        $transaction->allow_commit();

        return null;
    }
 /**
  * Returns true if the current user can delete this course.
  *
  * Note: this function does not check that the current user can access the course.
  * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
  *
  * @return bool
  */
 public function can_delete()
 {
     return can_delete_course($this->id);
 }
Esempio n. 3
0
 $up = $count > 1 || !$atfirstpage;
 $down = $count < $numcourses || !$atlastpage;
 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
 echo '<tr>';
 $coursename = get_course_display_name_for_list($acourse);
 echo '<td><a ' . $linkcss . ' href="view.php?id=' . $acourse->id . '">' . format_string($coursename) . '</a></td>';
 if ($editingon) {
     echo '<td>';
     if (has_capability('moodle/course:update', $coursecontext)) {
         echo $OUTPUT->action_icon(new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category')), new pix_icon('t/edit', $strsettings));
     }
     // role assignment link
     if (has_capability('moodle/course:enrolreview', $coursecontext)) {
         echo $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $acourse->id)), new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
     }
     if (can_delete_course($acourse->id)) {
         echo $OUTPUT->action_icon(new moodle_url('/course/delete.php', array('id' => $acourse->id)), new pix_icon('t/delete', $strdelete));
     }
     // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
     if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
         if (!empty($acourse->visible)) {
             echo $OUTPUT->action_icon(new moodle_url('/course/category.php', array('id' => $category->id, 'page' => $page, 'perpage' => $perpage, 'hide' => $acourse->id, 'sesskey' => sesskey())), new pix_icon('t/hide', $strhide));
         } else {
             echo $OUTPUT->action_icon(new moodle_url('/course/category.php', array('id' => $category->id, 'page' => $page, 'perpage' => $perpage, 'show' => $acourse->id, 'sesskey' => sesskey())), new pix_icon('t/show', $strshow));
         }
     }
     if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
         echo $OUTPUT->action_icon(new moodle_url('/backup/backup.php', array('id' => $acourse->id)), new pix_icon('t/backup', $strbackup));
     }
     if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
         echo $OUTPUT->action_icon(new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id)), new pix_icon('t/restore', $strrestore));
Esempio n. 4
0
 * Admin-only code to delete a course utterly.
 *
 * @package core_course
 * @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../config.php';
require_once $CFG->dirroot . '/course/lib.php';
$id = required_param('id', PARAM_INT);
// Course ID.
$delete = optional_param('delete', '', PARAM_ALPHANUM);
// Confirmation hash.
$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
$coursecontext = context_course::instance($course->id);
require_login();
if ($SITE->id == $course->id || !can_delete_course($id)) {
    // Can not delete frontpage or don't have permission to delete the course.
    print_error('cannotdeletecourse');
}
$categorycontext = context_coursecat::instance($course->category);
$PAGE->set_url('/course/delete.php', array('id' => $id));
$PAGE->set_context($categorycontext);
$PAGE->set_pagelayout('admin');
navigation_node::override_active_url(new moodle_url('/course/management.php', array('categoryid' => $course->category)));
$courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
$coursefullname = format_string($course->fullname, true, array('context' => $coursecontext));
$categoryurl = new moodle_url('/course/management.php', array('categoryid' => $course->category));
// Check if we've got confirmation.
if ($delete === md5($course->timemodified)) {
    // We do - time to delete the course.
    require_sesskey();
Esempio n. 5
0
 /**
  * Delete courses
  *
  * @param array $courseids A list of course ids
  * @since Moodle 2.2
  */
 public static function delete_courses($courseids)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . "/course/lib.php";
     // Parameter validation.
     $params = self::validate_parameters(self::delete_courses_parameters(), array('courseids' => $courseids));
     $warnings = array();
     foreach ($params['courseids'] as $courseid) {
         $course = $DB->get_record('course', array('id' => $courseid));
         if ($course === false) {
             $warnings[] = array('item' => 'course', 'itemid' => $courseid, 'warningcode' => 'unknowncourseidnumber', 'message' => 'Unknown course ID ' . $courseid);
             continue;
         }
         // Check if the context is valid.
         $coursecontext = context_course::instance($course->id);
         self::validate_context($coursecontext);
         // Check if the current user has permission.
         if (!can_delete_course($courseid)) {
             $warnings[] = array('item' => 'course', 'itemid' => $courseid, 'warningcode' => 'cannotdeletecourse', 'message' => 'You do not have the permission to delete this course' . $courseid);
             continue;
         }
         if (delete_course($course, false) === false) {
             $warnings[] = array('item' => 'course', 'itemid' => $courseid, 'warningcode' => 'cannotdeletecategorycourse', 'message' => 'Course ' . $courseid . ' failed to be deleted');
             continue;
         }
     }
     fix_course_sortorder();
     return array('warnings' => $warnings);
 }
Esempio n. 6
0
 /**
  * Returns true if user can delete current category and all its contents
  *
  * To be able to delete course category the user must have permission
  * 'moodle/category:manage' in ALL child course categories AND
  * be able to delete all courses
  *
  * @return bool
  */
 public function can_delete_full()
 {
     global $DB;
     if (!$this->id) {
         // fool-proof
         return false;
     }
     $context = context_coursecat::instance($this->id);
     if (!$this->is_uservisible() || !has_capability('moodle/category:manage', $context)) {
         return false;
     }
     // Check all child categories (not only direct children)
     $sql = context_helper::get_preload_record_columns_sql('ctx');
     $childcategories = $DB->get_records_sql('SELECT c.id, c.visible, ' . $sql . ' FROM {context} ctx ' . ' JOIN {course_categories} c ON c.id = ctx.instanceid' . ' WHERE ctx.path like ? AND ctx.contextlevel = ?', array($context->path . '/%', CONTEXT_COURSECAT));
     foreach ($childcategories as $childcat) {
         context_helper::preload_from_record($childcat);
         $childcontext = context_coursecat::instance($childcat->id);
         if (!$childcat->visible && !has_capability('moodle/category:viewhiddencategories', $childcontext) || !has_capability('moodle/category:manage', $childcontext)) {
             return false;
         }
     }
     // Check courses
     $sql = context_helper::get_preload_record_columns_sql('ctx');
     $coursescontexts = $DB->get_records_sql('SELECT ctx.instanceid AS courseid, ' . $sql . ' FROM {context} ctx ' . 'WHERE ctx.path like :pathmask and ctx.contextlevel = :courselevel', array('pathmask' => $context->path . '/%', 'courselevel' => CONTEXT_COURSE));
     foreach ($coursescontexts as $ctxrecord) {
         context_helper::preload_from_record($ctxrecord);
         if (!can_delete_course($ctxrecord->courseid)) {
             return false;
         }
     }
     return true;
 }
Esempio n. 7
0
 function definition()
 {
     global $CFG, $DB;
     $mform =& $this->_form;
     $category = $this->_customdata;
     $categorycontext = context_coursecat::instance($category->id);
     $this->_category = $category;
     /// Check permissions, to see if it OK to give the option to delete
     /// the contents, rather than move elsewhere.
     /// Are there any subcategories of this one, can they be deleted?
     $candeletecontent = true;
     $tocheck = get_child_categories($category->id);
     $containscategories = !empty($tocheck);
     $categoryids = array($category->id);
     while (!empty($tocheck)) {
         $checkcat = array_pop($tocheck);
         $childcategoryids[] = $checkcat->id;
         $tocheck = $tocheck + get_child_categories($checkcat->id);
         $chcontext = context_coursecat::instance($checkcat->id);
         if ($candeletecontent && !has_capability('moodle/category:manage', $chcontext)) {
             $candeletecontent = false;
         }
     }
     /// Are there any courses in here, can they be deleted?
     list($test, $params) = $DB->get_in_or_equal($categoryids);
     $containedcourses = $DB->get_records_sql("SELECT id,1 FROM {course} c WHERE c.category {$test}", $params);
     $containscourses = false;
     if ($containedcourses) {
         $containscourses = true;
         foreach ($containedcourses as $courseid => $notused) {
             if ($candeletecontent && !can_delete_course($courseid)) {
                 $candeletecontent = false;
                 break;
             }
         }
     }
     /// Are there any questions in the question bank here?
     $containsquestions = question_context_has_any_questions($categorycontext);
     /// Get the list of categories we might be able to move to.
     $testcaps = array();
     if ($containscourses) {
         $testcaps[] = 'moodle/course:create';
     }
     if ($containscategories || $containsquestions) {
         $testcaps[] = 'moodle/category:manage';
     }
     $displaylist = array();
     $notused = array();
     if (!empty($testcaps)) {
         make_categories_list($displaylist, $notused, $testcaps, $category->id);
     }
     /// Now build the options.
     $options = array();
     if ($displaylist) {
         $options[0] = get_string('movecontentstoanothercategory');
     }
     if ($candeletecontent) {
         $options[1] = get_string('deleteallcannotundo');
     }
     /// Now build the form.
     $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', format_string($category->name, true, array('context' => $categorycontext))));
     if ($containscourses || $containscategories || $containsquestions) {
         if (empty($options)) {
             print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name, true, array('context' => $categorycontext)));
         }
         /// Describe the contents of this category.
         $contents = '<ul>';
         if ($containscategories) {
             $contents .= '<li>' . get_string('subcategories') . '</li>';
         }
         if ($containscourses) {
             $contents .= '<li>' . get_string('courses') . '</li>';
         }
         if ($containsquestions) {
             $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
         }
         $contents .= '</ul>';
         $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents);
         /// Give the options for what to do.
         $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
         if (count($options) == 1) {
             $optionkeys = array_keys($options);
             $option = reset($optionkeys);
             $mform->hardFreeze('fulldelete');
             $mform->setConstant('fulldelete', $option);
         }
         if ($displaylist) {
             $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
             if (in_array($category->parent, $displaylist)) {
                 $mform->setDefault('newparent', $category->parent);
             }
             $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
         }
     } else {
         $mform->addElement('hidden', 'fulldelete', 1);
         $mform->setType('fulldelete', PARAM_INT);
         $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
     }
     $mform->addElement('hidden', 'delete');
     $mform->setType('delete', PARAM_ALPHANUM);
     $mform->addElement('hidden', 'sure');
     $mform->setType('sure', PARAM_ALPHANUM);
     $mform->setDefault('sure', md5(serialize($category)));
     //--------------------------------------------------------------------------------
     $this->add_action_buttons(true, get_string('delete'));
 }
Esempio n. 8
0
$PAGE->set_url('/course/delete.php', array('id' => $id));
$PAGE->set_context(context_system::instance());
require_login();
$site = get_site();
$strdeletecourse = get_string("deletecourse");
$stradministration = get_string("administration");
$strcategories = get_string("categories");
if (!($course = $DB->get_record("course", array("id" => $id)))) {
    print_error("invalidcourseid");
}
if ($site->id == $course->id) {
    // can not delete frontpage!
    print_error("invalidcourseid");
}
$coursecontext = context_course::instance($course->id);
if (!can_delete_course($id)) {
    print_error('cannotdeletecourse');
}
$category = $DB->get_record("course_categories", array("id" => $course->category));
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
$categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id)));
$PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php/'));
$PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'));
$PAGE->navbar->add($categoryname, new moodle_url('/course/category.php', array('id' => $course->category)));
if (!$delete) {
    $strdeletecheck = get_string("deletecheck", "", $courseshortname);
    $strdeletecoursecheck = get_string("deletecoursecheck");
    $PAGE->navbar->add($strdeletecheck);
    $PAGE->set_title("{$site->shortname}: {$strdeletecheck}");
    $PAGE->set_heading($site->fullname);
    echo $OUTPUT->header();