From ffd0fbb7ac53dab1900108c9d864baea809381e8 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 21 Mar 2006 07:34:33 +0000 Subject: [PATCH] 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. --- course/reset.php | 104 ++++++++++++++++++++++++++++++++++++++++ lang/en_utf8/forum.php | 1 + lang/en_utf8/moodle.php | 10 +++- lib/moodlelib.php | 72 ++++++++++++---------------- mod/forum/lib.php | 57 ++++++++++++++++++++++ pix/i/return.gif | Bin 0 -> 868 bytes 6 files changed, 201 insertions(+), 43 deletions(-) create mode 100755 course/reset.php create mode 100755 pix/i/return.gif diff --git a/course/reset.php b/course/reset.php new file mode 100755 index 0000000000..7b010eeff6 --- /dev/null +++ b/course/reset.php @@ -0,0 +1,104 @@ +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, + ''.$course->shortname.' -> '.$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 '
'; + + print_heading(get_string('course'), 'left', 3); + + echo '
'; + echo $strremove.':
'; + print_checkbox('reset_students', 1, true, get_string('students'), '', ''); echo '
'; + print_checkbox('reset_teachers', 1, true, get_string('teachers'), '', ''); echo '
'; + print_checkbox('reset_events', 1, true, get_string('courseevents', 'calendar'), '', ''); echo '
'; + print_checkbox('reset_logs', 1, true, get_string('logs'), '', ''); echo '
'; + print_checkbox('reset_groups', 1, true, get_string('groups'), '', ''); echo '
'; + print_checkbox('reset_start_date', 1, true, get_string('startdate'), '', ''); + print_date_selector('startday', 'startmonth', 'startyear'); + helpbutton('coursestartdate', get_string('startdate')); + echo '
'; + + // 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 '
'; + $mod_reset_course_form($course); + echo '
'; + } + } + } + } else { + error('No modules are installed!'); + } + + echo '

'; + echo '

'; + echo '

'; + echo '
'; + + print_simple_box_end(); + print_footer($course); + +?> diff --git a/lang/en_utf8/forum.php b/lang/en_utf8/forum.php index 7a66e3532d..d231656920 100644 --- a/lang/en_utf8/forum.php +++ b/lang/en_utf8/forum.php @@ -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'; diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index d564255fa7..2aacb71808 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -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'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 2ac3f6de63..805680cf8e 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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. + * 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 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. + * @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 remove_course_userdata($courseid, $showfeedback=true, - $removestudents=true, $removeteachers=false, $removegroups=true, - $removeevents=true, $removelogs=false) { +function reset_course_userdata($data, $showfeedback=true) { - global $CFG; + 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 ///////////////////////////////////////////////////////// diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 7f26e0b3bc..a578445438 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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 ':
'; + print_checkbox('reset_forum_news', 1, true, get_string('namenews','forum'), '', ''); echo '
'; + print_checkbox('reset_forum_teacher', 1, true, get_string('nameteacher','forum'), '', ''); echo '
'; + print_checkbox('reset_forum_single', 1, true, get_string('singleforum','forum'), '', ''); echo '
'; + print_checkbox('reset_forum_eachuser', 1, true, get_string('eachuserforum','forum'), '', ''); echo '
'; + print_checkbox('reset_forum_general', 1, true, get_string('generalforum','forum'), '', ''); echo '
'; +} + ?> diff --git a/pix/i/return.gif b/pix/i/return.gif new file mode 100755 index 0000000000000000000000000000000000000000..45c5ac14abc8a8b88444ed2113e2f38dfbdc66a2 GIT binary patch literal 868 zcmc(ey-t;349EY+KqQfnLmbrPgoH#ViAxudg@Zvp!pIHi(2>E383zVdmTm?YNAM09 z989`!58eZDD!)EA!Lv1O|6hII{lm|jTVJM{>Orc0Srt`YWmW1bvob2Z(kj)>$cm`& z3ae1hdX`7Ims`1d)v_$gyv)i3QYlMO@{*M(zy=yDmYG)b^ovZ-v`jTERYZnoScX~! zO^w{*2AW-Zb@&C-NMR#TDkl%-%rHDHKFC9>#23^veUv8*=KHP0g~LhTP)D)(>; zSKCDeP4h4d6YdekY{)|v!X9}blWpR_KO%65YQPYUN=DHEG1x$ZrB1Hf)I4)n2Z=vu zsm$C&uGmEdO_#aIk)#O#U5pbnZi$jSkjXZ2pb5h`L^WWDW)oTHfEa9`)hFs{+SEL8 zF@nS&v{a`4I6BvH?{1D0cFf}{Jv)N#)JJx`I^yUf42O+0ME6aGjs_mkd=6ZR@bCoH zQ@!>73zKJ%Z?u5^Cw7+Rnk>x@Zhp-5>ci~K+1vZOi{~%jecWBW{&Ttbe&zV!_UHb^ jbpCaA`1{}8i{-u3lV7VlpU&5|kFH*SyZ-xqGST)U0ZUw$ literal 0 HcmV?d00001 -- 2.39.5