MDL-31355 mod_forum: Create, update and delete due date event

This commit is contained in:
Shamim Rezaie 2018-06-13 02:57:35 +10:00
parent 0556f39373
commit bbbf182089
4 changed files with 135 additions and 2 deletions

View File

@ -52,6 +52,7 @@ $string['blockperioddisabled'] = 'Don\'t block';
$string['blogforum'] = 'Standard forum displayed in a blog-like format';
$string['bynameondate'] = 'by {$a->name} - {$a->date}';
$string['cachedef_forum_is_tracked'] = 'Forum tracking status for user';
$string['calendardue'] = '{$a} is due';
$string['cannotadd'] = 'Could not add the discussion for this forum';
$string['cannotadddiscussion'] = 'Adding discussions to this forum requires group membership.';
$string['cannotadddiscussionall'] = 'You do not have permission to add a new discussion topic for all participants.';

View File

@ -86,6 +86,8 @@ define('FORUM_DISCUSSION_UNPINNED', 0);
function forum_add_instance($forum, $mform = null) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/forum/locallib.php');
$forum->timemodified = time();
if (empty($forum->assessed)) {
@ -127,6 +129,7 @@ function forum_add_instance($forum, $mform = null) {
}
}
forum_update_calendar($forum, $forum->coursemodule);
forum_grade_item_update($forum);
$completiontimeexpected = !empty($forum->completionexpected) ? $forum->completionexpected : null;
@ -162,7 +165,9 @@ function forum_instance_created($context, $forum) {
* @return bool success
*/
function forum_update_instance($forum, $mform) {
global $DB, $OUTPUT, $USER;
global $CFG, $DB, $OUTPUT, $USER;
require_once($CFG->dirroot.'/mod/forum/locallib.php');
$forum->timemodified = time();
$forum->id = $forum->instance;
@ -249,6 +254,7 @@ function forum_update_instance($forum, $mform) {
}
}
forum_update_calendar($forum, $forum->coursemodule);
forum_grade_item_update($forum);
$completiontimeexpected = !empty($forum->completionexpected) ? $forum->completionexpected : null;

View File

@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@ -17,8 +16,15 @@
/**
* Library of functions for forum outside of the core api
*
* @package mod_forum
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Event types.
define('FORUM_EVENT_TYPE_DUE', 'due');
require_once($CFG->dirroot . '/mod/forum/lib.php');
require_once($CFG->libdir . '/portfolio/caller.php');
@ -700,3 +706,50 @@ function mod_forum_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0,
$exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
}
}
/**
* Update the calendar entries for this forum activity.
*
* @param stdClass $forum the row from the database table forum.
* @param int $cmid The coursemodule id
* @return bool
*/
function forum_update_calendar($forum, $cmid) {
global $DB, $CFG;
require_once($CFG->dirroot.'/calendar/lib.php');
$event = new stdClass();
if (!empty($forum->duedate)) {
$event->name = get_string('calendardue', 'forum', $forum->name);
$event->description = format_module_intro('forum', $forum, $cmid);
$event->courseid = $forum->course;
$event->modulename = 'forum';
$event->instance = $forum->id;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = FORUM_EVENT_TYPE_DUE;
$event->timestart = $forum->duedate;
$event->timesort = $forum->duedate;
$event->visible = instance_is_visible('forum', $forum);
}
$event->id = $DB->get_field('event', 'id',
array('modulename' => 'forum', 'instance' => $forum->id, 'eventtype' => FORUM_EVENT_TYPE_DUE));
if ($event->id) {
$calendarevent = calendar_event::load($event->id);
if (!empty($forum->duedate)) {
// Calendar event exists so update it.
$calendarevent->update($event);
} else {
// Calendar event is no longer needed.
$calendarevent->delete();
}
} else if (!empty($forum->duedate)) {
// Event doesn't exist so create one.
calendar_event::create($event);
}
return true;
}

View File

@ -0,0 +1,73 @@
<?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/>.
/**
* File containing the forum module local library function tests.
*
* @package mod_forum
* @category test
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/forum/lib.php');
/**
* Class mod_forum_locallib_testcase.
*
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_forum_locallib_testcase extends advanced_testcase {
public function test_forum_update_calendar() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a forum activity.
$time = time();
$forum = $this->getDataGenerator()->create_module('forum',
array(
'course' => $course->id,
'duedate' => $time
)
);
// Check that there is now an event in the database.
$events = $DB->get_records('event');
$this->assertCount(1, $events);
// Get the event.
$event = reset($events);
// Confirm the event is correct.
$this->assertEquals('forum', $event->modulename);
$this->assertEquals($forum->id, $event->instance);
$this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
$this->assertEquals(FORUM_EVENT_TYPE_DUE, $event->eventtype);
$this->assertEquals($time, $event->timestart);
$this->assertEquals($time, $event->timesort);
}
}