1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace mod_quiz\question\bank\filter; 18 19 use core_question\local\bank\question_version_status; 20 21 /** 22 * A custom filter condition helper for quiz to select question categories. 23 * 24 * This is required as quiz will only use ready questions and the count should show according to that. 25 * 26 * @package mod_quiz 27 * @category question 28 * @copyright 2021 Catalyst IT Australia Pty Ltd 29 * @author Safat Shahin <safatshahin@catalyst-au.net> 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 */ 32 class custom_category_condition_helper extends \qbank_managecategories\helper { 33 34 public static function question_category_options(array $contexts, bool $top = false, int $currentcat = 0, 35 bool $popupform = false, int $nochildrenof = -1, 36 bool $escapecontextnames = true): array { 37 global $CFG; 38 $pcontexts = []; 39 foreach ($contexts as $context) { 40 $pcontexts[] = $context->id; 41 } 42 $contextslist = join(', ', $pcontexts); 43 44 $categories = self::get_categories_for_contexts($contextslist, 'parent, sortorder, name ASC', $top); 45 46 if ($top) { 47 $categories = self::question_fix_top_names($categories); 48 } 49 50 $categories = self::question_add_context_in_key($categories); 51 $categories = self::add_indented_names($categories, $nochildrenof); 52 53 // Sort cats out into different contexts. 54 $categoriesarray = []; 55 foreach ($pcontexts as $contextid) { 56 $context = \context::instance_by_id($contextid); 57 $contextstring = $context->get_context_name(true, true, $escapecontextnames); 58 foreach ($categories as $category) { 59 if ($category->contextid == $contextid) { 60 $cid = $category->id; 61 if ($currentcat != $cid || $currentcat == 0) { 62 $a = new \stdClass; 63 $a->name = format_string($category->indentedname, true, 64 ['context' => $context]); 65 if ($category->idnumber !== null && $category->idnumber !== '') { 66 $a->idnumber = s($category->idnumber); 67 } 68 if (!empty($category->questioncount)) { 69 $a->questioncount = $category->questioncount; 70 } 71 if (isset($a->idnumber) && isset($a->questioncount)) { 72 $formattedname = get_string('categorynamewithidnumberandcount', 'question', $a); 73 } else if (isset($a->idnumber)) { 74 $formattedname = get_string('categorynamewithidnumber', 'question', $a); 75 } else if (isset($a->questioncount)) { 76 $formattedname = get_string('categorynamewithcount', 'question', $a); 77 } else { 78 $formattedname = $a->name; 79 } 80 $categoriesarray[$contextstring][$cid] = $formattedname; 81 } 82 } 83 } 84 } 85 if ($popupform) { 86 $popupcats = []; 87 foreach ($categoriesarray as $contextstring => $optgroup) { 88 $group = []; 89 foreach ($optgroup as $key => $value) { 90 $key = str_replace($CFG->wwwroot, '', $key); 91 $group[$key] = $value; 92 } 93 $popupcats[] = [$contextstring => $group]; 94 } 95 return $popupcats; 96 } else { 97 return $categoriesarray; 98 } 99 } 100 101 public static function get_categories_for_contexts($contexts, string $sortorder = 'parent, sortorder, name ASC', 102 bool $top = false, int $showallversions = 0): array { 103 global $DB; 104 $topwhere = $top ? '' : 'AND c.parent <> 0'; 105 $statuscondition = "AND qv.status = '". question_version_status::QUESTION_STATUS_READY . "' "; 106 107 $sql = "SELECT c.*, 108 (SELECT COUNT(1) 109 FROM {question} q 110 JOIN {question_versions} qv ON qv.questionid = q.id 111 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid 112 WHERE q.parent = '0' 113 $statuscondition 114 AND c.id = qbe.questioncategoryid 115 AND ($showallversions = 1 116 OR (qv.version = (SELECT MAX(v.version) 117 FROM {question_versions} v 118 JOIN {question_bank_entries} be ON be.id = v.questionbankentryid 119 WHERE be.id = qbe.id) 120 ) 121 ) 122 ) AS questioncount 123 FROM {question_categories} c 124 WHERE c.contextid IN ($contexts) $topwhere 125 ORDER BY $sortorder"; 126 127 return $DB->get_records_sql($sql); 128 } 129 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body