Merge branch 'MDL-61033-master' of https://github.com/nashtechdev01/moodle
authorAndrew Nicols <andrew@nicols.co.uk>
Mon, 9 Apr 2018 02:37:23 +0000 (10:37 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 9 Apr 2018 02:37:23 +0000 (10:37 +0800)
1  2 
mod/quiz/locallib.php

diff --combined mod/quiz/locallib.php
@@@ -1333,12 -1333,12 +1333,12 @@@ function quiz_question_edit_button($cmi
      // What sort of icon should we show?
      $action = '';
      if (!empty($question->id) &&
 -            (question_has_capability_on($question, 'edit', $question->category) ||
 -                    question_has_capability_on($question, 'move', $question->category))) {
 +            (question_has_capability_on($question, 'edit') ||
 +                    question_has_capability_on($question, 'move'))) {
          $action = $stredit;
          $icon = 't/edit';
      } else if (!empty($question->id) &&
 -            question_has_capability_on($question, 'view', $question->category)) {
 +            question_has_capability_on($question, 'view')) {
          $action = $strview;
          $icon = 'i/info';
      }
@@@ -1390,7 -1390,7 +1390,7 @@@ function quiz_question_preview_url($qui
   */
  function quiz_question_preview_button($quiz, $question, $label = false, $variant = null) {
      global $PAGE;
 -    if (!question_has_capability_on($question, 'use', $question->category)) {
 +    if (!question_has_capability_on($question, 'use')) {
          return '';
      }
  
@@@ -2516,4 -2516,36 +2516,36 @@@ function quiz_extract_random_question_t
  
      // Only work with tags that exist.
      return array_filter(array_column($tags, 'id'));
- }
+ }
+ /**
+  * Get quiz attempt and handling error.
+  *
+  * @param int $attemptid the id of the current attempt.
+  * @param int|null $cmid the course_module id for this quiz.
+  * @return quiz_attempt $attemptobj all the data about the quiz attempt.
+  * @throws moodle_exception
+  */
+ function quiz_create_attempt_handling_errors($attemptid, $cmid = null) {
+     try {
+         $attempobj = quiz_attempt::create($attemptid);
+     } catch (moodle_exception $e) {
+         if (!empty($cmid)) {
+             list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
+             $continuelink = new moodle_url('/mod/quiz/view.php', array('id' => $cmid));
+             $context = context_module::instance($cm->id);
+             if (has_capability('mod/quiz:preview', $context)) {
+                 throw new moodle_exception('attempterrorcontentchange', 'quiz', $continuelink);
+             } else {
+                 throw new moodle_exception('attempterrorcontentchangeforuser', 'quiz', $continuelink);
+             }
+         } else {
+             throw new moodle_exception('attempterrorinvalid', 'quiz');
+         }
+     }
+     if (!empty($cmid) && $attempobj->get_cmid() != $cmid) {
+         throw new moodle_exception('invalidcoursemodule');
+     } else {
+         return $attempobj;
+     }
+ }