libdir.'/questionlib.php'); define('DEFAULT_QUESTIONS_PER_PAGE', 20); /** * Function to read all questions for category into big array * * @param int $category category number * @param bool $noparent if true only questions with NO parent will be selected * @param bool $recurse include subdirectories * @author added by Howard Miller June 2004 */ function get_questions_category( $category, $noparent=false, $recurse=true ) { global $QTYPES; // questions will be added to an array $qresults = array(); // build sql bit for $noparent $npsql = ''; if ($noparent) { $npsql = " and parent='0' "; } // get (list) of categories if ($recurse) { $categorylist = question_categorylist( $category->id ); } else { $categorylist = $category->id; } // get the list of questions for the category if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) { // iterate through questions, getting stuff we need foreach($questions as $question) { $questiontype = $QTYPES[$question->qtype]; $questiontype->get_question_options( $question ); $qresults[] = $question; } } return $qresults; } /** * Gets the default category in a course * * It returns the first category with no parent category. If no categories * exist yet then one is created. * @return object The default category * @param integer $courseid The id of the course whose default category is wanted */ function get_default_question_category($courseid) { if ($categories = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", "id")) { foreach ($categories as $category) { return $category; // Return the first one (lowest id) } } // Otherwise, we need to make one $category->name = get_string("default", "quiz"); $category->info = get_string("defaultinfo", "quiz"); $category->course = $courseid; $category->parent = 0; // TODO: Figure out why we use 999 below $category->sortorder = 999; $category->publish = 0; $category->stamp = make_unique_id_code(); if (!$category->id = insert_record("question_categories", $category)) { notify("Error creating a default category!"); return false; } return $category; } /** * Return a list of categories nicely formatted * @param int courseid id of course * @param bool published true=include all published categories * @return array formatted category names */ function question_category_menu($courseid, $published=false) { /// Returns the list of categories $publish = ""; if ($published) { $publish = "OR publish = '1'"; } if (!has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) { $categories = get_records_select("question_categories", "course = '$courseid' $publish", 'parent, sortorder, name ASC'); } else { $categories = get_records_select("question_categories", '', 'parent, sortorder, name ASC'); } if (!$categories) { return false; } $categories = add_indented_names($categories); foreach ($categories as $category) { if ($catcourse = get_record("course", "id", $category->course)) { if ($category->publish && ($category->course != $courseid)) { $category->indentedname .= " ($catcourse->shortname)"; } $catmenu[$category->id] = $category->indentedname; } } return $catmenu; } /** * prints a form to choose categories */ function question_category_form($course, $current, $recurse=1, $showhidden=false, $showquestiontext=false) { global $CFG; /// Make sure the default category exists for this course if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) { if (!$category = get_default_question_category($course->id)) { notify("Error creating a default category!"); } } /// Get all the existing categories now if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) { notify("Could not find any question categories!"); return false; // Something is really wrong } $categories = add_indented_names( $categories ); foreach ($categories as $key => $category) { if ($catcourse = get_record("course", "id", $category->course)) { if ($category->publish && $category->course != $course->id) { $category->indentedname .= " ($catcourse->shortname)"; } $catmenu[$category->id] = $category->indentedname; } } $strcategory = get_string("category", "quiz"); $strshow = get_string("show", "quiz"); $streditcats = get_string("editcategories", "quiz"); echo "
"; echo "$strcategory: "; echo " | "; popup_form ("edit.php?courseid=$course->id&cat=", $catmenu, "catmenu", $current, "", "", "", false, "self"); echo " | "; echo ""; echo ' |
"; print_string("selectcategoryabove", "quiz"); echo "
"; if ($quizid) { echo ""; print_string("addingquestions", "quiz"); echo "
"; } return; } if (!$category = get_record("question_categories", "id", "$categoryid")) { notify("Category not found!"); return; } echo '$strcreatenewquestion: | "; echo ''; popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&qtype=", $qtypemenu, "addquestion", "", "choose", "", "", false, "self"); echo ' | '; helpbutton("questiontypes", $strcreatenewquestion, "quiz"); echo ' |
'; print_string("publishedit","quiz"); echo ' |
"; print_string("noquestions", "quiz"); echo "
"; return; } if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) { // There are no questions on the requested page. $page = 0; if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) { // There are no questions at all echo ""; print_string("noquestions", "quiz"); echo "
"; return; } } print_paging_bar($totalnumber, $page, $perpage, "edit.php?courseid={$course->id}&perpage=$perpage&"); $canedit = has_capability('moodle/question:manage', $context); echo '\n"; } ?>