Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Event observers used in forum.
19
 *
20
 * @package    report
21
 * @subpackage coursestats
22
 * @copyright  2017 Paulo Jr.
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
defined('MOODLE_INTERNAL') || die();
26
require(__DIR__. '/../constants.php');
27
 
28
class report_coursestats_observer {
29
    public static function forum_discussion_created(\mod_forum\event\discussion_created $event) {
30
		global $DB;
31
 
32
		// Check if the forum instance is for announcements
33
		$result = $DB->get_record(FORUM_TABLE_NAME, array('id'=>$event->other['forumid']));
34
		if ($result->type === NEWS_FORUM_NAME) {
35
			// Get the course, based on its id
36
			$course = $DB->get_record(COURSE_TABLE_NAME, array('id'=>$event->courseid));
37
 
38
			/*
39
			 * Check if there is no records for the 'courseid' in the table 'report_coursestats'.
40
			 * If yes, a record is created with usage type classified as 'forum'.
41
			 */
42
			if (!$DB->record_exists(PLUGIN_TABLE_NAME, array('courseid'=>$event->courseid))) {
43
				$record = new stdClass();
44
				$record->courseid = $event->courseid;
45
				$record->prev_usage_type = NULL_USAGE_TYPE;
46
				$record->curr_usage_type = FORUM_USAGE_TYPE;
47
				$record->last_update = time();
48
				$DB->insert_record(PLUGIN_TABLE_NAME, $record);
49
			}
50
		}
51
	}
52
 
53
	private static function handle_module($event) {
54
		global $DB;
55
 
56
		/*
57
		* If the module name is 'url', 'folder' or 'resource', then the usage type is 'repository'.
58
		* Otherwise, the usage type is 'activity'
59
		*/
60
		$usage_type = '';
61
		if (in_array($event->other['modulename'], unserialize(REPOSITORY_MODULES))) {
62
 			$usage_type	= REPOSITORY_USAGE_TYPE;
63
		} else {
64
 			$usage_type	= ACTIVITY_USAGE_TYPE;
65
		}
66
 
67
		// Get the course, based on its id
68
		$course = $DB->get_record(COURSE_TABLE_NAME, array('id'=>$event->courseid));
69
 
70
		if (!$DB->record_exists(PLUGIN_TABLE_NAME, array('courseid'=>$event->courseid))) {
71
 			$record = new stdClass();
72
 			$record->courseid = $event->courseid;
73
 			$record->prev_usage_type = NULL_USAGE_TYPE;
74
 			$record->curr_usage_type = $usage_type;
75
 			$record->last_update =  time();
76
 			$record->categoryid = $course->category;
77
 			$DB->insert_record(PLUGIN_TABLE_NAME, $record);
78
		} else {
79
 			$result = $DB->get_record(PLUGIN_TABLE_NAME, array('courseid'=>$event->courseid));
80
 			if ($result->curr_usage_type === FORUM_USAGE_TYPE or
81
	 			($result->curr_usage_type === REPOSITORY_USAGE_TYPE and $usage_type === ACTIVITY_USAGE_TYPE)) {
82
	 			$result->prev_usage_type = $result->curr_usage_type;
83
	 			$result->curr_usage_type = $usage_type;
84
	 			$result->categoryid = $course->category;
85
	 			$result->last_update =  time();
86
	 			$DB->update_record(PLUGIN_TABLE_NAME, $result);
87
 			}
88
		}
89
	}
90
 
91
	public static function course_module_created(\core\event\course_module_created $event) {
92
		self::handle_module($event);
93
	}
94
 
95
	public static function course_module_updated(\core\event\course_module_updated $event) {
96
		self::handle_module($event);
97
	}
98
}