From: tjhunt Date: Wed, 19 Sep 2007 15:38:14 +0000 (+0000) Subject: MDL-6043 - supplimental - make corse reset function update quiz open and close dates... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=6ef56c991f43f441b88d127ce9e75a7e4cf19c38;p=moodle.git MDL-6043 - supplimental - make corse reset function update quiz open and close dates. Thanks to Shamim Rezaei for the implementation, which I edited slightly. --- diff --git a/lang/en_utf8/quiz.php b/lang/en_utf8/quiz.php index 1b2205b624..0923b81234 100644 --- a/lang/en_utf8/quiz.php +++ b/lang/en_utf8/quiz.php @@ -369,6 +369,7 @@ $string['numberabbr'] = '#'; $string['numerical'] = 'Numerical'; $string['onlyteachersexport'] = 'Only teachers can export questions'; $string['onlyteachersimport'] = 'Only teachers with editing rights can import questions'; +$string['openclosedatesupdated'] = 'Quiz open and close dates updated'; $string['optional'] = 'optional'; $string['outof'] = '$a->grade out of a maximum of $a->maxgrade'; $string['overallfeedback'] = 'Overall feedback'; diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 484506dfbb..b92cf28465 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -858,6 +858,9 @@ function quiz_reset_course_form($course) { * Actual implementation of the rest coures functionality, delete all the * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is * 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. @@ -865,35 +868,64 @@ function quiz_reset_course_form($course) { function quiz_delete_userdata($data, $showfeedback=true) { global $CFG; - if (empty($data->reset_quiz_attempts)) { - return; - } - - $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); + /// 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 $divider, $attemptid->uniqueid; - $divider = ', '; + 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"; } } - if ($showfeedback) { - echo "

\n"; + 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'); } } - 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'); + + /// 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(); + } } }