settings_form.php 16 KB

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