Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTP-3704-Coursework-error-when-theme-initialised-on-course-mod-creation #4

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions classes/calendar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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/>.

/**
* Class to handle interaction with core calendar.
* @package mod_coursework
* @copyright 2024 UCL
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_coursework;

/**
* Class to handle interaction with core calendar.
*
* @package mod_coursework
*/
class calendar {
/**
* Get an object to create a calendar event for coursework.
* @param object $coursework
* @param string $eventtype
* @param int|null $deadline
* @return \stdClass
*/
public static function coursework_event(object $coursework, string $eventtype, ?int $deadline): \stdClass {

$event = new \stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;

// Description field needs some formatting in same way a mod_assign does it.
// Convert the links to pluginfile. It is a bit hacky but at this stage the files
// might not have been saved in the module area yet.
if ($draftid = file_get_submitted_draft_itemid('introeditor')) {
$intro = file_rewrite_urls_to_pluginfile($coursework->intro, $draftid);
} else {
$intro = $coursework->intro;
}

$cm = get_coursemodule_from_instance('coursework', $coursework->id);

// We need to remove the links to files as the calendar is not ready
// to support module events with file areas.
$intro = strip_pluginfile_content($intro);
if ($cm->showdescription) {
$event->description = array(
'text' => $intro,
'format' => $coursework->introformat
);
} else {
$event->description = array(
'text' => '',
'format' => $coursework->introformat
);
}

$event->courseid = $coursework->course;
$event->name = $coursework->name;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'coursework';
$event->instance = $coursework->id;
$event->eventtype = $eventtype;
$event->timestart = $deadline;
$event->timeduration = 0;
$event->timesort = $deadline;
$event->visible = instance_is_visible('coursework', $coursework);

return $event;
}


/**
* @param object $coursework
* @param string $eventtype if null then will remove all
* @return void
* @throws \dml_exception
*/
public static function remove_event($coursework, $eventtype = '') {
global $DB;

$params = array('modulename' => 'coursework', 'instance' => $coursework->id);

if ($eventtype) {
$params['eventtype'] = $eventtype;
}

$events = $DB->get_records('event', $params);
foreach ($events as $eventid) {
$event = \calendar_event::load($eventid->id);
$event->delete(); // delete events from mdl_event table
}
}
}
74 changes: 9 additions & 65 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,24 +196,19 @@ function coursework_add_instance($formdata) {

// Create event for coursework deadline [due]
if ($coursework && $coursework->deadline) {
$event = coursework_event($coursework, format_module_intro('coursework', $coursework,
$coursemodule->id), $returnid, 'due', $coursework->deadline);

$event = \mod_coursework\calendar::coursework_event($coursework, 'due', $coursework->deadline);
calendar_event::create($event);
}

// Create event for coursework initialmarking deadline [initialgradingdue]
if ($coursework && $coursework->marking_deadline_enabled() && $coursework->initialmarkingdeadline) {
$event = coursework_event($coursework, format_module_intro('coursework', $coursework,
$coursemodule->id), $returnid, 'initialgradingdue', $coursework->initialmarkingdeadline);

$event = \mod_coursework\calendar::coursework_event($coursework, 'initialgradingdue', $coursework->initialmarkingdeadline);
calendar_event::create($event);
}

// Create event for coursework agreedgrademarking deadline [agreedgradingdue]
if ($coursework && $coursework->marking_deadline_enabled() && $coursework->agreedgrademarkingdeadline && $coursework->has_multiple_markers()) {
$event = coursework_event($coursework, format_module_intro('coursework', $coursework,
$coursemodule->id), $returnid, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline);
$event = \mod_coursework\calendar::coursework_event($coursework, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline);
calendar_event::create($event);
}

Expand Down Expand Up @@ -500,18 +495,18 @@ function coursework_update_instance($coursework) {
coursework_update_events($coursework, 'initialgradingdue'); // Cw initial grading deadine
} else {
// Remove it
remove_event($coursework, 'initialgradingdue');
\mod_coursework\calendar::remove_event($coursework, 'initialgradingdue');
}
if ($coursework->agreedgrademarkingdeadline && $coursework->numberofmarkers > 1) {
// Update
coursework_update_events($coursework, 'agreedgradingdue'); // Cw agreed grade deadine
} else {
// Remove it
remove_event($coursework, 'agreedgradingdue' );
\mod_coursework\calendar::remove_event($coursework, 'agreedgradingdue' );
}
} else {
// Remove all deadline events for this coursework regardless the type
remove_event($coursework);
\mod_coursework\calendar::remove_event($coursework);
}

return $DB->update_record('coursework', $coursework);
Expand All @@ -534,7 +529,7 @@ function coursework_update_events($coursework, $eventtype) {

// Update/create event for coursework deadline [due]
if ($eventtype == 'due') {
$data = coursework_event($coursework, $coursework->intro, $coursework->id, $eventtype, $coursework->deadline);
$data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->deadline);
if ($event) {
$event->update($data); //update if event exists
} else {
Expand All @@ -544,7 +539,7 @@ function coursework_update_events($coursework, $eventtype) {

// Update/create event for coursework initialmarking deadline [initialgradingdue]
if ($eventtype == 'initialgradingdue') {
$data = coursework_event($coursework, $coursework->intro, $coursework->id, $eventtype, $coursework->initialmarkingdeadline);
$data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->initialmarkingdeadline);
if ($event) {
$event->update($data); //update if event exists
} else {
Expand All @@ -554,7 +549,7 @@ function coursework_update_events($coursework, $eventtype) {

// Update/create event for coursework agreedgrademarking deadline [agreedgradingdue]
if ($eventtype == 'agreedgradingdue') {
$data = coursework_event($coursework, $coursework->intro, $coursework->id, $eventtype, $coursework->agreedgrademarkingdeadline);
$data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->agreedgrademarkingdeadline);
if ($event) {
$event->update($data); //update if event exists
} else {
Expand All @@ -563,22 +558,6 @@ function coursework_update_events($coursework, $eventtype) {
}
}

function remove_event($coursework, $eventtype = false) {
global $DB;

$params = array('modulename' => 'coursework', 'instance' => $coursework->id);

if ($eventtype) {
$params['eventtype'] = $eventtype;
}

$events = $DB->get_records('event', $params);
foreach ($events as $eventid) {
$event = calendar_event::load($eventid->id);
$event->delete(); // delete events from mdl_event table
}
}

/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
Expand Down Expand Up @@ -1476,41 +1455,6 @@ function coursework_personal_deadline_passed($courseworkid) {

}

/**
* @param coursework $coursework
* @param $description
* @param $instance
* @param $eventtype
* @param $deadline
* @return stdClass
*/
function coursework_event($coursework, $description, $instance, $eventtype, $deadline) {

$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;

$event->description = $description;
$event->courseid = $coursework->course;
$event->name = $coursework->name;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'coursework';
$event->instance = $instance;
$event->eventtype = $eventtype;
$event->timestart = $deadline;
$event->timeduration = 0;
$event->timesort = $deadline;
$event->visible = instance_is_visible('coursework', $coursework);

/* if ($eventtype == 'initialgradingdue') {
$event->name .= " (Initial stage)";
} else if ($eventtype == 'agreedgradingdue') {
$event->name .= " (Agreed Grade stage)";
}*/

return $event;
}

/**
* Purge coursework cache if a role with specific capability passed
*
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$plugin->component = 'mod_coursework';

$plugin->version = 2024081900; // If version == 0 then module will not be installed
$plugin->version = 2024082700; // If version == 0 then module will not be installed
$plugin->requires = 2023100400; // Requires this Moodle version

$plugin->cron = 300; // Period for cron to check this module (secs).
Expand Down
Loading