PageRenderTime 213ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/course/recent_form.php

https://bitbucket.org/moodle/moodle
PHP | 175 lines | 123 code | 24 blank | 28 comment | 23 complexity | 625c14192985b76820754d84d319650d MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Display all recent activity in a flexible way
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package course
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once($CFG->dirroot . '/course/lib.php');
  27. require_once($CFG->libdir.'/formslib.php');
  28. class recent_form extends moodleform {
  29. function definition() {
  30. global $CFG, $COURSE, $USER;
  31. $mform =& $this->_form;
  32. $context = context_course::instance($COURSE->id);
  33. $modinfo = get_fast_modinfo($COURSE);
  34. $mform->addElement('header', 'filters', get_string('managefilters')); //TODO: add better string
  35. $groupoptions = array();
  36. if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
  37. // limited group access
  38. $groups = groups_get_user_groups($COURSE->id);
  39. $allgroups = groups_get_all_groups($COURSE->id);
  40. if (!empty($groups[$COURSE->defaultgroupingid])) {
  41. foreach ($groups[$COURSE->defaultgroupingid] AS $groupid) {
  42. $groupoptions[$groupid] = format_string($allgroups[$groupid]->name, true, array('context'=>$context));
  43. }
  44. }
  45. } else {
  46. $groupoptions = array('0'=>get_string('allgroups'));
  47. if (has_capability('moodle/site:accessallgroups', $context)) {
  48. // user can see all groups
  49. $allgroups = groups_get_all_groups($COURSE->id);
  50. } else {
  51. // user can see course level groups
  52. $allgroups = groups_get_all_groups($COURSE->id, 0, $COURSE->defaultgroupingid);
  53. }
  54. foreach($allgroups as $group) {
  55. $groupoptions[$group->id] = format_string($group->name, true, array('context'=>$context));
  56. }
  57. }
  58. if ($COURSE->id == SITEID) {
  59. $viewparticipants = course_can_view_participants(context_system::instance());
  60. } else {
  61. $viewparticipants = course_can_view_participants($context);
  62. }
  63. if ($viewparticipants) {
  64. $viewfullnames = has_capability('moodle/site:viewfullnames', context_course::instance($COURSE->id));
  65. $options = array();
  66. $options[0] = get_string('allparticipants');
  67. $options[$CFG->siteguest] = get_string('guestuser');
  68. $userfieldsapi = \core_user\fields::for_userpic();
  69. $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
  70. if (isset($groupoptions[0])) {
  71. // can see all enrolled users
  72. if ($enrolled = get_enrolled_users($context, null, 0, $ufields)) {
  73. foreach ($enrolled as $euser) {
  74. $options[$euser->id] = fullname($euser, $viewfullnames);
  75. }
  76. }
  77. } else {
  78. // can see users from some groups only
  79. foreach ($groupoptions as $groupid=>$unused) {
  80. if ($enrolled = get_enrolled_users($context, null, $groupid, $ufields)) {
  81. foreach ($enrolled as $euser) {
  82. if (!array_key_exists($euser->id, $options)) {
  83. $options[$euser->id] = fullname($euser, $viewfullnames);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. $mform->addElement('select', 'user', get_string('participants'), $options);
  90. $mform->setAdvanced('user');
  91. } else {
  92. // Default to no user.
  93. $mform->addElement('hidden', 'user', 0);
  94. $mform->setType('user', PARAM_INT);
  95. $mform->setConstant('user', 0);
  96. }
  97. $options = array(''=>get_string('allactivities'));
  98. $modsused = array();
  99. foreach($modinfo->cms as $cm) {
  100. if (!$cm->uservisible) {
  101. continue;
  102. }
  103. $modsused[$cm->modname] = true;
  104. }
  105. foreach ($modsused as $modname=>$unused) {
  106. $libfile = "$CFG->dirroot/mod/$modname/lib.php";
  107. if (!file_exists($libfile)) {
  108. unset($modsused[$modname]);
  109. continue;
  110. }
  111. include_once($libfile);
  112. $libfunction = $modname."_get_recent_mod_activity";
  113. if (!function_exists($libfunction)) {
  114. unset($modsused[$modname]);
  115. continue;
  116. }
  117. $options["mod/$modname"] = get_string('allmods', '', get_string('modulenameplural', $modname));
  118. }
  119. foreach ($modinfo->sections as $section=>$cmids) {
  120. $options["section/$section"] = "-- ".get_section_name($COURSE, $section)." --";
  121. foreach ($cmids as $cmid) {
  122. $cm = $modinfo->cms[$cmid];
  123. if (empty($modsused[$cm->modname]) or !$cm->uservisible) {
  124. continue;
  125. }
  126. $options[$cm->id] = format_string($cm->name);
  127. }
  128. }
  129. $mform->addElement('select', 'modid', get_string('activities'), $options);
  130. $mform->setAdvanced('modid');
  131. if ($groupoptions) {
  132. $mform->addElement('select', 'group', get_string('groups'), $groupoptions);
  133. $mform->setAdvanced('group');
  134. } else {
  135. // no access to groups in separate mode
  136. $mform->addElement('hidden','group');
  137. $mform->setType('group', PARAM_INT);
  138. $mform->setConstants(array('group'=>-1));
  139. }
  140. $options = array('default' => get_string('bycourseorder'),
  141. 'dateasc' => get_string('datemostrecentlast'),
  142. 'datedesc' => get_string('datemostrecentfirst'));
  143. $mform->addElement('select', 'sortby', get_string('sortby'), $options);
  144. $mform->setAdvanced('sortby');
  145. $mform->addElement('date_time_selector', 'date', get_string('since'), array('optional'=>true));
  146. $mform->addElement('hidden','id');
  147. $mform->setType('id', PARAM_INT);
  148. $mform->setType('courseid', PARAM_INT);
  149. $this->add_action_buttons(false, get_string('showrecent'));
  150. }
  151. }