iassign_form.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. // Get recently used ilm id: @igor
  55. $params = array('curso' => $COURSE->id);
  56. $result_recent = $DB->get_record_sql(
  57. "SELECT stm.iassign_ilmid FROM {iassign_statement} stm, {iassign} ias
  58. WHERE stm.iassignid = ias.id AND ias.course=:curso ORDER BY stm.id DESC LIMIT 1", $params);
  59. $all_ilm = $DB->get_records('iassign_ilm', array('enable' => 1)); // or use ./lib.php function: $all_ilm = search_iLM(1);
  60. $iassigns = $DB->get_records('iassign_statement', array('iassignid' => $COURSE->iassignid));
  61. if ($all_ilm) {
  62. $ids = "";
  63. $names = "";
  64. $evaluates = "";
  65. foreach ($all_ilm as $one_ilm) {
  66. $ids .= "'" . $one_ilm->id . "',";
  67. $names .= "'" . $one_ilm->name . "',";
  68. $evaluates .= "'" . $one_ilm->evaluate . "',";
  69. }
  70. $ids .= "'0'";
  71. $evaluates .= "'0'";
  72. }
  73. $name_iassigns = "";
  74. if ($iassigns) {
  75. foreach ($iassigns as $iassign) {
  76. $name_iassigns .= "'" . addslashes($iassign->name) . "',"; // if the name has ' it implies close JavaScript string => error!
  77. }
  78. }
  79. $name_iassigns .= "''";
  80. $error_name = get_string('error_iassign_name', 'iassign');
  81. // @todo Código Javascript, verificar alternativa.
  82. $code_javascript = "
  83. <script type='text/javascript'>
  84. //<![CDATA[
  85. var i;
  86. var ids = new Array($ids);
  87. var evaluates = new Array($evaluates);
  88. document.forms['mform1'].filename.disabled=1;
  89. if (document.forms['mform1'].type_iassign.value==1) {
  90. if (typeof document.forms['mform1'].grade === 'undefined') ; // do nothing
  91. else document.forms['mform1'].grade.style.display='none';
  92. document.forms['mform1'].max_experiment.style.display='none';
  93. } else {
  94. if (typeof document.forms['mform1'].grade === 'undefined') ; // do nothing
  95. else document.forms['mform1'].grade.style.display='block';
  96. document.forms['mform1'].max_experiment.style.display='block';
  97. }
  98. for (i=0;i<ids.length;i++) {
  99. if (ids[i]==document.forms['mform1'].iassign_ilmid.value && evaluates[i]==0) {
  100. document.forms['mform1'].automatic_evaluate.style.display='none';
  101. document.forms['mform1'].show_answer.style.display='none';
  102. //document.forms['mform1'].automatic_evaluate.disabled=1;
  103. // document.forms['mform1'].show_answer.disabled=1;
  104. }
  105. }
  106. if (document.forms['mform1'].iassign_ilmid.value==" . $idigeom .") { // iGeom has special parameter 'script'
  107. document.forms['mform1'].special_param1.style.display='block';
  108. document.forms['mform1'].special_param1.disabled=0;
  109. }
  110. else {
  111. document.forms['mform1'].special_param1.style.display='none';
  112. document.forms['mform1'].special_param1.value=0;
  113. document.forms['mform1'].special_param1.disabled=1;
  114. }
  115. function confirm_name (name) {
  116. var i;
  117. var names = new Array(" . $name_iassigns . ");
  118. for (i=0;i<names.length;i++) {
  119. if (names[i]==name)
  120. alert('" . $error_name . "');
  121. }
  122. }
  123. function config_ilm (id) {
  124. //alert('config_ilm('+id+'): idigeom=$idigeom, ilmid='+document.forms['mform1'].iassign_ilmid.value);
  125. if (id==$idigeom) {
  126. document.forms['mform1'].special_param1.style.display='block';
  127. document.forms['mform1'].special_param1.disabled=0;
  128. }
  129. else {
  130. document.forms['mform1'].special_param1.style.display='none';
  131. document.forms['mform1'].special_param1.value=0;
  132. document.forms['mform1'].special_param1.disabled=1;
  133. }
  134. var i;
  135. var ids = new Array($ids);
  136. var evaluates = new Array($evaluates);
  137. if (document.forms['mform1'].type_iassign.value==1) {
  138. document.forms['mform1'].automatic_evaluate.disabled=1;
  139. document.forms['mform1'].show_answer.disabled=1;
  140. document.forms['mform1'].automatic_evaluate.value=0;
  141. document.forms['mform1'].show_answer.value=0;
  142. }
  143. else { // if (document.forms['mform1'].type_iassign.value==1)
  144. for (i=0;i<ids.length;i++) {
  145. if (ids[i]==id) {
  146. if (document.forms['mform1'].action.value=='edit') {
  147. if (evaluates[i]==0){
  148. document.forms['mform1'].automatic_evaluate.style.display='none';
  149. document.forms['mform1'].show_answer.style.display='none';
  150. document.forms['mform1'].automatic_evaluate.disabled=1;
  151. document.forms['mform1'].show_answer.disabled=1;
  152. document.forms['mform1'].automatic_evaluate.value=0;
  153. document.forms['mform1'].show_answer.value=0;
  154. }
  155. else {
  156. document.forms['mform1'].automatic_evaluate.style.display='block';
  157. document.forms['mform1'].show_answer.style.display='block';
  158. document.forms['mform1'].automatic_evaluate.disabled=0;
  159. document.forms['mform1'].show_answer.disabled=0;
  160. document.forms['mform1'].automatic_evaluate.value=1;
  161. document.forms['mform1'].show_answer.value=1;
  162. }
  163. }
  164. if (document.forms['mform1'].action.value=='add') {
  165. if (evaluates[i]==0) {
  166. document.forms['mform1'].automatic_evaluate.style.display='none';
  167. document.forms['mform1'].show_answer.style.display='none';
  168. document.forms['mform1'].automatic_evaluate.disabled=1;
  169. document.forms['mform1'].show_answer.disabled=1;
  170. document.forms['mform1'].automatic_evaluate.value=0;
  171. document.forms['mform1'].show_answer.value=0;
  172. }
  173. else {
  174. document.forms['mform1'].automatic_evaluate.style.display='block';
  175. document.forms['mform1'].show_answer.style.display='block';
  176. document.forms['mform1'].automatic_evaluate.disabled=0;
  177. document.forms['mform1'].show_answer.disabled=0;
  178. document.forms['mform1'].automatic_evaluate.value=1;
  179. document.forms['mform1'].show_answer.value=1;
  180. }
  181. }
  182. } // if (ids[i]==id)
  183. } // for (i=0;i<ids.length;i++)
  184. } // else if (document.forms['mform1'].type_iassign.value==1)
  185. } // function config_ilm(id)
  186. function disable_answer (resp) {
  187. if (resp==0) {
  188. document.forms['mform1'].show_answer.value=0;
  189. document.forms['mform1'].show_answer.disabled=1;
  190. }
  191. else {
  192. document.forms['mform1'].show_answer.disabled=0;
  193. }
  194. }
  195. function view_ilm_manager () {
  196. document.forms['mform1'].filename.disabled=1;
  197. 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');
  198. }
  199. //]]>
  200. </script>";
  201. //-------------------------------------------------------------------------------
  202. // Adding the "title_type_iassign" fieldset, where all the common settings are showed
  203. // Data is insert through 'locallib.php ! action() -> new_iassign($param) : 3433/6441'
  204. $mform->addElement('header', 'title_type_iassign', get_string('type_iassign', 'iassign'));
  205. $type_iassign = array();
  206. $type_iassign[1] = get_string('example', 'iassign');
  207. $type_iassign[2] = get_string('test', 'iassign');
  208. $type_iassign[3] = get_string('exercise', 'iassign');
  209. $mform->addElement('select', 'type_iassign', get_string('choose_type_activity', 'iassign'), $type_iassign, array('onChange' => 'config_type(this.value);'));
  210. $mform->setDefault('type_iassign', 3); // default type_iassign = 3
  211. $mform->addHelpButton('type_iassign', 'helptypeiassign', 'iassign');
  212. //-------------------------------------------------------------------------------
  213. // Adding the "data_activity" fieldset, where all the common settings are showed
  214. $mform->addElement('header', 'data_activity', get_string('data_activity', 'iassign'));
  215. $mform->addElement('static', 'author', get_string('author_id', 'iassign'));
  216. $mform->addElement('static', 'author_modified', get_string('author_id_modified', 'iassign'));
  217. // Adding the standard "name" field
  218. $mform->addElement('text', 'name', get_string('iassigntitle', 'iassign'), array('size' => '55', 'onChange' => 'confirm_name(this.value);'));
  219. $mform->setType('name', PARAM_TEXT);
  220. $mform->addRule('name', get_string('required', 'iassign'), 'required');
  221. // Adding the standard "proposition" field
  222. //moodle2: $mform->addElement('htmleditor', 'proposition', get_string('proposition', 'iassign'));
  223. $mform->addElement('editor', 'proposition', get_string('proposition', 'iassign')); //moodle 3
  224. $mform->setType('proposition', PARAM_RAW);
  225. $mform->addRule('proposition', get_string('required', 'iassign'), 'required');
  226. //-----------------------------------------------------------------------------
  227. // Adding the "interactivy_learning_module" fieldset, where all the common settings are showed
  228. $mform->addElement('header', 'interactivy_learning_module', get_string('interactivy_learning_module', 'iassign'));
  229. $mform->setExpanded('interactivy_learning_module');
  230. // Search iLM registered in the database
  231. // Field : 'iassign_ilmid'
  232. // $ilms = search_iLM(1);
  233. $ilms = $all_ilm;
  234. //TODO Trick: was difficult to use 'mform->addGroup(...)', then I made by hand <select name='iassign_ilmid'...>' with 'optgroup' by hand!
  235. //TODO But MoodleForm clear/do not register the 'iassign_ilmid' in 'locallib.php!new_iassign($param)'
  236. //TODO Then (in 'new_iassign(...)') get $_POST['iassign_ilmid'] directly!!
  237. //TODO See: 'locallib.php!add_edit_iassign()', 'locallib.php!function add_edit_iassign()' and 'locallib.php!function new_iassign($param)'
  238. //2019 $applets = array();
  239. //2019 foreach ($ilms as $ilm)
  240. //2019 $applets[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  241. //2019 $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $applets, array('onChange' => 'config_ilm(this.value);'));
  242. //2019 $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  243. //D echo "<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>iassign_form.php:<br/>";
  244. // Split all iLM in those HTML from Java
  245. $list_html = array(); $list_html_id = array();
  246. $list_applets = array(); $list_applets_id = array();
  247. $num_iLM_html = $num_iLM_java = 0;
  248. foreach ($ilms as $ilm) {
  249. $type4 = substr($ilm->type, 0,4); // ensure the use of the only first 4 character (avoid difference if type is defined as 'HTML5')
  250. 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.
  251. $list_html[$num_iLM_html] = $ilm->name . ' (' . $ilm->version . ')';
  252. $list_html_id[$num_iLM_html] = $ilm->id;
  253. $num_iLM_html++;
  254. } else {
  255. $list_applets[$num_iLM_java] = $ilm->name . ' (' . $ilm->version . ')';
  256. $list_applets_id[$num_iLM_java] = $ilm->id;
  257. $num_iLM_java++;
  258. } //D echo $ilm->id . " : " . $ilm->type . " <br/>";
  259. }
  260. //D echo "" . get_string('interactivy_learning_module', 'iassign') . ": num_iLM_html=$num_iLM_html, num_iLM_java=$num_iLM_java<br/>";
  261. //D Interactive Learning Modules: num_iLM_html=0, num_iLM_java=9
  262. //TODO Assim o '/lib/formslib.php : get_data()' destroi o campo 'iassign_ilmid'...
  263. $html_group = get_string("group", "iassign") . " HTML";
  264. $java_group = get_string("group", "iassign") . " Java";
  265. $arrayHTML = array();
  266. $arrayJava = array();
  267. for ($ii=0; $ii<$num_iLM_html; $ii++) {
  268. $arrayHTML[$list_html_id[$ii]] = $list_html[$ii];
  269. }
  270. for ($ii=0; $ii<$num_iLM_java; $ii++) {
  271. $arrayJava[$list_applets_id[$ii]] = $list_applets[$ii];
  272. }
  273. $selectElems = array(
  274. $html_group => $arrayHTML,
  275. $java_group => $arrayJava
  276. );
  277. $ilm_select = $mform->addElement('selectgroups', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $selectElems);
  278. $mform->addHelpButton('iassign_ilmid', 'choose_iLM', 'iassign');
  279. if ($result_recent && isset($result_recent->iassign_ilmid)) {
  280. $code_javascript .= ' <script> document.forms[0].iassign_ilmid.value = ' . $result_recent->iassign_ilmid . '</script>' ;
  281. }
  282. //TODO Adaptives to use API of MoodleForm
  283. // addOption($optgroup, $text, $value, $attributes=null)
  284. // $allOptions = array(); $ilmHtml = array(); $ilmJava = array();
  285. // $ilmHtml[] = $mform->createElement('optgroup', 'groupHtml', 'Group HTML');
  286. // 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]);
  287. // $mform->addGroup($ilmHtml, 'groupHtml', '', array(' '), false);
  288. // $list_html $list_html_id $num_iLM_html
  289. // $list_applets $list_applets_id $num_iLM_java
  290. // 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;
  291. // $allOptions[] = array('-1' => 'Group HTML'); for ($ii=0; $ii<$num_iLM_html; $ii++) $allOptions[] = array(''.$list_html_id[$ii] => $list_html[$ii]);
  292. // $allOptions[] = array('-2' => 'Group Java'); for ($ii=0; $ii<$num_iLM_java; $ii++) $allOptions[] = array(''.$list_applets_id[$ii] => $list_applets[$ii]);
  293. // $allOptions[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  294. // $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $allOptions, array('onChange' => 'config_ilm(this.value);'));
  295. // for ($ii=0; $ii<$num_iLM_html; $ii++) $allOptions[] = $list_html[$ii];
  296. // for ($jj=0; $jj<$num_iLM_java; $jj++) $allOptions[] = $list_applets[$jj];
  297. // $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $allOptions, array('onChange' => 'config_ilm(this.value);'));
  298. // $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  299. // Field 'action'
  300. $fileurl = "";
  301. $filename = "";
  302. if (!is_null($COURSE->iassign_file_id)) {
  303. $mform->addElement('hidden', 'action', 'viewpluginpage');
  304. $fs = get_file_storage();
  305. $file = $fs->get_file_by_id($COURSE->iassign_file_id);
  306. $fileurl = $CFG->wwwroot . "/pluginfile.php/" . $file->get_contextid() . "/mod_iassign/exercise" . '/' . $file->get_itemid() . $file->get_filepath() . $file->get_filename();
  307. $filename = $file->get_filename();
  308. }
  309. //D echo "123";
  310. $html_div = '<div id="fitem_id_iassign_file_id" class="fitem required fitem_fgroup" style="padding: 35px; padding-left: 0;">';
  311. $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'
  312. $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>';
  313. $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;">';
  314. $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>';
  315. if ($fileurl != "")
  316. $html_div .= '&nbsp;&nbsp;&nbsp;';
  317. $html_div .= '<input onclick="view_ilm_manager()" name="add_ilm" value="' . get_string('add_ilm', 'iassign') . '" type="button" id="id_add_ilm"/></div>';
  318. $html_div .= '</div>';
  319. $html_div .= '</div>';
  320. $mform->addElement('html', $html_div);
  321. //Applies only iLM iGeom
  322. $mform->addElement('selectyesno', 'special_param1', get_string('special_param', 'iassign')); //$ynoptions
  323. $mform->setDefault('special_param1', 0);
  324. $mform->addHelpButton('special_param1', 'helpspecial_param', 'iassign');
  325. //-----------------------------------------------------------------------------
  326. //Applies only when the iLM is automatic evaluate.
  327. $mform->addElement('header', 'id_automatic_evaluate', get_string('only_automatic_evaluate', 'iassign'));
  328. // Using automatic evaluation activity? 0 - no / 1 – yes
  329. $mform->addElement('selectyesno', 'automatic_evaluate', get_string('automatic_evaluate', 'iassign'), array('onChange' => 'disable_answer(this.value);'));
  330. $mform->disabledIf('automatic_evaluate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  331. $mform->setDefault('automatic_evaluate', 0);
  332. // @todo Ver código comentado
  333. //$mform->addHelpButton('automatic_evaluate', 'helpautomatic_evaluate', 'iassign');
  334. //Show automatic evaluation results to students? 0 - no / 1 - yes
  335. $mform->addElement('selectyesno', 'show_answer', get_string('show_answer', 'iassign'));
  336. $mform->disabledIf('show_answer', 'type_iassign', 'eq', 1); //activity does not display if the type example
  337. // $mform->disabledIf('show_answer', 'automatic_evaluate', 'neq', 0);
  338. $mform->setDefault('show_answer', 0);
  339. //$mform->addHelpButton('show_answer', 'helpshow_answer', 'iassign');
  340. //-----------------------------------------------------------------------------
  341. // Adding the "duration_activity" fieldset, where all the common settings are showed
  342. $mform->addElement('header', 'duration_activity', get_string('duration_activity', 'iassign'));
  343. $mform->addElement('date_time_selector', 'timeavailable', get_string('availabledate', 'iassign'));
  344. $mform->setDefault('timeavailable', time());
  345. $mform->disabledIf('timeavailable', 'type_iassign', 'eq', 1); // activity does not display if the type example
  346. $mform->addElement('date_time_selector', 'timedue', get_string('duedate', 'iassign'));
  347. $mform->setDefault('timedue', time() + 7 * 24 * 3600);
  348. $mform->disabledIf('timedue', 'type_iassign', 'eq', 1); //activity does not display if the type example
  349. //Allow sending late? 0 - no or unlocked / 1 - yes or locked
  350. $mform->addElement('selectyesno', 'preventlate', get_string('preventlate', 'iassign'));
  351. $mform->setDefault('preventlate', 0);
  352. $mform->addHelpButton('preventlate', 'helppreventlate', 'iassign');
  353. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  354. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 2); //activity does not display if the type test
  355. //Allow test after delivery? 0 - no or unlocked / 1 - yes or locked
  356. $mform->addElement('selectyesno', 'test', get_string('permission_test', 'iassign'));
  357. $mform->setDefault('test', 0);
  358. $mform->addHelpButton('test', 'helptest', 'iassign');
  359. $mform->disabledIf('test', 'type_iassign', 'eq', 1); //activity does not display if the type example
  360. $mform->disabledIf('test', 'type_iassign', 'eq', 2); //activity does not display if the type test
  361. //--------------
  362. $mform->addElement('header', 'op_val', get_string('op_val', 'iassign'));
  363. $mform->addElement('modgrade', 'grade', get_string('grade', 'iassign'));
  364. $mform->setDefault('grade', 100);
  365. $mform->disabledIf('grade', 'type_iassign', 'eq', 1); //activity does not display if the type example
  366. $mform->disabledIf('grade', 'type_iassign', 'eq', 2); //activity does not display if the type test
  367. $max_experiment_options = array(0 => get_string('ilimit', 'iassign'));
  368. for ($i = 1; $i <= 20; $i++)
  369. $max_experiment_options[$i] = $i;
  370. $mform->addElement('select', 'max_experiment', get_string('experiment', 'iassign'), $max_experiment_options);
  371. $mform->setDefault('max_experiment', 0);
  372. $mform->addHelpButton('max_experiment', 'helpexperiment', 'iassign');
  373. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 1); //activity does not display if the type example
  374. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 2); //activity does not display if the type test
  375. if ($COURSE->iassign_list) {
  376. //-------------- dependency
  377. $mform->addElement('header', 'headerdependency', get_string('dependency', 'iassign'));
  378. $mform->addHelpButton('headerdependency', 'helpdependency', 'iassign');
  379. foreach ($COURSE->iassign_list as $iassign) {
  380. $tmp = 'iassign_list[' . $iassign->id . ']';
  381. if ($iassign->enable == 1)
  382. $mform->addElement('checkbox', $tmp, $iassign->name);
  383. } //foreach ($COURSE->iassign_list as $iassign)
  384. } //if ($COURSE->iassign_list)
  385. $mform->addElement('hidden', 'dependency');
  386. $mform->setType('dependency', PARAM_RAW);
  387. //-------------- config
  388. $mform->addElement('header', 'config', get_string('general', 'iassign'));
  389. $visibleoptions = array(1 => get_string('show'), 0 => get_string('hide'));
  390. $mform->addElement('select', 'visible', get_string('visible', 'iassign'), $visibleoptions);
  391. $mform->setDefault('visible', 0);
  392. //-------------------------------------------------------------------------------
  393. // Hidden fields
  394. $mform->addElement('hidden', 'action');
  395. $mform->setType('action', PARAM_TEXT);
  396. $mform->addElement('hidden', 'oldname');
  397. $mform->setType('oldname', PARAM_TEXT);
  398. $mform->addElement('hidden', 'id');
  399. $mform->setType('id', PARAM_TEXT);
  400. $mform->addElement('hidden', 'iassign_id');
  401. $mform->setType('iassign_id', PARAM_TEXT);
  402. $mform->addElement('hidden', 'file', '0');
  403. $mform->setType('file', PARAM_INT);
  404. $mform->addElement('hidden', 'filename');
  405. $mform->setType('filename', PARAM_TEXT);
  406. $mform->addElement('hidden', 'fileold');
  407. $mform->setType('fileold', PARAM_TEXT);
  408. $mform->addElement('hidden', 'iassignid');
  409. $mform->setType('iassignid', PARAM_TEXT);
  410. $mform->addElement('hidden', 'author_name');
  411. $mform->setType('author_name', PARAM_TEXT);
  412. $mform->addElement('hidden', 'author_modified_name');
  413. $mform->setType('author_modified_name', PARAM_TEXT);
  414. $mform->addElement('hidden', 'timecreated');
  415. $mform->setType('timecreated', PARAM_TEXT);
  416. $mform->addElement('hidden', 'position');
  417. $mform->setType('position', PARAM_TEXT);
  418. $mform->addElement('html', $code_javascript);
  419. // add standard elements, common to all modules
  420. $this->add_action_buttons();
  421. } // function definition()
  422. function validation ($data, $files) {
  423. global $COURSE, $DB;
  424. $errors = parent::validation($data, $files);
  425. $mform = & $this->_form;
  426. $errors = array();
  427. if ($mform->elementExists('name')) {
  428. $value = trim($data['name']);
  429. if ($value == '') {
  430. $errors['name'] = get_string('required', 'iassign');
  431. }
  432. }
  433. if ($mform->elementExists('proposition')) {
  434. // echo "iassign_form.php : data['proposition'] :<br/>"; print_r($data['proposition']);
  435. // $data['proposition'] = Array ([text] => ... [format] => ) - segundo esta' vazio!
  436. if (is_array($data['proposition'])) {
  437. // foreach ($data['proposition'] as $key => $value) echo "key=" . $key . " - value=" . $value . "<br/>";
  438. // $data['proposition'] = [format] => ) key=text - value=
  439. $aux = $data['proposition']; // Format JSON: {"em":"...","pt":"..."}
  440. if (isset($aux['text']))
  441. $value = trim($aux['text']);
  442. else
  443. $value = trim($aux[0]);
  444. }
  445. else
  446. $value = trim($data['proposition']);
  447. if ($value == '') {
  448. $errors['proposition'] = get_string('required', 'iassign');
  449. }
  450. }
  451. // echo "iassign_form.php : mform :<br/>"; // print_r($mform); exit();
  452. if ($mform->elementExists('file')) {
  453. $value = trim($data['file']);
  454. if ($value == 0) {
  455. $errors['iassign_ilmid_t'] = get_string('required_iassign_file', 'iassign');
  456. echo "<script>
  457. window.onload = function(e){
  458. document.getElementById('error_message_file').innerHTML += '<span style=\"font-size: 80%;color: #d9534f; margin-left: 1em;\">".get_string('required_iassign_file', 'iassign')."</span>';
  459. document.getElementById('file_border').style.borderColor = '#d9534f';
  460. } </script>";
  461. } else {
  462. $fs = get_file_storage(); // Get reference to all files in Moodle data
  463. $file = $fs->get_file_by_id($value);
  464. if ($file) {
  465. // Verify if file extension is correct to iLM
  466. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $data['iassign_ilmid']));
  467. echo "<script>
  468. window.onload = function(e){
  469. document.getElementById('iassign_file_link').innerHTML = '".$file->get_filename()."&nbsp;&nbsp;&nbsp;';";
  470. if ($iassign_ilm->extension != pathinfo($file->get_filename(), PATHINFO_EXTENSION)) {
  471. $errors['iassign_ilmid_t'] = get_string('incompatible_extension_file', 'iassign');
  472. echo "document.getElementById('error_message_file').innerHTML += '<span style=\"font-size: 80%;color: #d9534f; margin-left: 1em;\">".get_string('incompatible_extension_file', 'iassign')."</span>';
  473. document.getElementById('file_border').style.borderColor = '#d9534f';";
  474. }
  475. echo "} </script>";
  476. }
  477. }
  478. }
  479. return $errors;
  480. } // function validation($data, $files)
  481. } // class mod_iassign_form extends moodleform