PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/course/completion_form.php

https://bitbucket.org/andrewdavidson/sl-clone
PHP | 222 lines | 132 code | 43 blank | 47 comment | 13 complexity | 4b77f7297b38fc5eb571f09ce628e01a MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. if (!defined('MOODLE_INTERNAL')) {
  25. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  26. }
  27. require_once($CFG->libdir.'/formslib.php');
  28. require_once($CFG->libdir.'/completionlib.php');
  29. class course_completion_form extends moodleform {
  30. function definition() {
  31. global $USER, $CFG, $DB, $js_enabled;
  32. $courseconfig = get_config('moodlecourse');
  33. $mform =& $this->_form;
  34. $course = $this->_customdata['course'];
  35. $completion = new completion_info($course);
  36. $params = array(
  37. 'course' => $course->id
  38. );
  39. /// form definition
  40. //--------------------------------------------------------------------------------
  41. // Check if there is existing criteria completions
  42. if ($completion->is_course_locked()) {
  43. $mform->addElement('header', '', get_string('completionsettingslocked', 'completion'));
  44. $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
  45. $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
  46. }
  47. // Get array of all available aggregation methods
  48. $aggregation_methods = $completion->get_aggregation_methods();
  49. // Overall criteria aggregation
  50. $mform->addElement('header', 'overallcriteria', get_string('overallcriteriaaggregation', 'completion'));
  51. $mform->addElement('select', 'overall_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
  52. $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
  53. // Course prerequisite completion criteria
  54. $mform->addElement('header', 'courseprerequisites', get_string('courseprerequisites', 'completion'));
  55. // Get applicable courses
  56. $courses = $DB->get_records_sql(
  57. "
  58. SELECT DISTINCT
  59. c.id,
  60. c.category,
  61. c.fullname,
  62. cc.id AS selected
  63. FROM
  64. {course} c
  65. LEFT JOIN
  66. {course_completion_criteria} cc
  67. ON cc.courseinstance = c.id
  68. AND cc.course = {$course->id}
  69. INNER JOIN
  70. {course_completion_criteria} ccc
  71. ON ccc.course = c.id
  72. WHERE
  73. c.enablecompletion = ".COMPLETION_ENABLED."
  74. AND c.id <> {$course->id}
  75. "
  76. );
  77. if (!empty($courses)) {
  78. if (count($courses) > 1) {
  79. $mform->addElement('select', 'course_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
  80. $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
  81. }
  82. // Get category list
  83. $list = array();
  84. $parents = array();
  85. make_categories_list($list, $parents);
  86. // Get course list for select box
  87. $selectbox = array();
  88. $selected = array();
  89. foreach ($courses as $c) {
  90. $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $c->id)));
  91. // If already selected
  92. if ($c->selected) {
  93. $selected[] = $c->id;
  94. }
  95. }
  96. // Show multiselect box
  97. $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox, array('multiple' => 'multiple', 'size' => 6));
  98. // Select current criteria
  99. $mform->setDefault('criteria_course', $selected);
  100. // Explain list
  101. $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
  102. } else {
  103. $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
  104. }
  105. // Manual self completion
  106. $mform->addElement('header', 'manualselfcompletion', get_string('manualselfcompletion', 'completion'));
  107. $criteria = new completion_criteria_self($params);
  108. $criteria->config_form_display($mform);
  109. // Role completion criteria
  110. $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion'));
  111. $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, get_context_instance(CONTEXT_COURSE, $course->id));
  112. if (!empty($roles)) {
  113. $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
  114. $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
  115. foreach ($roles as $role) {
  116. $params_a = array('role' => $role->id);
  117. $criteria = new completion_criteria_role(array_merge($params, $params_a));
  118. $criteria->config_form_display($mform, $role);
  119. }
  120. } else {
  121. $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
  122. }
  123. // Activity completion criteria
  124. $mform->addElement('header', 'activitiescompleted', get_string('activitiescompleted', 'completion'));
  125. $activities = $completion->get_activities();
  126. if (!empty($activities)) {
  127. if (count($activities) > 1) {
  128. $mform->addElement('select', 'activity_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
  129. $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
  130. }
  131. foreach ($activities as $activity) {
  132. $params_a = array('moduleinstance' => $activity->id);
  133. $criteria = new completion_criteria_activity(array_merge($params, $params_a));
  134. $criteria->config_form_display($mform, $activity);
  135. }
  136. } else {
  137. $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
  138. }
  139. // Completion on date
  140. $mform->addElement('header', 'date', get_string('date'));
  141. $criteria = new completion_criteria_date($params);
  142. $criteria->config_form_display($mform);
  143. // Completion after enrolment duration
  144. $mform->addElement('header', 'duration', get_string('durationafterenrolment', 'completion'));
  145. $criteria = new completion_criteria_duration($params);
  146. $criteria->config_form_display($mform);
  147. // Completion on course grade
  148. $mform->addElement('header', 'grade', get_string('coursegrade', 'completion'));
  149. // Grade enable and passing grade
  150. $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
  151. if (!$course_grade) {
  152. $course_grade = '0.00000';
  153. }
  154. $criteria = new completion_criteria_grade($params);
  155. $criteria->config_form_display($mform, $course_grade);
  156. // Completion on unenrolment
  157. $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
  158. $criteria = new completion_criteria_unenrol($params);
  159. $criteria->config_form_display($mform);
  160. //--------------------------------------------------------------------------------
  161. $this->add_action_buttons();
  162. //--------------------------------------------------------------------------------
  163. $mform->addElement('hidden', 'id', $course->id);
  164. $mform->setType('id', PARAM_INT);
  165. // If the criteria are locked, freeze values and submit button
  166. if ($completion->is_course_locked()) {
  167. $except = array('settingsunlock');
  168. $mform->hardFreezeAllVisibleExcept($except);
  169. $mform->addElement('cancel');
  170. }
  171. }
  172. /// perform some extra moodle validation
  173. function validation($data, $files) {
  174. global $DB, $CFG;
  175. $errors = parent::validation($data, $files);
  176. return $errors;
  177. }
  178. }
  179. ?>