return $this->canbeedited;
}
+ /**
+ * This quiz can only be edited if they have not been attempted.
+ * Throw an exception if this is not the case.
+ */
+ public function check_can_be_edited() {
+ if (!$this->can_be_edited()) {
+ $reportlink = quiz_attempt_summary_link_to_reports($this->get_quiz(),
+ $this->quizobj->get_cm(), $this->quizobj->get_context());
+ throw new \moodle_exception('cannoteditafterattempts', 'quiz',
+ new \moodle_url('/mod/quiz/edit.php', array('cmid' => $this->get_cmid())), $reportlink);
+ }
+ }
+
/**
* How many questions are allowed per page in the quiz.
* This setting controls how frequently extra page-breaks should be inserted
public function move_slot($idmove, $idbefore, $page) {
global $DB;
+ $this->check_can_be_edited();
+
$movingslot = $this->slots[$idmove];
if (empty($movingslot)) {
throw new moodle_exception('Bad slot ID ' . $idmove);
*/
public function refresh_page_numbers_and_update_db($quiz) {
global $DB;
+ $this->check_can_be_edited();
+
$slots = $this->refresh_page_numbers($quiz);
// Record new page order.
public function remove_slot($quiz, $slotnumber) {
global $DB;
+ $this->check_can_be_edited();
+
$slot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id, 'slot' => $slotnumber));
$maxslot = $DB->get_field_sql('SELECT MAX(slot) FROM {quiz_slots} WHERE quizid = ?', array($quiz->id));
if (!$slot) {
public function update_page_break($quiz, $slotid, $type) {
global $DB;
+ $this->check_can_be_edited();
+
$quizslots = $DB->get_records('quiz_slots', array('quizid' => $quiz->id), 'slot');
$repaginate = new \mod_quiz\repaginate($quiz->id, $quizslots);
$repaginate->repaginate_slots($quizslots[$slotid]->slot, $type);
if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
// Re-paginate the quiz.
+ $structure->check_can_be_edited();
$questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
quiz_repaginate_questions($quiz->id, $questionsperpage );
quiz_delete_previews($quiz);
if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
// Add a single question to the current quiz.
+ $structure->check_can_be_edited();
quiz_require_question_use($addquestion);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
quiz_add_quiz_question($addquestion, $quiz, $addonpage);
}
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
+ $structure->check_can_be_edited();
$addonpage = optional_param('addonpage', 0, PARAM_INT);
// Add selected questions to the current quiz.
$rawdata = (array) data_submitted();
if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
// Add random questions to the quiz.
+ $structure->check_can_be_edited();
$recurse = optional_param('recurse', 0, PARAM_BOOL);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
}
if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
+ $structure->check_can_be_edited();
$deletepreviews = false;
$recomputesummarks = false;
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course);
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
require_login($course, false, $cm);
+
$quizobj = new quiz($quiz, $cm, $course);
$structure = $quizobj->get_structure();
$modcontext = context_module::instance($cm->id);
$quizobj = quiz::create($quizid);
require_login($quizobj->get_course(), false, $quizobj->get_cm());
require_capability('mod/quiz:manage', $quizobj->get_context());
+if (quiz_has_attempts($quizid)) {
+ $reportlink = quiz_attempt_summary_link_to_reports($quizobj->get_quiz(),
+ $quizobj->get_cm(), $quizobj->get_context());
+ throw new \moodle_exception('cannoteditafterattempts', 'quiz',
+ new moodle_url('/mod/quiz/edit.php', array('cmid' => $cmid)), $reportlink);
+}
$slotnumber++;
$repage = new \mod_quiz\repaginate($quizid);