first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,140 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_course\task;
use core\task\adhoc_task;
/**
* Class handling course content updates notifications.
*
* @package core_course
* @copyright 2021 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class content_notification_task extends adhoc_task {
// Use the logging trait for better logging.
use \core\task\logging_trait;
/**
* Run the main task.
*/
public function execute() {
global $CFG, $OUTPUT;
require_once($CFG->libdir . '/enrollib.php');
$data = $this->get_custom_data();
$course = get_course($data->courseid);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->cms[$data->cmid];
$isupdate = !empty($data->update);
if (!$course->visible || !$cm->visible) {
// The course or module is hidden. We don't check if the user can see hidden courses, does not make sense here.
// Permissions may have changed since it was queued.
return;
}
// Get only active users.
$coursecontext = \context_course::instance($course->id);
$users = get_enrolled_users($coursecontext, '', 0, 'u.*', null, 0, 0, true);
if (empty($users)) {
return;
}
$userfrom = \core_user::get_user($data->userfrom);
// Now send the messages
$countusers = count($users);
$sentcount = $errorcount = 0;
$this->log_start("Sending course content update notifications to {$countusers} potential users
from user with id {$userfrom->id}.");
foreach ($users as $user) {
\core\cron::setup_user($user, $course);
// Ensure that the activity is available/visible to the user.
$cm = get_fast_modinfo($course)->cms[$cm->id];
if (!\core_availability\info_module::is_user_visible($cm, $user->id, false)) {
$this->log("Ignoring user {$user->id} (no permissions to see the module)", 1);
continue;
}
// Get module names in the user's language.
$modnames = get_module_types_names();
$a = [
'coursename' => format_string(get_course_display_name_for_list($course), true, ['context' => $coursecontext]),
'courselink' => (new \moodle_url('/course/view.php', ['id' => $course->id]))->out(false),
'modulename' => $cm->get_formatted_name(),
'moduletypename' => $modnames[$cm->modname],
'link' => (new \moodle_url('/mod/' . $cm->modname . '/view.php', ['id' => $cm->id]))->out(false),
'notificationpreferenceslink' =>
(new \moodle_url('/message/notificationpreferences.php', ['userid' => $user->id]))->out(false),
];
if ($isupdate) {
$messagesubject = get_string('coursecontentnotifupdate', 'course', $a);
$messagebody = get_string('coursecontentnotifupdatebody', 'course', $a);
} else {
$messagesubject = get_string('coursecontentnotifnew', 'course', $a);
$messagebody = get_string('coursecontentnotifnewbody', 'course', $a);
}
// Send notification.
$eventdata = new \core\message\message();
$eventdata->courseid = $course->id;
$eventdata->component = 'moodle';
$eventdata->name = 'coursecontentupdated';
$eventdata->userfrom = $userfrom;
$eventdata->userto = $user;
$eventdata->subject = $messagesubject;
$eventdata->fullmessageformat = FORMAT_HTML;
$eventdata->fullmessagehtml = $messagebody;
$eventdata->smallmessage = strip_tags($eventdata->fullmessagehtml);
$eventdata->contexturl = (new \moodle_url('/mod/' . $cm->modname . '/view.php', ['id' => $cm->id]))->out(false);
$eventdata->contexturlname = $cm->get_formatted_name();
$eventdata->notification = 1;
// Add notification custom data.
$eventcustomdata = ['notificationiconurl' => $cm->get_icon_url()->out(false)];
if ($courseimage = \core_course\external\course_summary_exporter::get_course_image($course)) {
$eventcustomdata['notificationpictureurl'] = $courseimage;
}
$eventdata->customdata = $eventcustomdata;
$activitydates = \core\activity_dates::get_dates_for_module($cm, $user->id);
if (!empty($activitydates)) {
$data = (new \core_course\output\activity_dates($activitydates))->export_for_template($OUTPUT);
foreach ($data->activitydates as $date) {
$eventdata->fullmessagehtml .= \html_writer::div($date['label'] . ' ' . $date['datestring']);
}
}
$eventdata->fullmessage = html_to_text($eventdata->fullmessagehtml);
if (message_send($eventdata)) {
$this->log("Notification sent to user with id {$user->id}", 1);
$sentcount++;
} else {
$this->log("Failed to send notification to user with id {$user->id}", 1);
$errorcount++;
}
}
$this->log_finish("Sent {$sentcount} notifications with {$errorcount} failures");
}
}
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Adhoc task handling course module deletion.
*
* @package core_course
* @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\task;
defined('MOODLE_INTERNAL') || die();
/**
* Class handling course module deletion.
*
* This task supports an array of course module object as custom_data, and calls course_delete_module() in synchronous deletion
* mode for each of them.
* This will:
* 1. call any 'mod_xxx_pre_course_module_deleted' functions (e.g. Recycle bin)
* 2. delete the module
* 3. fire the deletion event
*
* @package core_course
* @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_delete_modules extends \core\task\adhoc_task {
/**
* Run the deletion task.
*
* @throws \coding_exception if the module could not be removed.
*/
public function execute() {
global $CFG;
require_once($CFG->dirroot. '/course/lib.php');
// Set the proper user.
if ($this->get_custom_data()->userid !== $this->get_custom_data()->realuserid) {
$realuser = \core_user::get_user($this->get_custom_data()->realuserid, '*', MUST_EXIST);
\core\cron::setup_user($realuser);
\core\session\manager::loginas($this->get_custom_data()->userid, \context_system::instance(), false);
} else {
$user = \core_user::get_user($this->get_custom_data()->userid, '*', MUST_EXIST);
\core\cron::setup_user($user);
}
$cms = $this->get_custom_data()->cms;
$exceptions = [];
$cmsfailed = [];
foreach ($cms as $key => $cm) {
try {
course_delete_module($cm->id);
} catch (\Exception $e) {
// Keep the information instead of throw an exception and continue with next cms.
$exceptions[] = ("The course module {$cm->id} could not be deleted. "
. "{$e->getMessage()}: {$e->getFile()}({$e->getLine()}) {$e->getTraceAsString()}");
// Save the cms that has failed to set the data only with this values.
$cmsfailed[$key] = $cm;
continue;
}
}
// Throw the existing exceptions if there is any.
if (!empty($exceptions)) {
// Save the failed CMS.
$customdata = $this->get_custom_data();
$customdata->cms = $cmsfailed;
$this->set_custom_data($customdata);
throw new \coding_exception("The following course modules could not be deleted:\n " .
implode('\n', $exceptions));
}
}
/**
* Sets attemptsavailable to false.
*
* @return boolean
*/
public function retry_until_success(): bool {
return false;
}
}