]> git.mjollnir.org Git - moodle.git/commitdiff
Refactor getting question bank capabilties in mod/quiz/lib.php - don't hard code...
authortjhunt <tjhunt>
Wed, 10 Sep 2008 05:28:25 +0000 (05:28 +0000)
committertjhunt <tjhunt>
Wed, 10 Sep 2008 05:28:25 +0000 (05:28 +0000)
Also, functions for getting a URL for editing questions in a particular context, ready for MDL-16409.

lib/questionlib.php
mod/quiz/lib.php

index 5bafa6166e775cb034cb628350ceb1bef6d99cd1..3c6e2ccd19dc7e38cd9b6b120df31924f0350ad3 100644 (file)
@@ -2165,6 +2165,49 @@ function question_category_select_menu($contexts, $top = false, $currentcat = 0,
     choose_from_menu_nested($categoriesarray, 'category', $selected, $nothing);
 }
 
+/**
+ * @param integer $contextid a context id.
+ * @return object the default question category for that context, or false if none.
+ */
+function question_get_default_category($contextid) {
+    global $DB;
+    $category = $DB->get_records('question_categories', array('contextid' => $contextid),'id','*',0,1);
+    if (!empty($category)) {
+        return reset($category);
+    } else {
+        return false;
+    }
+}
+
+/**
+ * @param object $context a context
+ * @return string A URL for editing questions in this context.
+ */
+function question_edit_url($context) {
+    global $CFG, $SITE;
+    if (!has_any_capability(question_get_question_capabilities(), $context)) {
+        return false;
+    }
+    $baseurl = $CFG->wwwroot . '/question/edit.php?';
+    $defaultcategory = question_get_default_category($context->id);
+    if ($defaultcategory) {
+        $baseurl .= 'cat=' . $defaultcategory->id . '&amp;';
+    }
+    switch ($context->contextlevel) {
+        case CONTEXT_SYSTEM:
+            return $baseurl . 'courseid=' . $SITE->id;
+        case CONTEXT_COURSECAT:
+            // This is nasty, becuase we can only edit questions in a course
+            // context at the moment, so for now we just return false.
+            return false;
+        case CONTEXT_COURSE:
+            return $baseurl . 'courseid=' . $context->instanceid;
+        case CONTEXT_MODULE:
+            return $baseurl . 'cmid=' . $context->instanceid;
+    }
+    
+}
+
 /**
 * Gets the default category in the most specific context.
 * If no categories exist yet then default ones are created in all contexts.
@@ -2192,7 +2235,7 @@ function question_make_default_categories($contexts) {
                 print_error('cannotcreatedefaultcat', '', '', print_context_name($context));
             }
         } else {
-            $category = $DB->get_record('question_categories', array('contextid' => $context->id),'*',true);
+            $category = question_get_default_category($context->id);
         }
 
         if ($context->contextlevel == CONTEXT_COURSE){
@@ -2447,6 +2490,32 @@ class context_to_string_translator{
 
 }
 
+/**
+ * @return array all the capabilities that relate to accessing particular questions.
+ */
+function question_get_question_capabilities() {
+    return array(
+        'moodle/question:add',
+        'moodle/question:editmine',
+        'moodle/question:editall',
+        'moodle/question:viewmine',
+        'moodle/question:viewall',
+        'moodle/question:usemine',
+        'moodle/question:useall',
+        'moodle/question:movemine',
+        'moodle/question:moveall',
+    );
+}
+
+/**
+ * @return array all the question bank capabilities.
+ */
+function question_get_all_capabilities() {
+    $caps = question_get_question_capabilities();
+    $caps[] = 'moodle/question:managecategory';
+    $caps[] = 'moodle/question:flag';
+    return $caps;
+}
 
 /**
  * Check capability on category
index aae42f55890bddb986498fcc10741aa3e03e98a1..a153073d4388192b66b45576ca46fffbf481c051 100644 (file)
@@ -1262,23 +1262,12 @@ function quiz_supports($feature) {
 }
 
 /**
- * Returns all other caps used in module
+ * @return array all other caps used in module
  */
 function quiz_get_extra_capabilities() {
-    return array(
-        'moodle/site:accessallgroups',
-        'moodle/question:add',
-        'moodle/question:editmine',
-        'moodle/question:editall',
-        'moodle/question:viewmine',
-        'moodle/question:viewall',
-        'moodle/question:usemine',
-        'moodle/question:useall',
-        'moodle/question:movemine',
-        'moodle/question:moveall',
-        'moodle/question:managecategory',
-        'moodle/question:flag',
-    );
+    $caps = question_get_all_capabilities();
+    $caps[] = 'moodle/site:accessallgroups';
+    return $caps;
 }
 
 ?>