| 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 | /**
 | 
        
           |  |  | 18 |  * A scheduled task for workshop cron.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    mod_workshop
 | 
        
           |  |  | 21 |  * @copyright  2019 Simey Lameze <simey@moodle.com>
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 | namespace mod_workshop\task;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | /**
 | 
        
           |  |  | 29 |  * The main scheduled task for the workshop.
 | 
        
           |  |  | 30 |  *
 | 
        
           |  |  | 31 |  * @package   mod_workshop
 | 
        
           |  |  | 32 |  * @copyright 2019 Simey Lameze <simey@moodle.com>
 | 
        
           |  |  | 33 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | class cron_task extends \core\task\scheduled_task {
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      * Get a descriptive name for this task (shown to admins).
 | 
        
           |  |  | 39 |      *
 | 
        
           |  |  | 40 |      * @return string
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     public function get_name() {
 | 
        
           |  |  | 43 |         return get_string('crontask', 'mod_workshop');
 | 
        
           |  |  | 44 |     }
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     /**
 | 
        
           |  |  | 47 |      * Run workshop cron.
 | 
        
           |  |  | 48 |      */
 | 
        
           |  |  | 49 |     public function execute() {
 | 
        
           |  |  | 50 |         global $CFG, $DB;
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |         $now = time();
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         mtrace(' processing workshop subplugins ...');
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         // Check if there are some workshops to switch into the assessment phase.
 | 
        
           |  |  | 57 |         $workshops = $DB->get_records_select("workshop",
 | 
        
           |  |  | 58 |             "phase = 20 AND phaseswitchassessment = 1 AND submissionend > 0 AND submissionend < ?", [$now]);
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         if (!empty($workshops)) {
 | 
        
           |  |  | 61 |             mtrace('Processing automatic assessment phase switch in ' . count($workshops) . ' workshop(s) ... ', '');
 | 
        
           |  |  | 62 |             require_once($CFG->dirroot . '/mod/workshop/locallib.php');
 | 
        
           |  |  | 63 |             foreach ($workshops as $workshop) {
 | 
        
           |  |  | 64 |                 $cm = get_coursemodule_from_instance('workshop', $workshop->id, $workshop->course, false, MUST_EXIST);
 | 
        
           |  |  | 65 |                 $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
 | 
        
           |  |  | 66 |                 $workshop = new \workshop($workshop, $cm, $course);
 | 
        
           |  |  | 67 |                 $workshop->switch_phase(\workshop::PHASE_ASSESSMENT);
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |                 $params = [
 | 
        
           |  |  | 70 |                     'objectid' => $workshop->id,
 | 
        
           |  |  | 71 |                     'context' => $workshop->context,
 | 
        
           |  |  | 72 |                     'courseid' => $workshop->course->id,
 | 
        
           |  |  | 73 |                     'other' => [
 | 
        
           |  |  | 74 |                         'targetworkshopphase' => $workshop->phase,
 | 
        
           |  |  | 75 |                         'previousworkshopphase' => \workshop::PHASE_SUBMISSION,
 | 
        
           |  |  | 76 |                     ]
 | 
        
           |  |  | 77 |                 ];
 | 
        
           |  |  | 78 |                 $event = \mod_workshop\event\phase_automatically_switched::create($params);
 | 
        
           |  |  | 79 |                 $event->trigger();
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |                 // Disable the automatic switching now so that it is not executed again by accident.
 | 
        
           |  |  | 82 |                 // That can happen if the teacher changes the phase back to the submission one.
 | 
        
           |  |  | 83 |                 $DB->set_field('workshop', 'phaseswitchassessment', 0, ['id' => $workshop->id]);
 | 
        
           |  |  | 84 |             }
 | 
        
           |  |  | 85 |             mtrace('done');
 | 
        
           |  |  | 86 |         }
 | 
        
           |  |  | 87 |     }
 | 
        
           |  |  | 88 | }
 |