diff --git a/course/reset.php b/course/reset.php
index 4f078f139bc..cf165cd51cc 100755
--- a/course/reset.php
+++ b/course/reset.php
@@ -7,101 +7,78 @@ of general course data like students, teachers, logs, events and groups as well
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);
+require('../config.php');
+require_once('reset_form.php');
- if (! $course = get_record('course', 'id', $id)) {
- error("Course is misconfigured");
- }
+$id = required_param('id', PARAM_INT);
- require_login($course->id);
+if (!$course = get_record('course', 'id', $id)) {
+ error("Course is misconfigured");
+}
- require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
+require_login($course);
+require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
- $strreset = get_string('reset');
- $strresetcourse = get_string('resetcourse');
- $strremove = get_string('remove');
+$strreset = get_string('reset');
+$strresetcourse = get_string('resetcourse');
+$strremove = get_string('remove');
- $navlinks = array();
- $navlinks[] = array('name' => $strresetcourse, 'link' => null, 'type' => 'misc');
- $navigation = build_navigation($navlinks);
+$navlinks = array(array('name' => $strresetcourse, 'link' => null, 'type' => 'misc'));
+$navigation = build_navigation($navlinks);
- print_header($course->fullname.': '.$strresetcourse, $course->fullname.': '.$strresetcourse, $navigation);
+$mform = new course_reset_form();
- print_simple_box_start();
+if ($mform->is_cancelled()) {
+ redirect($CFG->wwwroot.'/course/view.php?id='.$id);
- print_heading($strresetcourse);
+} else if ($data = $mform->get_data(false)) { // no magic quotes
-/// If we have data, then process it.
- if ($data = data_submitted() and confirm_sesskey()) {
+ if (isset($data->selectdefault)) {
+ $_POST = array();
+ $mform = new course_reset_form();
+ $mform->load_defaults();
- $data->courseid = $course->id;
+ } else if (isset($data->deselectall)) {
+ $_POST = array();
+ $mform = new course_reset_form();
- reset_course_userdata($data, true);
+ } else {
+ print_header($course->fullname.': '.$strresetcourse, $course->fullname.': '.$strresetcourse, $navigation);
+ print_heading($strresetcourse);
- 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');
- }
+ $data->reset_start_date_old = $course->startdate;
+ $status = reset_course_userdata($data);
+
+ $data = array();;
+ foreach ($status as $item) {
+ $line = array();
+ $line[] = $item['component'];
+ $line[] = $item['item'];
+ $line[] = ($item['error']===false) ? get_string('ok') : '
';
$fullname = fullname($post, has_capability('moodle/site:viewfullnames', $post->modcontext));
+ $by = new object();
$by->name = '
'.$fullname.'';
$by->date = userdate($post->modified);
@@ -2577,6 +2596,7 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring=""
echo '
';
$usedate = (empty($post->timemodified)) ? $post->modified : $post->timemodified; // Just in case
$parenturl = (empty($post->lastpostid)) ? '' : '&parent='.$post->lastpostid;
+ $usermodified = new object();
$usermodified->id = $post->usermodified;
$usermodified->firstname = $post->umfirstname;
$usermodified->lastname = $post->umlastname;
@@ -3082,6 +3102,7 @@ function forum_add_discussion($discussion,&$message) {
// The first post is stored as a real post, and linked
// to from the discuss entry.
+ $post = new object();
$post->discussion = 0;
$post->parent = 0;
$post->userid = $USER->id;
@@ -3254,6 +3275,7 @@ function forum_subscribe($userid, $forumid) {
return true;
}
+ $sub = new object();
$sub->userid = $userid;
$sub->forum = $forumid;
@@ -3285,6 +3307,7 @@ function forum_post_subscription($post) {
return "";
}
+ $info = new object();
$info->name = fullname($USER);
$info->forum = $forum->name;
@@ -4070,6 +4093,7 @@ function forum_print_posts_threaded($parent, $courseid, $depth, $ratings, $reply
if (!forum_user_can_see_post($post->forum,$post->discussion,$post)) {
continue;
}
+ $by = new object();
$by->name = fullname($post, $canviewfullnames);
$by->date = userdate($post->modified);
@@ -4313,7 +4337,7 @@ function forum_role_assign($userid, $context, $roleid) {
}
-/*
+/**
* This function gets run whenever a role is assigned to a user in a context
*
* @param integer $userid
@@ -4321,7 +4345,14 @@ function forum_role_assign($userid, $context, $roleid) {
* @return bool
*/
function forum_role_unassign($userid, $context) {
- return forum_remove_user_subscriptions($userid, $context);
+ if (empty($context->contextlevel)) {
+ return false;
+ }
+
+ forum_remove_user_subscriptions($userid, $context);
+ forum_remove_user_tracking($userid, $context);
+
+ return true;
}
@@ -4400,9 +4431,9 @@ function forum_add_user_default_subscriptions($userid, $context) {
* Remove subscriptions for a user in a context
*/
function forum_remove_user_subscriptions($userid, $context) {
-
+
global $CFG;
-
+
if (empty($context->contextlevel)) {
return false;
}
@@ -4412,14 +4443,12 @@ function forum_remove_user_subscriptions($userid, $context) {
case CONTEXT_SYSTEM: // For the whole site
//if ($courses = get_my_courses($userid)) {
// find all courses in which this user has a forum subscription
- if ($courses = get_records_sql("SELECT c.*
- FROM {$CFG->prefix}course c,
- {$CFG->prefix}forum_subscriptions fs,
- {$CFG->prefix}forum f
- WHERE c.id = f.course
- AND f.id = fs.forum
- AND fs.userid = $userid")) {
-
+ if ($courses = get_records_sql("SELECT c.id
+ FROM {$CFG->prefix}course c,
+ {$CFG->prefix}forum_subscriptions fs,
+ {$CFG->prefix}forum f
+ WHERE c.id = f.course AND f.id = fs.forum AND fs.userid = $userid")) {
+
foreach ($courses as $course) {
$subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
forum_remove_user_subscriptions($userid, $subcontext);
@@ -4428,13 +4457,13 @@ function forum_remove_user_subscriptions($userid, $context) {
break;
case CONTEXT_COURSECAT: // For a whole category
- if ($courses = get_records('course', 'category', $context->instanceid)) {
+ if ($courses = get_records('course', 'category', $context->instanceid, '', 'id')) {
foreach ($courses as $course) {
$subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
forum_remove_user_subscriptions($userid, $subcontext);
}
}
- if ($categories = get_records('course_categories', 'parent', $context->instanceid)) {
+ if ($categories = get_records('course_categories', 'parent', $context->instanceid, '', 'id')) {
foreach ($categories as $category) {
$subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
forum_remove_user_subscriptions($userid, $subcontext);
@@ -4443,21 +4472,17 @@ function forum_remove_user_subscriptions($userid, $context) {
break;
case CONTEXT_COURSE: // For a whole course
- if ($course = get_record('course', 'id', $context->instanceid)) {
+ if ($course = get_record('course', 'id', $context->instanceid, '', '', '', '', 'id')) {
// find all forums in which this user has a subscription, and its coursemodule id
if ($forums = get_records_sql("SELECT f.id, cm.id as coursemodule
- FROM {$CFG->prefix}forum f,
- {$CFG->prefix}modules m,
- {$CFG->prefix}course_modules cm,
- {$CFG->prefix}forum_subscriptions fs
- WHERE fs.userid = $userid
- AND fs.forum = f.id
- AND f.course = $context->instanceid
- AND cm.instance = f.id
- AND cm.module = m.id
- AND m.name = 'forum'")) {
-
- //if ($forums = get_all_instances_in_course('forum', $course, $userid, true)) {
+ FROM {$CFG->prefix}forum f,
+ {$CFG->prefix}modules m,
+ {$CFG->prefix}course_modules cm,
+ {$CFG->prefix}forum_subscriptions fs
+ WHERE fs.userid = $userid AND f.course = $context->instanceid
+ AND fs.forum = f.id AND cm.instance = f.id
+ AND cm.module = m.id AND m.name = 'forum'")) {
+
foreach ($forums as $forum) {
if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) {
if (!has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) {
@@ -4484,45 +4509,139 @@ function forum_remove_user_subscriptions($userid, $context) {
}
// Functions to do with read tracking.
+
/**
- *
+ * Remove post tracking for a user in a context
*/
-function forum_tp_add_read_record($userid, $postid, $discussionid=-1, $forumid=-1) {
- if (($readrecord = forum_tp_get_read_records($userid, $postid)) === false) {
+function forum_remove_user_tracking($userid, $context) {
+
+ global $CFG;
+
+ if (empty($context->contextlevel)) {
+ return false;
+ }
+
+ switch ($context->contextlevel) {
+
+ case CONTEXT_SYSTEM: // For the whole site
+ // find all courses in which this user has tracking info
+ $allcourses = array();
+ if ($courses = get_records_sql("SELECT c.id
+ FROM {$CFG->prefix}course c,
+ {$CFG->prefix}forum_read fr,
+ {$CFG->prefix}forum f
+ WHERE c.id = f.course AND f.id = fr.forumid AND fr.userid = $userid")) {
+
+ $allcourses = $allcourses + $courses;
+ }
+ if ($courses = get_records_sql("SELECT c.id
+ FROM {$CFG->prefix}course c,
+ {$CFG->prefix}forum_track_prefs ft,
+ {$CFG->prefix}forum f
+ WHERE c.id = f.course AND f.id = ft.forumid AND ft.userid = $userid")) {
+
+ $allcourses = $allcourses + $courses;
+ }
+ foreach ($allcourses as $course) {
+ $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
+ forum_remove_user_tracking($userid, $subcontext);
+ }
+ break;
+
+ case CONTEXT_COURSECAT: // For a whole category
+ if ($courses = get_records('course', 'category', $context->instanceid, '', 'id')) {
+ foreach ($courses as $course) {
+ $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
+ forum_remove_user_tracking($userid, $subcontext);
+ }
+ }
+ if ($categories = get_records('course_categories', 'parent', $context->instanceid, '', 'id')) {
+ foreach ($categories as $category) {
+ $subcontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
+ forum_remove_user_tracking($userid, $subcontext);
+ }
+ }
+ break;
+
+ case CONTEXT_COURSE: // For a whole course
+ if ($course = get_record('course', 'id', $context->instanceid, '', '', '', '', 'id')) {
+ // find all forums in which this user has reading tracked
+ if ($forums = get_records_sql("SELECT f.id, cm.id as coursemodule
+ FROM {$CFG->prefix}forum f,
+ {$CFG->prefix}modules m,
+ {$CFG->prefix}course_modules cm,
+ {$CFG->prefix}forum_read fr
+ WHERE fr.userid = $userid AND f.course = $context->instanceid
+ AND fr.forumid = f.id AND cm.instance = f.id
+ AND cm.module = m.id AND m.name = 'forum'")) {
+
+ foreach ($forums as $forum) {
+ if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) {
+ if (!has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) {
+ forum_tp_delete_read_records($userid, -1, -1, $forum->id);
+ }
+ }
+ }
+ }
+
+ // find all forums in which this user has a disabled tracking
+ if ($forums = get_records_sql("SELECT f.id, cm.id as coursemodule
+ FROM {$CFG->prefix}forum f,
+ {$CFG->prefix}modules m,
+ {$CFG->prefix}course_modules cm,
+ {$CFG->prefix}forum_track_prefs ft
+ WHERE ft.userid = $userid AND f.course = $context->instanceid
+ AND ft.forumid = f.id AND cm.instance = f.id
+ AND cm.module = m.id AND m.name = 'forum'")) {
+
+ foreach ($forums as $forum) {
+ if ($modcontext = get_context_instance(CONTEXT_MODULE, $forum->coursemodule)) {
+ if (!has_capability('mod/forum:viewdiscussion', $modcontext, $userid)) {
+ delete_records('forum_track_prefs', 'userid', $userid, 'forumid', $forum->id);
+ }
+ }
+ }
+ }
+ }
+ break;
+
+ case CONTEXT_MODULE: // Just one forum
+ if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) {
+ if ($forum = get_record('forum', 'id', $cm->instance)) {
+ if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
+ delete_records('forum_track_prefs', 'userid', $userid, 'forumid', $forum->id);
+ forum_tp_delete_read_records($userid, -1, -1, $forum->id);
+ }
+ }
+ }
+ break;
+ }
+
+ return true;
+}
+
+/**
+ *
+ */
+function forum_tp_add_read_record($userid, $postid, $discussionid, $forumid) {
+ if (!$readrecords = forum_tp_get_read_records($userid, $postid)) {
// New read record
- unset($readrecord);
- $readrecord->userid = $userid;
- $readrecord->postid = $postid;
+ $readrecord = new object();
+ $readrecord->userid = $userid;
+ $readrecord->postid = $postid;
$readrecord->discussionid = $discussionid;
- $readrecord->forumid = $forumid;
- $readrecord->firstread = time();
- $readrecord->lastread = $readrecord->firstread;
+ $readrecord->forumid = $forumid;
+ $readrecord->firstread = time();
+ $readrecord->lastread = $readrecord->firstread;
return insert_record('forum_read', $readrecord, true);
} else {
// Update read record
- $readrecord = reset($readrecord);
- $readrecord->lastread = time();
-
- $update = NULL;
- $update->id = $readrecord->id;
- $update->lastread = $readrecord->lastread;
-
- // This shouldn't happen, but just in case...
- if (!$readrecord->firstread) {
- // Update the 'firstread' field.
- $update->firstread = $readrecord->lastread;
- }
- if ($discussionid > -1) {
- // Update the 'discussionid' field.
- $update->discussionid = $discussionid;
- }
- if ($forumid > -1) {
- // Update the 'forumid' field.
- $update->forumid = $forumid;
- }
-
- return update_record('forum_read', $update);
+ $readrecord = reset($readrecords);
+ $readrecord->lastread = time();
+ $readrecord->discussionid = $discussionid;
+ $readrecord->forumid = $forumid;
+ return update_record('forum_read', $readrecord);
}
}
@@ -5042,6 +5161,7 @@ function forum_check_throttling($forum) {
.' ON p.discussion = d.id WHERE d.forum = '.$forum->id
.' AND p.userid = '.$USER->id.' AND p.created > '.$timeafter);
+ $a = new object();
$a->blockafter = $forum->blockafter;
$a->numposts = $numposts;
$a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
@@ -5058,106 +5178,180 @@ 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.
+ * Removes all grades from gradebook
+ * @param int $courseid
+ * @param string optional type
*/
-function forum_delete_userdata($data, $showfeedback=true) {
+function forum_reset_gradebook($courseid, $type='') {
global $CFG;
- $sql = "DELETE FROM {$CFG->prefix}forum_posts
- WHERE discussion IN (
- SELECT fd.id FROM {$CFG->prefix}forum_discussions fd, {$CFG->prefix}forum f
- WHERE f.course={$data->courseid} AND f.id=fd.forum "; // closing ) added bellow
-
- $strreset = get_string('reset');
-
- $attforumtype = '';
- $postsarr = array();
- if (!empty($data->reset_forum_news)) {
- $select = "$sql AND f.type = 'news' )";
- $postsarr = forum_get_posts_with_attachments($data->courseid," 'news' ",$postsarr);//select posts from news forum with attachments
- if (execute_sql($select, false) and $showfeedback) {
- notify($strreset.': '.get_string('namenews','forum'), 'notifysuccess');
- }
- }
- if (!empty($data->reset_forum_single)) {
- $select = "$sql AND f.type = 'single' ) AND parent <> 0 ";
- $postsarr = forum_get_posts_with_attachments($data->courseid," 'single' AND fp.parent<>0 ",$postsarr);
- 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' )";
- $postsarr = forum_get_posts_with_attachments($data->courseid," 'eachuser' ",$postsarr);
- 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' )";
- $postsarr = forum_get_posts_with_attachments($data->courseid," 'general' ",$postsarr);
- if (execute_sql($select, false) and $showfeedback) {
- notify($strreset.': '.get_string('generalforum','forum'), 'notifysuccess');
- }
- }
-
- //selected posts with attachments to delete attachments files
- foreach($postsarr as $post){
- forum_delete_old_attachments($post);
- }
-
- if (!empty($data->reset_forum_subscriptions)) {
- $subscripsql = "DELETE FROM {$CFG->prefix}forum_subscriptions
- WHERE forum IN (
- SELECT id FROM {$CFG->prefix}forum
- WHERE course = {$data->courseid} )";
+ $type = $type ? "AND f.type='$type'" : '';
- if (execute_sql($subscripsql, false) and $showfeedback) {
- notify($strreset.': '.get_string('resetsubscriptions','forum'), 'notifysuccess');
+ $sql = "SELECT f.*, cm.idnumber as cmidnumber, f.course as courseid
+ FROM {$CFG->prefix}forum f, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ WHERE m.name='forum' AND m.id=cm.module AND cm.instance=f.id AND f.course=$courseid $type";
+
+ if ($forums = get_records_sql($sql)) {
+ foreach ($forums as $forum) {
+ forum_grade_item_update($forum, 'reset');
}
}
}
-/*
- * Gets posts from selected course and forum type with attachments to delete this attachements files
- *
- * @param int $courseid course id
- * @param string $ftype type of forum
- * @param array $postsarr array of selected posts, we are adding to this array new posts
- *
- * @return mixed an array of objects, or false if no records were found or an error occured.
- */
-function forum_get_posts_with_attachments($courseid,$ftype,$postsarr){
- global $CFG;
- $attquery = "SELECT fp.*,fd.course,fd.forum
- FROM {$CFG->prefix}forum_discussions fd,{$CFG->prefix}forum_posts fp,{$CFG->prefix}forum f
- WHERE fd.course = $courseid
- AND fd.id = fp.discussion
- AND fp.attachment <>''
- AND f.id = fd.forum
- AND f.type = $ftype ";
- if ($records = get_records_sql($attquery)){
- return array_merge((array)$postsarr,$records);
+/**
+ * This function is used by the reset_course_userdata function in moodlelib.
+ * This function will remove all posts from the specified forum
+ * and clean up any related data.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function forum_reset_userdata($data) {
+ global $CFG, $FORUM_TYPES_ALL;
+ require_once($CFG->libdir.'/filelib.php');
+
+ $componentstr = get_string('modulenameplural', 'forum');
+ $status = array();
+
+ $removeposts = false;
+ if (!empty($data->reset_forum_all)) {
+ $removeposts = true;
+ $typesql = "";
+ $typesstr = get_string('resetforumsall', 'forum');
+ $types = array();
+
+ } else if (!empty($data->reset_forum_types)){
+ $removeposts = true;
+ $typesql = "";
+ $types = array();
+ foreach ($data->reset_forum_types as $type) {
+ if (!array_key_exists($type, $FORUM_TYPES_ALL)) {
+ continue;
+ }
+ $typesql .= " AND f.type='$type'";
+ $types[] = $FORUM_TYPES_ALL[$type];
+ }
+ $typesstr = get_string('resetforums', 'forum').': '.implode(', ', $types);
+
}
- return $postsarr;
+
+ $alldiscussionssql = "SELECT fd.id
+ FROM {$CFG->prefix}forum_discussions fd, {$CFG->prefix}forum f
+ WHERE f.course={$data->courseid} AND f.id=fd.forum";
+
+ $allforumssql = "SELECT f.id
+ FROM {$CFG->prefix}forum f
+ WHERE f.course={$data->courseid}";
+
+ $allpostssql = "SELECT fp.id
+ FROM {$CFG->prefix}forum_posts fp, {$CFG->prefix}forum_discussions fd, {$CFG->prefix}forum f
+ WHERE f.course={$data->courseid} AND f.id=fd.forum AND fd.id=fp.discussion";
+
+ if ($removeposts) {
+ $discussionssql = "$alldiscussionssql $typesql";
+ $forumssql = "$allforumssql $typesql";
+ $postssql = "$allpostssql $typesql";
+
+ // first delete all read flags
+ delete_records_select('forum_read', "forumid IN ($forumssql)");
+
+ // remove tracking prefs
+ delete_records_select('forum_track_prefs', "forumid IN ($forumssql)");
+
+ // remove posts from queue
+ delete_records_select('forum_queue', "discussionid IN ($discussionssql)");
+
+ // remove ratings
+ delete_records_select('forum_ratings', "post IN ($postssql)");
+
+ // all posts
+ delete_records_select('forum_posts', "discussion IN ($discussionssql)");
+
+ // finally all discussions
+ delete_records_select('forum_discussions', "forum IN ($forumssql)");
+
+ // now get rid of all attachments
+ if ($forums = get_records_sql($forumssql)) {
+ foreach ($forums as $forumid=>$unused) {
+ fulldelete($CFG->dataroot.'/'.$data->courseid.'/moddata/forum/'.$forumid);
+ }
+ }
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ if (empty($types)) {
+ forum_reset_gradebook($data->courseid);
+ } else {
+ foreach ($types as $type) {
+ forum_reset_gradebook($data->courseid, $type);
+ }
+ }
+ }
+
+ $status[] = array('component'=>$componentstr, 'item'=>$typesstr, 'error'=>false);
+ }
+
+ // remove all ratings
+ if (!empty($data->reset_forum_ratings)) {
+ delete_records_select('forum_ratings', "post IN ($allpostssql)");
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ forum_reset_gradebook($data->courseid);
+ }
+ }
+
+ // remove all subscriptions unconditionally - even for users still enrolled in course
+ if (!empty($data->reset_forum_subscriptions)) {
+ delete_records_select('forum_subscriptions', "forum IN ($allforumssql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('resetsubscriptions','forum'), 'error'=>false);
+ }
+
+ // remove all tracking prefs unconditionally - even for users still enrolled in course
+ if (!empty($data->reset_forum_track_prefs)) {
+ delete_records_select('forum_track_prefs', "forumid IN ($allforumssql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('resettrackprefs','forum'), 'error'=>false);
+ }
+
+ /// updating dates - shift may be negative too
+ if ($data->timeshift) {
+ shift_course_mod_dates('forum', array('assesstimestart', 'assesstimefinish'), $data->timeshift, $data->courseid);
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
+ }
+
+ return $status;
}
/**
* Called by course/reset.php
+ * @param $mform form passed by reference
*/
-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_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 ' ';
- echo '';
- print_checkbox('reset_forum_subscriptions', 1, true, get_string('resetsubscriptions','forum'), '', '');
- echo ' ';
+function forum_reset_course_form_definition(&$mform) {
+ global $FORUM_TYPES_ALL;
+
+ $mform->addElement('header', 'forumheader', get_string('modulenameplural', 'forum'));
+
+ $mform->addElement('checkbox', 'reset_forum_all', get_string('resetforumsall','forum'));
+
+ $mform->addElement('select', 'reset_forum_types', get_string('resetforums', 'forum'), $FORUM_TYPES_ALL, array('multiple' => 'multiple'));
+ $mform->setAdvanced('reset_forum_types');
+ $mform->disabledIf('reset_forum_types', 'reset_forum_all', 'checked');
+
+ $mform->addElement('checkbox', 'reset_forum_subscriptions', get_string('resetsubscriptions','forum'));
+ $mform->setAdvanced('reset_forum_subscriptions');
+
+ $mform->addElement('checkbox', 'reset_forum_track_prefs', get_string('resettrackprefs','forum'));
+ $mform->setAdvanced('reset_forum_track_prefs');
+ $mform->disabledIf('reset_forum_track_prefs', 'reset_forum_all', 'checked');
+
+ $mform->addElement('checkbox', 'reset_forum_ratings', get_string('deleteallratings'));
+ $mform->disabledIf('reset_forum_ratings', 'reset_forum_all', 'checked');
}
+/**
+ * Course reset form defaults.
+ */
+function forum_reset_course_form_defaults($course) {
+ return array('reset_forum_all'=>1, 'reset_forum_subscriptions'=>0, 'reset_forum_track_prefs'=>0, 'reset_forum_ratings'=>1);
+}
/**
* Converts a forum to use the Roles System
diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php
index 83053f8b304..f3058c9fff6 100644
--- a/mod/glossary/lib.php
+++ b/mod/glossary/lib.php
@@ -373,9 +373,10 @@ function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) {
* Create/update grade item for given glossary
*
* @param object $glossary object with extra cmidnumber
+ * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int, 0 if ok, error code otherwise
*/
-function glossary_grade_item_update($glossary) {
+function glossary_grade_item_update($glossary, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
@@ -396,7 +397,12 @@ function glossary_grade_item_update($glossary) {
$params['scaleid'] = -$glossary->scale;
}
- return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params);
+ if ($grades === 'reset') {
+ $params['reset'] = true;
+ $grades = NULL;
+ }
+
+ return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades, $params);
}
/**
@@ -2123,4 +2129,203 @@ function glossary_get_post_actions() {
return array('add category','add comment','add entry','approve entry','delete category','delete comment','delete entry','edit category','update comment','update entry');
}
+
+/**
+ * Implementation of the function for printing the form elements that control
+ * whether the course reset functionality affects the glossary.
+ * @param $mform form passed by reference
+ */
+function glossary_reset_course_form_definition(&$mform) {
+ $mform->addElement('header', 'glossaryheader', get_string('modulenameplural', 'glossary'));
+ $mform->addElement('checkbox', 'reset_glossary_all', get_string('resetglossariesall','glossary'));
+
+ $mform->addElement('select', 'reset_glossary_types', get_string('resetglossaries', 'glossary'),
+ array('main'=>get_string('mainglossary', 'glossary'), 'secondary'=>get_string('secondaryglossary', 'glossary')), array('multiple' => 'multiple'));
+ $mform->setAdvanced('reset_glossary_types');
+ $mform->disabledIf('reset_glossary_types', 'reset_glossary_all', 'checked');
+
+ $mform->addElement('checkbox', 'reset_glossary_notenrolled', get_string('deletenotenrolled', 'glossary'));
+ $mform->disabledIf('reset_glossary_notenrolled', 'reset_glossary_all', 'checked');
+
+ $mform->addElement('checkbox', 'reset_glossary_ratings', get_string('deleteallratings'));
+ $mform->disabledIf('reset_glossary_ratings', 'reset_glossary_all', 'checked');
+
+ $mform->addElement('checkbox', 'reset_glossary_comments', get_string('deleteallcomments'));
+ $mform->disabledIf('reset_glossary_comments', 'reset_glossary_all', 'checked');
+}
+
+/**
+ * Course reset form defaults.
+ */
+function glossary_reset_course_form_defaults($course) {
+ return array('reset_glossary_all'=>0, 'reset_glossary_ratings'=>1, 'reset_glossary_comments'=>1, 'reset_glossary_notenrolled'=>0);
+}
+
+/**
+ * Removes all grades from gradebook
+ * @param int $courseid
+ * @param string optional type
+ */
+function glossary_reset_gradebook($courseid, $type='') {
+ global $CFG;
+
+ switch ($type) {
+ case 'main' : $type = "AND g.mainglossary=1"; break;
+ case 'secondary' : $type = "AND g.mainglossary=0"; break;
+ default : $type = ""; //all
+ }
+
+ $sql = "SELECT g.*, cm.idnumber as cmidnumber, g.course as courseid
+ FROM {$CFG->prefix}glossary g, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id AND g.course=$courseid $type";
+
+ if ($glossarys = get_records_sql($sql)) {
+ foreach ($glossarys as $glossary) {
+ glossary_grade_item_update($glossary, 'reset');
+ }
+ }
+}
+/**
+ * Actual implementation of the rest coures functionality, delete all the
+ * glossary responses for course $data->courseid.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function glossary_reset_userdata($data) {
+ global $CFG;
+ require_once($CFG->libdir.'/filelib.php');
+
+ $componentstr = get_string('modulenameplural', 'glossary');
+ $status = array();
+
+ $allentriessql = "SELECT e.id
+ FROM {$CFG->prefix}glossary_entries e
+ INNER JOIN {$CFG->prefix}glossary g ON e.glossaryid = g.id
+ WHERE g.course = {$data->courseid}";
+
+ $allglossariessql = "SELECT g.id
+ FROM {$CFG->prefix}glossary g
+ WHERE g.course={$data->courseid}";
+
+ // delete entries if requested
+ if (!empty($data->reset_glossary_all)
+ or (!empty($data->reset_glossary_types) and in_array('main', $data->reset_glossary_types) and in_array('secondary', $data->reset_glossary_types))) {
+
+ delete_records_select('glossary_ratings', "entryid IN ($allentriessql)");
+ delete_records_select('glossary_comments', "entryid IN ($allentriessql)");
+ delete_records_select('glossary_entries', "glossaryid IN ($allglossariessql)");
+
+ if ($glossaries = get_records_sql($allglossariessql)) {
+ foreach ($glossaries as $glossaryid=>$unused) {
+ fulldelete($CFG->dataroot."/$data->courseid/moddata/glossary/$glossaryid");
+ }
+ }
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ glossary_reset_gradebook($data->courseid);
+ }
+
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('resetglossariesall', 'glossary'), 'error'=>false);
+
+ } else if (!empty($data->reset_glossary_types)) {
+ $mainentriessql = "$allentries AND g.mainglossary=1";
+ $secondaryentriessql = "$allentries AND g.mainglossary=0";
+
+ $mainglossariessql = "$allglossariessql AND g.mainglossary=1";
+ $secondaryglossariessql = "$allglossariessql AND g.mainglossary=0";
+
+ if (in_array('main', $data->reset_glossary_types)) {
+ delete_records_select('glossary_ratings', "entryid IN ($mainentriessql)");
+ delete_records_select('glossary_comments', "entryid IN ($mainentriessql)");
+ delete_records_select('glossary_entries', "glossaryid IN ($mainglossariessql)");
+
+ if ($glossaries = get_records_sql($mainglossariessql)) {
+ foreach ($glossaries as $glossaryid=>$unused) {
+ fulldelete("$CFG->dataroot/$data->courseid/moddata/glossary/$glossaryid");
+ }
+ }
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ glossary_reset_gradebook($data->courseid, 'main');
+ }
+
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('resetglossaries', 'glossary'), 'error'=>false);
+
+ } else if (in_array('secondary', $data->reset_glossary_types)) {
+ delete_records_select('glossary_ratings', "entryid IN ($secondaryentriessql)");
+ delete_records_select('glossary_comments', "entryid IN ($secondaryentriessql)");
+ delete_records_select('glossary_entries', "glossaryid IN ($secondaryglossariessql)");
+ // remove exported source flag from entries in main glossary
+ execute_sql("UPDATE {$CFG->prefix}glossary_entries
+ SET sourceglossaryid=0
+ WHERE glossaryid IN ($mainglossariessql)", false);
+
+ if ($glossaries = get_records_sql($secondaryglossariessql)) {
+ foreach ($glossaries as $glossaryid=>$unused) {
+ fulldelete("$CFG->dataroot/$data->courseid/moddata/glossary/$glossaryid");
+ }
+ }
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ glossary_reset_gradebook($data->courseid, 'secondary');
+ }
+
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('resetglossaries', 'glossary').': '.get_string('secondaryglossary', 'glossary'), 'error'=>false);
+ }
+ }
+
+ // remove entries by users not enrolled into course
+ if (!empty($data->reset_glossary_notenrolled)) {
+ $entriessql = "SELECT e.id, e.userid, e.glossaryid, u.id AS userexists, u.deleted AS userdeleted
+ FROM {$CFG->prefix}glossary_entries e
+ INNER JOIN {$CFG->prefix}glossary g ON e.glossaryid = g.id
+ LEFT OUTER JOIN {$CFG->prefix}user u ON e.userid = u.id
+ WHERE g.course = {$data->courseid} AND e.userid > 0";
+
+ $course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
+ $notenrolled = array();
+ if ($rs = get_recordset_sql($entriessql)) {
+ while ($entry = rs_fetch_next_record($rs)) {
+ if (array_key_exists($entry->userid, $notenrolled) or !$entry->userexists or $entry->userdeleted
+ or !has_capability('moodle/course:view', $course_context , $entry->userid)) {
+ delete_records('glossary_ratings', 'entryid', $entry->id);
+ delete_records('glossary_comments', 'entryid', $entry->id);
+ delete_records('glossary_entries', 'id', $entry->id);
+ fulldelete("$CFG->dataroot/$data->courseid/moddata/glossary/$entry->glossaryid");
+ $notenrolled[$entry->userid] = true;
+ }
+ }
+ rs_close($rs);
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'glossary'), 'error'=>false);
+ }
+ }
+
+ // remove all ratings
+ if (!empty($data->reset_glossary_ratings)) {
+ delete_records_select('glossary_ratings', "entryid IN ($allentriessql)");
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ glossary_reset_gradebook($data->courseid);
+ }
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallratings'), 'error'=>false);
+ }
+
+ // remove all comments
+ if (!empty($data->reset_glossary_comments)) {
+ delete_records_select('glossary_comments', "entryid IN ($allentriessql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false);
+ }
+
+ /// updating dates - shift may be negative too
+ if ($data->timeshift) {
+ shift_course_mod_dates('glossary', array('assesstimestart', 'assesstimefinish'), $data->timeshift, $data->courseid);
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
+ }
+
+ return $status;
+}
+
?>
diff --git a/mod/label/lib.php b/mod/label/lib.php
index b55a66055e0..58c7fcb22f7 100644
--- a/mod/label/lib.php
+++ b/mod/label/lib.php
@@ -99,4 +99,14 @@ function label_get_types() {
return $types;
}
+
+/**
+ * This function is used by the reset_course_userdata function in moodlelib.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function label_reset_userdata($data) {
+ return array();
+}
+
?>
diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php
index 43450948941..7b479ee057d 100644
--- a/mod/lesson/lib.php
+++ b/mod/lesson/lib.php
@@ -407,9 +407,10 @@ function lesson_update_grades($lesson=null, $userid=0, $nullifnone=true) {
* Create grade item for given lesson
*
* @param object $lesson object with extra cmidnumber
+ * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
-function lesson_grade_item_update($lesson) {
+function lesson_grade_item_update($lesson, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
@@ -432,7 +433,12 @@ function lesson_grade_item_update($lesson) {
$params['multfactor'] = 1.0;
}
- return grade_update('mod/lesson', $lesson->course, 'mod', 'lesson', $lesson->id, 0, NULL, $params);
+ if ($grades === 'reset') {
+ $params['reset'] = true;
+ $grades = NULL;
+ }
+
+ return grade_update('mod/lesson', $lesson->course, 'mod', 'lesson', $lesson->id, 0, $grades, $params);
}
/**
@@ -580,4 +586,81 @@ function lesson_process_post_save(&$lesson) {
}
}
+
+/**
+ * Implementation of the function for printing the form elements that control
+ * whether the course reset functionality affects the lesson.
+ * @param $mform form passed by reference
+ */
+function lesson_reset_course_form_definition(&$mform) {
+ $mform->addElement('header', 'lessonheader', get_string('modulenameplural', 'lesson'));
+ $mform->addElement('advcheckbox', 'reset_lesson', get_string('deleteallattempts','lesson'));
+}
+
+/**
+ * Course reset form defaults.
+ */
+function lesson_reset_course_form_defaults($course) {
+ return array('reset_lesson'=>1);
+}
+
+/**
+ * Removes all grades from gradebook
+ * @param int $courseid
+ * @param string optional type
+ */
+function lesson_reset_gradebook($courseid, $type='') {
+ global $CFG;
+
+ $sql = "SELECT l.*, cm.idnumber as cmidnumber, l.course as courseid
+ FROM {$CFG->prefix}lesson l, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ WHERE m.name='lesson' AND m.id=cm.module AND cm.instance=l.id AND l.course=$courseid";
+
+ if ($lessons = get_records_sql($sql)) {
+ foreach ($lessons as $lesson) {
+ lesson_grade_item_update($lesson, 'reset');
+ }
+ }
+}
+
+/**
+ * Actual implementation of the rest coures functionality, delete all the
+ * lesson attempts for course $data->courseid.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function lesson_reset_userdata($data) {
+ global $CFG;
+
+ $componentstr = get_string('modulenameplural', 'lesson');
+ $status = array();
+
+ if (!empty($data->reset_lesson)) {
+ $lessonssql = "SELECT l.id
+ FROM {$CFG->prefix}lesson l
+ WHERE l.course={$data->courseid}";
+
+
+ delete_records_select('lesson_timer', "lessonid IN ($lessonssql)");
+ delete_records_select('lesson_high_scores', "lessonid IN ($lessonssql)");
+ delete_records_select('lesson_grades', "lessonid IN ($lessonssql)");
+ delete_records_select('lesson_attempts', "lessonid IN ($lessonssql)");
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ lesson_reset_gradebook($data->courseid);
+ }
+
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'lesson'), 'error'=>false);
+ }
+
+ /// updating dates - shift may be negative too
+ if ($data->timeshift) {
+ shift_course_mod_dates('lesson', array('available', 'deadline'), $data->timeshift, $data->courseid);
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
+ }
+
+ return $status;
+}
+
?>
diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php
index be430cd6249..ad45148aa16 100644
--- a/mod/quiz/lib.php
+++ b/mod/quiz/lib.php
@@ -322,7 +322,7 @@ function quiz_update_grades($quiz=null, $userid=0, $nullifnone=true) {
* Create grade item for given quiz
*
* @param object $quiz object with extra cmidnumber
- * @param mixed optional array/object of grade(s)
+ * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function quiz_grade_item_update($quiz, $grades=NULL) {
@@ -370,6 +370,11 @@ function quiz_grade_item_update($quiz, $grades=NULL) {
$params['hidden'] = 0;
}
+ if ($grades === 'reset') {
+ $params['reset'] = true;
+ $grades = NULL;
+ }
+
return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grades, $params);
}
@@ -896,12 +901,37 @@ function quiz_question_list_instances($questionid) {
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the quiz.
- * @param $course The course id of the course the user is thinking of resetting.
+ * @param $mform form passed by reference
*/
-function quiz_reset_course_form($course) {
- echo '';
- print_checkbox('reset_quiz_attempts', 1, true, get_string('removeallquizattempts','quiz'));
- echo ' ';
+function quiz_reset_course_form_definition(&$mform) {
+ $mform->addElement('header', 'forumheader', get_string('modulenameplural', 'quiz'));
+ $mform->addElement('advcheckbox', 'reset_quiz_attempts', get_string('removeallquizattempts','quiz'));
+}
+
+/**
+ * Course reset form defaults.
+ */
+function quiz_reset_course_form_defaults($course) {
+ return array('reset_quiz_attempts'=>1);
+}
+
+/**
+ * Removes all grades from gradebook
+ * @param int $courseid
+ * @param string optional type
+ */
+function quiz_reset_gradebook($courseid, $type='') {
+ global $CFG;
+
+ $sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
+ FROM {$CFG->prefix}quiz q, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=q.id AND q.course=$courseid";
+
+ if ($quizs = get_records_sql($sql)) {
+ foreach ($quizs as $quiz) {
+ quiz_grade_item_update($quiz, 'reset');
+ }
+ }
}
/**
@@ -910,73 +940,63 @@ function quiz_reset_course_form($course) {
* set and true.
*
* Also, move the quiz open and close dates, if the course start date is changing.
- *
- * @param $data the data submitted from the reset course forum.
- * @param $showfeedback whether to output progress information as the reset
- * progresses.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
*/
-function quiz_delete_userdata($data, $showfeedback=true) {
- global $CFG;
+function quiz_reset_userdata($data) {
+ global $CFG, $QTYPES;
+
+ $componentstr = get_string('modulenameplural', 'quiz');
+ $status = array();
/// Delete attempts.
if (!empty($data->reset_quiz_attempts)) {
- $conditiononquizids = 'quiz IN (SELECT id FROM ' .
- $CFG->prefix . 'quiz q WHERE q.course = ' . $data->courseid . ')';
-
- $attemptids = get_records_select('quiz_attempts', $conditiononquizids, '', 'id, uniqueid');
- if ($attemptids) {
- if ($showfeedback) {
- echo '', get_string('deletingquestionattempts', 'quiz');
- $divider = ': ';
- }
- foreach ($attemptids as $attemptid) {
- delete_attempt($attemptid->uniqueid);
- if ($showfeedback) {
- echo $divider, $attemptid->uniqueid;
- $divider = ', ';
- }
- }
- if ($showfeedback) {
- echo " \n";
+
+ $stateslistsql = "SELECT s.id
+ FROM {$CFG->prefix}question_states s
+ INNER JOIN {$CFG->prefix}quiz_attempts qza ON s.attempt=qza.uniqueid
+ INNER JOIN {$CFG->prefix}quiz q ON qza.quiz=q.id
+ WHERE q.course={$data->courseid}";
+
+ $attemptssql = "SELECT a.uniqueid
+ FROM {$CFG->prefix}quiz_attempts a, {$CFG->prefix}quiz q
+ WHERE q.course={$data->courseid} AND a.quiz=q.id";
+
+ $quizessql = "SELECT q.id
+ FROM {$CFG->prefix}quiz q
+ WHERE q.course={$data->courseid}";
+
+ if ($states = get_records_sql($stateslistsql)) {
+ //TODO: not sure if this works
+ $stateslist = implode(',', array_keys($states));
+ foreach ($QTYPES as $qtype) {
+ $qtype->delete_states($stateslist);
}
}
- if (delete_records_select('quiz_grades', $conditiononquizids) && $showfeedback) {
- notify(get_string('gradesdeleted','quiz'), 'notifysuccess');
- }
- if (delete_records_select('quiz_attempts', $conditiononquizids) && $showfeedback) {
- notify(get_string('attemptsdeleted','quiz'), 'notifysuccess');
+
+ delete_records_select('question_states', "attempt IN ($attemptssql)");
+ delete_records_select('question_sessions', "attemptid IN ($attemptssql)");
+ delete_records_select('question_attempts', "id IN ($attemptssql)");
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ quiz_reset_gradebook($data->courseid);
}
+
+ delete_records_select('quiz_grades', "quiz IN ($quizessql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('gradesdeleted','quiz'), 'error'=>false);
+
+ delete_records_select('quiz_attempts', "quiz IN ($quizessql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('attemptsdeleted','quiz'), 'error'=>false);
}
- /// Update open and close dates
- if (!empty($data->reset_start_date)) {
- /// Work out offset.
- $olddate = get_field('course', 'startdate', 'id', $data->courseid);
- $olddate = usergetmidnight($olddate); // time part of $olddate should be zero
- $newdate = make_timestamp($data->startyear, $data->startmonth, $data->startday);
- $interval = $newdate - $olddate;
-
- /// Apply it to quizzes with an open or close date.
- $success = true;
- begin_sql();
- $success = $success && execute_sql(
- "UPDATE {$CFG->prefix}quiz
- SET timeopen = timeopen + $interval
- WHERE course = {$data->courseid} AND timeopen <> 0", false);
- $success = $success && execute_sql(
- "UPDATE {$CFG->prefix}quiz
- SET timeclose = timeclose + $interval
- WHERE course = {$data->courseid} AND timeclose <> 0", false);
-
- if ($success) {
- commit_sql();
- if ($showfeedback) {
- notify(get_string('openclosedatesupdated', 'quiz'), 'notifysuccess');
- }
- } else {
- rollback_sql();
- }
+ /// updating dates - shift may be negative too
+ if ($data->timeshift) {
+ shift_course_mod_dates('quiz', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('openclosedatesupdated', 'quiz'), 'error'=>false);
}
+
+ return $status;
}
/**
diff --git a/mod/resource/lib.php b/mod/resource/lib.php
index 080f3ea5288..73631dd87f8 100644
--- a/mod/resource/lib.php
+++ b/mod/resource/lib.php
@@ -667,4 +667,12 @@ function resource_delete_warning($course, $files) {
}
}
+/**
+ * This function is used by the reset_course_userdata function in moodlelib.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function resource_reset_userdata($data) {
+ return array();
+}
?>
diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php
index dfc90c19d35..f2048534e5f 100755
--- a/mod/scorm/lib.php
+++ b/mod/scorm/lib.php
@@ -488,9 +488,10 @@ function scorm_update_grades($scorm=null, $userid=0, $nullifnone=true) {
* Update/create grade item for given scorm
*
* @param object $scorm object with extra cmidnumber
+ * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return object grade_item
*/
-function scorm_grade_item_update($scorm) {
+function scorm_grade_item_update($scorm, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
@@ -512,7 +513,12 @@ function scorm_grade_item_update($scorm) {
$params['grademin'] = 0;
}
- return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, $params);
+ if ($grades === 'reset') {
+ $params['reset'] = true;
+ $grades = NULL;
+ }
+
+ return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
}
/**
@@ -559,4 +565,72 @@ function scorm_option2text($scorm) {
return $scorm;
}
+/**
+ * Implementation of the function for printing the form elements that control
+ * whether the course reset functionality affects the scorm.
+ * @param $mform form passed by reference
+ */
+function scorm_reset_course_form_definition(&$mform) {
+ $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
+ $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts','scorm'));
+}
+
+/**
+ * Course reset form defaults.
+ */
+function scorm_reset_course_form_defaults($course) {
+ return array('reset_scorm'=>1);
+}
+
+/**
+ * Removes all grades from gradebook
+ * @param int $courseid
+ * @param string optional type
+ */
+function scorm_reset_gradebook($courseid, $type='') {
+ global $CFG;
+
+ $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
+ FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=$courseid";
+
+ if ($scorms = get_records_sql($sql)) {
+ foreach ($scorms as $scorm) {
+ scorm_grade_item_update($scorm, 'reset');
+ }
+ }
+}
+
+/**
+ * Actual implementation of the rest coures functionality, delete all the
+ * scorm attempts for course $data->courseid.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function scorm_reset_userdata($data) {
+ global $CFG;
+
+ $componentstr = get_string('modulenameplural', 'scorm');
+ $status = array();
+
+ if (!empty($data->reset_scorm)) {
+ $scormssql = "SELECT s.id
+ FROM {$CFG->prefix}scorm s
+ WHERE s.course={$data->courseid}";
+
+ delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)");
+
+ // remove all grades from gradebook
+ if (empty($data->reset_gradebook_grades)) {
+ scorm_reset_gradebook($data->courseid);
+ }
+
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
+ }
+
+ // no dates to shift here
+
+ return $status;
+}
+
?>
diff --git a/mod/survey/lib.php b/mod/survey/lib.php
index 5db869e9a1d..f3d66d23a55 100644
--- a/mod/survey/lib.php
+++ b/mod/survey/lib.php
@@ -510,4 +510,55 @@ function survey_get_post_actions() {
return array('submit');
}
+
+/**
+ * Implementation of the function for printing the form elements that control
+ * whether the course reset functionality affects the survey.
+ * @param $mform form passed by reference
+ */
+function survey_reset_course_form_definition(&$mform) {
+ $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey'));
+ $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey'));
+ $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey'));
+ $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked');
+}
+
+/**
+ * Course reset form defaults.
+ */
+function survey_reset_course_form_defaults($course) {
+ return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1);
+}
+
+/**
+ * Actual implementation of the rest coures functionality, delete all the
+ * survey responses for course $data->courseid.
+ * @param $data the data submitted from the reset course.
+ * @return array status array
+ */
+function survey_reset_userdata($data) {
+ global $CFG;
+
+ $componentstr = get_string('modulenameplural', 'survey');
+ $status = array();
+
+ $surveyssql = "SELECT ch.id
+ FROM {$CFG->prefix}survey ch
+ WHERE ch.course={$data->courseid}";
+
+ if (!empty($data->reset_survey_answers)) {
+ delete_records_select('survey_answers', "survey IN ($surveyssql)");
+ delete_records_select('survey_analysis', "survey IN ($surveyssql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
+ }
+
+ if (!empty($data->reset_survey_analysis)) {
+ delete_records_select('survey_analysis', "survey IN ($surveyssql)");
+ $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false);
+ }
+
+ // no date shifting
+ return $status;
+}
+
?>
diff --git a/notes/lib.php b/notes/lib.php
index ca9a372ec83..cf71631ece8 100644
--- a/notes/lib.php
+++ b/notes/lib.php
@@ -254,4 +254,12 @@ function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $coursei
}
}
+/**
+ * Delete all notes about users in course-
+ * @param int $courseid
+ * @return bool success
+ */
+function note_delete_all($courseid) {
+ return delete_records('post', 'module', 'notes', 'courseid', $courseid);
+}
?>
|