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 |
/**
|
|
|
20 |
* Simple task to automatically set the course visibility to shown when the course start date matches the current day.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @copyright 2023 Sara Arjona <sara@moodle.com> based on code from 2016 Tim Gagen and Amanda Doughty
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class show_started_courses_task extends scheduled_task {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Get a descriptive name for this task (shown to admins).
|
|
|
30 |
*
|
|
|
31 |
* @return string
|
|
|
32 |
*/
|
|
|
33 |
public function get_name() {
|
|
|
34 |
return get_string('showstartedcoursestask', 'course');
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Update course visibility.
|
|
|
39 |
* IMPORTANT: It only processes courses with start/end dates within the past 24 hours and with start/end dates higher than
|
|
|
40 |
* the current one, to avoid updating the course visibility early.
|
|
|
41 |
*
|
|
|
42 |
* @return void
|
|
|
43 |
*/
|
|
|
44 |
public function execute() {
|
|
|
45 |
global $CFG, $DB;
|
|
|
46 |
|
|
|
47 |
// Use the configured timezone.
|
|
|
48 |
date_default_timezone_set($CFG->timezone);
|
|
|
49 |
|
|
|
50 |
$start = time();
|
|
|
51 |
|
|
|
52 |
// Get list of courses to update.
|
|
|
53 |
mtrace('\n Searching for courses to set visibility to ' . $this->get_trace_message() . ' ...');
|
|
|
54 |
$fielddate = $this->get_field_date();
|
|
|
55 |
// Only process courses with dates in the past 24 hours with start/end dates higher than the current, to avoid updating
|
|
|
56 |
// the course visibility early.
|
|
|
57 |
$select = "visible = :visibility AND
|
|
|
58 |
{$fielddate} BETWEEN :beginofday AND :endofday";
|
|
|
59 |
$params = [
|
|
|
60 |
// Get courses that have the opposite visibility to the one we want to set.
|
|
|
61 |
'visibility' => !$this->get_visibility(),
|
|
|
62 |
'beginofday' => strtotime('-1 day', $start),
|
|
|
63 |
'endofday' => $start,
|
|
|
64 |
];
|
|
|
65 |
$courses = $DB->get_recordset_select('course', $select, $params);
|
|
|
66 |
$this->update_courses_visibility($courses, $this->get_visibility());
|
|
|
67 |
$courses->close();
|
|
|
68 |
|
|
|
69 |
$end = time();
|
|
|
70 |
mtrace(($end - $start) / 60 . ' mins');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Make course visible or hidden if the start date has become due.
|
|
|
75 |
*
|
|
|
76 |
* @param \moodle_recordset $courses
|
|
|
77 |
* @param int $visibility The given courses will be set to this visibility
|
|
|
78 |
* @return void
|
|
|
79 |
*/
|
|
|
80 |
private function update_courses_visibility(\moodle_recordset $courses, int $visibility): void {
|
|
|
81 |
global $DB;
|
|
|
82 |
|
|
|
83 |
mtrace("\n There are courses to change visibility...");
|
|
|
84 |
foreach ($courses as $course) {
|
|
|
85 |
if (!$DB->set_field('course', 'visible', $visibility, ['id' => $course->id])) {
|
|
|
86 |
mtrace(" Error updating course visibility for {$course->id}: {$course->shortname}.");
|
|
|
87 |
} else {
|
|
|
88 |
mtrace(" {$course->id}: {$course->shortname} visibility is now '" . $this->get_trace_message() . "'");
|
|
|
89 |
$this->trigger_event($course);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Method to trigger a course event.
|
|
|
96 |
*
|
|
|
97 |
* @param \stdClass $course The course that has been updated.
|
|
|
98 |
*/
|
|
|
99 |
private function trigger_event(\stdClass $course): void {
|
|
|
100 |
$params = [
|
|
|
101 |
'objectid' => $course->id,
|
|
|
102 |
'context' => \context_course::instance($course->id),
|
|
|
103 |
'other' => [
|
|
|
104 |
'shortname' => $course->shortname,
|
|
|
105 |
'fullname' => $course->fullname,
|
|
|
106 |
'idnumber' => $course->idnumber,
|
|
|
107 |
],
|
|
|
108 |
];
|
|
|
109 |
$event = call_user_func([$this->get_event_classname(), 'create'], $params);
|
|
|
110 |
$event->add_record_snapshot('course', $course);
|
|
|
111 |
$event->trigger();
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Get the database field where the date to check is stored (startdate for showing courses and enddate for hiding courses).
|
|
|
116 |
*
|
|
|
117 |
* @return string
|
|
|
118 |
*/
|
|
|
119 |
protected function get_field_date(): string {
|
|
|
120 |
return 'startdate';
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* The expected visibility of the courses after running this task (show = 1 and hidden = 0).
|
|
|
125 |
*
|
|
|
126 |
* @return int
|
|
|
127 |
*/
|
|
|
128 |
protected function get_visibility(): int {
|
|
|
129 |
return 1;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* The text to display in the trace message about the action that has been applied to the course.
|
|
|
134 |
*
|
|
|
135 |
* @return string
|
|
|
136 |
*/
|
|
|
137 |
protected function get_trace_message(): string {
|
|
|
138 |
return 'Show';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* The event classname to be triggered for the courses that need to be updated.
|
|
|
143 |
*
|
|
|
144 |
* @return string
|
|
|
145 |
*/
|
|
|
146 |
protected function get_event_classname(): string {
|
|
|
147 |
return '\core\event\course_started';
|
|
|
148 |
}
|
|
|
149 |
}
|