Skip to content

Commit

Permalink
MDL-31355 mod_forum: Create, update and delete due date event
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Apr 11, 2019
1 parent 0556f39 commit bbbf182
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
1 change: 1 addition & 0 deletions mod/forum/lang/en/forum.php
Expand Up @@ -52,6 +52,7 @@
$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.';
Expand Down
8 changes: 7 additions & 1 deletion mod/forum/lib.php
Expand Up @@ -86,6 +86,8 @@
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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
55 changes: 54 additions & 1 deletion mod/forum/locallib.php
@@ -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
Expand All @@ -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');

Expand Down Expand Up @@ -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;
}
73 changes: 73 additions & 0 deletions mod/forum/tests/locallib_test.php
@@ -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);
}
}

0 comments on commit bbbf182

Please sign in to comment.