ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLPCronObjectStatistics.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "Services/Cron/classes/class.ilCronJob.php";
6 
14 {
15  protected $date; // [string]
16 
17  public function getId()
18  {
19  return "lp_object_statistics";
20  }
21 
22  public function getTitle()
23  {
24  global $lng;
25 
26  $lng->loadLanguageModule("trac");
27  return $lng->txt("trac_object_statistics");
28  }
29 
30  public function getDescription()
31  {
32  global $lng;
33 
34  $lng->loadLanguageModule("trac");
35  return $lng->txt("trac_object_statistics_info");
36  }
37 
38  public function getDefaultScheduleType()
39  {
41  }
42 
43  public function getDefaultScheduleValue()
44  {
45  return;
46  }
47 
48  public function hasAutoActivation()
49  {
50  return true;
51  }
52 
53  public function hasFlexibleSchedule()
54  {
55  return false;
56  }
57 
58  public function run()
59  {
60  // all date related operations are based on this timestamp
61  // should be midnight of yesterday (see gatherUserData()) to always have full day
62  $this->date = strtotime("yesterday");
63 
65  $message = array();
66 
67  $count = 0;
68  $count += $this->gatherCourseLPData();
69  $count += $this->gatherTypesData();
70  $count += $this->gatherUserData();
71 
72  if($count)
73  {
75  }
76 
77  $result = new ilCronJobResult();
78  $result->setStatus($status);
79 
80  return $result;
81  }
82 
83  protected function gatherCourseLPData()
84  {
85  global $tree, $ilDB;
86 
87  $count = 0;
88 
89  // process all courses
90  $all_courses = array_keys(ilObject::_getObjectsByType("crs"));
91  if($all_courses)
92  {
93  // gather objects in trash
94  $trashed_objects = $tree->getSavedNodeObjIds($all_courses);
95 
96  include_once 'Services/Object/classes/class.ilObjectLP.php';
97  include_once "Modules/Course/classes/class.ilCourseParticipants.php";
98  include_once "Services/Tracking/classes/class.ilLPStatusWrapper.php";
99  foreach($all_courses as $crs_id)
100  {
101  // trashed objects will not change
102  if(!in_array($crs_id, $trashed_objects))
103  {
104  // only if LP is active
105  $olp = ilObjectLP::getInstance($crs_id);
106  if(!$olp->isActive())
107  {
108  continue;
109  }
110 
111  // only save once per day
112  $ilDB->manipulate("DELETE FROM obj_lp_stat WHERE".
113  " obj_id = ".$ilDB->quote($crs_id, "integer").
114  " AND fulldate = ".$ilDB->quote(date("Ymd", $this->date), "integer"));
115 
116  $members = new ilCourseParticipants($crs_id);
117  $members = $members->getMembers();
118 
119  $in_progress = count(ilLPStatusWrapper::_lookupInProgressForObject($crs_id, $members));
120  $completed = count(ilLPStatusWrapper::_lookupCompletedForObject($crs_id, $members));
121  $failed = count(ilLPStatusWrapper::_lookupFailedForObject($crs_id, $members));
122 
123  // calculate with other values - there is not direct method
124  $not_attempted = count($members) - $in_progress - $completed - $failed;
125 
126  $set = array(
127  "type" => array("text", "crs"),
128  "obj_id" => array("integer", $crs_id),
129  "yyyy" => array("integer", date("Y", $this->date)),
130  "mm" => array("integer", date("m", $this->date)),
131  "dd" => array("integer", date("d", $this->date)),
132  "fulldate" => array("integer", date("Ymd", $this->date)),
133  "mem_cnt" => array("integer", count($members)),
134  "in_progress" => array("integer", $in_progress),
135  "completed" => array("integer", $completed),
136  "failed" => array("integer", $failed),
137  "not_attempted" => array("integer", $not_attempted)
138  );
139 
140  $ilDB->insert("obj_lp_stat", $set);
141 
142  $count++;
143  }
144  }
145  }
146 
147  return $count;
148  }
149 
150  protected function gatherTypesData()
151  {
152  global $ilDB;
153 
154  $count = 0;
155 
156  include_once "Services/Tracking/classes/class.ilTrQuery.php";
158  foreach($data as $type => $item)
159  {
160  // only save once per day
161  $ilDB->manipulate("DELETE FROM obj_type_stat WHERE".
162  " type = ".$ilDB->quote($type, "text").
163  " AND fulldate = ".$ilDB->quote(date("Ymd", $this->date), "integer"));
164 
165  $set = array(
166  "type" => array("text", $type),
167  "yyyy" => array("integer", date("Y", $this->date)),
168  "mm" => array("integer", date("m", $this->date)),
169  "dd" => array("integer", date("d", $this->date)),
170  "fulldate" => array("integer", date("Ymd", $this->date)),
171  "cnt_references" => array("integer", (int)$item["references"]),
172  "cnt_objects" => array("integer", (int)$item["objects"]),
173  "cnt_deleted" => array("integer", (int)$item["deleted"])
174  );
175 
176  $ilDB->insert("obj_type_stat", $set);
177 
178  $count++;
179  }
180 
181  return $count;
182  }
183 
184  protected function gatherUserData()
185  {
186  global $ilDB;
187 
188  $count = 0;
189 
190  $to = mktime(23, 59, 59, date("m", $this->date), date("d", $this->date), date("Y", $this->date));
191 
192  $sql = "SELECT COUNT(DISTINCT(usr_id)) counter,obj_id FROM read_event".
193  " WHERE last_access >= ".$ilDB->quote($this->date, "integer").
194  " AND last_access <= ".$ilDB->quote($to, "integer").
195  " GROUP BY obj_id";
196  $set = $ilDB->query($sql);
197  while($row = $ilDB->fetchAssoc($set))
198  {
199  // only save once per day
200  $ilDB->manipulate("DELETE FROM obj_user_stat".
201  " WHERE fulldate = ".$ilDB->quote(date("Ymd", $this->date), "integer").
202  " AND obj_id = ".$ilDB->quote($row["obj_id"], "integer"));
203 
204  $iset = array(
205  "obj_id" => array("integer", $row["obj_id"]),
206  "yyyy" => array("integer", date("Y", $this->date)),
207  "mm" => array("integer", date("m", $this->date)),
208  "dd" => array("integer", date("d", $this->date)),
209  "fulldate" => array("integer", date("Ymd", $this->date)),
210  "counter" => array("integer", $row["counter"])
211  );
212 
213  $ilDB->insert("obj_user_stat", $iset);
214 
215  $count++;
216  }
217 
218  return $count;
219  }
220 }
221 
222 ?>