settings_form.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * Form to add and edit interactive Learning Module (iLM).
  4. * The fields are initially filled at locallib.php:add_edit_copy_ilm($ilm_id, $action)
  5. * The final processing is performed by 'locallib.php', that is called bye './settings_ilm.php' (with ilm_settings::copy_new_version_ilm($formdata))
  6. * then by 'ilm_handlers/(html5 or java).php' with copy_new_version_ilm($param, $files_extract)'
  7. *
  8. * Release Notes:
  9. * - v 1.6.1 2017/12/02
  10. * + New help button, improvements on some helps, new code comments and indentation.
  11. * - v 1.6 2013/12/12
  12. * + Insert support of import iLM from zip packages.
  13. * - v 1.5 2013/10/31
  14. * + Insert support of import iLM from zip packages.
  15. * - v 1.4 2013/10/24
  16. * + Fix required upload an file for new iLM.
  17. * - v 1.3 2013/07/12
  18. * + Fix error messages of 'setType' in debug mode for hidden fields.
  19. * + Form now accept actions: add, edit, copy (new version from an iLM), and new version (empty new version).
  20. *
  21. * To use help button. For instance, to a field named 'file_class' use
  22. * $mform->addHelpButton('file_class', 'file_class_form_help', 'iassign');
  23. * it means that the first parameter is used as 'title' (mouseover) and the second when user "clickover"
  24. * In lang, it is necessary
  25. * file_class_form_help = 'to the title - when mouse over'
  26. * file_class_form_help_help = 'to the complete explanation, when the user click over the help signal'
  27. * 'auto_evaluate_help'] = 'If the iLM has automatic evaluation select Yes to use an activity with it.'; // 'What is automatic evaluation
  28. *
  29. * @see ./locallib.php : 'add_edit_copy_ilm($ilm_id,$action)' load this script and provides data to it (defines initial values to the form fields)
  30. * @see ./settings_ilm.php : load and process this form, it uses data under the name $param
  31. * @see ./ilm_handlers/html5.php : save_ilm_by_xml($application_xml, $files_extract): return null;
  32. *
  33. * @author Patricia Alves Rodrigues
  34. * @author Leônidas O. Brandão
  35. * @version v 1.6 2013/12/12
  36. * @package mod_iassign_settings
  37. * @since 2010/09/27
  38. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  39. *
  40. * <b>License</b>
  41. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42. */
  43. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  44. if (!defined('MOODLE_INTERNAL')) {
  45. die('Direct access to this script is forbidden.'); // By security reasons, this must be included in all Moodle pages!
  46. }
  47. require_once($CFG->libdir . '/formslib.php');
  48. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  49. require_once($CFG->dirroot . '/mod/iassign/lib.php');
  50. /// This class create form based moodleform.
  51. // @see moodleform
  52. class mod_ilm_form extends moodleform {
  53. /// Add elements to form
  54. function definition () {
  55. global $CFG, $COURSE, $USER, $DB;
  56. global $description; // defined in 'settings_ilm.php'
  57. $mform = & $this->_form;
  58. if ($CFG->action_ilm != 'import') {
  59. if ($CFG->action_ilm == 'add') {
  60. // $params = array('parent' => '0');
  61. // $tmps = $DB->get_records_sql("SELECT s.id, s.name, s.extension, s.file_jar
  62. // FROM {iassign_ilm} s WHERE s.parent = :parent", $params); // " - jed/emacs
  63. $tmps = $DB->get_records('iassign_ilm', array('parent' => 0));
  64. }
  65. else {
  66. // $params = array('id' => $CFG->ilm_id);
  67. // $tmps = $DB->get_records_sql("SELECT s.id, s.name, s.extension, s.file_jar
  68. // FROM {iassign_ilm} s WHERE s.id != :id AND s.parent = 0", $params); // " - jed/emacs
  69. $tmps = $DB->get_records('iassign_ilm', array('id' => $CFG->ilm_id));
  70. }
  71. $extensions = "";
  72. $names = "";
  73. $versions = "";
  74. $filejars = "";
  75. foreach ($tmps as $tmp) {
  76. $exts = explode(",", $tmp->extension);
  77. foreach ($exts as $ext) {
  78. $extensions .= "'" . $ext . "',";
  79. }
  80. $names .= "'" . $tmp->name . "',";
  81. $filejars .= "'" . $tmp->file_jar . "',";
  82. }
  83. if (!$tmps)
  84. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $CFG->ilm_id));
  85. else
  86. $iassign_ilm = $tmps; // it already has these data
  87. if (!$iassign_ilm) {
  88. $iassign_ilm = new stdClass();
  89. $iassign_ilm->parent = 0;
  90. }
  91. //TODO JavaScript code, verify alternatives.
  92. $code_javascript = "\n <script type='text/javascript'>
  93. //<![CDATA[
  94. document.getElementById('id_data_file_html').style.display = 'none'
  95. document.getElementById('id_data_file_jar').classList.remove('collapsed');
  96. document.getElementById('id_data_ilm').classList.remove('collapsed');
  97. function search_name (name) {
  98. var i;
  99. var names = new Array(";
  100. $code_javascript .= $names . "'');\n";
  101. $code_javascript .= "
  102. for (i=0;i<names.length;i++) {
  103. if (names[i].toLowerCase()==name.toLowerCase()) {
  104. document.forms['mform1'].name.value='';
  105. confirm('" . get_string('invalid_name_ilm', 'iassign') . " '+names[i]+'!');
  106. }
  107. }";
  108. $code_javascript .= " }
  109. function search_extension(extensions) {
  110. var i;
  111. var ext_inserteds = extensions.split(',');
  112. var ext_exists = new Array(";
  113. $code_javascript .= $extensions . "'');" . chr(13);
  114. $code_javascript .= "
  115. for (k=0;k<ext_inserteds.length;k++) {
  116. for (i=0;i<ext_exists.length;i++) {
  117. if (ext_exists[i].toLowerCase()==ext_inserteds[k].toLowerCase()) {
  118. document.forms['mform1'].extension.value='';
  119. confirm('" . get_string('invalid_extension_ilm', 'iassign') . " '+ext_exists[i]+'!');
  120. }
  121. }
  122. }\n";
  123. $code_javascript .= " }
  124. function search_filejar (fullfilejar) {
  125. var i;
  126. var tmp = fullfilejar.split('/');
  127. var filejar = tmp[tmp.length-1];
  128. var filejars = new Array(";
  129. $code_javascript .= $filejars . "'');" . chr(13);
  130. $code_javascript .= "
  131. for (i=0;i<filejars.length;i++) {
  132. if (filejars[i].toLowerCase()==filejar.toLowerCase()) {
  133. document.forms['mform1'].file_jar[0].value='';
  134. confirm('" . get_string('invalid_filejar_ilm', 'iassign') . " '+filejars[i]+'!');
  135. }
  136. }\n";
  137. $code_javascript .= " }
  138. function change_language (lang) {
  139. if (document.forms['mform1'].description_lang.value != '') {
  140. descriptions = eval('(' + document.forms['mform1'].description_lang.value + ')');
  141. descriptions[document.forms['mform1'].set_lang.value] = tinyMCE.activeEditor.getContent();
  142. document.forms['mform1'].description_lang.value = JSON.stringify(descriptions);
  143. if (descriptions[lang] != undefined)
  144. tinyMCE.activeEditor.setContent(descriptions[lang]);
  145. else
  146. tinyMCE.activeEditor.setContent('');
  147. }
  148. else {
  149. document.forms['mform1'].description_lang.value = '{ \"' + document.forms['mform1'].set_lang.value + '\" : \"' + tinyMCE.activeEditor.getContent() + '\" }';
  150. tinyMCE.activeEditor.setContent('');
  151. }
  152. document.forms['mform1'].set_lang.value = lang;
  153. }
  154. //]]>
  155. </script>\n";
  156. //-------------------------------------------------------------------------------
  157. /// Adding the "data_ilm" fieldset, where all the common settings are showed
  158. if ($CFG->action_ilm != 'add') {
  159. // $mform->addElement('header', 'type_ilm', get_string('type_ilm', 'iassign'));
  160. // // Adding the select option for HTML5 or Java
  161. // $options = array('Java' => 'Java', 'HTML5' => 'HTML5');
  162. // $mform->addElement('select', 'ilm_type', get_string('ilm_type', 'iassign'), $options);
  163. // $mform->setDefault('ilm_type', PARAM_TEXT);
  164. // $mform->addHelpButton('ilm_type', 'ilm_type', 'iassign');
  165. // $mform->addElement('header', 'data_ilm', get_string('data_ilm', 'iassign'));
  166. // Adding the standard "name" field
  167. if ($CFG->action_ilm != 'add') {
  168. $mform->addElement('static', 'name_ilm', get_string('name_ilm', 'iassign'));
  169. $mform->addElement('hidden', 'name');
  170. $mform->setType('name', PARAM_TEXT);
  171. }
  172. else {
  173. $mform->addElement('text', 'name', get_string('name_ilm', 'iassign'), array('size' => '55', 'onchange' => 'search_name(this.value);'));
  174. $mform->setType('name', PARAM_TEXT);
  175. }
  176. // Adding the standard "version" field
  177. $mform->addElement('text', 'version', get_string('version_ilm', 'iassign'), array('size' => '55'));
  178. $mform->setType('version', PARAM_TEXT);
  179. $mform->addHelpButton('version', 'version_ilm', 'iassign');
  180. // Adding the type of iLM
  181. $mform->addElement('static', 'ilm_type', get_string('type_ilm', 'iassign'));
  182. $mform->addElement('hidden', 'type');
  183. $mform->setType('type', PARAM_TEXT);
  184. // Adding the standard "url" field
  185. $mform->addElement('text', 'url', get_string('url_ilm', 'iassign'), array('size' => '55'));
  186. $mform->setType('url', PARAM_TEXT);
  187. $mform->addElement('select', 'lang', get_string('language_label', 'iassign'), get_string_manager()->get_list_of_translations(), array('onChange' => 'change_language(this.value);'));
  188. $mform->setDefault('lang', current_language());
  189. //D Came from 'settings_ilm.php: if ($action == 'edit')
  190. //D With 'editor' does not work!
  191. //D $param = $description; // ilm_settings::add_edit_copy_ilm($ilm_id, $action);
  192. //D $param = $mform->get_data(); - erro, function not defined
  193. //D echo "settings_form.php: param="; print_r($param); echo "<br/>";
  194. //D echo "description=" . $description . "<br/>";
  195. // Adding the standard "description" field
  196. //moodle2 $mform->addElement('htmleditor', 'description', get_string('description', 'iassign'));
  197. //TODO $mform->addElement('editor', 'description', get_string('description', 'iassign'), $description); // moodle3 => 'editor', but not working
  198. $mform->addElement('text', 'description', get_string('description', 'iassign'), array('size' => '80'));//, $description, 200); //moodle3
  199. $mform->setType('description', PARAM_RAW);
  200. // Adding the "data_file_jar" fieldset, where all the common settings are showed ---
  201. //REVER texto 'Pacote do novo iMA (Java) a ser inserido (JAR)'
  202. $mform->addElement('header', 'data_file_jar', get_string('data_file_jar', 'iassign'));
  203. $mform->addElement('static', 'file_jar_static', get_string('fields_info', 'iassign')); // non editable text
  204. // Fields: file_jar file_class
  205. $mform->addElement('text', 'file_jar', get_string('file_jar_path', 'iassign'), array('size' => '55')); // editable text
  206. $mform->setType('file_jar', PARAM_TEXT);
  207. $mform->addElement('text', 'file_class', get_string('file_class', 'iassign')); // editable text
  208. $mform->setType('file_class', PARAM_TEXT);
  209. //TODO : acho que vale a pena incluir o campo 'file_jar'...
  210. // $mform->addElement('static', 'file_jar', get_string('file_jar', 'iassign'));
  211. // $mform->addElement('hidden', 'file_jar'); - already bellow! line 288/320
  212. // $mform->setType('file_jar', PARAM_TEXT);
  213. //TD // Adding the standard "file_jar" field
  214. //TD $mform->addElement('text', 'file_jar', get_string('file_jar', 'iassign'), array('size' => '55'));
  215. //TD $mform->setType('file_jar', PARAM_TEXT);
  216. //TD $mform->addHelpButton('file_jar', 'file_jar_form_help', 'iassign'); // first => field name; second => text to be presented
  217. // Adding the standard "extension" field
  218. $mform->addElement('text', 'extension', get_string('extension', 'iassign'), array('size' => '30', 'onchange' => 'search_extension(this.value);'));
  219. $mform->setType('extension', PARAM_TEXT);
  220. //$mform->addRule('extension', get_string('required', 'iassign'), 'required');
  221. $mform->addHelpButton('extension', 'extension', 'iassign');
  222. // Adding the standard "width" field
  223. $mform->addElement('text', 'width', get_string('width', 'iassign'), array('size' => '10'));
  224. $mform->setType('width', PARAM_TEXT);
  225. //$mform->addRule('width', get_string('required', 'iassign'), 'required');
  226. // Adding the standard "height" field
  227. $mform->addElement('text', 'height', get_string('height', 'iassign'), array('size' => '10'));
  228. $mform->setType('height', PARAM_TEXT);
  229. //$mform->addRule('height', get_string('required', 'iassign'), 'required');
  230. // Adding the standard "evaluate" field
  231. $mform->addElement('selectyesno', 'evaluate', get_string('auto_evaluate', 'iassign'));
  232. $mform->setDefault('evaluate', 1);
  233. //$mform->addRule('evaluate', get_string('required', 'iassign'), 'required');
  234. $mform->addHelpButton('evaluate', 'auto_evaluate', 'iassign');
  235. // Adding the "data_file_jar" fieldset, where all the common settings are showed
  236. $mform->addElement('header', 'data_file_html', get_string('data_file_html', 'iassign'));
  237. // Adding static text
  238. $mform->addElement('static', 'data_file_html_static', get_string('data_file_html_static', 'iassign'));
  239. }
  240. // // Upload file ilm
  241. // $mform->addElement('header', 'upload_jar', get_string('upload_jar', 'iassign'));
  242. // //$mform->setExpanded('upload_jar');
  243. // $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => -1, 'accepted_types' => '*');
  244. // $mform->addElement('filemanager', 'file', null, null, $options);
  245. $mform->addElement('header', 'upload_jar', get_string('upload_jar', 'iassign'));
  246. $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => 1, 'accepted_types' => array('*'));
  247. $mform->addElement('filepicker', 'file', null, null, $options);
  248. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  249. if ($CFG->action_ilm == 'add' || $CFG->action_ilm == 'copy' || $CFG->action_ilm == 'new_version')
  250. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  251. /// Adding the standard "hidden" field
  252. if ($CFG->action_ilm == 'edit') {
  253. $mform->addElement('hidden', 'id');
  254. $mform->setType('id', PARAM_INT);
  255. }
  256. $mform->addElement('hidden', 'set_lang');
  257. $mform->setType('set_lang', PARAM_TEXT);
  258. $mform->setDefault('set_lang', current_language());
  259. $mform->addElement('hidden', 'description_lang');
  260. $mform->setType('description_lang', PARAM_TEXT);
  261. // $mform->addElement('hidden', 'file_jar'); //Remove inserted above to allow to edit this field
  262. // $mform->setType('file_jar', PARAM_TEXT); //Remove idem
  263. $mform->addElement('hidden', 'author');
  264. $mform->setType('author', PARAM_TEXT);
  265. $mform->addElement('hidden', 'action');
  266. $mform->setType('action', PARAM_TEXT);
  267. $mform->addElement('hidden', 'timecreated');
  268. $mform->setType('timecreated', PARAM_TEXT);
  269. $mform->addElement('hidden', 'timemodified');
  270. $mform->setType('timemodified', PARAM_TEXT);
  271. $mform->addElement('hidden', 'parent');
  272. $mform->setType('parent', PARAM_INT);
  273. $mform->addElement('hidden', 'enable');
  274. $mform->setType('enable', PARAM_INT);
  275. $mform->addElement('html', $code_javascript);
  276. } // if ($CFG->action_ilm != 'import')
  277. else {
  278. $mform->addElement('header', 'upload_ilm', get_string('upload_ilm', 'iassign'));
  279. //$mform->setExpanded('upload_ilm');
  280. $options = array('subdirs' => 0, 'maxbytes' => $CFG->userquota, 'maxfiles' => 1, 'accepted_types' => array('*'));
  281. $mform->addElement('filepicker', 'file', null, null, $options);
  282. $mform->addRule('file', get_string('required', 'iassign'), 'required');
  283. $mform->addElement('hidden', 'action');
  284. $mform->setType('action', PARAM_TEXT);
  285. }
  286. $this->add_action_buttons();
  287. }
  288. }