// Only work with tags that exist.
return array_filter(array_column($tags, 'id'));
-}
\ No newline at end of file
+}
+
+/**
+ * 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;
+ }
+}