Skip to content

Commit

Permalink
CTP-3704 move to calendar class
Browse files Browse the repository at this point in the history
  • Loading branch information
watson8 committed Aug 27, 2024
1 parent 60ef0bc commit 5a97d6e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 107 deletions.
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
}
}
}
36 changes: 9 additions & 27 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ function mod_coursework_pluginfile($course, $cm, $context, $filearea, $args, $fo
*/
function coursework_add_instance($formdata) {
global $DB, $CFG;
require_once("$CFG->dirroot/mod/coursework/locallib.php");

$formdata->timecreated = time();

Expand Down Expand Up @@ -197,19 +196,19 @@ function coursework_add_instance($formdata) {

// Create event for coursework deadline [due]
if ($coursework && $coursework->deadline) {
$event = coursework_event($coursework, '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, '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, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline);
$event = \mod_coursework\calendar::coursework_event($coursework, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline);
calendar_event::create($event);
}

Expand Down Expand Up @@ -496,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 @@ -520,7 +519,6 @@ function coursework_update_instance($coursework) {
*/
function coursework_update_events($coursework, $eventtype) {
global $DB, $CFG;
require_once("$CFG->dirroot/mod/coursework/locallib.php");

$event = "";
$eventid = $DB->get_record('event', array('modulename' => 'coursework', 'instance' => $coursework->id, 'eventtype' => $eventtype));
Expand All @@ -531,7 +529,7 @@ function coursework_update_events($coursework, $eventtype) {

// Update/create event for coursework deadline [due]
if ($eventtype == 'due') {
$data = coursework_event($coursework, $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 @@ -541,7 +539,7 @@ function coursework_update_events($coursework, $eventtype) {

// Update/create event for coursework initialmarking deadline [initialgradingdue]
if ($eventtype == 'initialgradingdue') {
$data = coursework_event($coursework, $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 @@ -551,7 +549,7 @@ function coursework_update_events($coursework, $eventtype) {

// Update/create event for coursework agreedgrademarking deadline [agreedgradingdue]
if ($eventtype == 'agreedgradingdue') {
$data = coursework_event($coursework, $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 @@ -560,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
80 changes: 0 additions & 80 deletions locallib.php

This file was deleted.

0 comments on commit 5a97d6e

Please sign in to comment.