Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 /** 18 * A scheduled task to remove unneeded random questions. 19 * 20 * @package qtype_random 21 * @category task 22 * @copyright 2018 Bo Pierce <email.bO.pierce@gmail.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace qtype_random\task; 27 28 use core\task\manager; 29 30 defined('MOODLE_INTERNAL') || die(); 31 32 33 /** 34 * A scheduled task to remove unneeded random questions. 35 * 36 * @copyright 2018 Bo Pierce <email.bO.pierce@gmail.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class remove_unused_questions extends \core\task\scheduled_task { 40 41 public function get_name() { 42 return get_string('taskunusedrandomscleanup', 'qtype_random'); 43 } 44 45 public function execute() { 46 global $DB, $CFG; 47 require_once($CFG->libdir . '/questionlib.php'); 48 49 // Confirm, that there is no restore in progress to make sure we do not 50 // clean up questions that have their quiz slots not restored yet. 51 $restoretasks = [ 52 '\core\task\asynchronous_copy_task', 53 '\core\task\asynchronous_restore_task', 54 ]; 55 56 $running = manager::get_running_tasks(); 57 foreach ($running as $task) { 58 if (in_array($task->classname, $restoretasks)) { 59 mtrace('Detected running async restore. Aborting the task.'); 60 return; 61 } 62 } 63 64 // Find potentially unused random questions (up to 10000). 65 // Note, because we call question_delete_question below, 66 // the question will not actually be deleted if something else 67 // is using them, but nothing else in Moodle core uses qtype_random, 68 // and not many third-party plugins do. 69 $unusedrandomids = $DB->get_records_sql(" 70 SELECT q.id, 1 71 FROM {question} q 72 LEFT JOIN {quiz_slots} qslots ON q.id = qslots.questionid 73 WHERE qslots.questionid IS NULL 74 AND q.qtype = ? AND hidden = ?", ['random', 0], 0, 10000); 75 76 $count = 0; 77 foreach ($unusedrandomids as $unusedrandomid => $notused) { 78 question_delete_question($unusedrandomid); 79 // In case the question was not actually deleted (because it was in use somehow 80 // mark it as hidden so the query above will not return it again. 81 $DB->set_field('question', 'hidden', 1, ['id' => $unusedrandomid]); 82 $count += 1; 83 } 84 mtrace('Cleaned up ' . $count . ' unused random questions.'); 85 } 86 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body