PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/course/completion_form.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 296 lines | 197 code | 37 blank | 62 comment | 38 complexity | f82406e7a424a8136d1d6d5969bc9a17 MD5 | raw file
  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. * Edit course completion settings - the form definition.
  18. *
  19. * @package core_completion
  20. * @category completion
  21. * @copyright 2009 Catalyst IT Ltd
  22. * @author Aaron Barnes <aaronb@catalyst.net.nz>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->libdir.'/formslib.php');
  27. require_once($CFG->libdir.'/completionlib.php');
  28. /**
  29. * Defines the course completion settings form.
  30. */
  31. class course_completion_form extends moodleform {
  32. /**
  33. * Defines the form fields.
  34. */
  35. public function definition() {
  36. global $USER, $CFG, $DB;
  37. $courseconfig = get_config('moodlecourse');
  38. $mform = $this->_form;
  39. $course = $this->_customdata['course'];
  40. $completion = new completion_info($course);
  41. $params = array(
  42. 'course' => $course->id
  43. );
  44. // Check if there are existing criteria completions.
  45. if ($completion->is_course_locked()) {
  46. $mform->addElement('header', 'completionsettingslocked', get_string('completionsettingslocked', 'completion'));
  47. $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
  48. $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
  49. }
  50. // Get array of all available aggregation methods.
  51. $aggregation_methods = $completion->get_aggregation_methods();
  52. // Overall criteria aggregation.
  53. $mform->addElement('header', 'overallcriteria', get_string('general', 'core_form'));
  54. // Map aggregation methods to context-sensitive human readable dropdown menu.
  55. $overallaggregationmenu = array();
  56. foreach ($aggregation_methods as $methodcode => $methodname) {
  57. if ($methodcode === COMPLETION_AGGREGATION_ALL) {
  58. $overallaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('overallaggregation_all', 'core_completion');
  59. } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
  60. $overallaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('overallaggregation_any', 'core_completion');
  61. } else {
  62. $overallaggregationmenu[$methodcode] = $methodname;
  63. }
  64. }
  65. $mform->addElement('select', 'overall_aggregation', get_string('overallaggregation', 'core_completion'), $overallaggregationmenu);
  66. $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
  67. // Activity completion criteria
  68. $label = get_string('coursecompletioncondition', 'core_completion', get_string('activitiescompleted', 'core_completion'));
  69. $mform->addElement('header', 'activitiescompleted', $label);
  70. // Get the list of currently specified conditions and expand the section if some are found.
  71. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY);
  72. if (!empty($current)) {
  73. $mform->setExpanded('activitiescompleted');
  74. }
  75. $activities = $completion->get_activities();
  76. if (!empty($activities)) {
  77. if (!$completion->is_course_locked()) {
  78. $this->add_checkbox_controller(1, null, null, 0);
  79. }
  80. foreach ($activities as $activity) {
  81. $params_a = array('moduleinstance' => $activity->id);
  82. $criteria = new completion_criteria_activity(array_merge($params, $params_a));
  83. $criteria->config_form_display($mform, $activity);
  84. }
  85. $mform->addElement('static', 'criteria_role_note', '', get_string('activitiescompletednote', 'core_completion'));
  86. if (count($activities) > 1) {
  87. // Map aggregation methods to context-sensitive human readable dropdown menu.
  88. $activityaggregationmenu = array();
  89. foreach ($aggregation_methods as $methodcode => $methodname) {
  90. if ($methodcode === COMPLETION_AGGREGATION_ALL) {
  91. $activityaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('activityaggregation_all', 'core_completion');
  92. } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
  93. $activityaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('activityaggregation_any', 'core_completion');
  94. } else {
  95. $activityaggregationmenu[$methodcode] = $methodname;
  96. }
  97. }
  98. $mform->addElement('select', 'activity_aggregation', get_string('activityaggregation', 'core_completion'), $activityaggregationmenu);
  99. $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
  100. }
  101. } else {
  102. $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
  103. }
  104. // Course prerequisite completion criteria.
  105. $label = get_string('coursecompletioncondition', 'core_completion', get_string('dependenciescompleted', 'core_completion'));
  106. $mform->addElement('header', 'courseprerequisites', $label);
  107. // Get the list of currently specified conditions and expand the section if some are found.
  108. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_COURSE);
  109. if (!empty($current)) {
  110. $mform->setExpanded('courseprerequisites');
  111. }
  112. // Get applicable courses (prerequisites).
  113. $courses = $DB->get_records_sql("
  114. SELECT DISTINCT c.id, c.category, c.fullname, cc.id AS selected
  115. FROM {course} c
  116. LEFT JOIN {course_completion_criteria} cc ON cc.courseinstance = c.id AND cc.course = {$course->id}
  117. INNER JOIN {course_completion_criteria} ccc ON ccc.course = c.id
  118. WHERE c.enablecompletion = ".COMPLETION_ENABLED."
  119. AND c.id <> {$course->id}");
  120. if (!empty($courses)) {
  121. // Get category list.
  122. require_once($CFG->libdir. '/coursecatlib.php');
  123. $list = coursecat::make_categories_list();
  124. // Get course list for select box.
  125. $selectbox = array();
  126. $selected = array();
  127. foreach ($courses as $c) {
  128. $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true,
  129. array('context' => context_course::instance($c->id)));
  130. // If already selected ...
  131. if ($c->selected) {
  132. $selected[] = $c->id;
  133. }
  134. }
  135. // Show multiselect box.
  136. $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox,
  137. array('multiple' => 'multiple', 'size' => 6));
  138. // Select current criteria.
  139. $mform->setDefault('criteria_course', $selected);
  140. // Explain list.
  141. $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
  142. if (count($courses) > 1) {
  143. // Map aggregation methods to context-sensitive human readable dropdown menu.
  144. $courseaggregationmenu = array();
  145. foreach ($aggregation_methods as $methodcode => $methodname) {
  146. if ($methodcode === COMPLETION_AGGREGATION_ALL) {
  147. $courseaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('courseaggregation_all', 'core_completion');
  148. } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
  149. $courseaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('courseaggregation_any', 'core_completion');
  150. } else {
  151. $courseaggregationmenu[$methodcode] = $methodname;
  152. }
  153. }
  154. $mform->addElement('select', 'course_aggregation', get_string('courseaggregation', 'core_completion'), $courseaggregationmenu);
  155. $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
  156. }
  157. } else {
  158. $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
  159. }
  160. // Completion on date
  161. $label = get_string('coursecompletioncondition', 'core_completion', get_string('completionondate', 'core_completion'));
  162. $mform->addElement('header', 'date', $label);
  163. // Expand the condition section if it is currently enabled.
  164. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DATE);
  165. if (!empty($current)) {
  166. $mform->setExpanded('date');
  167. }
  168. $criteria = new completion_criteria_date($params);
  169. $criteria->config_form_display($mform);
  170. // Completion after enrolment duration
  171. $label = get_string('coursecompletioncondition', 'core_completion', get_string('enrolmentduration', 'core_completion'));
  172. $mform->addElement('header', 'duration', $label);
  173. // Expand the condition section if it is currently enabled.
  174. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DURATION);
  175. if (!empty($current)) {
  176. $mform->setExpanded('duration');
  177. }
  178. $criteria = new completion_criteria_duration($params);
  179. $criteria->config_form_display($mform);
  180. // Completion on unenrolment
  181. $label = get_string('coursecompletioncondition', 'core_completion', get_string('unenrolment', 'core_completion'));
  182. $mform->addElement('header', 'unenrolment', $label);
  183. // Expand the condition section if it is currently enabled.
  184. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_UNENROL);
  185. if (!empty($current)) {
  186. $mform->setExpanded('unenrolment');
  187. }
  188. $criteria = new completion_criteria_unenrol($params);
  189. $criteria->config_form_display($mform);
  190. // Completion on course grade
  191. $label = get_string('coursecompletioncondition', 'core_completion', get_string('coursegrade', 'core_completion'));
  192. $mform->addElement('header', 'grade', $label);
  193. // Expand the condition section if it is currently enabled.
  194. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_GRADE);
  195. if (!empty($current)) {
  196. $mform->setExpanded('grade');
  197. }
  198. $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
  199. if (!$course_grade) {
  200. $course_grade = '0.00000';
  201. }
  202. $criteria = new completion_criteria_grade($params);
  203. $criteria->config_form_display($mform, $course_grade);
  204. // Manual self completion
  205. $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualselfcompletion', 'core_completion'));
  206. $mform->addElement('header', 'manualselfcompletion', $label);
  207. // Expand the condition section if it is currently enabled.
  208. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_SELF);
  209. if (!empty($current)) {
  210. $mform->setExpanded('manualselfcompletion');
  211. }
  212. $criteria = new completion_criteria_self($params);
  213. $criteria->config_form_display($mform);
  214. $mform->addElement('static', 'criteria_self_note', '', get_string('manualselfcompletionnote', 'core_completion'));
  215. // Role completion criteria
  216. $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualcompletionby', 'core_completion'));
  217. $mform->addElement('header', 'roles', $label);
  218. // Expand the condition section if it is currently enabled.
  219. $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
  220. if (!empty($current)) {
  221. $mform->setExpanded('roles');
  222. }
  223. $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING));
  224. if (!empty($roles)) {
  225. foreach ($roles as $role) {
  226. $params_a = array('role' => $role->id);
  227. $criteria = new completion_criteria_role(array_merge($params, $params_a));
  228. $criteria->config_form_display($mform, $role);
  229. }
  230. $mform->addElement('static', 'criteria_role_note', '', get_string('manualcompletionbynote', 'core_completion'));
  231. // Map aggregation methods to context-sensitive human readable dropdown menu.
  232. $roleaggregationmenu = array();
  233. foreach ($aggregation_methods as $methodcode => $methodname) {
  234. if ($methodcode === COMPLETION_AGGREGATION_ALL) {
  235. $roleaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('roleaggregation_all', 'core_completion');
  236. } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
  237. $roleaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('roleaggregation_any', 'core_completion');
  238. } else {
  239. $roleaggregationmenu[$methodcode] = $methodname;
  240. }
  241. }
  242. $mform->addElement('select', 'role_aggregation', get_string('roleaggregation', 'core_completion'), $roleaggregationmenu);
  243. $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
  244. } else {
  245. $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
  246. }
  247. // Add common action buttons.
  248. $this->add_action_buttons();
  249. // Add hidden fields.
  250. $mform->addElement('hidden', 'id', $course->id);
  251. $mform->setType('id', PARAM_INT);
  252. // If the criteria are locked, freeze values and submit button.
  253. if ($completion->is_course_locked()) {
  254. $except = array('settingsunlock');
  255. $mform->hardFreezeAllVisibleExcept($except);
  256. $mform->addElement('cancel');
  257. }
  258. }
  259. }