diff --git a/mod/quiz/classes/structure.php b/mod/quiz/classes/structure.php index ac6405bc729..ad2bb5e3c25 100644 --- a/mod/quiz/classes/structure.php +++ b/mod/quiz/classes/structure.php @@ -181,6 +181,19 @@ class structure { 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 @@ -472,6 +485,8 @@ class structure { 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); @@ -575,6 +590,8 @@ class structure { */ 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. @@ -594,6 +611,8 @@ class structure { 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) { @@ -663,6 +682,8 @@ class structure { 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); diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 6f9af09c720..0b906509a31 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -98,6 +98,7 @@ if ($scrollpos) { 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); @@ -106,6 +107,7 @@ if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) { 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); @@ -116,6 +118,7 @@ if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sess } 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(); @@ -133,6 +136,7 @@ if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { 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); @@ -145,6 +149,7 @@ if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) { } if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) { + $structure->check_can_be_edited(); $deletepreviews = false; $recomputesummarks = false; diff --git a/mod/quiz/edit_rest.php b/mod/quiz/edit_rest.php index 9bfd95813a4..7396ba739ae 100644 --- a/mod/quiz/edit_rest.php +++ b/mod/quiz/edit_rest.php @@ -53,6 +53,7 @@ $quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST); $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); diff --git a/mod/quiz/repaginate.php b/mod/quiz/repaginate.php index ee46e735182..9b430144498 100644 --- a/mod/quiz/repaginate.php +++ b/mod/quiz/repaginate.php @@ -34,6 +34,12 @@ require_sesskey(); $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);