Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/log.c
blob: 0927380920c28cb9df386a27b192ec906606a769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* gcompris - log.c
 *
 * Copyright (C) 2004 Bruno Coudoin
 *
 *   This program 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.
 *
 *   This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
 */


/* Trace formating are saved in our sqlite database with the fields
 * date,user,activity,level,sublevel,status,duration,comment
 *
 * status can be : PASSED or FAILED or COMPLETED
 * comment is a free optional string that can describe what went wrong. For
 * example, in a reading activity, you could write here the word that make the
 * kid fail. It may be usefull for the teacher.
 *
 *
 */

#include <time.h>

#include <gcompris.h>
#include "gcompris_db.h"
#include "profile.h"

#define KEYLOG_MAX 256

static gchar		*comment_set;
static gchar		 keylog[KEYLOG_MAX];
static GcomprisBoard	*gcomprisBoard_set;
static time_t		 start_time;
static time_t		 start_time_key;

/* By default, we use local time, not UTC */
#define USE_UTC 0

/** gc_log_start
 * \param GcomprisBoard *gcomprisBoard: the board for which the event happen
 *
 */
void gc_log_start (GcomprisBoard *gcomprisBoard) {

  gcomprisBoard_set = gcomprisBoard;
  start_time     = time(NULL);
  start_time_key = time(NULL);

  comment_set = g_strdup("");
  keylog[0]   = '\0';
}

/** gc_log_set_comment
 * \param GcomprisBoard *gcomprisBoard: the board for which the event happen
 * \param expected: string describing what was expected. This string is copied.
 * \param got: string describing what we got from the user. This string is copied.
 *
 */
void
gc_log_set_comment(GcomprisBoard *gcomprisBoard, gchar *expected, gchar *got)
{
  if(gcomprisBoard_set != gcomprisBoard) {
    return;
  }

  if(expected==NULL)
    expected="";

  if(got==NULL)
    got="";

  g_free(comment_set);
  comment_set = g_strdup_printf("%s;%s.", expected, got);
}

/** gc_log_end
 * \param GcomprisBoard *gcomprisBoard: the board for which the event happen
 * \param status
 *
 */
void gc_log_end (GcomprisBoard *gcomprisBoard, GCBonusStatusList status) {

  /* Prepare our log */

  /* The default format for time represenation.  See strftime(3) */
  /* Warning %F not supported on Windows */
  char *fmt = "%Y-%m-%d %H:%M:%S";

  char buf[256];

  /* get the current time from the Unix kernel */
  time_t end_time = time(NULL);
  double duration = difftime(end_time,start_time);

  struct tm *tp;

  GcomprisUser *gcomprisUser = gc_profile_get_current_user();

  /* A board change in between doesn't make sense */
  if(gcomprisBoard_set != gcomprisBoard)
    return;

  /* and convert it to UTC or local time representation */
  if (USE_UTC)
    tp = gmtime(&start_time);
  else
    tp = localtime(&start_time);

  /* convert the time to a string according to the format specification in fmt */
  strftime(buf, sizeof(buf), fmt, tp);


  gc_db_log(buf, (guint)duration,
	    gcomprisUser->user_id, gcomprisBoard->board_id,
	    gcomprisBoard->level, gcomprisBoard->sublevel,
	    status, comment_set);

  g_free(comment_set);
}