iassign_form.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. /**
  3. * Form to add and edit interactive activities
  4. *
  5. *
  6. * Release Notes:
  7. * - v 1.5.1 2020/01/21
  8. * + Filter 'addslach(.)' to avoid ' and " close command JavaScript and verify if field 'grade' in JS is 'undefined'
  9. * - v 1.5 2013/09/19
  10. * + Insert function for validation form (mod_iassign_form::validation).
  11. * + Fix bugs in download exercise file.
  12. * - v 1.4 2013/08/21
  13. * + Change title link with message for get file for donwload file.
  14. * - v 1.3 2013/08/15
  15. * + Change view file for allow download file.
  16. * - v 1.2 2013/08/01
  17. * + Fix error in sql query for var $igeom.
  18. * - v 1.1 2013/07/12
  19. * + Fix error messages of 'setType' in debug mode for hidden fields.
  20. *
  21. * @author Patricia Alves Rodrigues
  22. * @author Leônidas O. Brandão
  23. * @version v 1.5 2013/09/19
  24. * @package mod_iassign
  25. * @since 2010/09/27
  26. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  27. *
  28. * <b>License</b>
  29. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  32. if (!defined('MOODLE_INTERNAL')) {
  33. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  34. }
  35. require_once($CFG->libdir . '/formslib.php');
  36. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  37. require_once($CFG->dirroot . '/mod/iassign/lib.php');
  38. /// This class create form based moodleform.
  39. // @see moodleform
  40. class mod_iassign_form extends moodleform {
  41. /// Add elements to form
  42. function definition () {
  43. global $CFG, $COURSE, $USER, $DB, $OUTPUT; // $OUTPUT only used to get '$OUTPUT->help_icon(...)'
  44. $mform = $this->_form;
  45. $instance = $this->_customdata;
  46. // iGeom has special parameter 'script'
  47. $params = array('name' => '%iGeom%', 'parent' => 0);
  48. $igeom = $DB->get_records_sql(
  49. "SELECT s.id, s.name, s.parent FROM {iassign_ilm} s
  50. WHERE s.name LIKE :name AND s.parent = :parent", $params);
  51. $id = $COURSE->cm;
  52. foreach ($igeom as $item)
  53. $idigeom = $item->id; // get the last id of iGeom
  54. $all_ilm = $DB->get_records('iassign_ilm', array('enable' => 1)); // or use ./lib.php function: $all_ilm = search_iLM(1);
  55. $iassigns = $DB->get_records('iassign_statement', array('iassignid' => $COURSE->iassignid));
  56. if ($all_ilm) {
  57. $ids = "";
  58. $names = "";
  59. $evaluates = "";
  60. foreach ($all_ilm as $one_ilm) {
  61. $ids .= "'" . $one_ilm->id . "',";
  62. $names .= "'" . $one_ilm->name . "',";
  63. $evaluates .= "'" . $one_ilm->evaluate . "',";
  64. }
  65. $ids .= "'0'";
  66. $evaluates .= "'0'";
  67. }
  68. $name_iassigns = "";
  69. if ($iassigns) {
  70. foreach ($iassigns as $iassign) {
  71. $name_iassigns .= "'" . addslashes($iassign->name) . "',"; // if the name has ' it implies close JavaScript string => error!
  72. }
  73. }
  74. $name_iassigns .= "''";
  75. $error_name = get_string('error_iassign_name', 'iassign');
  76. // @todo Código Javascript, verificar alternativa.
  77. $code_javascript = "
  78. <script type='text/javascript'>
  79. //<![CDATA[
  80. var i;
  81. var ids = new Array($ids);
  82. var evaluates = new Array($evaluates);
  83. document.forms['mform1'].filename.disabled=1;
  84. if (document.forms['mform1'].type_iassign.value==1) {
  85. if (typeof document.forms['mform1'].grade === 'undefined') ; // do nothing
  86. else document.forms['mform1'].grade.style.display='none';
  87. document.forms['mform1'].max_experiment.style.display='none';
  88. } else {
  89. if (typeof document.forms['mform1'].grade === 'undefined') ; // do nothing
  90. else document.forms['mform1'].grade.style.display='block';
  91. document.forms['mform1'].max_experiment.style.display='block';
  92. }
  93. for (i=0;i<ids.length;i++) {
  94. if (ids[i]==document.forms['mform1'].iassign_ilmid.value && evaluates[i]==0) {
  95. document.forms['mform1'].automatic_evaluate.style.display='none';
  96. document.forms['mform1'].show_answer.style.display='none';
  97. //document.forms['mform1'].automatic_evaluate.disabled=1;
  98. // document.forms['mform1'].show_answer.disabled=1;
  99. }
  100. }
  101. if (document.forms['mform1'].iassign_ilmid.value==" . $idigeom .") { // iGeom has special parameter 'script'
  102. document.forms['mform1'].special_param1.style.display='block';
  103. document.forms['mform1'].special_param1.disabled=0;
  104. }
  105. else {
  106. document.forms['mform1'].special_param1.style.display='none';
  107. document.forms['mform1'].special_param1.value=0;
  108. document.forms['mform1'].special_param1.disabled=1;
  109. }
  110. function confirm_name (name) {
  111. var i;
  112. var names = new Array(" . $name_iassigns . ");
  113. for (i=0;i<names.length;i++) {
  114. if (names[i]==name)
  115. alert('" . $error_name . "');
  116. }
  117. }
  118. function config_ilm (id) {
  119. //alert('config_ilm('+id+'): idigeom=$idigeom, ilmid='+document.forms['mform1'].iassign_ilmid.value);
  120. if (id==$idigeom) {
  121. document.forms['mform1'].special_param1.style.display='block';
  122. document.forms['mform1'].special_param1.disabled=0;
  123. }
  124. else {
  125. document.forms['mform1'].special_param1.style.display='none';
  126. document.forms['mform1'].special_param1.value=0;
  127. document.forms['mform1'].special_param1.disabled=1;
  128. }
  129. var i;
  130. var ids = new Array($ids);
  131. var evaluates = new Array($evaluates);
  132. if (document.forms['mform1'].type_iassign.value==1) {
  133. document.forms['mform1'].automatic_evaluate.disabled=1;
  134. document.forms['mform1'].show_answer.disabled=1;
  135. document.forms['mform1'].automatic_evaluate.value=0;
  136. document.forms['mform1'].show_answer.value=0;
  137. }
  138. else { // if (document.forms['mform1'].type_iassign.value==1)
  139. for (i=0;i<ids.length;i++) {
  140. if (ids[i]==id) {
  141. if (document.forms['mform1'].action.value=='edit') {
  142. if (evaluates[i]==0){
  143. document.forms['mform1'].automatic_evaluate.style.display='none';
  144. document.forms['mform1'].show_answer.style.display='none';
  145. document.forms['mform1'].automatic_evaluate.disabled=1;
  146. document.forms['mform1'].show_answer.disabled=1;
  147. document.forms['mform1'].automatic_evaluate.value=0;
  148. document.forms['mform1'].show_answer.value=0;
  149. }
  150. else {
  151. document.forms['mform1'].automatic_evaluate.style.display='block';
  152. document.forms['mform1'].show_answer.style.display='block';
  153. document.forms['mform1'].automatic_evaluate.disabled=0;
  154. document.forms['mform1'].show_answer.disabled=0;
  155. document.forms['mform1'].automatic_evaluate.value=1;
  156. document.forms['mform1'].show_answer.value=1;
  157. }
  158. }
  159. if (document.forms['mform1'].action.value=='add') {
  160. if (evaluates[i]==0) {
  161. document.forms['mform1'].automatic_evaluate.style.display='none';
  162. document.forms['mform1'].show_answer.style.display='none';
  163. document.forms['mform1'].automatic_evaluate.disabled=1;
  164. document.forms['mform1'].show_answer.disabled=1;
  165. document.forms['mform1'].automatic_evaluate.value=0;
  166. document.forms['mform1'].show_answer.value=0;
  167. }
  168. else {
  169. document.forms['mform1'].automatic_evaluate.style.display='block';
  170. document.forms['mform1'].show_answer.style.display='block';
  171. document.forms['mform1'].automatic_evaluate.disabled=0;
  172. document.forms['mform1'].show_answer.disabled=0;
  173. document.forms['mform1'].automatic_evaluate.value=1;
  174. document.forms['mform1'].show_answer.value=1;
  175. }
  176. }
  177. } // if (ids[i]==id)
  178. } // for (i=0;i<ids.length;i++)
  179. } // else if (document.forms['mform1'].type_iassign.value==1)
  180. } // function config_ilm(id)
  181. function disable_answer (resp) {
  182. if (resp==0) {
  183. document.forms['mform1'].show_answer.value=0;
  184. document.forms['mform1'].show_answer.disabled=1;
  185. }
  186. else {
  187. document.forms['mform1'].show_answer.disabled=0;
  188. }
  189. }
  190. function view_ilm_manager () {
  191. document.forms['mform1'].filename.disabled=1;
  192. open_ilm_manager=window.open('$CFG->wwwroot/mod/iassign/ilm_manager.php?id=$COURSE->id&from=iassign&ilmid='+document.forms['mform1'].iassign_ilmid.value,'','width=1000,height=880,menubar=0,location=0,scrollbars,status,fullscreen,resizable');
  193. }
  194. //]]>
  195. </script>";
  196. //-------------------------------------------------------------------------------
  197. // Adding the "title_type_iassign" fieldset, where all the common settings are showed
  198. // Data is insert through 'locallib.php ! action() -> new_iassign($param) : 3433/6441'
  199. $mform->addElement('header', 'title_type_iassign', get_string('type_iassign', 'iassign'));
  200. $type_iassign = array();
  201. $type_iassign[1] = get_string('example', 'iassign');
  202. $type_iassign[2] = get_string('test', 'iassign');
  203. $type_iassign[3] = get_string('exercise', 'iassign');
  204. $mform->addElement('select', 'type_iassign', get_string('choose_type_activity', 'iassign'), $type_iassign, array('onChange' => 'config_type(this.value);'));
  205. $mform->setDefault('type_iassign', 3); // default type_iassign = 3
  206. $mform->addHelpButton('type_iassign', 'helptypeiassign', 'iassign');
  207. //-------------------------------------------------------------------------------
  208. // Adding the "data_activity" fieldset, where all the common settings are showed
  209. $mform->addElement('header', 'data_activity', get_string('data_activity', 'iassign'));
  210. $mform->addElement('static', 'author', get_string('author_id', 'iassign'));
  211. $mform->addElement('static', 'author_modified', get_string('author_id_modified', 'iassign'));
  212. // Adding the standard "name" field
  213. $mform->addElement('text', 'name', get_string('iassigntitle', 'iassign'), array('size' => '55', 'onChange' => 'confirm_name(this.value);'));
  214. $mform->setType('name', PARAM_TEXT);
  215. $mform->addRule('name', get_string('required', 'iassign'), 'required');
  216. // Adding the standard "proposition" field
  217. //moodle2: $mform->addElement('htmleditor', 'proposition', get_string('proposition', 'iassign'));
  218. $mform->addElement('editor', 'proposition', get_string('proposition', 'iassign')); //moodle 3
  219. $mform->setType('proposition', PARAM_RAW);
  220. $mform->addRule('proposition', get_string('required', 'iassign'), 'required');
  221. //-----------------------------------------------------------------------------
  222. // Adding the "interactivy_learning_module" fieldset, where all the common settings are showed
  223. $mform->addElement('header', 'interactivy_learning_module', get_string('interactivy_learning_module', 'iassign'));
  224. $mform->setExpanded('interactivy_learning_module');
  225. // Search iLM registered in the database
  226. // Field : 'iassign_ilmid'
  227. // $ilms = search_iLM(1);
  228. $ilms = $all_ilm;
  229. //TODO Trick: was difficult to use 'mform->addGroup(...)', then I made by hand <select name='iassign_ilmid'...>' with 'optgroup' by hand!
  230. //TODO But MoodleForm clear/do not register the 'iassign_ilmid' in 'locallib.php!new_iassign($param)'
  231. //TODO Then (in 'new_iassign(...)') get $_POST['iassign_ilmid'] directly!!
  232. //TODO See: 'locallib.php!add_edit_iassign()', 'locallib.php!function add_edit_iassign()' and 'locallib.php!function new_iassign($param)'
  233. //2019 $applets = array();
  234. //2019 foreach ($ilms as $ilm)
  235. //2019 $applets[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  236. //2019 $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $applets, array('onChange' => 'config_ilm(this.value);'));
  237. //2019 $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  238. //D echo "<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>iassign_form.php:<br/>";
  239. // Split all iLM in those HTML from Java
  240. $list_html = array(); $list_html_id = array();
  241. $list_applets = array(); $list_applets_id = array();
  242. $num_iLM_html = $num_iLM_java = 0;
  243. foreach ($ilms as $ilm) {
  244. $type4 = substr($ilm->type, 0,4); // ensure the use of the only first 4 character (avoid difference if type is defined as 'HTML5')
  245. if (strcasecmp($type4, "HTML") == 0) { // Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
  246. $list_html[$num_iLM_html] = $ilm->name . ' (' . $ilm->version . ')';
  247. $list_html_id[$num_iLM_html] = $ilm->id;
  248. $num_iLM_html++;
  249. } else {
  250. $list_applets[$num_iLM_java] = $ilm->name . ' (' . $ilm->version . ')';
  251. $list_applets_id[$num_iLM_java] = $ilm->id;
  252. $num_iLM_java++;
  253. } //D echo $ilm->id . " : " . $ilm->type . " <br/>";
  254. }
  255. //D echo "" . get_string('interactivy_learning_module', 'iassign') . ": num_iLM_html=$num_iLM_html, num_iLM_java=$num_iLM_java<br/>";
  256. //D Interactive Learning Modules: num_iLM_html=0, num_iLM_java=9
  257. //TODO Assim o '/lib/formslib.php : get_data()' destroi o campo 'iassign_ilmid'...
  258. $html_group = get_string("group", "iassign") . " HTML";
  259. $java_group = get_string("group", "iassign") . " Java";
  260. $groups_ilm_select = array($html_group => array(), $java_group => array());
  261. for ($ii=0; $ii<$num_iLM_html; $ii++) {
  262. $groups_ilm_select[$html_group][$list_html_id[$ii]] = $list_html[$ii];
  263. }
  264. for ($ii=0; $ii<$num_iLM_java; $ii++) {
  265. $groups_ilm_select[$java_group][$list_applets_id[$ii]] = $list_applets[$ii];
  266. }
  267. $mform->addElement('selectgroups', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $groups_ilm_select);
  268. $mform->addHelpButton('iassign_ilmid', 'choose_iLM', 'iassign');
  269. //TODO Adaptives to use API of MoodleForm
  270. // addOption($optgroup, $text, $value, $attributes=null)
  271. // $allOptions = array(); $ilmHtml = array(); $ilmJava = array();
  272. // $ilmHtml[] = $mform->createElement('optgroup', 'groupHtml', 'Group HTML');
  273. // for ($ii=0; $ii<$num_iLM_html; $ii++) $ilmHtml[] = $mform->createElement('optgroup', 'groupHtml', 'Group HTML'); //$allOptions[] = $mform->addOption('Group HTML', $list_html[$ii], $list_html_id[$ii]);
  274. // $mform->addGroup($ilmHtml, 'groupHtml', '', array(' '), false);
  275. // $list_html $list_html_id $num_iLM_html
  276. // $list_applets $list_applets_id $num_iLM_java
  277. // echo "<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>iassign_form.php: num_iLM_html=$num_iLM_html, num_iLM_java=$num_iLM_java<br/>";exit;
  278. // $allOptions[] = array('-1' => 'Group HTML'); for ($ii=0; $ii<$num_iLM_html; $ii++) $allOptions[] = array(''.$list_html_id[$ii] => $list_html[$ii]);
  279. // $allOptions[] = array('-2' => 'Group Java'); for ($ii=0; $ii<$num_iLM_java; $ii++) $allOptions[] = array(''.$list_applets_id[$ii] => $list_applets[$ii]);
  280. // $allOptions[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  281. // $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $allOptions, array('onChange' => 'config_ilm(this.value);'));
  282. // for ($ii=0; $ii<$num_iLM_html; $ii++) $allOptions[] = $list_html[$ii];
  283. // for ($jj=0; $jj<$num_iLM_java; $jj++) $allOptions[] = $list_applets[$jj];
  284. // $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $allOptions, array('onChange' => 'config_ilm(this.value);'));
  285. // $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  286. // Field 'action'
  287. $fileurl = "";
  288. $filename = "";
  289. if (!is_null($COURSE->iassign_file_id)) {
  290. $mform->addElement('hidden', 'action', 'viewpluginpage');
  291. $fs = get_file_storage();
  292. $file = $fs->get_file_by_id($COURSE->iassign_file_id);
  293. $fileurl = $CFG->wwwroot . "/pluginfile.php/" . $file->get_contextid() . "/mod_iassign/exercise" . '/' . $file->get_itemid() . $file->get_filepath() . $file->get_filename();
  294. $filename = $file->get_filename();
  295. }
  296. //D echo "123";
  297. $html_div = '<div id="fitem_id_iassign_file_id" class="fitem required fitem_fgroup" style="padding: 35px; padding-left: 0;">';
  298. $html_div .= '<div class="fitemtitle col-md-3" style="padding: 0; float: left;">' . get_string('choose_file', 'iassign'); // 'Choose the file with the iLM activity'
  299. $html_div .= '<span><a><i class="icon fa fa-exclamation-circle text-danger fa-fw " title="' . get_string('requiredelement', 'form') . '" aria-label="' . get_string('requiredelement', 'form') . '" style="float: right; cursor: help; padding-right: 15px;"></i></a></span></div>';
  300. $html_div .= '<div class="felement fselect" id="error_message_file"><div class="file_iassign" id="file_border" style="display: inline;margin-left: 14px;border: 1px solid #cecfd1;padding: 8px;padding-right: 8px;border-radius: 4px;padding-right: 2px;">';
  301. $html_div .= '<i class="icon fa fa-file-text-o fa-fw" id="icon_doc" style="color: #8f8f8f;"></i><span id="iassign_file_link" style="color:#000000;"><a href="' . $fileurl . '" target="_blank" title="' . get_string('download_file', 'iassign') . $filename . '">' . $filename . '</a></span>';
  302. if ($fileurl != "")
  303. $html_div .= '&nbsp;&nbsp;&nbsp;';
  304. $html_div .= '<input onclick="view_ilm_manager()" name="add_ilm" value="' . get_string('add_ilm', 'iassign') . '" type="button" id="id_add_ilm"/></div>';
  305. $html_div .= '</div>';
  306. $html_div .= '</div>';
  307. $mform->addElement('html', $html_div);
  308. //Applies only iLM iGeom
  309. $mform->addElement('selectyesno', 'special_param1', get_string('special_param', 'iassign')); //$ynoptions
  310. $mform->setDefault('special_param1', 0);
  311. $mform->addHelpButton('special_param1', 'helpspecial_param', 'iassign');
  312. //-----------------------------------------------------------------------------
  313. //Applies only when the iLM is automatic evaluate.
  314. $mform->addElement('header', 'id_automatic_evaluate', get_string('only_automatic_evaluate', 'iassign'));
  315. // Using automatic evaluation activity? 0 - no / 1 – yes
  316. $mform->addElement('selectyesno', 'automatic_evaluate', get_string('automatic_evaluate', 'iassign'), array('onChange' => 'disable_answer(this.value);'));
  317. $mform->disabledIf('automatic_evaluate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  318. $mform->setDefault('automatic_evaluate', 0);
  319. // @todo Ver código comentado
  320. //$mform->addHelpButton('automatic_evaluate', 'helpautomatic_evaluate', 'iassign');
  321. //Show automatic evaluation results to students? 0 - no / 1 - yes
  322. $mform->addElement('selectyesno', 'show_answer', get_string('show_answer', 'iassign'));
  323. $mform->disabledIf('show_answer', 'type_iassign', 'eq', 1); //activity does not display if the type example
  324. // $mform->disabledIf('show_answer', 'automatic_evaluate', 'neq', 0);
  325. $mform->setDefault('show_answer', 0);
  326. //$mform->addHelpButton('show_answer', 'helpshow_answer', 'iassign');
  327. //-----------------------------------------------------------------------------
  328. // Adding the "duration_activity" fieldset, where all the common settings are showed
  329. $mform->addElement('header', 'duration_activity', get_string('duration_activity', 'iassign'));
  330. $mform->addElement('date_time_selector', 'timeavailable', get_string('availabledate', 'iassign'));
  331. $mform->setDefault('timeavailable', time());
  332. $mform->disabledIf('timeavailable', 'type_iassign', 'eq', 1); // activity does not display if the type example
  333. $mform->addElement('date_time_selector', 'timedue', get_string('duedate', 'iassign'));
  334. $mform->setDefault('timedue', time() + 7 * 24 * 3600);
  335. $mform->disabledIf('timedue', 'type_iassign', 'eq', 1); //activity does not display if the type example
  336. //Allow sending late? 0 - no or unlocked / 1 - yes or locked
  337. $mform->addElement('selectyesno', 'preventlate', get_string('preventlate', 'iassign'));
  338. $mform->setDefault('preventlate', 0);
  339. $mform->addHelpButton('preventlate', 'helppreventlate', 'iassign');
  340. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  341. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 2); //activity does not display if the type test
  342. //Allow test after delivery? 0 - no or unlocked / 1 - yes or locked
  343. $mform->addElement('selectyesno', 'test', get_string('permission_test', 'iassign'));
  344. $mform->setDefault('test', 0);
  345. $mform->addHelpButton('test', 'helptest', 'iassign');
  346. $mform->disabledIf('test', 'type_iassign', 'eq', 1); //activity does not display if the type example
  347. $mform->disabledIf('test', 'type_iassign', 'eq', 2); //activity does not display if the type test
  348. //--------------
  349. $mform->addElement('header', 'op_val', get_string('op_val', 'iassign'));
  350. $mform->addElement('modgrade', 'grade', get_string('grade', 'iassign'));
  351. $mform->setDefault('grade', 100);
  352. $mform->disabledIf('grade', 'type_iassign', 'eq', 1); //activity does not display if the type example
  353. $mform->disabledIf('grade', 'type_iassign', 'eq', 2); //activity does not display if the type test
  354. $max_experiment_options = array(0 => get_string('ilimit', 'iassign'));
  355. for ($i = 1; $i <= 20; $i++)
  356. $max_experiment_options[$i] = $i;
  357. $mform->addElement('select', 'max_experiment', get_string('experiment', 'iassign'), $max_experiment_options);
  358. $mform->setDefault('max_experiment', 0);
  359. $mform->addHelpButton('max_experiment', 'helpexperiment', 'iassign');
  360. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 1); //activity does not display if the type example
  361. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 2); //activity does not display if the type test
  362. if ($COURSE->iassign_list) {
  363. //-------------- dependency
  364. $mform->addElement('header', 'headerdependency', get_string('dependency', 'iassign'));
  365. $mform->addHelpButton('headerdependency', 'helpdependency', 'iassign');
  366. foreach ($COURSE->iassign_list as $iassign) {
  367. $tmp = 'iassign_list[' . $iassign->id . ']';
  368. if ($iassign->enable == 1)
  369. $mform->addElement('checkbox', $tmp, $iassign->name);
  370. } //foreach ($COURSE->iassign_list as $iassign)
  371. } //if ($COURSE->iassign_list)
  372. $mform->addElement('hidden', 'dependency');
  373. $mform->setType('dependency', PARAM_RAW);
  374. //-------------- config
  375. $mform->addElement('header', 'config', get_string('general', 'iassign'));
  376. $visibleoptions = array(1 => get_string('show'), 0 => get_string('hide'));
  377. $mform->addElement('select', 'visible', get_string('visible', 'iassign'), $visibleoptions);
  378. $mform->setDefault('visible', 0);
  379. //-------------------------------------------------------------------------------
  380. // Hidden fields
  381. $mform->addElement('hidden', 'action');
  382. $mform->setType('action', PARAM_TEXT);
  383. $mform->addElement('hidden', 'oldname');
  384. $mform->setType('oldname', PARAM_TEXT);
  385. $mform->addElement('hidden', 'id');
  386. $mform->setType('id', PARAM_TEXT);
  387. $mform->addElement('hidden', 'iassign_id');
  388. $mform->setType('iassign_id', PARAM_TEXT);
  389. $mform->addElement('hidden', 'file', '0');
  390. $mform->setType('file', PARAM_INT);
  391. $mform->addElement('hidden', 'filename');
  392. $mform->setType('filename', PARAM_TEXT);
  393. $mform->addElement('hidden', 'fileold');
  394. $mform->setType('fileold', PARAM_TEXT);
  395. $mform->addElement('hidden', 'iassignid');
  396. $mform->setType('iassignid', PARAM_TEXT);
  397. $mform->addElement('hidden', 'author_name');
  398. $mform->setType('author_name', PARAM_TEXT);
  399. $mform->addElement('hidden', 'author_modified_name');
  400. $mform->setType('author_modified_name', PARAM_TEXT);
  401. $mform->addElement('hidden', 'timecreated');
  402. $mform->setType('timecreated', PARAM_TEXT);
  403. $mform->addElement('hidden', 'position');
  404. $mform->setType('position', PARAM_TEXT);
  405. $mform->addElement('html', $code_javascript);
  406. // add standard elements, common to all modules
  407. $this->add_action_buttons();
  408. } // function definition()
  409. function validation ($data, $files) {
  410. global $COURSE, $DB;
  411. $errors = parent::validation($data, $files);
  412. $mform = & $this->_form;
  413. $errors = array();
  414. if ($mform->elementExists('name')) {
  415. $value = trim($data['name']);
  416. if ($value == '') {
  417. $errors['name'] = get_string('required', 'iassign');
  418. }
  419. }
  420. if ($mform->elementExists('proposition')) {
  421. // echo "iassign_form.php : data['proposition'] :<br/>"; print_r($data['proposition']);
  422. // $data['proposition'] = Array ([text] => ... [format] => ) - segundo esta' vazio!
  423. if (is_array($data['proposition'])) {
  424. // foreach ($data['proposition'] as $key => $value) echo "key=" . $key . " - value=" . $value . "<br/>";
  425. // $data['proposition'] = [format] => ) key=text - value=
  426. $aux = $data['proposition']; // Format JSON: {"em":"...","pt":"..."}
  427. if (isset($aux['text']))
  428. $value = trim($aux['text']);
  429. else
  430. $value = trim($aux[0]);
  431. }
  432. else
  433. $value = trim($data['proposition']);
  434. if ($value == '') {
  435. $errors['proposition'] = get_string('required', 'iassign');
  436. }
  437. }
  438. // echo "iassign_form.php : mform :<br/>"; // print_r($mform); exit();
  439. if ($mform->elementExists('file')) {
  440. $value = trim($data['file']);
  441. if ($value == 0) {
  442. $errors['iassign_ilmid_t'] = get_string('required_iassign_file', 'iassign');
  443. echo "<script>
  444. window.onload = function(e){
  445. document.getElementById('error_message_file').innerHTML += '<span style=\"font-size: 80%;color: #d9534f; margin-left: 1em;\">".get_string('required_iassign_file', 'iassign')."</span>';
  446. document.getElementById('file_border').style.borderColor = '#d9534f';
  447. } </script>";
  448. } else {
  449. $fs = get_file_storage(); // Get reference to all files in Moodle data
  450. $file = $fs->get_file_by_id($value);
  451. if ($file) {
  452. // Verify if file extension is correct to iLM
  453. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $data['iassign_ilmid']));
  454. echo "<script>
  455. window.onload = function(e){
  456. document.getElementById('iassign_file_link').innerHTML = '".$file->get_filename()."&nbsp;&nbsp;&nbsp;';";
  457. if ($iassign_ilm->extension != pathinfo($file->get_filename(), PATHINFO_EXTENSION)) {
  458. $errors['iassign_ilmid_t'] = get_string('incompatible_extension_file', 'iassign');
  459. echo "document.getElementById('error_message_file').innerHTML += '<span style=\"font-size: 80%;color: #d9534f; margin-left: 1em;\">".get_string('incompatible_extension_file', 'iassign')."</span>';
  460. document.getElementById('file_border').style.borderColor = '#d9534f';";
  461. }
  462. echo "} </script>";
  463. }
  464. }
  465. }
  466. return $errors;
  467. } // function validation($data, $files)
  468. } // class mod_iassign_form extends moodleform