MDL-63738 question bank: a link to download a single question
[moodle.git] / lib / questionlib.php
index 1439ea8..61764ed 100644 (file)
@@ -2251,6 +2251,33 @@ function question_make_export_url($contextid, $categoryid, $format, $withcategor
             "/{$withcontexts}/{$filename}", true);
 }
 
+/**
+ * Get the URL to export a single question (exportone.php).
+ *
+ * @param stdClass|question_definition $question the question definition as obtained from
+ *      question_bank::load_question_data() or question_bank::make_question().
+ *      (Only ->id and ->contextid are used.)
+ * @return moodle_url the requested URL.
+ */
+function question_get_export_single_question_url($question) {
+    $params = ['id' => $question->id, 'sesskey' => sesskey()];
+    $context = context::instance_by_id($question->contextid);
+    switch ($context->contextlevel) {
+        case CONTEXT_MODULE:
+            $params['cmid'] = $context->instanceid;
+            break;
+
+        case CONTEXT_COURSE:
+            $params['courseid'] = $context->instanceid;
+            break;
+
+        default:
+            $params['courseid'] = SITEID;
+    }
+
+    return new moodle_url('/question/exportone.php', $params);
+}
+
 /**
  * Return a list of page types
  * @param string $pagetype current page type