New feature: you can now reset a course simply and easily using the new

"Reset" item on the course admin menu.

This allows you to delete user data from the course without touching the
course itself.
This commit is contained in:
moodler
2006-03-21 07:34:33 +00:00
parent 6305db8858
commit ffd0fbb7ac
6 changed files with 205 additions and 47 deletions
+104
View File
@@ -0,0 +1,104 @@
<?php // $Id$
/*
resetcourse.php - Mark Flach and moodle.com
The purpose of this feature is to quickly remove all user related data from a course
in order to make it available for a new semester. This feature can handle the removal
of general course data like students, teachers, logs, events and groups as well as module
specific data. Each module must be modified to take advantage of this new feature.
The feature will also reset the start date of the course if necessary.
*/
require('../config.php');
$id = required_param('id', PARAM_INT);
if (! $course = get_record('course', 'id', $id)) {
error("Course is misconfigured");
}
if (!isteacheredit($course->id)) {
error('Only editing teachers can use this script');
}
$strreset = get_string('reset');
$strresetcourse = get_string('resetcourse');
$strremove = get_string('remove');
print_header($course->fullname.': '.$strresetcourse, $course->fullname.': '.$strresetcourse,
'<a href="view.php?id='.$course->id.'">'.$course->shortname.'</a> -> '.$strresetcourse);
print_simple_box_start();
print_heading($strresetcourse);
/// If we have data, then process it.
if ($data = data_submitted() and confirm_sesskey()) {
$data->courseid = $course->id;
reset_course_userdata($data, true);
if (!empty($data->reset_start_date)) {
if (set_field('course', 'startdate',
make_timestamp($data->startyear, $data->startmonth, $data->startday),
'id', $course->id)) {
notify(get_string('datechanged'), 'notifysuccess');
}
}
print_continue('view.php?id='.$course->id); // Back to course page
print_simple_box_end();
print_footer($course);
exit;
}
/// Print forms so the user can make choices about what to delete
print_simple_box(get_string('resetinfo'), 'center', '60%');
echo '<form name="reset" action="reset.php" method="POST">';
print_heading(get_string('course'), 'left', 3);
echo '<div class="courseinfo">';
echo $strremove.':<br />';
print_checkbox('reset_students', 1, true, get_string('students'), '', ''); echo '<br />';
print_checkbox('reset_teachers', 1, true, get_string('teachers'), '', ''); echo '<br />';
print_checkbox('reset_events', 1, true, get_string('courseevents', 'calendar'), '', ''); echo '<br />';
print_checkbox('reset_logs', 1, true, get_string('logs'), '', ''); echo '<br />';
print_checkbox('reset_groups', 1, true, get_string('groups'), '', ''); echo '<br />';
print_checkbox('reset_start_date', 1, true, get_string('startdate'), '', '');
print_date_selector('startday', 'startmonth', 'startyear');
helpbutton('coursestartdate', get_string('startdate'));
echo '</div>';
// Check each module and see if there is specific data to be removed
if ($allmods = get_records('modules') ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
$mod_reset_course_form = $modname .'_reset_course_form';
if (file_exists($modfile)) {
@include_once($modfile);
if (function_exists($mod_reset_course_form)) {
print_heading(get_string('modulenameplural', $modname), 'left', 3);
echo '<div class="'.$modname.'info">';
$mod_reset_course_form($course);
echo '</div>';
}
}
}
} else {
error('No modules are installed!');
}
echo '<input name="id" value="'.$course->id.'" type="hidden" /></p>';
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" /></p>';
echo '<p align="center"><input name="submit" value="'.$strresetcourse.'" type="submit" /></p>';
echo '</form>';
print_simple_box_end();
print_footer($course);
?>
+1
View File
@@ -177,6 +177,7 @@ $string['repliesmany'] = '$a replies so far';
$string['repliesone'] = '$a reply so far';
$string['reply'] = 'Reply';
$string['replyforum'] = 'Reply to forum';
$string['resetforums'] = 'Remove all posts from these types of forums';
$string['rsssubscriberssdiscussions'] = 'Display the RSS feed for \'$a\' discussions';
$string['rsssubscriberssposts'] = 'Display the RSS feed for \'$a\' posts';
$string['search'] = 'Search';
+8 -2
View File
@@ -225,13 +225,13 @@ $string['coursebackup'] = 'Course backup';
$string['coursecategories'] = 'Course categories';
$string['coursecategory'] = 'Course category';
$string['coursecreators'] = 'Course creators';
$string['coursemessage'] = 'Message course users';
$string['coursenotaccessible'] = 'This course does not allow public access';
$string['coursefiles'] = 'Course files';
$string['courseformats'] = 'Course formats';
$string['coursegrades'] = 'Course grades';
$string['courseimportnotaught'] = 'You don\'t seem to be an editing teacher in any other courses, there are no courses for you to import from.';
$string['courseinfo'] = 'Course info';
$string['coursemessage'] = 'Message course users';
$string['coursenotaccessible'] = 'This course does not allow public access';
$string['courseoverview'] = 'Course overview';
$string['coursereject'] = 'Reject a course request';
$string['courserejected'] = 'Course has been rejected and the requester has been notified.';
@@ -290,6 +290,7 @@ $string['databaseupgradeblocks'] = 'Blocks version is now $a';
$string['databaseupgradelocal'] = 'Local database customisations version is now $a';
$string['databaseupgrades'] = 'Upgrading database';
$string['date'] = 'Date';
$string['datechanged'] = 'Date changed';
$string['datemostrecentfirst'] = 'Date - most recent first';
$string['datemostrecentlast'] = 'Date - most recent last';
$string['day'] = 'day';
@@ -1038,6 +1039,7 @@ $string['registrationno'] = 'No, I do not want to receive email';
$string['registrationsend'] = 'Send registration information to moodle.org';
$string['registrationyes'] = 'Yes, please notify me about important issues';
$string['reject'] = 'Reject';
$string['remove'] = 'Remove';
$string['removeadmin'] = 'Remove admin';
$string['removecreator'] = 'Remove course creator';
$string['removestudent'] = 'Remove student';
@@ -1052,6 +1054,10 @@ $string['requestreason'] = 'Reason for course request';
$string['required'] = 'Required';
$string['requireskey'] = 'This course requires an enrolment key';
$string['requirespayment'] = 'This course requires payment for access';
$string['reset'] = 'Reset';
$string['resetcourse'] = 'Reset course';
$string['resetinfo'] = 'This page allows you to empty a course of user data, while retaining the activities and other settings. Please be warned that by choosing items below and submitting this page you will delete your chosen user data from this course forever!';
$string['resetstartdate'] = 'Reset start date';
$string['resettodefaults'] = 'Reset to defaults';
$string['resortcoursesbyname'] = 'Re-sort courses by name';
$string['resources'] = 'Resources';
+35 -45
View File
@@ -3261,35 +3261,26 @@ function remove_course_contents($courseid, $showfeedback=true) {
}
/**
* This function will empty a course of user data as much as
* possible. It will retain the activities and the structure
* of the course.
*
* @uses $CFG
* @param int $courseid The id of the course that is being emptied of user data.
* @param bool $showfeedback Whether to display notifications of each action the function performs.
* @param bool $removestudents Whether to remove matching records from the user_students and groups_members table.
* @param bool $removeteachers Whether to remove matching records from the user_teachers table.
* @param bool $removegroups Whether to remove matching records from the groups table.
* @param bool $removeevents Whether to remove matching records from the event table.
* @param bool $removelogs Whether to remove matching records from the log table.
* @return bool true if all the removals succeeded. false if there were any failures. If this
* method returns false, some of the removals will probably have succeeded, and others
* failed, but you have no way of knowing which.
*/
function remove_course_userdata($courseid, $showfeedback=true,
$removestudents=true, $removeteachers=false, $removegroups=true,
$removeevents=true, $removelogs=false) {
global $CFG;
/**
* This function will empty a course of USER data as much as
/// possible. It will retain the activities and the structure
/// of the course.
*
* @uses $USER
* @uses $SESSION
* @uses $CFG
* @param object $data an object containing all the boolean settings and courseid
* @param bool $showfeedback if false then do it all silently
* @return bool
* @todo Finish documenting this function
*/
function reset_course_userdata($data, $showfeedback=true) {
global $CFG, $USER, $SESSION;
$result = true;
if (! $course = get_record('course', 'id', $courseid)) {
error('Course ID was incorrect (can\'t find it)');
}
$strdeleted = get_string('deleted');
// Look in every instance of every module for data to delete
@@ -3299,11 +3290,10 @@ function remove_course_userdata($courseid, $showfeedback=true,
$modname = $mod->name;
$modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
$moddeleteuserdata = $modname .'_delete_userdata'; // Function to delete user data
$count=0;
if (file_exists($modfile)) {
@include_once($modfile);
if (function_exists($moddeleteuserdata)) {
$moddeleteuserdata($course, $showfeedback);
$moddeleteuserdata($data, $showfeedback);
}
}
}
@@ -3313,21 +3303,21 @@ function remove_course_userdata($courseid, $showfeedback=true,
// Delete other stuff
if ($removestudents) {
if (!empty($data->reset_students)) {
/// Delete student enrolments
if (delete_records('user_students', 'course', $course->id)) {
if (delete_records('user_students', 'course', $data->courseid)) {
if ($showfeedback) {
notify($strdeleted .' user_students');
notify($strdeleted .' user_students', 'notifysuccess');
}
} else {
$result = false;
}
/// Delete group members (but keep the groups)
if ($groups = get_records('groups', 'courseid', $course->id)) {
if ($groups = get_records('groups', 'courseid', $data->courseid)) {
foreach ($groups as $group) {
if (delete_records('groups_members', 'groupid', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups_members');
notify($strdeleted .' groups_members', 'notifysuccess');
}
} else {
$result = false;
@@ -3336,22 +3326,22 @@ function remove_course_userdata($courseid, $showfeedback=true,
}
}
if ($removeteachers) {
if (delete_records('user_teachers', 'course', $course->id)) {
if (!empty($data->reset_teachers)) {
if (delete_records('user_teachers', 'course', $data->courseid)) {
if ($showfeedback) {
notify($strdeleted .' user_teachers');
notify($strdeleted .' user_teachers', 'notifysuccess');
}
} else {
$result = false;
}
}
if ($removegroups) {
if ($groups = get_records('groups', 'courseid', $course->id)) {
if (!empty($data->reset_groups)) {
if ($groups = get_records('groups', 'courseid', $data->courseid)) {
foreach ($groups as $group) {
if (delete_records('groups', 'id', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups');
notify($strdeleted .' groups', 'notifysuccess');
}
} else {
$result = false;
@@ -3360,20 +3350,20 @@ function remove_course_userdata($courseid, $showfeedback=true,
}
}
if ($removeevents) {
if (delete_records('event', 'courseid', $course->id)) {
if (!empty($data->reset_events)) {
if (delete_records('event', 'courseid', $data->courseid)) {
if ($showfeedback) {
notify($strdeleted .' event');
notify($strdeleted .' event', 'notifysuccess');
}
} else {
$result = false;
}
}
if ($removelogs) {
if (delete_records('log', 'course', $course->id)) {
if (!empty($data->reset_logs)) {
if (delete_records('log', 'course', $data->courseid)) {
if ($showfeedback) {
notify($strdeleted .' log');
notify($strdeleted .' log', 'notifysuccess');
}
} else {
$result = false;
@@ -3381,9 +3371,9 @@ function remove_course_userdata($courseid, $showfeedback=true,
}
return $result;
}
/// GROUPS /////////////////////////////////////////////////////////
+57
View File
@@ -3979,4 +3979,61 @@ function forum_check_throttling($forum) {
}
//This function is used by the remove_course_userdata function in moodlelib.
//If this function exists, remove_course_userdata will execute it.
//This function will remove all posts from the specified forum.
function forum_delete_userdata($data, $showfeedback=true) {
global $CFG;
$sql = 'DELETE FROM fp USING '.$CFG->prefix.'forum_discussions fd,
'.$CFG->prefix.'forum_posts fp,
'.$CFG->prefix.'forum f
WHERE fp.discussion=fd.id and f.course='.$data->courseid.' AND f.id=fd.forum';
$strreset = get_string('reset');
if (!empty($data->reset_forum_news)) {
$select = "$sql and f.type = 'news' ";
if (execute_sql($select, false) and $showfeedback) {
notify($strreset.': '.get_string('namenews','forum'), 'notifysuccess');
}
}
if (!empty($data->reset_forum_teacher)) {
$select = "$sql and f.type = 'teacher' ";
if (execute_sql($select, false) and $showfeedback) {
notify($strreset.': '.get_string('nameteacher','forum'), 'notifysuccess');
}
}
if (!empty($data->reset_forum_single)) {
$select = "$sql and f.type = 'single' and fp.parent > 0 ";
if (execute_sql($select, false) and $showfeedback) {
notify($strreset.': '.get_string('singleforum','forum'), 'notifysuccess');
}
}
if (!empty($data->reset_forum_eachuser)) {
$select = "$sql and f.type = 'eachuser' ";
if (execute_sql($select, false) and $showfeedback) {
notify($strreset.': '.get_string('eachuserforum','forum'), 'notifysuccess');
}
}
if (!empty($data->reset_forum_general)) {
$select = "$sql and f.type = 'general' ";
if (execute_sql($select, false) and $showfeedback) {
notify($strreset.': '.get_string('generalforum','forum'), 'notifysuccess');
}
}
}
// Called by course/reset.php
function forum_reset_course_form($course) {
echo get_string('resetforums', 'forum'); echo ':<br />';
print_checkbox('reset_forum_news', 1, true, get_string('namenews','forum'), '', ''); echo '<br />';
print_checkbox('reset_forum_teacher', 1, true, get_string('nameteacher','forum'), '', ''); echo '<br />';
print_checkbox('reset_forum_single', 1, true, get_string('singleforum','forum'), '', ''); echo '<br />';
print_checkbox('reset_forum_eachuser', 1, true, get_string('eachuserforum','forum'), '', ''); echo '<br />';
print_checkbox('reset_forum_general', 1, true, get_string('generalforum','forum'), '', ''); echo '<br />';
}
?>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B