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
 * The mod_hvp event logger, makes it easy to track events throughout
19
 * the H5P system.
20
 *
21
 * @package    mod_hvp
22
 * @copyright  2016 Joubel AS
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_hvp;
27
defined('MOODLE_INTERNAL') || die();
28
 
29
class event extends \H5PEventBase {
30
    private $user;
31
 
32
     /**
33
      * @inheritdoc
34
      */
35
    public function __construct($type, $subtype = null, $contentid = null,
36
        $contenttitle = null, $libraryname = null, $libraryversion = null) {
37
        global $USER;
38
 
39
        // Track the who initiated the event.
40
        $this->user = $USER->id;
41
 
42
        parent::__construct($type, $subtype, $contentid, $contenttitle, $libraryname, $libraryversion);
43
    }
44
 
45
    /**
46
     * Store the event.
47
     *
48
     * @return int Event ID
49
     */
50
    protected function save() {
51
        global $DB;
52
 
53
        // Get data in array format without null values.
54
        $data = $this->getDataArray();
55
 
56
        // Add user.
57
        $data['user_id'] = $this->user;
58
 
59
        return $DB->insert_record('hvp_events', $data);
60
    }
61
 
62
    /**
63
     * @inheritdoc
64
     */
65
    // @codingStandardsIgnoreLine
66
    protected function saveStats() {
67
        global $DB;
68
        $type = $this->type . ' ' . $this->sub_type;
69
 
70
        // Grab current counter to check if it exists.
71
        $id = $DB->get_field_sql(
72
            "SELECT id
73
               FROM {hvp_counters}
74
              WHERE type = ?
75
                AND library_name = ?
76
                AND library_version = ?",
77
            array($type, $this->library_name, $this->library_version)
78
        );
79
 
80
        if ($id === false) {
81
            // No counter found, insert new one.
82
            $DB->insert_record('hvp_counters', array(
83
                'type' => $type,
84
                'library_name' => $this->library_name,
85
                'library_version' => $this->library_version,
86
                'num' => 1
87
            ));
88
        } else {
89
            // Update num+1.
90
            $DB->execute(
91
                "UPDATE {hvp_counters}
92
                    SET num = num + 1
93
                  WHERE id = ?",
94
                array($id)
95
            );
96
        }
97
    }
98
}