remove old readme
[atutor.git] / docs / mods / _standard / tracker / course_tracker.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14 exit('this file is no longer used');
15
16 define('AT_INCLUDE_PATH', '../include/');
17 require(AT_INCLUDE_PATH.'vitals.inc.php');
18
19 $_section[0][0] = _AT('tools');
20
21 //get the login name or real name for member_id translation
22 $sql14 = "select member_id, login, first_name, last_name from ".TABLE_PREFIX."members";
23 $result14=mysql_query($sql14, $db);
24 while($row=mysql_fetch_array($result14)){
25         if($row['first_name'] && $row['last_name']){
26                 $this_user[$row['member_id']]= $row['first_name'].' '. $row['last_name'];
27         }else{
28                 $this_user[$row['member_id']]= $row['login'];
29         }
30 }
31
32 ///////////
33 // Create a CSV dump of the tracking data for this course
34 if($_GET['csv']=='1'){
35         $sql5 = "select * from ".TABLE_PREFIX."g_refs";
36         $result = mysql_query($sql5,$db);
37         $refs = array();
38         while ($row= mysql_fetch_array($result)) {
39                 $refs[$row['g_id']] = $row['reference'];
40         }
41         //get the g translation for non content pages
42         $sql8= "select
43                 G.g,
44                 R.reference,
45                 R.g_id
46         from
47                 ".TABLE_PREFIX."g_click_data G,
48                 ".TABLE_PREFIX."g_refs R
49         where
50                 G.g = R.g_id
51                 AND
52                 course_id='$_SESSION[course_id]'";
53
54         if(!$result8 = mysql_query($sql8,$db)){
55                 echo "query failed";
56                 require(AT_INCLUDE_PATH.'footer.inc.php');
57                 exit;
58         }else{
59
60                 $title_refs = array();
61                 while ($row= mysql_fetch_array($result8)) {
62                         $title_refs2[$row['g']] = $row['reference'];
63
64                 }
65         }
66         //get the translations for the content id numbers
67         $sql7 = "select
68                         C.title,
69                         C.content_id
70
71                 from
72                         ".TABLE_PREFIX."content C
73
74                 where
75                         course_id='$_SESSION[course_id]'";
76         if(!$result7 = mysql_query($sql7,$db)){
77                 echo "query failed";
78                 require(AT_INCLUDE_PATH.'footer.inc.php');
79                 exit;
80         }
81         $title_refs = array();
82         while ($row= mysql_fetch_array($result7)) {
83                 $title_refs[$row['content_id']] = $row['title'];
84
85         }
86
87         $name=str_replace(" ", "_", $_SESSION['course_title']);
88         $name=str_replace("'", "", $name);
89         header('Content-Type: text/csv');
90         header('Content-Disposition: inline; filename="'.$name.'_tracking.csv"');
91         header('Expires: 0');
92         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
93         header('Pragma: public');
94
95         $sqlall="select * from ".TABLE_PREFIX."g_click_data where course_id='$_SESSION[course_id]'";
96
97         $result_all=mysql_query($sqlall, $db);
98         $num_fields = mysql_num_fields($result_all);
99         for($i=0; $i<$num_fields; $i++){
100                 if($i==($num_fields-1)){
101                         $this_row .= mysql_field_name($result_all,$i);
102                 }else{
103                         $this_row .= mysql_field_name($result_all,$i).',';
104                 }
105
106         }
107         $this_row .= "\n";
108         while($row=mysql_fetch_array($result_all)){
109                 $this_row .= quote_csv($this_user[$row['member_id']]).",";
110                 $this_row .= quote_csv($row['course_id']).",";
111                 if($row['from_cid']=='' || $row['from_cid'] == '0'){
112                         $this_row .= '"0",';
113                 }else if ($title_refs[$row['from_cid']] == ''){
114                         $this_row .= quote_csv(_AT('deleted')).',';
115                 }else if ($title_refs[$row['from_cid']] != ''){
116                         $this_row .= quote_csv($title_refs[$row['from_cid']]).",";
117                 }else{
118                         $this_row .= '"0",';
119                 }
120                 if($row['to_cid']=='' || $row['to_cid'] == '0'){
121                         $this_row .= '"0",';
122                 }else if($title_refs[$row['to_cid']] == ''){
123                         $this_row .= quote_csv(_AT('deleted')).',';
124                 }else if($title_refs[$row['to_cid']] != ''){
125                         $this_row .= quote_csv($title_refs[$row['to_cid']]).",";
126                 }else{
127                         $this_row .= '"0",';
128                 }
129
130                 $this_row .= quote_csv(_AT($title_refs2[$row['g']])).",";
131                 $this_row.= AT_date(_AT('forum_date_format'),$row['timestamp'], AT_DATE_UNIX_TIMESTAMP).",";
132                 $this_row.= quote_csv($row['duration'])."\n";
133
134         }
135         if (!@opendir(AT_CONTENT_DIR . 'export/')){
136                 mkdir(AT_CONTENT_DIR . 'export/', 0777);
137         }
138
139         $fp = @fopen(AT_CONTENT_DIR . 'export/'.$name.'_tracking.csv', 'w');
140         if (!$fp) {
141                 $errors=array('CSV_FAILED', $name);
142                 $msg->printErrors($errors);
143                 exit;
144         }
145         @fputs($fp, $this_row); @fclose($fp);
146         @readfile(AT_CONTENT_DIR . 'export/'.escapeshellcmd($name).'_tracking.csv');
147         @unlink(AT_CONTENT_DIR . 'export/'.escapeshellcmd($name).'_tracking.csv');
148         exit;
149
150
151 }
152
153 ///////
154 require(AT_INCLUDE_PATH.'header.inc.php');
155
156 // Give the user two chances when deleting tracking data
157 if ($_GET['reset']==1){
158         echo '<a name="warning"></a>';
159         $warnings=array('DELETE_TRACKING', $_SERVER['PHP_SELF']);
160         $msg->printWarnings($warnings);
161         
162         /* Since we do not know which choice will be taken, assume it No/Cancel, addFeedback('CENCELLED)
163          * If sent to results.php then OK, else if sent back here & if $_GET['reset']=2 then assumed choice was not taken
164          * ensure that addFeeback('CANCELLED') is properly cleaned up, see below
165          */
166         $msg->addFeedback('CANCELLED');
167         echo '<center><a href="'.$_SERVER['PHP_SELF'].'?reset=2">'._AT('yes_delete').'</a> | <a href="'.$_SERVER['PHP_SELF'].'">'._AT('no').'</a></center>';
168         require(AT_INCLUDE_PATH.'footer.inc.php');
169         exit;
170 } else if($_GET['reset']==2) {
171         /* We must ensure that any previous feedback is flushed, 
172          * since AT_FEEDBACK_CANCELLED might be present
173          * if Yes/Delete was chosen above when reset was = 1
174          */
175         $msg->deleteFeedback('CANCELLED'); // makes sure its not there 
176                 
177         $sql_delete= "delete from ".TABLE_PREFIX."g_click_data where course_id='$_SESSION[course_id]'";
178         if ($result_delete_track=mysql_query($sql_delete, $db)){
179                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
180         } else {
181                 $msg->addError('TRACKING_NOT_DELETED');
182                 require(AT_INCLUDE_PATH.'footer.inc.php');
183                 exit;
184         }
185 }
186 /////////////////////////////
187 // Top of the page
188 $msg->printAll();
189
190 //This page is only for instructor/owners
191 if(!authenticate(AT_PRIV_ADMIN, AT_PRIV_RETURN)){
192         $msg->printInfos('NO_PERMISSION');
193         require(AT_INCLUDE_PATH.'footer.inc.php');
194         exit;
195 }
196
197 //}
198 //see if tracking is turned on
199 $sql="SELECT tracking from ".TABLE_PREFIX."courses where course_id=$_SESSION[course_id]";
200 $result=mysql_query($sql, $db);
201 while($row= mysql_fetch_array($result)){
202         if($row['tracking'] == "off"){
203                 if(authenticate(AT_PRIV_ADMIN, AT_PRIV_RETURN)){
204                         $msg->addInfo('TRACKING_OFFIN');
205                 }else{
206                         $msg->addInfo('TRACKING_OFFST');
207                 }
208         $msg->printInfos();
209         require(AT_INCLUDE_PATH.'footer.inc.php');
210         exit;
211         }
212 }
213
214 $msg->printWarnings();
215
216 ?>
217         <ul>
218         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?stats=summary#show_pages"><?php echo _AT('g_show_page_stats'); ?></a>
219         <br /><?php echo _AT('g_show_page_stats_desc'); ?>
220         </li>
221         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?stats=student#show_members"><?php echo _AT('g_show_member_stats'); ?></a>
222         <br /><?php echo _AT('g_show_member_stats_desc'); ?>
223         </li>
224         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?csv=1"><?php echo _AT('g_download_tracking_csv'); ?></a>
225         <br /><?php echo _AT('g_download_tracking_csv_desc'); ?>
226         </li>
227         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?reset=1#warning"><?php echo _AT('g_reset_tracking'); ?></a>
228         <br /><?php echo _AT('g_reset_tracking_desc'); ?>
229         </li>
230         </ul>
231
232 <hr />
233 <?php
234
235 // present the id picker
236 if($_GET['stats']=='student' || $_GET['member_id']){
237
238         $sql = "select DISTINCT member_id from ".TABLE_PREFIX."g_click_data where course_id='$_SESSION[course_id]' order by member_id DESC";
239         $result = mysql_query($sql, $db);
240
241         //get the course enrollment
242         $sql2="select * from ".TABLE_PREFIX."course_enrollment where course_id='$_SESSION[course_id]' AND approved='y'";
243         $result2 = mysql_query($sql2, $db);
244         while($row2 = mysql_fetch_array($result2)){
245                 $enrolled[$row2['member_id']] = $row2['member_id'];
246         }
247         ?>
248         <a name="show_members"></a>
249         <table class="data static" rules="cols" summary="">
250         <thead>
251         <tr>
252                 <th><?php echo _AT('select_member'); ?></th>
253         </tr>
254         </thead>
255
256         <tbody>
257         <tr>
258                 <td><?php
259                         if($_GET['summary2'] == "summary") {
260                                 $select_summary = ' checked="checked"';
261                         } else if($_GET['summary2'] == "raw") {
262                                 $select_raw = ' checked="checked"';
263                         }
264                 ?>
265         <form action="<?php echo $_SERVER['PHP_SELF']; ?>#show_members" method="GET">
266         <input type="radio" name="summary2" id="summary2" value="summary" <?php echo $select_summary;  ?>><label for="summary2"><?php echo _AT('summary');  ?> </label>
267         <input type="radio" id="summary" name="summary2" value="raw" <?php echo $select_raw;  ?>><label for="summary"><?php echo _AT('raw');  ?> </label>
268         <select name="member_id">
269         <?php
270         while($row = mysql_fetch_array($result)){
271                 if($row['member_id'] == $enrolled[$row['member_id']]){
272
273                         echo '<option  value="'.$row['member_id'].'" ';
274                         if($_GET['member_id']==$row['member_id']){
275                                 echo ' selected="selected"';
276                         }
277                         echo '>'.$this_user[$row['member_id']].'</option>'."\n";
278                 }
279         }
280         ?>
281         </select>
282         <input type="submit" value="<?php echo _AT('view_tracking');  ?>" class="button" />
283         </form>
284         </td>
285         </tr>
286         </tbody>
287         </table>
288 <?php
289 }
290
291 if($_GET['stats'] =="details" || $_GET['stats'] == "summary" || $_GET['g_id'] || $_GET['csv']== 1) {
292         require(AT_INCLUDE_PATH.'lib/tracker_stats.inc.php');
293 }
294
295 else if ($_GET['summary2'] == "summary") {
296         require(AT_INCLUDE_PATH.'lib/tracker_stats2.inc.php');
297
298
299 else {
300         require(AT_INCLUDE_PATH.'lib/tracker.inc.php');
301 }
302
303         require(AT_INCLUDE_PATH.'footer.inc.php');
304
305         
306 function quote_csv($line) {
307         $line = str_replace('"', '""', $line);
308
309         $line = str_replace("\n", '\n', $line);
310         $line = str_replace("\r", '\r', $line);
311         $line = str_replace("\x00", '\0', $line);
312
313         return '"'.$line.'"';
314 }
315 ?>