| 1 | efrain | 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 core\task;
 | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 |  * Class badges_adhoc_task
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * @package    core
 | 
        
           |  |  | 22 |  * @copyright  2023 Jay Oswald <jayoswald@catalyst-au.net>
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 | class badges_adhoc_task extends adhoc_task {
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |     /**
 | 
        
           |  |  | 28 |      * Sets the name of the badges adhoc task
 | 
        
           |  |  | 29 |      *
 | 
        
           |  |  | 30 |      * @return void
 | 
        
           |  |  | 31 |      */
 | 
        
           |  |  | 32 |     public function get_name() {
 | 
        
           |  |  | 33 |         return get_string('taskbadgesadhoc', 'admin');
 | 
        
           |  |  | 34 |     }
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |     /**
 | 
        
           |  |  | 37 |      * Badge adhoc task to assign a single badge
 | 
        
           |  |  | 38 |      *
 | 
        
           |  |  | 39 |      * @return void
 | 
        
           |  |  | 40 |      */
 | 
        
           |  |  | 41 |     public function execute() {
 | 
        
           |  |  | 42 |         $data = $this->get_custom_data();
 | 
        
           |  |  | 43 |         $badge = new \core_badges\badge($data->badgeid);
 | 
        
           |  |  | 44 |         $traceprefix = "Badge $data->badgeid: $badge->name: ";
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |         try {
 | 
        
           |  |  | 47 |             if (!$badge->has_criteria()) {
 | 
        
           |  |  | 48 |                 mtrace("$traceprefix Badge has no criteria to be processed");
 | 
        
           |  |  | 49 |                 return;
 | 
        
           |  |  | 50 |             }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |             $issued = $badge->review_all_criteria();
 | 
        
           |  |  | 53 |             mtrace("$traceprefix badge was issued to $issued users.");
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |         } catch (\moodle_exception $e) {
 | 
        
           |  |  | 56 |             $badgeeditlink =
 | 
        
           |  |  | 57 |                 new \moodle_url('/badges/edit.php', ['id' => $data->badgeid, 'action' => 'badge']);
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |             switch($e->errorcode){
 | 
        
           |  |  | 60 |                 case 'invalidcoursemoduleid':
 | 
        
           |  |  | 61 |                     $badge->set_status(BADGE_STATUS_INACTIVE);
 | 
        
           |  |  | 62 |                     mtrace("$traceprefix has invalid course modules, it has been made inactive $badgeeditlink");
 | 
        
           |  |  | 63 |                     break;
 | 
        
           |  |  | 64 |                 default:
 | 
        
           |  |  | 65 |                     mtrace("$traceprefix Error: {$e->getMessage()} $badgeeditlink");
 | 
        
           |  |  | 66 |                     throw($e);
 | 
        
           |  |  | 67 |             }
 | 
        
           |  |  | 68 |         }
 | 
        
           |  |  | 69 |     }
 | 
        
           |  |  | 70 | }
 |