PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/export/grade_export_form.php

https://bitbucket.org/moodle/moodle
PHP | 232 lines | 154 code | 33 blank | 45 comment | 38 complexity | 37088e5a131c38f203423a2c27cd83a2 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. if (!defined('MOODLE_INTERNAL')) {
  17. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  18. }
  19. require_once $CFG->libdir.'/formslib.php';
  20. class grade_export_form extends moodleform {
  21. function definition() {
  22. global $CFG, $COURSE, $USER, $DB;
  23. $isdeprecatedui = false;
  24. $mform =& $this->_form;
  25. if (isset($this->_customdata)) { // hardcoding plugin names here is hacky
  26. $features = $this->_customdata;
  27. } else {
  28. $features = array();
  29. }
  30. if (empty($features['simpleui'])) {
  31. debugging('Grade export plugin needs updating to support one step exports.', DEBUG_DEVELOPER);
  32. }
  33. $mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));
  34. $mform->setExpanded('gradeitems', true);
  35. if (!empty($features['idnumberrequired'])) {
  36. $mform->addElement('static', 'idnumberwarning', get_string('useridnumberwarning', 'grades'));
  37. }
  38. $switch = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
  39. // Grab the grade_seq for this course
  40. $gseq = new grade_seq($COURSE->id, $switch);
  41. if ($grade_items = $gseq->items) {
  42. $needs_multiselect = false;
  43. $canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($COURSE->id));
  44. foreach ($grade_items as $grade_item) {
  45. // Is the grade_item hidden? If so, can the user see hidden grade_items?
  46. if ($grade_item->is_hidden() && !$canviewhidden) {
  47. continue;
  48. }
  49. if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
  50. $mform->addElement('checkbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
  51. $mform->hardFreeze('itemids['.$grade_item->id.']');
  52. } else {
  53. $mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
  54. $mform->setDefault('itemids['.$grade_item->id.']', 1);
  55. $needs_multiselect = true;
  56. }
  57. }
  58. if ($needs_multiselect) {
  59. $this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value
  60. }
  61. }
  62. $mform->addElement('header', 'options', get_string('exportformatoptions', 'grades'));
  63. if (!empty($features['simpleui'])) {
  64. $mform->setExpanded('options', false);
  65. }
  66. $mform->addElement('advcheckbox', 'export_feedback', get_string('exportfeedback', 'grades'));
  67. $exportfeedback = isset($CFG->grade_export_exportfeedback) ? $CFG->grade_export_exportfeedback : 0;
  68. $mform->setDefault('export_feedback', $exportfeedback);
  69. $coursecontext = context_course::instance($COURSE->id);
  70. if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
  71. $mform->addElement('advcheckbox', 'export_onlyactive', get_string('exportonlyactive', 'grades'));
  72. $mform->setType('export_onlyactive', PARAM_BOOL);
  73. $mform->setDefault('export_onlyactive', 1);
  74. $mform->addHelpButton('export_onlyactive', 'exportonlyactive', 'grades');
  75. } else {
  76. $mform->addElement('hidden', 'export_onlyactive', 1);
  77. $mform->setType('export_onlyactive', PARAM_BOOL);
  78. $mform->setConstant('export_onlyactive', 1);
  79. }
  80. if (empty($features['simpleui'])) {
  81. $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
  82. $mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
  83. }
  84. if (!empty($features['updategradesonly'])) {
  85. $mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
  86. }
  87. /// selections for decimal points and format, MDL-11667, defaults to site settings, if set
  88. //$default_gradedisplaytype = $CFG->grade_export_displaytype;
  89. $options = array(GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
  90. GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
  91. GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'));
  92. /*
  93. foreach ($options as $key=>$option) {
  94. if ($key == $default_gradedisplaytype) {
  95. $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
  96. break;
  97. }
  98. }
  99. */
  100. if ($features['multipledisplaytypes']) {
  101. /*
  102. * Using advcheckbox because we need the grade display type (name) as key and grade display type (constant) as value.
  103. * The method format_column_name requires the lang file string and the format_grade method requires the constant.
  104. */
  105. $checkboxes = array();
  106. $checkboxes[] = $mform->createElement('advcheckbox', 'display[real]', null, get_string('real', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_REAL));
  107. $checkboxes[] = $mform->createElement('advcheckbox', 'display[percentage]', null, get_string('percentage', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_PERCENTAGE));
  108. $checkboxes[] = $mform->createElement('advcheckbox', 'display[letter]', null, get_string('letter', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_LETTER));
  109. $mform->addGroup($checkboxes, 'displaytypes', get_string('gradeexportdisplaytypes', 'grades'), ' ', false);
  110. $mform->setDefault('display[real]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_REAL);
  111. $mform->setDefault('display[percentage]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE);
  112. $mform->setDefault('display[letter]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER);
  113. } else {
  114. // Only used by XML grade export format.
  115. $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
  116. $mform->setDefault('display', $CFG->grade_export_displaytype);
  117. }
  118. //$default_gradedecimals = $CFG->grade_export_decimalpoints;
  119. $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
  120. $mform->addElement('select', 'decimals', get_string('gradeexportdecimalpoints', 'grades'), $options);
  121. $mform->setDefault('decimals', $CFG->grade_export_decimalpoints);
  122. $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
  123. /*
  124. if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
  125. $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
  126. }
  127. */
  128. if (!empty($features['includeseparator'])) {
  129. $radio = array();
  130. $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
  131. $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
  132. $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
  133. $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
  134. $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
  135. $mform->setDefault('separator', 'comma');
  136. }
  137. if (!empty($CFG->gradepublishing) and !empty($features['publishing'])) {
  138. $mform->addElement('header', 'publishing', get_string('publishingoptions', 'grades'));
  139. if (!empty($features['simpleui'])) {
  140. $mform->setExpanded('publishing', false);
  141. }
  142. $options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
  143. $keys = $DB->get_records_select('user_private_key', "script='grade/export' AND instance=? AND userid=?",
  144. array($COURSE->id, $USER->id));
  145. if ($keys) {
  146. foreach ($keys as $key) {
  147. $options[$key->value] = $key->value; // TODO: add more details - ip restriction, valid until ??
  148. }
  149. }
  150. $mform->addElement('select', 'key', get_string('userkey', 'userkey'), $options);
  151. $mform->addHelpButton('key', 'userkey', 'userkey');
  152. $mform->addElement('static', 'keymanagerlink', get_string('keymanager', 'userkey'),
  153. '<a href="'.$CFG->wwwroot.'/grade/export/keymanager.php?id='.$COURSE->id.'">'.get_string('keymanager', 'userkey').'</a>');
  154. $mform->addElement('text', 'iprestriction', get_string('keyiprestriction', 'userkey'), array('size'=>80));
  155. $mform->addHelpButton('iprestriction', 'keyiprestriction', 'userkey');
  156. $mform->setDefault('iprestriction', getremoteaddr()); // own IP - just in case somebody does not know what user key is
  157. $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
  158. $mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
  159. $mform->addHelpButton('validuntil', 'keyvaliduntil', 'userkey');
  160. $mform->setDefault('validuntil', time()+3600*24*7); // only 1 week default duration - just in case somebody does not know what user key is
  161. $mform->setType('validuntil', PARAM_INT);
  162. $mform->disabledIf('iprestriction', 'key', 'noteq', 1);
  163. $mform->disabledIf('validuntil', 'key', 'noteq', 1);
  164. }
  165. $mform->addElement('hidden', 'id', $COURSE->id);
  166. $mform->setType('id', PARAM_INT);
  167. $submitstring = get_string('download');
  168. if (empty($features['simpleui'])) {
  169. $submitstring = get_string('submit');
  170. } else if (!empty($CFG->gradepublishing)) {
  171. $submitstring = get_string('export', 'grades');
  172. }
  173. $this->add_action_buttons(false, $submitstring);
  174. }
  175. /**
  176. * Overrides the mform get_data method.
  177. *
  178. * Created to force a value since the validation method does not work with multiple checkbox.
  179. *
  180. * @return stdClass form data object.
  181. */
  182. public function get_data() {
  183. global $CFG;
  184. $data = parent::get_data();
  185. if ($data && $this->_customdata['multipledisplaytypes']) {
  186. if (count(array_filter($data->display)) == 0) {
  187. // Ensure that a value was selected as the export plugins expect at least one value.
  188. if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER) {
  189. $data->display['letter'] = GRADE_DISPLAY_TYPE_LETTER;
  190. } else if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE) {
  191. $data->display['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
  192. } else {
  193. $data->display['real'] = GRADE_DISPLAY_TYPE_REAL;
  194. }
  195. }
  196. }
  197. return $data;
  198. }
  199. }