|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 require_once(dirname(__FILE__) . '/../../config.php'); 00028 require_once($CFG->dirroot.'/mod/quiz/lib.php'); 00029 require_once($CFG->dirroot.'/mod/quiz/locallib.php'); 00030 require_once($CFG->dirroot.'/mod/quiz/override_form.php'); 00031 00032 00033 $cmid = optional_param('cmid', 0, PARAM_INT); 00034 $overrideid = optional_param('id', 0, PARAM_INT); 00035 $action = optional_param('action', null, PARAM_ALPHA); 00036 $reset = optional_param('reset', false, PARAM_BOOL); 00037 00038 $override = null; 00039 if ($overrideid) { 00040 00041 if (! $override = $DB->get_record('quiz_overrides', array('id' => $overrideid))) { 00042 print_error('invalidoverrideid', 'quiz'); 00043 } 00044 if (! $quiz = $DB->get_record('quiz', array('id' => $override->quiz))) { 00045 print_error('invalidcoursemodule'); 00046 } 00047 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $quiz->course)) { 00048 print_error('invalidcoursemodule'); 00049 } 00050 } else if ($cmid) { 00051 00052 if (! $cm = get_coursemodule_from_id('quiz', $cmid)) { 00053 print_error('invalidcoursemodule'); 00054 } 00055 if (! $quiz = $DB->get_record('quiz', array('id' => $cm->instance))) { 00056 print_error('invalidcoursemodule'); 00057 } 00058 } else { 00059 print_error('invalidcoursemodule'); 00060 } 00061 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); 00062 00063 $url = new moodle_url('/mod/quiz/overrideedit.php'); 00064 if ($action) { 00065 $url->param('action', $action); 00066 } 00067 if ($overrideid) { 00068 $url->param('id', $overrideid); 00069 } else { 00070 $url->param('cmid', $cmid); 00071 } 00072 00073 $PAGE->set_url($url); 00074 00075 require_login($course, false, $cm); 00076 00077 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00078 00079 // Add or edit an override 00080 00081 require_capability('mod/quiz:manageoverrides', $context); 00082 00083 if ($overrideid) { 00084 // editing override 00085 $data = clone $override; 00086 } else { 00087 // new override 00088 $data = new stdClass(); 00089 } 00090 00091 // merge quiz defaults with data 00092 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password'); 00093 foreach ($keys as $key) { 00094 if (!isset($data->{$key}) || $reset) { 00095 $data->{$key} = $quiz->{$key}; 00096 } 00097 } 00098 00099 // If we are duplicating an override, then clear the user/group and override id 00100 // since they will change. 00101 if ($action === 'duplicate') { 00102 $override->id = null; 00103 $override->userid = null; 00104 $override->groupid = null; 00105 } 00106 00107 // true if group-based override 00108 $groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid)); 00109 00110 $overridelisturl = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id)); 00111 if (!$groupmode) { 00112 $overridelisturl->param('mode', 'user'); 00113 } 00114 00115 // Setup the form. 00116 $mform = new quiz_override_form($url, $cm, $quiz, $context, $groupmode, $override); 00117 $mform->set_data($data); 00118 00119 if ($mform->is_cancelled()) { 00120 // redirect back to override list 00121 redirect($overridelisturl); 00122 } else if (optional_param('resetbutton', 0, PARAM_ALPHA)) { 00123 // redirect back to current page and reset the form. 00124 $url->param('reset', true); 00125 redirect($url); 00126 } else if ($fromform = $mform->get_data()) { 00127 // Process the data 00128 $fromform->quiz = $quiz->id; 00129 00130 // Replace unchanged values with null 00131 foreach ($keys as $key) { 00132 if ($fromform->{$key} == $quiz->{$key}) { 00133 $fromform->{$key} = null; 00134 } 00135 } 00136 00137 // See if we are replacing an existing override 00138 $userorgroupchanged = false; 00139 if (empty($override->id)) { 00140 $userorgroupchanged = true; 00141 } else if (!empty($fromform->userid)) { 00142 $userorgroupchanged = $fromform->userid !== $override->userid; 00143 } else { 00144 $userorgroupchanged = $fromform->groupid !== $override->groupid; 00145 } 00146 if ($userorgroupchanged) { 00147 $conditions = array( 00148 'quiz' => $quiz->id, 00149 'userid' => empty($fromform->userid)? null : $fromform->userid, 00150 'groupid' => empty($fromform->groupid)? null : $fromform->groupid); 00151 if ($oldoverride = $DB->get_record('quiz_overrides', $conditions)) { 00152 // There is an old override, so we merge any new settings on top of 00153 // the older override 00154 foreach ($keys as $key) { 00155 if (is_null($fromform->{$key})) { 00156 $fromform->{$key} = $oldoverride->{$key}; 00157 } 00158 } 00159 // Delete the old override 00160 $DB->delete_records('quiz_overrides', array('id' => $oldoverride->id)); 00161 } 00162 } 00163 00164 if (!empty($override->id)) { 00165 $fromform->id = $override->id; 00166 $DB->update_record('quiz_overrides', $fromform); 00167 } else { 00168 unset($fromform->id); 00169 $fromform->id = $DB->insert_record('quiz_overrides', $fromform); 00170 } 00171 00172 quiz_update_events($quiz, $fromform); 00173 00174 add_to_log($cm->course, 'quiz', 'edit override', 00175 "overrideedit.php?id=$fromform->id", $quiz->id, $cm->id); 00176 00177 if (!empty($fromform->submitbutton)) { 00178 // redirect back to override list 00179 redirect($overridelisturl); 00180 } 00181 00182 // 'again' button pressed, so redirect back to this page 00183 $url->remove_params('cmid'); 00184 $url->param('action', 'duplicate'); 00185 $url->param('id', $fromform->id); 00186 redirect($url); 00187 00188 } 00189 00190 // Print the form 00191 00192 $pagetitle = get_string('editoverride', 'quiz'); 00193 $PAGE->navbar->add($pagetitle); 00194 $PAGE->set_pagelayout('admin'); 00195 $PAGE->set_title($pagetitle); 00196 $PAGE->set_heading($course->fullname); 00197 echo $OUTPUT->header(); 00198 echo $OUTPUT->heading($pagetitle); 00199 00200 $mform->display(); 00201 00202 echo $OUTPUT->footer();