tracker.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * Defines a class for viewing the SLOODLE Second Life Tracker module in Moodle.
  4. * Derived from the module view base class.
  5. *
  6. */
  7. /** The base module view class */
  8. require_once(SLOODLE_DIRROOT.'/view/base/base_view_module.php');
  9. /** The SLOODLE Session data structures */
  10. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  11. /**
  12. * Class for rendering a view of a Second Life Tracker module in Moodle.
  13. * @package sloodle
  14. */
  15. class sloodle_view_tracker extends sloodle_base_view_module
  16. {
  17. /**
  18. * When multiple users will be displayed, this value indicates which record to start from.
  19. * It will be read from HTTP parameters.
  20. * @var int
  21. * @access private
  22. */
  23. var $start = 0;
  24. /**
  25. * Number of users to display per page.
  26. * @var int
  27. * @access private
  28. */
  29. var $usersPerPage = 10;
  30. /**
  31. * The number of the page of users to be displayed.
  32. * 1-based.
  33. * @var int
  34. * @access private
  35. */
  36. var $page = 1;
  37. /**
  38. * Constructor.
  39. */
  40. function sloodle_view_tracker()
  41. {
  42. }
  43. /**
  44. * Processes request data to determine which Second Life Tracker is being accessed.
  45. */
  46. function process_request()
  47. {
  48. // Process the basic data
  49. parent::process_request();
  50. $this->page = optional_param('page', 1, PARAM_INT);
  51. if ($this->page < 1) $this->page = 1;
  52. $this->start = ($this->page - 1) * $this->usersPerPage;
  53. // Nothing else to get just now
  54. }
  55. /**
  56. * Process any form data which has been submitted.
  57. */
  58. function process_form()
  59. {
  60. }
  61. /**
  62. * Get the students enroled in this course.
  63. * @return $userlist: A list with the users, empty if there are no users enroled
  64. */
  65. function get_class_list()
  66. {
  67. $userlist = array();
  68. // Ideally, use the capability-based user search.
  69. // This may not exist in particularly early versions of Moodle 1.8 and 1.9.
  70. // Fall-back on the function to get all users in a course if necessary,
  71. // but note that it is a deprecated function, and it only seems to return students.
  72. if (function_exists('get_users_by_capability'))
  73. {
  74. $userlist = get_users_by_capability($this->course_context, 'mod/sloodle:courseparticipate', 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
  75. }
  76. else
  77. {
  78. $userlist = get_course_users($this->courseid, 'firstname, lastname', '', 'u.id, firstname, lastname');
  79. }
  80. // Return an empty array if something went wrong
  81. if (!$userlist) return array();
  82. return $userlist;
  83. }
  84. /**
  85. * Render the view of the SecondLife Tracker.
  86. */
  87. function render()
  88. {
  89. global $CFG, $USER;
  90. $session = new SloodleSession(false);
  91. $tracker = new SloodleModuleTracker($session);
  92. // Check if this user is allowed to view all users in this activity, and if s/he is allowed to manage this activity
  93. $canManage = $this->canedit;
  94. // Load data of the current Second Life Tracker
  95. if (!$tracker->load($this->cm->id)) error("FAILED TO LOAD MODULE");
  96. $this->courseid = $this->course->id;
  97. $sloodleid=$this->sloodle->id;
  98. if ($canManage)
  99. {
  100. $strtrackeradmin = get_string('trackeradmin', 'sloodle');
  101. $strresetallprogress = get_string('resetallprogress', 'sloodle');
  102. $strdeletealltasks = get_string('deletealltasks', 'sloodle');
  103. echo <<<XXXEODXXX
  104. <table style="margin:auto auto; ">
  105. <!--<tr><th colspan="2">{$strtrackeradmin}</th></tr>-->
  106. <tr>
  107. <td style="text-align:center;">
  108. <form action="" method="POST" style="padding:6px;">
  109. <input type="hidden" name="id" value="{$this->cm->id}"/>
  110. <input type="hidden" name="action" value="reset_all_progress"/>
  111. <input type="submit" value="{$strresetallprogress}"/>
  112. </form>
  113. </td>
  114. <td style="text-align:center;">
  115. <form action="" method="POST" style="padding:6px;">
  116. <input type="hidden" name="id" value="{$this->cm->id}"/>
  117. <input type="hidden" name="action" value="delete_all_tasks"/>
  118. <input type="submit" value="{$strdeletealltasks}"/>
  119. </form>
  120. </td>
  121. </tr>
  122. </table>
  123. XXXEODXXX;
  124. }
  125. print('<h3 style="color:black;text-align:center;">'.get_string('secondlifetracker:activity','sloodle')).'</h3> ';
  126. // Check if some kind of action has been requested, used if tasks has been reset
  127. $action = optional_param('action', '', PARAM_TEXT);
  128. // Has a reset task progress action been requested?
  129. if ($action == 'reset_tasks' && $canManage)
  130. {
  131. // Go through each request parameter
  132. foreach ($_REQUEST as $name => $val)
  133. {
  134. if ($val != 'true') continue;
  135. $parts = explode('_', $name);
  136. if (count($parts) == 2 && $parts[0] == 'sloodledeleteobj')
  137. {
  138. // Only delete the activity if it belongs to this controller
  139. sloodle_delete_records('sloodle_activity_tracker', 'trackerid', $this->cm->id, 'id', (int)$parts[1]);
  140. }
  141. }
  142. }
  143. // Has a reset all tasks action been requested? (This deletes all activities from the module)
  144. if ($action == 'delete_all_tasks' && $canManage)
  145. {
  146. sloodle_delete_records('sloodle_activity_tracker', 'trackerid', $this->cm->id);
  147. sloodle_delete_records('sloodle_activity_tool', 'trackerid', $this->cm->id);
  148. }
  149. // Has a reset all progress action been requested?
  150. if ($action == 'reset_all_progress' && $canManage)
  151. {
  152. sloodle_delete_records('sloodle_activity_tracker', 'trackerid', $this->cm->id);
  153. }
  154. //Obtain the users in this course
  155. $userlist = $this->get_class_list();
  156. if ($userlist) {
  157. // How many pages of users are there?
  158. $numresults = count($userlist);
  159. $numpages = (int)ceil($numresults / $this->usersPerPage);
  160. if ($this->page > $numpages) $this->page = $numpages;
  161. $this->start = ($this->page - 1) * $this->usersPerPage;
  162. $sloodletable = new stdClass();
  163. // Create column headers for html table
  164. $sloodletable->head = array( get_string('user', 'sloodle'),
  165. get_string('avatar', 'sloodle'),
  166. );
  167. // Set alignment of table cells
  168. $sloodletable->align = array('center', 'center');
  169. // Set size of table cells
  170. $sloodletable->size = array('50%', '50%');
  171. // Check if our start is past the end of our results
  172. if (empty($this->start) || $this->start >= count($userlist)) $this->start = 0;
  173. // Ignore the start parameter if the user can only see his/her own record
  174. if (!$canManage) $this->start = 0;
  175. $resultnum = 0;
  176. $resultsdisplayed = 0;
  177. // Go through each user
  178. foreach ($userlist as $u)
  179. {
  180. // Skip until the desired start point of the results
  181. if ($resultnum < $this->start)
  182. {
  183. $resultnum++;
  184. continue;
  185. }
  186. // If this user is not a teacher, and this is not the user's own details, then skip this iteration
  187. if (!($u->id == $USER->id || $canManage)) continue;
  188. // This variable will contain the avatar identifier, necessary to search in the "sloodle_activity_tracker" DB table.
  189. $avatarid = '';
  190. // These variables will be used to obtain the overall percentage of tasks completed
  191. $tasks = 0;
  192. $completed = 0;
  193. // Only display this result if it is after our starting result number
  194. //if ($resultnum >= $this->start) {
  195. // Reset the line's content
  196. $line = array();
  197. // Construct URLs to this user's Moodle and SLOODLE profile pages
  198. $url_moodleprofile = $CFG->wwwroot."/user/view.php?id={$u->id}&amp;course={$this->courseid}";
  199. $url_sloodleprofile = SLOODLE_WWWROOT."/view.php?_type=user&amp;id={$u->id}&amp;course={$this->courseid}";
  200. // Add the Moodle name
  201. $line[0] = "<a href=\"{$url_moodleprofile}\">{$u->firstname} {$u->lastname}</a>";
  202. // Get the Sloodle data for this Moodle user
  203. $sloodledata = sloodle_get_records('sloodle_users', 'userid', $u->id, 'uuid, avname');
  204. // Initialize our search index
  205. /////////////////////////////////////////////////////////////////////////$dateIndex=false;
  206. if ($sloodledata)
  207. {
  208. // Display all avatars names, if available
  209. $avnames = '';
  210. $firstentry = true;
  211. foreach ($sloodledata as $sd) {
  212. // If this entry is empty, then skip it
  213. if (empty($sd->avname) || ctype_space($sd->avname)) continue;
  214. // Comma separated entries
  215. if ($firstentry) $firstentry = false;
  216. else $avnames .= ', ';
  217. // Add the current name
  218. $avnames .= $sd->avname;
  219. $avatarid = $sd->uuid;
  220. }
  221. // Add the avatar name(s) to the line
  222. $line[1] = "<a href=\"{$url_sloodleprofile}\">{$avnames}</a>";
  223. } else {
  224. // The query failed - if we are showing only Sloodle-enabled users, then skip the rest
  225. if (!empty($this->sloodleonly) && $this->sloodleonly) continue;
  226. $line[1] = get_string('secondlifetracker:noavatar','sloodle');
  227. }
  228. $sloodletable->data[0] = $line;
  229. $resultsdisplayed++;
  230. //}
  231. sloodle_print_table($sloodletable);
  232. //Now all the tasks in the Tracker are displayed
  233. echo "<div style=\"text-align:center;\">\n";
  234. //echo '<h3>'.get_string('secondlifetasks','sloodle').'</h3>';
  235. echo "<br/>";
  236. // Get all the tasks for this Tracker, ordered by "taskname"
  237. $recs = sloodle_get_records('sloodle_activity_tool', 'trackerid', $this->cm->id, 'taskname');
  238. if (is_array($recs) && count($recs) > 0)
  239. {
  240. $objects_table = new stdClass();
  241. // Create column headers for html table
  242. $objects_table->head = array(get_string('objectname','sloodle'),get_string('secondlifeobjdesc','sloodle'),get_string('secondlifelevelcompl','sloodle'),'Date','');
  243. // Set alignment of table cells
  244. $objects_table->align = array('left', 'left', 'centre', 'centre', 'centre');
  245. $overall = 0;
  246. foreach ($recs as $obj) {
  247. // Skip this object if it has no type information
  248. if (empty($obj->type)) continue;
  249. //Has this user completed the task?
  250. $act = sloodle_get_record('sloodle_activity_tracker','avuuid',$avatarid,'objuuid',$obj->uuid,'trackerid',$this->cm->id);
  251. //Yes. Activity completed
  252. if (!empty($act)){
  253. $timezone = $act->timeupdated - 3600;
  254. $date = date("F j, Y, g:i:s a", $timezone);
  255. // Only the admin can reset tasks
  256. if ($canManage)
  257. {
  258. $objects_table->data[] = array('<span style="text-align:center;">'.$obj->taskname.'</a>', $obj->description, '<span style="text-align:center;color:green;">'.get_string('secondlifetracker:completed','sloodle').'</span><br>',$date,"<input type=\"checkbox\" name=\"sloodledeleteobj_{$act->id}\" value=\"true\" /");
  259. }
  260. else {
  261. $objects_table->data[] = array('<span style="text-align:center;">'.$obj->taskname.'</a>', $obj->description, '<span style="text-align:center;color:green">'.get_string('secondlifetracker:completed','sloodle').'</span><br>',$date,' - ');
  262. }
  263. $tasks += 1;
  264. $completed += 1;
  265. }
  266. //No. Activity not completed
  267. else {
  268. $objects_table->data[] = array('<span style="text-align:center;">'.$obj->taskname.'</a>', $obj->description, '<span style="text-align:center;color:red">'.get_string('secondlifetracker:notcompleted','sloodle').'</span><br>',' - ',' - ');
  269. $tasks += 1;
  270. }
  271. //Overall percentage of tasks completed?
  272. $overall = 0;
  273. if ($tasks > 0) $overall = ((integer)(($completed / $tasks) * 1000.0)) / 10.0;
  274. }
  275. // If is the admin, show the reset button
  276. if ($canManage){
  277. echo '<form action="" method="POST">';
  278. echo '<input type="hidden" name="id" value="'.$this->cm->id.'"/>';
  279. echo '<input type="hidden" name="action" value="reset_tasks"/>';
  280. sloodle_print_table($objects_table);
  281. echo '<h3>Completed: '.$overall.'%</h3>';
  282. echo '<input type="submit" value="'.get_string('resettasks','sloodle').'"/>';
  283. echo '</form>';
  284. }
  285. else
  286. {
  287. sloodle_print_table($objects_table);
  288. echo '<h3>Completed: '.$overall.'%</h3>';
  289. }
  290. }
  291. //No tasks in the Tracker
  292. else {
  293. echo '<span style="text-align:center;color:red">'.'No tasks found'.'</span><br>';
  294. }
  295. //echo '<p>&nbsp;</p>';
  296. echo "<br/><hr><br/>";
  297. echo "</div>\n";
  298. // Have we displayed the maximum number of results for this page?
  299. $resultnum++;
  300. if ($resultsdisplayed >= $this->usersPerPage) break;
  301. }
  302. $basicurl = SLOODLE_WWWROOT."/view.php?id={$this->cm->id}&amp;course={$this->courseid}";
  303. // Construct the links for page navigation
  304. $prevlink = ""; $nextlink = "";
  305. if ($this->page > 1)
  306. {
  307. $prevpage = $this->page - 1;
  308. $prevlink = "<a href=\"{$basicurl}&amp;page={$prevpage}\">&lt;&lt;</a>&nbsp;&nbsp;";
  309. } else {
  310. $prevlink = "<span style=\"color:#777777;\">&lt;&lt;</span>&nbsp;&nbsp;";
  311. }
  312. if ($this->page < $numpages)
  313. {
  314. $nextpage = $this->page + 1;
  315. $nextlink = "&nbsp;&nbsp;<a href=\"{$basicurl}&amp;page={$nextpage}\">&gt;&gt;</a>";
  316. } else {
  317. $nextlink = "&nbsp;&nbsp;<span style=\"color:#777777;\">&gt;&gt;</span>";
  318. }
  319. // Display the page navigation
  320. echo "<div style=\"text-align:center; font-size:130%; font-weight:bold;\">";
  321. echo $prevlink;
  322. $pagenav = new stdClass();
  323. $pagenav->num = $this->page;
  324. $pagenav->total = $numpages;
  325. print_string('pagecount', 'sloodle', $pagenav);
  326. echo $nextlink;
  327. echo "</div>";
  328. }
  329. //No users enroled in the course
  330. else {
  331. echo "<div style=\"text-align:center;\">\n";
  332. echo '<span style="color:red">'.get_string('tracker:nousers','sloodle').'</span><br>';
  333. }
  334. }
  335. }
  336. ?>