PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/data/field.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 371 lines | 248 code | 76 blank | 47 comment | 53 complexity | f62327e17fc214de9f5fb7e392297a09 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. * This file is part of the Database module for Moodle
  18. *
  19. * @copyright 2005 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package mod_data
  22. */
  23. require_once('../../config.php');
  24. require_once('lib.php');
  25. $id = optional_param('id', 0, PARAM_INT); // course module id
  26. $d = optional_param('d', 0, PARAM_INT); // database id
  27. $fid = optional_param('fid', 0 , PARAM_INT); // update field id
  28. $newtype = optional_param('newtype','',PARAM_ALPHA); // type of the new field
  29. $mode = optional_param('mode','',PARAM_ALPHA);
  30. $defaultsort = optional_param('defaultsort', 0, PARAM_INT);
  31. $defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT);
  32. $cancel = optional_param('cancel', 0, PARAM_BOOL);
  33. if ($cancel) {
  34. $mode = 'list';
  35. }
  36. $url = new moodle_url('/mod/data/field.php');
  37. if ($fid !== 0) {
  38. $url->param('fid', $fid);
  39. }
  40. if ($newtype !== '') {
  41. $url->param('newtype', $newtype);
  42. }
  43. if ($mode !== '') {
  44. $url->param('mode', $mode);
  45. }
  46. if ($defaultsort !== 0) {
  47. $url->param('defaultsort', $defaultsort);
  48. }
  49. if ($defaultsortdir !== 0) {
  50. $url->param('defaultsortdir', $defaultsortdir);
  51. }
  52. if ($cancel !== 0) {
  53. $url->param('cancel', $cancel);
  54. }
  55. if ($id) {
  56. $url->param('id', $id);
  57. $PAGE->set_url($url);
  58. if (! $cm = get_coursemodule_from_id('data', $id)) {
  59. print_error('invalidcoursemodule');
  60. }
  61. if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  62. print_error('coursemisconf');
  63. }
  64. if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
  65. print_error('invalidcoursemodule');
  66. }
  67. } else {
  68. $url->param('d', $d);
  69. $PAGE->set_url($url);
  70. if (! $data = $DB->get_record('data', array('id'=>$d))) {
  71. print_error('invalidid', 'data');
  72. }
  73. if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  74. print_error('invalidcoursemodule');
  75. }
  76. if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  77. print_error('invalidcoursemodule');
  78. }
  79. }
  80. require_login($course, true, $cm);
  81. $context = context_module::instance($cm->id);
  82. require_capability('mod/data:managetemplates', $context);
  83. /************************************
  84. * Data Processing *
  85. ***********************************/
  86. switch ($mode) {
  87. case 'add': ///add a new field
  88. if (confirm_sesskey() and $fieldinput = data_submitted()){
  89. //$fieldinput->name = data_clean_field_name($fieldinput->name);
  90. /// Only store this new field if it doesn't already exist.
  91. if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id)) {
  92. $displaynoticebad = get_string('invalidfieldname','data');
  93. } else {
  94. /// Check for arrays and convert to a comma-delimited string
  95. data_convert_arrays_to_strings($fieldinput);
  96. /// Create a field object to collect and store the data safely
  97. $type = required_param('type', PARAM_FILE);
  98. $field = data_get_field_new($type, $data);
  99. $field->define_field($fieldinput);
  100. $field->insert_field();
  101. /// Update some templates
  102. data_append_new_field_to_templates($data, $fieldinput->name);
  103. $displaynoticegood = get_string('fieldadded','data');
  104. }
  105. }
  106. break;
  107. case 'update': ///update a field
  108. if (confirm_sesskey() and $fieldinput = data_submitted()){
  109. //$fieldinput->name = data_clean_field_name($fieldinput->name);
  110. if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id, $fieldinput->fid)) {
  111. $displaynoticebad = get_string('invalidfieldname','data');
  112. } else {
  113. /// Check for arrays and convert to a comma-delimited string
  114. data_convert_arrays_to_strings($fieldinput);
  115. /// Create a field object to collect and store the data safely
  116. $field = data_get_field_from_id($fid, $data);
  117. $oldfieldname = $field->field->name;
  118. $field->field->name = $fieldinput->name;
  119. $field->field->description = $fieldinput->description;
  120. $field->field->required = !empty($fieldinput->required) ? 1 : 0;
  121. for ($i=1; $i<=10; $i++) {
  122. if (isset($fieldinput->{'param'.$i})) {
  123. $field->field->{'param'.$i} = $fieldinput->{'param'.$i};
  124. } else {
  125. $field->field->{'param'.$i} = '';
  126. }
  127. }
  128. $field->update_field();
  129. /// Update the templates.
  130. data_replace_field_in_templates($data, $oldfieldname, $field->field->name);
  131. $displaynoticegood = get_string('fieldupdated','data');
  132. }
  133. }
  134. break;
  135. case 'delete': // Delete a field
  136. if (confirm_sesskey()){
  137. if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
  138. // Delete the field completely
  139. if ($field = data_get_field_from_id($fid, $data)) {
  140. $field->delete_field();
  141. // Update the templates.
  142. data_replace_field_in_templates($data, $field->field->name, '');
  143. // Update the default sort field
  144. if ($fid == $data->defaultsort) {
  145. $rec = new stdClass();
  146. $rec->id = $data->id;
  147. $rec->defaultsort = 0;
  148. $rec->defaultsortdir = 0;
  149. $DB->update_record('data', $rec);
  150. }
  151. $displaynoticegood = get_string('fielddeleted', 'data');
  152. }
  153. } else {
  154. data_print_header($course,$cm,$data, false);
  155. // Print confirmation message.
  156. $field = data_get_field_from_id($fid, $data);
  157. echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
  158. 'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
  159. 'field.php?d='.$data->id);
  160. echo $OUTPUT->footer();
  161. exit;
  162. }
  163. }
  164. break;
  165. case 'sort': // Set the default sort parameters
  166. if (confirm_sesskey()) {
  167. $rec = new stdClass();
  168. $rec->id = $data->id;
  169. $rec->defaultsort = $defaultsort;
  170. $rec->defaultsortdir = $defaultsortdir;
  171. $DB->update_record('data', $rec);
  172. redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
  173. exit;
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. /// Print the browsing interface
  180. ///get the list of possible fields (plugins)
  181. $plugins = core_component::get_plugin_list('datafield');
  182. $menufield = array();
  183. foreach ($plugins as $plugin=>$fulldir){
  184. $menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin); //get from language files
  185. }
  186. asort($menufield); //sort in alphabetical order
  187. $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
  188. $PAGE->set_heading($course->fullname);
  189. $PAGE->set_pagetype('mod-data-field-' . $newtype);
  190. if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field
  191. data_print_header($course, $cm, $data,'fields');
  192. $field = data_get_field_new($newtype, $data);
  193. $field->display_edit_field();
  194. } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
  195. data_print_header($course, $cm, $data,'fields');
  196. $field = data_get_field_from_id($fid, $data);
  197. $field->display_edit_field();
  198. } else { /// Display the main listing of all fields
  199. data_print_header($course, $cm, $data,'fields');
  200. if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {
  201. echo $OUTPUT->notification(get_string('nofieldindatabase','data')); // nothing in database
  202. echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id)); // link to presets
  203. } else { //else print quiz style list of fields
  204. $table = new html_table();
  205. $table->head = array(
  206. get_string('fieldname', 'data'),
  207. get_string('type', 'data'),
  208. get_string('required', 'data'),
  209. get_string('fielddescription', 'data'),
  210. get_string('action', 'data'),
  211. );
  212. $table->align = array('left','left','left', 'center');
  213. $table->wrap = array(false,false,false,false);
  214. if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
  215. foreach ($fff as $ff) {
  216. $field = data_get_field($ff, $data);
  217. $baseurl = new moodle_url('/mod/data/field.php', array(
  218. 'd' => $data->id,
  219. 'fid' => $field->field->id,
  220. 'sesskey' => sesskey(),
  221. ));
  222. $displayurl = new moodle_url($baseurl, array(
  223. 'mode' => 'display',
  224. ));
  225. $deleteurl = new moodle_url($baseurl, array(
  226. 'mode' => 'delete',
  227. ));
  228. $table->data[] = array(
  229. html_writer::link($displayurl, $field->field->name),
  230. $field->image() . '&nbsp;' . $field->name(),
  231. $field->field->required ? get_string('yes') : get_string('no'),
  232. shorten_text($field->field->description, 30),
  233. html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) .
  234. '&nbsp;' .
  235. html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))),
  236. );
  237. }
  238. }
  239. echo html_writer::table($table);
  240. }
  241. echo '<div class="fieldadd">';
  242. $popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='. sesskey();
  243. echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array('' => 'choosedots'),
  244. 'fieldform', array('label' => get_string('newfield', 'data')));
  245. echo $OUTPUT->help_icon('newfield', 'data');
  246. echo '</div>';
  247. echo '<div class="sortdefault">';
  248. echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
  249. echo '<div>';
  250. echo '<input type="hidden" name="d" value="'.$data->id.'" />';
  251. echo '<input type="hidden" name="mode" value="sort" />';
  252. echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
  253. echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
  254. echo '<select id="defaultsort" name="defaultsort" class="custom-select">';
  255. if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
  256. echo '<optgroup label="'.get_string('fields', 'data').'">';
  257. foreach ($fields as $field) {
  258. if ($data->defaultsort == $field->id) {
  259. echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
  260. } else {
  261. echo '<option value="'.$field->id.'">'.$field->name.'</option>';
  262. }
  263. }
  264. echo '</optgroup>';
  265. }
  266. $options = array();
  267. $options[DATA_TIMEADDED] = get_string('timeadded', 'data');
  268. // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
  269. /* $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
  270. $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
  271. $options[DATA_LASTNAME] = get_string('authorlastname', 'data');
  272. if ($data->approval and has_capability('mod/data:approve', $context)) {
  273. $options[DATA_APPROVED] = get_string('approved', 'data');
  274. }*/
  275. echo '<optgroup label="'.get_string('other', 'data').'">';
  276. foreach ($options as $key => $name) {
  277. if ($data->defaultsort == $key) {
  278. echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
  279. } else {
  280. echo '<option value="'.$key.'">'.$name.'</option>';
  281. }
  282. }
  283. echo '</optgroup>';
  284. echo '</select>';
  285. $options = array(0 => get_string('ascending', 'data'),
  286. 1 => get_string('descending', 'data'));
  287. echo html_writer::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
  288. echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false, array('class' => 'custom-select'));
  289. echo '<input type="submit" class="btn btn-secondary m-l-1" value="'.get_string('save', 'data').'" />';
  290. echo '</div>';
  291. echo '</form>';
  292. echo '</div>';
  293. }
  294. /// Finish the page
  295. echo $OUTPUT->footer();