iassign_form.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * Form to add and edit interactive activities
  4. *
  5. *
  6. * Release Notes:
  7. * - v 1.5 2013/09/19
  8. * + Insert function for validation form (mod_iassign_form::validation).
  9. * + Fix bugs in download exercise file.
  10. * - v 1.4 2013/08/21
  11. * + Change title link with message for get file for donwload file.
  12. * - v 1.3 2013/08/15
  13. * + Change view file for allow download file.
  14. * - v 1.2 2013/08/01
  15. * + Fix error in sql query for var $igeom.
  16. * - v 1.1 2013/07/12
  17. * + Fix error messages of 'setType' in debug mode for hidden fields.
  18. *
  19. * @author Patricia Alves Rodrigues
  20. * @author Leônidas O. Brandão
  21. * @version v 1.5 2013/09/19
  22. * @package mod_iassign
  23. * @since 2010/09/27
  24. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  25. *
  26. * <b>License</b>
  27. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  30. if (!defined('MOODLE_INTERNAL')) {
  31. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  32. }
  33. require_once($CFG->libdir . '/formslib.php');
  34. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  35. require_once($CFG->dirroot . '/mod/iassign/lib.php');
  36. /// This class create form based moodleform.
  37. // @see moodleform
  38. class mod_iassign_form extends moodleform {
  39. /// Add elements to form
  40. function definition () {
  41. global $CFG, $COURSE, $USER, $DB;
  42. $mform = & $this->_form;
  43. $instance = $this->_customdata;
  44. // iGeom has special parameter 'script'
  45. $params = array('name' => '%iGeom%', 'parent' => 0);
  46. $igeom = $DB->get_records_sql(
  47. "SELECT s.id, s.name, s.parent FROM {iassign_ilm} s
  48. WHERE s.name LIKE :name AND s.parent = :parent", $params);
  49. $id = $COURSE->cm;
  50. foreach ($igeom as $item)
  51. $idigeom = $item->id; // get the last id of iGeom
  52. $all_ilm = $DB->get_records('iassign_ilm', array('enable' => 1)); // or use ./lib.php function: $all_ilm = search_iLM(1);
  53. $iassigns = $DB->get_records('iassign_statement', array('iassignid' => $COURSE->iassignid));
  54. if ($all_ilm) {
  55. $ids = "";
  56. $names = "";
  57. $evaluates = "";
  58. foreach ($all_ilm as $one_ilm) {
  59. $ids .= "'" . $one_ilm->id . "',";
  60. $names .= "'" . $one_ilm->name . "',";
  61. $evaluates .= "'" . $one_ilm->evaluate . "',";
  62. }
  63. $ids .= "'0'";
  64. $evaluates .= "'0'";
  65. }
  66. $name_iassigns = "";
  67. if ($iassigns) {
  68. foreach ($iassigns as $iassign) {
  69. $name_iassigns .= "'" . $iassign->name . "',";
  70. }
  71. }
  72. $name_iassigns .= "''";
  73. $error_name = get_string('error_iassign_name', 'iassign');
  74. // @todo Código Javascript, verificar alternativa.
  75. $code_javascript = "
  76. <script type='text/javascript'>
  77. //<![CDATA[
  78. var i;
  79. var ids = new Array($ids);
  80. var evaluates = new Array($evaluates);
  81. document.forms['mform1'].filename.disabled=1;
  82. if (document.forms['mform1'].type_iassign.value==1) {
  83. document.forms['mform1'].grade.style.display='none';
  84. document.forms['mform1'].max_experiment.style.display='none';
  85. } else {
  86. document.forms['mform1'].grade.style.display='block';
  87. document.forms['mform1'].max_experiment.style.display='block';
  88. }
  89. for (i=0;i<ids.length;i++) {
  90. if (ids[i]==document.forms['mform1'].iassign_ilmid.value && evaluates[i]==0) {
  91. document.forms['mform1'].automatic_evaluate.style.display='none';
  92. document.forms['mform1'].show_answer.style.display='none';
  93. //document.forms['mform1'].automatic_evaluate.disabled=1;
  94. // document.forms['mform1'].show_answer.disabled=1;
  95. }
  96. }
  97. if (document.forms['mform1'].iassign_ilmid.value==" . $idigeom .") { // iGeom has special parameter 'script'
  98. document.forms['mform1'].special_param1.style.display='block';
  99. document.forms['mform1'].special_param1.disabled=0;
  100. }
  101. else {
  102. document.forms['mform1'].special_param1.style.display='none';
  103. document.forms['mform1'].special_param1.value=0;
  104. document.forms['mform1'].special_param1.disabled=1;
  105. }
  106. function confirm_name (name) {
  107. var i;
  108. var names = new Array($name_iassigns);
  109. for (i=0;i<names.length;i++) {
  110. if (names[i]==name)
  111. alert('" . $error_name . "');
  112. }
  113. }
  114. function config_ilm (id) {
  115. if (id==$idigeom) {
  116. document.forms['mform1'].special_param1.style.display='block';
  117. document.forms['mform1'].special_param1.disabled=0;
  118. }
  119. else {
  120. document.forms['mform1'].special_param1.style.display='none';
  121. document.forms['mform1'].special_param1.value=0;
  122. document.forms['mform1'].special_param1.disabled=1;
  123. }
  124. var i;
  125. var ids = new Array($ids);
  126. var evaluates = new Array($evaluates);
  127. if (document.forms['mform1'].type_iassign.value==1) {
  128. document.forms['mform1'].automatic_evaluate.disabled=1;
  129. document.forms['mform1'].show_answer.disabled=1;
  130. document.forms['mform1'].automatic_evaluate.value=0;
  131. document.forms['mform1'].show_answer.value=0;
  132. }
  133. else { // if (document.forms['mform1'].type_iassign.value==1)
  134. for (i=0;i<ids.length;i++) {
  135. if (ids[i]==id) {
  136. if (document.forms['mform1'].action.value=='edit') {
  137. if (evaluates[i]==0){
  138. document.forms['mform1'].automatic_evaluate.style.display='none';
  139. document.forms['mform1'].show_answer.style.display='none';
  140. document.forms['mform1'].automatic_evaluate.disabled=1;
  141. document.forms['mform1'].show_answer.disabled=1;
  142. document.forms['mform1'].automatic_evaluate.value=0;
  143. document.forms['mform1'].show_answer.value=0;
  144. }
  145. else {
  146. document.forms['mform1'].automatic_evaluate.style.display='block';
  147. document.forms['mform1'].show_answer.style.display='block';
  148. document.forms['mform1'].automatic_evaluate.disabled=0;
  149. document.forms['mform1'].show_answer.disabled=0;
  150. document.forms['mform1'].automatic_evaluate.value=1;
  151. document.forms['mform1'].show_answer.value=1;
  152. }
  153. }
  154. if (document.forms['mform1'].action.value=='add') {
  155. if (evaluates[i]==0) {
  156. document.forms['mform1'].automatic_evaluate.style.display='none';
  157. document.forms['mform1'].show_answer.style.display='none';
  158. document.forms['mform1'].automatic_evaluate.disabled=1;
  159. document.forms['mform1'].show_answer.disabled=1;
  160. document.forms['mform1'].automatic_evaluate.value=0;
  161. document.forms['mform1'].show_answer.value=0;
  162. }
  163. else {
  164. document.forms['mform1'].automatic_evaluate.style.display='block';
  165. document.forms['mform1'].show_answer.style.display='block';
  166. document.forms['mform1'].automatic_evaluate.disabled=0;
  167. document.forms['mform1'].show_answer.disabled=0;
  168. document.forms['mform1'].automatic_evaluate.value=1;
  169. document.forms['mform1'].show_answer.value=1;
  170. }
  171. }
  172. } // if (ids[i]==id)
  173. } // for (i=0;i<ids.length;i++)
  174. } // else if (document.forms['mform1'].type_iassign.value==1)
  175. } // function config_ilm(id)
  176. function disable_answer (resp) {
  177. if (resp==0) {
  178. document.forms['mform1'].show_answer.value=0;
  179. document.forms['mform1'].show_answer.disabled=1;
  180. }
  181. else {
  182. document.forms['mform1'].show_answer.disabled=0;
  183. }
  184. }
  185. function view_ilm_manager () {
  186. document.forms['mform1'].filename.disabled=1;
  187. 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');
  188. }
  189. //]]>
  190. </script>";
  191. //-------------------------------------------------------------------------------
  192. // Adding the "title_type_iassign" fieldset, where all the common settings are showed
  193. $mform->addElement('header', 'title_type_iassign', get_string('type_iassign', 'iassign'));
  194. $type_iassign = array();
  195. $type_iassign[1] = get_string('example', 'iassign');
  196. $type_iassign[2] = get_string('test', 'iassign');
  197. $type_iassign[3] = get_string('exercise', 'iassign');
  198. $mform->addElement('select', 'type_iassign', get_string('choose_type_activity', 'iassign'), $type_iassign, array('onChange' => 'config_type(this.value);'));
  199. $mform->setDefault('type_iassign', 3); // default type_iassign = 3
  200. $mform->addHelpButton('type_iassign', 'helptypeiassign', 'iassign');
  201. //-------------------------------------------------------------------------------
  202. // Adding the "data_activity" fieldset, where all the common settings are showed
  203. $mform->addElement('header', 'data_activity', get_string('data_activity', 'iassign'));
  204. $mform->addElement('static', 'author', get_string('author_id', 'iassign'));
  205. $mform->addElement('static', 'author_modified', get_string('author_id_modified', 'iassign'));
  206. // Adding the standard "name" field
  207. $mform->addElement('text', 'name', get_string('iassigntitle', 'iassign'), array('size' => '55', 'onChange' => 'confirm_name(this.value);'));
  208. $mform->setType('name', PARAM_TEXT);
  209. $mform->addRule('name', get_string('required', 'iassign'), 'required');
  210. // Adding the standard "proposition" field
  211. $mform->addElement('htmleditor', 'proposition', get_string('proposition', 'iassign'));
  212. $mform->setType('proposition', PARAM_RAW);
  213. $mform->addRule('proposition', get_string('required', 'iassign'), 'required');
  214. //-----------------------------------------------------------------------------
  215. // Adding the "interactivy_learning_module" fieldset, where all the common settings are showed
  216. $mform->addElement('header', 'interactivy_learning_module', get_string('interactivy_learning_module', 'iassign'));
  217. //$mform->setExpanded('interactivy_learning_module');
  218. // Search iLM registered in the database
  219. // $ilms = search_iLM(1);
  220. $ilms = $all_ilm;
  221. $applets = array();
  222. foreach ($ilms as $ilm)
  223. $applets[$ilm->id] = $ilm->name . ' ' . $ilm->version;
  224. $mform->addElement('select', 'iassign_ilmid', get_string('choose_iLM', 'iassign'), $applets, array('onChange' => 'config_ilm(this.value);'));
  225. $mform->addHelpButton('iassign_ilmid', 'helpchoose_ilm', 'iassign');
  226. $fileurl = "";
  227. $filename = "";
  228. if (!is_null($COURSE->iassign_file_id)) {
  229. $fs = get_file_storage();
  230. $file = $fs->get_file_by_id($COURSE->iassign_file_id);
  231. $fileurl = $CFG->wwwroot . "/pluginfile.php/" . $file->get_contextid() . "/mod_iassign/exercise" . '/' . $file->get_itemid() . $file->get_filepath() . $file->get_filename();
  232. $filename = $file->get_filename();
  233. }
  234. //D echo "123";
  235. $html_div = '<div id="fitem_id_iassign_file_id" class="fitem required fitem_fgroup">';
  236. $html_div .= '<div class="fitemtitle"><label for="id_iassign_file_id">' . get_string('choose_file', 'iassign');
  237. $html_div .= '<img class="req" title="' . get_string('requiredelement', 'form') . '" alt="' . get_string('requiredelement', 'form') . '" src="' . $CFG->wwwroot . '/theme/image.php/standard/core/1379534589/req"></label></div>';
  238. $html_div .= '<div class="felement fselect">';
  239. $html_div .= '<span id="iassign_file_link" style="color:#000000;"><a href="' . $fileurl . '" target="_blank" title="' . get_string('download_file', 'iassign') . $filename . '">' . $filename . '</a></span>';
  240. if ($fileurl != "")
  241. $html_div .= '&nbsp;&nbsp;&nbsp;';
  242. $html_div .= '<input onclick="view_ilm_manager()" name="add_ilm" value="' . get_string('add_ilm', 'iassign') . '" type="button" id="id_add_ilm"/>';
  243. $html_div .= '</div>';
  244. $html_div .= '</div>';
  245. $mform->addElement('html', $html_div);
  246. //Applies only iLM iGeom
  247. $mform->addElement('selectyesno', 'special_param1', get_string('special_param', 'iassign')); //$ynoptions
  248. $mform->setDefault('special_param1', 0);
  249. $mform->addHelpButton('special_param1', 'helpspecial_param', 'iassign');
  250. //-----------------------------------------------------------------------------
  251. //Applies only when the iLM is automatic evaluate.
  252. $mform->addElement('header', 'id_automatic_evaluate', get_string('only_automatic_evaluate', 'iassign'));
  253. // Using automatic evaluation activity? 0 - no / 1 – yes
  254. $mform->addElement('selectyesno', 'automatic_evaluate', get_string('automatic_evaluate', 'iassign'), array('onChange' => 'disable_answer(this.value);'));
  255. $mform->disabledIf('automatic_evaluate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  256. $mform->setDefault('automatic_evaluate', 0);
  257. // @todo Ver código comentado
  258. //$mform->addHelpButton('automatic_evaluate', 'helpautomatic_evaluate', 'iassign');
  259. //Show automatic evaluation results to students? 0 - no / 1 - yes
  260. $mform->addElement('selectyesno', 'show_answer', get_string('show_answer', 'iassign'));
  261. $mform->disabledIf('show_answer', 'type_iassign', 'eq', 1); //activity does not display if the type example
  262. // $mform->disabledIf('show_answer', 'automatic_evaluate', 'neq', 0);
  263. $mform->setDefault('show_answer', 0);
  264. //$mform->addHelpButton('show_answer', 'helpshow_answer', 'iassign');
  265. //-----------------------------------------------------------------------------
  266. // Adding the "duration_activity" fieldset, where all the common settings are showed
  267. $mform->addElement('header', 'duration_activity', get_string('duration_activity', 'iassign'));
  268. $mform->addElement('date_time_selector', 'timeavailable', get_string('availabledate', 'iassign'));
  269. $mform->setDefault('timeavailable', time());
  270. $mform->disabledIf('timeavailable', 'type_iassign', 'eq', 1); // activity does not display if the type example
  271. $mform->addElement('date_time_selector', 'timedue', get_string('duedate', 'iassign'));
  272. $mform->setDefault('timedue', time() + 7 * 24 * 3600);
  273. $mform->disabledIf('timedue', 'type_iassign', 'eq', 1); //activity does not display if the type example
  274. //Allow sending late? 0 - no or unlocked / 1 - yes or locked
  275. $mform->addElement('selectyesno', 'preventlate', get_string('preventlate', 'iassign'));
  276. $mform->setDefault('preventlate', 0);
  277. $mform->addHelpButton('preventlate', 'helppreventlate', 'iassign');
  278. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 1); //activity does not display if the type example
  279. $mform->disabledIf('preventlate', 'type_iassign', 'eq', 2); //activity does not display if the type test
  280. //Allow test after delivery? 0 - no or unlocked / 1 - yes or locked
  281. $mform->addElement('selectyesno', 'test', get_string('permission_test', 'iassign'));
  282. $mform->setDefault('test', 0);
  283. $mform->addHelpButton('test', 'helptest', 'iassign');
  284. $mform->disabledIf('test', 'type_iassign', 'eq', 1); //activity does not display if the type example
  285. $mform->disabledIf('test', 'type_iassign', 'eq', 2); //activity does not display if the type test
  286. //--------------
  287. $mform->addElement('header', 'op_val', get_string('op_val', 'iassign'));
  288. $mform->addElement('modgrade', 'grade', get_string('grade', 'iassign'));
  289. $mform->setDefault('grade', 100);
  290. $mform->disabledIf('grade', 'type_iassign', 'eq', 1); //activity does not display if the type example
  291. $mform->disabledIf('grade', 'type_iassign', 'eq', 2); //activity does not display if the type test
  292. $max_experiment_options = array(0 => get_string('ilimit', 'iassign'));
  293. for ($i = 1; $i <= 20; $i++)
  294. $max_experiment_options[$i] = $i;
  295. $mform->addElement('select', 'max_experiment', get_string('experiment', 'iassign'), $max_experiment_options);
  296. $mform->setDefault('max_experiment', 0);
  297. $mform->addHelpButton('max_experiment', 'helpexperiment', 'iassign');
  298. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 1); //activity does not display if the type example
  299. $mform->disabledIf('max_experiment', 'type_iassign', 'eq', 2); //activity does not display if the type test
  300. if ($COURSE->iassign_list) {
  301. //-------------- dependency
  302. $mform->addElement('header', 'headerdependency', get_string('dependency', 'iassign'));
  303. $mform->addHelpButton('headerdependency', 'helpdependency', 'iassign');
  304. foreach ($COURSE->iassign_list as $iassign) {
  305. $tmp = 'iassign_list[' . $iassign->id . ']';
  306. if ($iassign->enable == 1)
  307. $mform->addElement('checkbox', $tmp, $iassign->name);
  308. } //foreach ($COURSE->iassign_list as $iassign)
  309. } //if ($COURSE->iassign_list)
  310. $mform->addElement('hidden', 'dependency');
  311. $mform->setType('dependency', PARAM_RAW);
  312. //-------------- config
  313. $mform->addElement('header', 'config', get_string('general', 'iassign'));
  314. $visibleoptions = array(1 => get_string('show'), 0 => get_string('hide'));
  315. $mform->addElement('select', 'visible', get_string('visible', 'iassign'), $visibleoptions);
  316. $mform->setDefault('visible', 0);
  317. //-------------------------------------------------------------------------------
  318. // Hidden fields
  319. $mform->addElement('hidden', 'action');
  320. $mform->setType('action', PARAM_TEXT);
  321. $mform->addElement('hidden', 'oldname');
  322. $mform->setType('oldname', PARAM_TEXT);
  323. $mform->addElement('hidden', 'id');
  324. $mform->setType('id', PARAM_TEXT);
  325. $mform->addElement('hidden', 'iassign_id');
  326. $mform->setType('iassign_id', PARAM_TEXT);
  327. $mform->addElement('hidden', 'file', '0');
  328. $mform->setType('file', PARAM_INT);
  329. $mform->addElement('hidden', 'filename');
  330. $mform->setType('filename', PARAM_TEXT);
  331. $mform->addElement('hidden', 'fileold');
  332. $mform->setType('fileold', PARAM_TEXT);
  333. $mform->addElement('hidden', 'iassignid');
  334. $mform->setType('iassignid', PARAM_TEXT);
  335. $mform->addElement('hidden', 'author_name');
  336. $mform->setType('author_name', PARAM_TEXT);
  337. $mform->addElement('hidden', 'author_modified_name');
  338. $mform->setType('author_modified_name', PARAM_TEXT);
  339. $mform->addElement('hidden', 'timecreated');
  340. $mform->setType('timecreated', PARAM_TEXT);
  341. $mform->addElement('hidden', 'position');
  342. $mform->setType('position', PARAM_TEXT);
  343. $mform->addElement('html', $code_javascript);
  344. // add standard elements, common to all modules
  345. $this->add_action_buttons();
  346. } // function definition()
  347. function validation ($data, $files) {
  348. global $COURSE, $DB;
  349. $errors = parent::validation($data, $files);
  350. $mform = & $this->_form;
  351. $errors = array();
  352. if ($mform->elementExists('name')) {
  353. $value = trim($data['name']);
  354. if ($value == '') {
  355. $errors['name'] = get_string('required', 'iassign');
  356. }
  357. }
  358. if ($mform->elementExists('proposition')) {
  359. $value = trim($data['proposition']);
  360. if ($value == '') {
  361. $errors['proposition'] = get_string('required', 'iassign');
  362. }
  363. }
  364. if ($mform->elementExists('file')) {
  365. $value = trim($data['file']);
  366. if ($value == 0) {
  367. $errors['iassign_ilmid'] = get_string('required_iassign_file', 'iassign');
  368. }
  369. }
  370. return $errors;
  371. } // function validation($data, $files)
  372. } // class mod_iassign_form extends moodleform