|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - https://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 | + * CLI script allowing to perform bulk operations over AMOS contributions. |
| 19 | + * |
| 20 | + * @package local_amos |
| 21 | + * @copyright 2020 David Mudrák <[email protected]> |
| 22 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +define('CLI_SCRIPT', true); |
| 26 | + |
| 27 | +require(__DIR__ . '/../../../config.php'); |
| 28 | +require_once($CFG->libdir . '/clilib.php'); |
| 29 | +require_once($CFG->dirroot . '/local/amos/locallib.php'); |
| 30 | + |
| 31 | +$usage = "Perform bulk operations over AMOS contributed translations. |
| 32 | +
|
| 33 | +Usage: |
| 34 | + # php contrib-bulk.php --user=<userid> --approve |
| 35 | +
|
| 36 | +Options: |
| 37 | + --author=<userid> User ID of the contributor. |
| 38 | + --approve Approve all the pending contributions by the user. |
| 39 | +
|
| 40 | +"; |
| 41 | + |
| 42 | +define('AMOS_BOT_USERID', 2); |
| 43 | + |
| 44 | +[$options, $unrecognised] = cli_get_params([ |
| 45 | + 'help' => false, |
| 46 | + 'author' => null, |
| 47 | + 'approve' => false, |
| 48 | +], [ |
| 49 | + 'h' => 'help', |
| 50 | + 'e' => 'execute', |
| 51 | +]); |
| 52 | + |
| 53 | +if ($unrecognised) { |
| 54 | + $unrecognised = implode(PHP_EOL . ' ', $unrecognised); |
| 55 | + cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised)); |
| 56 | +} |
| 57 | + |
| 58 | +if ($options['help']) { |
| 59 | + cli_writeln($usage); |
| 60 | + exit(2); |
| 61 | +} |
| 62 | + |
| 63 | +if (empty($options['author'])) { |
| 64 | + cli_error('Missing mandatory argument: author', 2); |
| 65 | +} |
| 66 | + |
| 67 | +$sql = "SELECT c.id, c.lang, c.subject, c.stashid, c.status, c.timecreated, |
| 68 | + s.components, s.strings, " . |
| 69 | + user_picture::fields('a', null, 'authorid', 'author') . " |
| 70 | + FROM {amos_contributions} c |
| 71 | + JOIN {amos_stashes} s ON (c.stashid = s.id) |
| 72 | + JOIN {user} a ON c.authorid = a.id |
| 73 | + WHERE c.status < :rejected |
| 74 | + AND a.id = :authorid |
| 75 | + ORDER BY c.timemodified"; |
| 76 | + |
| 77 | +$contributions = $DB->get_records_sql($sql, [ |
| 78 | + 'rejected' => local_amos_contribution::STATE_REJECTED, |
| 79 | + 'authorid' => $options['author'], |
| 80 | +]); |
| 81 | + |
| 82 | +$stage = new mlang_stage(); |
| 83 | + |
| 84 | +foreach ($contributions as $contribution) { |
| 85 | + echo sprintf("#%d\t%s\t%s\t%d\n", $contribution->id, $contribution->subject, $contribution->components, $contribution->strings); |
| 86 | + |
| 87 | + if ($options['approve']) { |
| 88 | + $stash = mlang_stash::instance_from_id($contribution->stashid); |
| 89 | + $stash->apply($stage); |
| 90 | + |
| 91 | + $stage->commit('Bulk approval - contribution #' . $contribution->id, [ |
| 92 | + 'source' => 'bot', |
| 93 | + 'userinfo' => 'AMOS-bot <[email protected]>' |
| 94 | + ]); |
| 95 | + |
| 96 | + $update = [ |
| 97 | + 'id' => $contribution->id, |
| 98 | + 'timemodified' => time(), |
| 99 | + 'assignee' => AMOS_BOT_USERID, |
| 100 | + 'status' => local_amos_contribution::STATE_ACCEPTED, |
| 101 | + ]; |
| 102 | + |
| 103 | + $DB->update_record('amos_contributions', $update); |
| 104 | + } |
| 105 | +} |
0 commit comments