special_param1==1) $lTA=true;' to call '$ilm->view_iLM(...lTA)'
 *
 * - v 4.9 2017/03/13
 *   + Great number of changes to allow the use o iVProgH5 (e.g. function changed: from 'applet_ilm' to 'build_ilm_tags')
 *
 * - v 4.8 2016/05/12
 *   + Function add_to_log() is deprecated it was then  rewritten  to the new events API
 *   + Resolved: the field "description" table "iassign_ilm" was being filled with the codes in HTML when importing magnet package and
 *     due to this the language field was not being displayed because in place of the quotes was recorded in the & quot field.
 *     ("$description_str = htmlentities(str_replace(array('',' '), array('',''), $application_xml->description->asXML()));"
 *     replaced by
 *     "description_str = str_replace(array('',' '), array('',''), $application_xml->description->asXML());")
 * - v 4.7 2016/02/17
 *   + Moodle 3.X: now iAssign is working fine under version 3.X (iassign/version.php: '$module->' changed to '$plugin->'; iassign/locallib.php: 'format_text(...)' replaced 'filter_text(...)')
 *   + Improved: now is possible to see the iGeom menus in preview (from iAssign Repository) - it depends on the version 1.3 of iAssign filter!
 *   + Improved: new names for 'form.input.MA_POST_Archive' and 'form.input.MA_POST_Value', now: 'iLM_PARAM_ArchiveContent' and 'iLM_PARAM_ActivityEvaluation'
 *   + BUG fixed: now it is fine the "online" edition of activities (in iAssign Repository) - inserted 'iLM_PARAM_Authoring' (iLM 2) and 'MA_POST_ArchiveTeacher' (iLM 1)
 *   + BUG fixed: it is possible to change the name of any file in iAssign Repository - problems was in 'optional_param(...)', 'PARAM_TEXT' replaced 'PARAM_ALPHANUMEXT'
 *   + BUG fixed: it is possible to duplicate any file in iAssign Repository - problems also in 'optional_param(...)', 'PARAM_TEXT' replaced 'PARAM_ALPHANUMEXT'
 *   + BUG fixed: now is possible to edit an iAssign activity with no new object been created (in iGeom: turn an example in exercise)
 *
 * --------------- (code bellow was used at our MOOC 2014)
 * - v 4.6 2014/02/25
 *   + Fix bugs in filter function for open applets.
 * - v 4.5 2014/02/24
 *   + Fix bugs in params.
 *   + Insert new param type.
 * - v 4.4 2014/01/24
 *   + Allow select type of params.
 *   + Insert the use of applet params specific for activities.
 * - v 4.3 2014/01/23
 *   + Insert function for move activities for other iLM (ilm_settings::confirm_move_iassign, ilm_settings::move_iassign).
 * - v 4.2 2016/02/13
 *   + Fixed API usage to work fine under Moodle 3.X: ilm_editor_new()
 * --------------- (above code used at our MOOC 2014)
 *
 * - v 4.1 2013/12/13
 *   + Insert log in iAssign actions.
 *   + Allow use the language in iLM description (ilm_settings::new_file_ilm, ilm_settings::new_ilm, ilm_settings::edit_ilm, ilm_settings::copy_new_version_ilm, ilm_settings::add_edit_copy_ilm, iassign_language::get_description_lang, iassign_language::get_all_lang).
 *   + Insert class for Log actions in system.
 * - v 4.0 2013/10/31
 *   + Insert support of export iLM in zip packages (ilm_settings::export_ilm).
 *   + Insert support of import iLM from zip packages (ilm_settings::import_ilm).
 *   + Fix bugs in message alert in iassign title and remove message alert of the description by cache error.
 * - v 3.9 2013/10/25
 *   + Insert support of upgrade iLM.
 *   + Insert support for more than one extension in iLM.
 *   + Fix bugs in verion control.
 * - v 3.8 2013/09/19
 *   + Get data of general fields in iassign statement table (iassign::add_edit_iassign).
 * - v 3.7 2013/09/12
 *   + Change tag APPLET in all functions of module (ilm::view_iLM, ilm_manager::ilm_editor_new, ilm_manager::ilm_editor_update).
 *   + Insert tool for manage aditional params for iLM (ilm_settings::add_edit_copy_param, ilm_settings::visible_param, ilm_settings::add_param, ilm_settings::edit_param, ilm_settings::copy_param, ilm_settings::delete_param).
 * - v 3.6 2013/09/05
 *   + Insert function ilm_settings::applet_ilm for create APPLET html tag.
 *   + Insert function ilm_settings::applet_filetime for get modified date of iLM file.
 *   + Change tag APPLET in function ilm_settings::view_ilm.
 * - v 3.5 2013/08/26
 *   + Fix bug in download package iassign without answers (iassign::report).
 * - v 3.4 2013/08/23
 *   + Fix bug in export package iassign.
 * - v 3.3 2013/08/22
 *   + Insert functions for export users answer in iassign (iassign::export_file_answer, iassign::export_package_answer, iassign::view_iassign_current, iassign::report).
 *   + Insert function for rename iassign file (ilm_manager::rename_file_ilm, ilm_manager::view_files_ilm).
 * - v 3.2 2013/08/21
 *   + Change title link with message for get file for donwload file (ilm_manager::view_files_ilm).
 *   + Change functions for import files for ilm_manager.php.
 *   + Create static utils class for functions system utils (iassign_utils::format_filename, iassign_utils::version_filename).
 * - v 3.1 2013/08/15
 *   + Change return file selected (ilm_manager::add_ilm).
 *   + Insert functions for import files, export files and remove selected files (ilm_manager::view_files_ilm, ilm_manager::import_files_ilm, ilm_manager::export_files_ilm, ilm_manager::delete_selected_ilm).
 * - v 3.0 2013/08/02
 *   + Insert link for view informations of iLMs in teacher view, same screen of admin view but wiht some features hide (ilm_settings::list_ilm, ilm_settings::view_ilm, iassign::view_iassigns).
 * - v 2.9 2013/08/01
 *   + Fix bugs in functions ilm_settings::new_file_ilm, ilm_settings::copy_new_version_ilm, ilm_settings::add_edit_copy_ilm.
 * - v 2.8 2013/07/25
 *   + Insert the activity name in header of view (activity::view_dates).
 *   + Set function default iLM in view iLMs versions (ilm_settings::default_ilm and ilm_settings::confirm_default_ilm).
 * - v 2.7 2013/07/24
 *   + Create link previous and next for student view in one activity (activity::view_dates).
 *   + Fix bugs for view error in iLM not on DB in function iassign::view_iassign_current.
 * - v 2.6 2013/07/23
 *   + Fix bugs for view files in function ilm_manager::view_files_ilm.
 *   + Fix bugs for comment on teacher view in function iassign::view_iassign_current.
 * - v 2.5 2013/07/12
 *   + Change iLM settings for accept versions (ilm_settings::new_file_ilm, ilm_settings::new_ilm, ilm_settings::edit_ilm, ilm_settings::copy_new_version_ilm).
 *   + Insert new informations in iLMs table: created date, modified date, author, version, modified date of JAR (ilm_settings::view_ilm).
 *   + Added support for PHP 7.0 Constructors and fallback for previous Moodle Versions (<3.1) - Márcio
 *
 * @author Patricia Alves Rodrigues
 * @author Leônidas O. Brandão
 * @contributor Igor Moreira Félix (2018-)
 * @contributor Márcio de Lima Passos (2014)
 * @version v 4.9.2 2019/03/13
 * @version v 4.8 2016/05/12
 * @package mod_iassign_lib
 * @since 2010/09/27
 * @copyright iMath (http://www.matematica.br) and LInE (http://line.ime.usp.br) - Computer Science Dep. of IME-USP (Brazil)
 *
 * License 
 *  - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
//TODO Review: eliminate iLM JAR under MoodleData? It is necessary to HTML5 packages (like iVProgH5), anyway
//TODO Whenever under HTTPS, verify if MoodleData is working, if it is not, please use iLM JAR under WWW setting $CONF_WWW = 1
//TODO (under HTTPS could fail 'pluginfile', like 'https://saw.atp.usp.br/pluginfile.php/1/mod_iassign/ilm/182563135/iassign/ilm/igeom/5920/iGeom.jar"'
$CONF_WWW = 1; //TODO get iLM (JAR) from WWW, avoiding MoodleData
/**
 * Standard base class for all iAssign
 */
class iassign {
  var $cm;
  var $course;
  var $iassign;
  var $striassign;
  var $striassigns;
  var $context;
  var $activity;
  var $iassign_up;
  var $iassign_down;
  var $action;
  var $iassign_submission_current;
  var $userid_iassign;
  var $bottonPost;
  var $write_solution;
  var $view_iassign;
  // 3.1 update PHP 7.0 compatibility for all moodle versions
  //D public function iassign($iassign, $cm, $course) { self::__construct($iassign, $cm, $course); }
  /// Constructor for the base iassign class
  //  @calledby ./mod/iassign/view.php : $iassigninstance = new iassign($iassign, $cm, $course)
  //  @calledby ./mod/iassign/grade.php : $iassigninstance = new iassign($iassign, $cm, $course);
  //  @calledby ./mod/iassign/renderer.php : return $this->render(new iassign_files($context, $itemid, $filearea));
  function __construct ($iassign, $cm, $course) {
    global $COURSE, $CFG, $USER, $DB;
    $botton = optional_param('botton', NULL, PARAM_TEXT);
    $this->userid_iassign = optional_param('userid_iassign', 0, PARAM_INT);
    if (!is_null($botton))
      $USER->iassignEdit = $botton;
    // The Moodle function 'optional_param(...)' allow to filter GET parameters over a click that launches, e.g., './mod/iassign/view.php&userid_iassign=6'
    // If "$var1 = optional_param('userid_iassign', 'NOT', PARAM_TEXT);", $var1 will be set to 6 (if 'userid_iassign' is not present, $var1 will be set to 'NOT'
    $this->iassign_up = optional_param('iassign_up', 0, PARAM_INT); // if parameter 'iassign_up' does not exists or it is not integer => use 0 as "default"
    $this->iassign_down = optional_param('iassign_down', 0, PARAM_INT);
    $this->iassign_submission_current = optional_param('iassign_submission_current', 0, PARAM_INT);
    $this->write_solution = optional_param('write_solution', 0, PARAM_INT);
    $this->action = optional_param('action', NULL, PARAM_TEXT);
    $this->cm = $cm;
    $this->context = context_module::instance($this->cm->id);
    if (!has_capability('mod/iassign:evaluateiassign', $this->context, $USER->id))
      $this->userid_iassign = $USER->id;
    if ($course) {
      $this->course = $course;
      }
    else if ($this->cm->course == $COURSE->id) {
      $this->course = $COURSE;
      }
    else if (!$this->course = $DB->get_record('course', array('id' => $this->cm->course))) {
      print_error('invalidid', 'iassign');
      }
    $this->coursecontext = context_course::instance($this->course->id);
    if ($iassign) {
      $this->iassign = $iassign;
      }
    else if (!$this->iassign = $DB->get_record('iassign', array('id' => $this->cm->instance))) {
      print_error('invalidid', 'iassign');
      }
    $USER->context = context_module::instance($this->cm->id);
    $USER->cm = $this->cm->id;
    $this->iassign->cmidnumber = $this->cm->idnumber; // compatibility with modedit ia obj
    $this->iassign->courseid = $this->course->id; // compatibility with modedit ia obj
    $this->context = context_module::instance($this->cm->id);
    $this->striassign = get_string('modulename', 'iassign');
    $this->striassigns = get_string('modulenameplural', 'iassign');
    $this->return = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $this->cm->id;
    $this->bottonPost = 0;
    $this->view_iassign = optional_param('action', false, PARAM_BOOL);
    $this->activity = new activity(optional_param('iassign_current', NULL, PARAM_TEXT));
    $this->view();
    } // function __construct($iassign, $cm, $course)
  /// Show iAssign by using the security filter (temporary data on '*_iassign_security')
  //  This method provides the page to view each iLM with any interactive activity
  //  @calledby view.php : $iassigninstance->view($id); that instantiate 'iassign' (constructor above)
  function view () { // This is an standard function for each iAssign instance (to not use parameters, it is ignored)
    global $USER, $DB, $OUTPUT;
    // $iassign_statementid = $this->iassign->iassign_statementid;
    if ($this->activity!=null && $this->activity->get_activity()!=null && isset($this->activity->get_activity()->id))
      $iassign_statementid = $this->activity->get_activity()->id;
    else
      $iassign_statementid = "";
    // '$this' has : iassign Object = cm ; course ; iassign ; striassign ; striassigns ; context ; activity ; iassign_up ; iassign_down ; action ; ... ; coursecontext ; return
    // If this user has no capability to View 'iassign': stop here
    require_capability('mod/iassign:view', $this->context);
    // Trigger module view event
    $event = \mod_iassign\event\course_module_viewed::create(array(
      'objectid' => $this->iassign->id,
      'context' => $this->context
      ));
    $event->add_record_snapshot('course', $this->course);
    $event->trigger();
    if ($this->action) { // when student do/redo activity or teacher see student solution
      $this->action(); // calls '$this->view_iLM();'
      }
    else {
      print $OUTPUT->header();
      $this->view_iassigns(); // show the iLM with the content
      print $OUTPUT->footer();
      }
    // Security: delete all records with an error loading iLM - 'iassign_security : id iassign_statementid userid file timecreated view'
    //D $DB->delete_records("iassign_security", array("userid" => $USER->id, "view" => 1));
    //D $iassign_iLM_security = $DB->get_record("iassign_security", array("iassign_statementid" => $iassign_statementid));
    //D foreach ($iassign_iLM_security as $item) { echo $iassign_iLM_security->id . " ; " . $iassign_iLM_security->iassign_statementid . " ; " . $iassign_iLM_security->userid . " ; "  . $iassign_iLM_security->timecreated . " ; " .  $iassign_iLM_security->view . " ; " . $iassign_iLM_security->file . "
';
    
    if ($previous) {
      $url_prev = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=open_individual_submission&" . $param_aux . "&item=" . $previous->id;
      print '◀  '.get_string('all_submissions_manager_previous', 'iassign').'◀  '.get_string('all_submissions_manager_previous', 'iassign').''.get_string('all_submissions_manager_next', 'iassign').' ▶  ';
    } else {
      print ''.get_string('all_submissions_manager_next', 'iassign').' ▶  ';
    }
    $statement = $DB->get_record('iassign_statement', array('id' => $atual_submission->iassign_statementid));
    print ilm_settings::build_ilm_tags($statement->iassign_ilmid, ['type' => "view", 'Proposition' => $url]);
    print ' ';
  }
  /// Manage all submissions in exercises to table iassign_allsubmissions
  function all_submissions () {
    global $COURSE, $CFG, $USER, $DB, $OUTPUT, $PAGE;
    
    if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
      exit;
    
    print $OUTPUT->header();
    $id = $this->cm->id;
    $param_aux = "id=" . $id . "&userid_iassign=" . $USER->id;
    $enderecoPOST = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
    print '
    
      
        
          
          '
        . get_string('all_submissions_manager_confirm', 'iassign')    
        . '
          
         
       
     ';
    print '
        ';
    // Get list of exercises in the instance:
    $iassignid = optional_param('iassignid', NULL, PARAM_TEXT);
    $exercises_list = $DB->get_records("iassign_statement", array("iassignid" => $iassignid));
    
    print '' . get_string('all_submissions_manager', 'iassign') . '';
    print ' ';
    // Get list of enrolled students:
    $params = array('shortname' => 'student');
    $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params);
    $context = context_course::instance($this->course->id);
    $params = array('contextid' => $context->id, 'roleid' => $role->id);
    $students_list = $DB->get_records_sql(
        "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .  " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " . " ORDER BY a.firstname ASC,a.lastname ASC", $params);
    print '';
    foreach ($exercises_list as $exercise) {
      $submissions_list = $DB->get_records("iassign_allsubmissions", array("iassign_statementid" => $exercise->id));
      
      $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $exercise->iassign_ilmid));
      print '
        
 ' . count($submissions_list) . ' '
          . ' ';
      print '
';
      foreach ($students_list as $student) {
        $total = 0;
        $student_submissions = [];
        foreach ($submissions_list as $submission) {
          if ($submission->userid == $student->userid) {
            $student_submissions[] = $submission;
            $total ++;
          }
        }
        print '
              ' . $total . ' '
              . '  ';
        print '
';
        print '
                
                  # 
                  '.get_string('all_submissions_manager_date', 'iassign').' 
                  '.get_string('grade_student', 'iassign').' 
                  '.get_string('config_param_actions', 'iassign').' 
                 
               ';
        
        $i = 1;
        foreach ($student_submissions as $individual) {
          $date = new DateTime();
          $date->setTimestamp($individual->timecreated);
          print '';
          print ''.$i++.' ';
          print ''.date_format($date, 'd/m/Y H:i:s').' ';
          print ''.$individual->grade.' ';
          print '
             ';
          print ' ';
        }
        print ' 
';
        
        //print '
Item 1.1.1 ';
        print '
';
      }
      print '
';
    }
    print '
' . get_string('auto_evaluate_select', 'iassign') . '
';
    print '
      
        '.get_string('auto_evaluate_start', 'iassign').' 
        '.get_string('auto_evaluate_stop', 'iassign').' 
        
        
        ' + "\n";
    print ' ';
    print '
    '; //
    print '';
    print '
            
              
                '.get_string('students', 'iassign').' 
               
             
            '; // "
    foreach ($students_list as $student) {
      $user = $DB->get_record('user', array('id' => $student->userid));
      $avatar = new user_picture($user);
      $avatar->courseid = $COURSE->id;
      $avatar->link = true;
      print
            '
              ' . $OUTPUT->render($avatar) . ' ' . $user->firstname . ' ' . $user->lastname . '  
             '; // "
    }
    print ' 
            
'; // "
    print $OUTPUT->footer();
  } // function auto_evaluate()
  /// This method moves an iAssign activity
  function move_activity () {
    global $USER, $CFG, $DB, $COURSE;
    $iassign_id = optional_param('iassign_current', NULL, PARAM_TEXT);
    $iassign_destiny = optional_param('iassign_destiny', NULL, PARAM_TEXT);
    $update_entry_new_iassign = new stdClass();
    $update_entry_new_iassign->id = $iassign_id;
    $update_entry_new_iassign->iassignid = $iassign_destiny;
    // Update the iassign_statement
    $DB->update_record("iassign_statement", $update_entry_new_iassign);
    // Update file context
    $c_i = context_module::instance($this->cm->id);
    $ret = get_coursemodule_from_id("iassign", $c_i->instanceid);
    $course_module_info = $DB->get_record("course_modules", array("course" => $COURSE->id, "module" => $ret->module, "instance" => $iassign_destiny));
    $path_module = context_course::instance($COURSE->id);
    $context_file = $DB->get_record_sql('SELECT * FROM {context} WHERE instanceid="' . $course_module_info->id . '" AND path like "'.$path_module->path.'%"');
    if ($context_file) {
      $id_context = $context_file->id;
      $iassign_statement = $DB->get_record("iassign_statement", array("id" => $iassign_id));
      $fs = get_file_storage(); // Get reference to all files in Moodle data
      $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->file);
      foreach ($files as $value) {
        if ($value->get_filename() != ".") {
          $last_id = $DB->get_record_sql('SELECT itemid FROM {files} WHERE component="mod_iassign" ORDER BY itemid DESC LIMIT 1')->itemid;
          $newfile = $fs->create_file_from_storedfile(array('contextid' => $id_context, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_id), $value);
          $updateentry = new stdClass();
          $updateentry->id = $iassign_id;
          $updateentry->file = $newfile->get_itemid();
          $DB->update_record("iassign_statement", $updateentry);
          $fs->delete_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $value->get_itemid());
          break;
          }
        }
      }
    $this->return_home_course('moved_activity');
    exit;
    }
  /// This method duplicates an iAssign activity
  function duplicate_activity () {
    global $USER, $CFG, $COURSE, $DB, $OUTPUT;
    $id = $this->cm->id;
    $iassignid = optional_param('iassign_current', NULL, PARAM_TEXT);
    $context = context_module::instance($this->cm->id);
    $contextuser = context_user::instance($USER->id);
    // Get the the iAssign acitivity to be duplicated
    $iassign_statement = $DB->get_record("iassign_statement", array("id" => $iassignid));
    // Remove the current id of activity
    $iassign_statement->id=0;
    // Get the information about current author, and add this information in author_modified field
    $author = $DB->get_record("user", array('id' => $USER->id));
    $iassign_statement->author_modified_name = $author->firstname . ' ' . $author->lastname;
    $iassign_statement->author_modified = $iassign_statement->author_modified_name;
    // Store the activity in the table
    if ($id_ = $DB->insert_record("iassign_statement", $iassign_statement)) {
      // Duplicate activity file
      $fs = get_file_storage(); // Get reference to all files in Moodle data
      $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement->file);
      foreach ($files as $value) {
        if ($value->get_filename() != ".") {
          $last_id = $DB->get_record_sql('SELECT itemid FROM {files} WHERE component="mod_iassign" ORDER BY itemid DESC LIMIT 1')->itemid;
          $newfile = $fs->create_file_from_storedfile(array('contextid' => $context->id, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => ($last_id + 1)), $value);
          $updateentry = new stdClass();
          $updateentry->id = $id_;
          $updateentry->file = $newfile->get_itemid();
          // Update the duplicated iLM iAssign with new file id
          $DB->update_record("iassign_statement", $updateentry);
          }
        }
      }
    // log event --------------------------------------------------------------------------------------
    iassign_log::add_log('duplicate_iassign_exercise', 'name: ' . $author->firstname, $id_, $this->cm->id);
    $this->return_home_course('duplicated_activity');
    exit;
    }
  
  // / This method gets the content from comment and register it
  function add_comment () {
    global $USER, $DB;
    $submission_comment = optional_param('submission_comment', NULL, PARAM_TEXT);
    $comment = false;
    if ($submission_comment)
      $comment = $this->write_comment_submission();
    $id_submission = optional_param('iassign_submission_current', NULL, PARAM_TEXT);
    if (!$id_submission) {
      $iassign_submission = 
          $DB->get_record("iassign_submission", 
            array(
                "iassign_statementid" => $this->activity->get_activity()->id,
                "userid" => $USER->id
                )
          );
      if ($iassign_submission) {
        $id_submission = $iassign_submission->id;
      }
    }
    echo $this->search_comment_submission($id_submission);
  }
  /// This method gets the content from the iLM and register it
  //  It could be the "exercise template" (teacher) or an answer (student)
  function get_answer () {
    global $USER, $CFG, $DB, $OUTPUT;
    $id = $this->cm->id;
    $submission_comment = optional_param('submission_comment', NULL, PARAM_TEXT);
    $submission_ajax    = optional_param('iLM_ajax', NULL, PARAM_TEXT);
    if ($submission_ajax == 1) ob_start();
    $comment = false;
    if ($submission_comment)
      $comment = $this->write_comment_submission();
    // receives data of iLM using the current activity
    $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $this->activity->get_activity()->iassign_ilmid)); // has automatic evaluation?
    $iassign = $DB->get_record("iassign", array("id" => $this->activity->get_activity()->iassignid)); // activity
    // receives data of submission of current activity
    $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $this->userid_iassign)); // data about student solution
    // receives post and get
    $iLM_PARAM_ActivityEvaluation = optional_param('iLM_PARAM_ActivityEvaluation', 0, PARAM_INT); // 1 - activity evaluated as correct / 0 - activity evaluated as incorrect
    $iLM_PARAM_RealGrade = optional_param('iLM_PARAM_RealGrade', 0, PARAM_FLOAT);
    //2016/02/16: IMPORTANTE trocar formatador para "nao formatado", pois esta destruindo o conteudo do arquivo
    $iLM_PARAM_ArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_RAW); // answer file (ATTENTION: do not change format, use RAW in order to ensure the correct content)
    $MA_POST_Info = optional_param('MA_POST_Info', NULL, PARAM_FORMAT);
    $MA_POST_SystemData = optional_param('MA_POST_SystemData', NULL, PARAM_FORMAT);
    $return_get_answer = optional_param('return_get_answer', 0, PARAM_INT);
    $msg = '';
    if ($this->activity->get_activity()->store_all_submissions == 1) {
      $newentry = new stdClass();
      $newentry->iassign_statementid = $this->activity->get_activity()->id;
      $newentry->userid = $this->userid_iassign;
      $newentry->timecreated = time();
      $newentry->grade = round($iLM_PARAM_RealGrade, 2);
      $newentry->answer = $iLM_PARAM_ArchiveContent;
      $DB->insert_record("iassign_allsubmissions", $newentry);
    }
    // Feedback
    // Activity status: 0 => not post 1; => post; 2 => evaluated as incorrect; 3 => evaluated as correct
    $str_action = "view"; // repeat
    if (strtolower($iassign_ilm->name) == "igeom") {
      }
    $title = get_string('evaluate_iassign', 'iassign');
    print $OUTPUT->header();
    print $OUTPUT->box_start();
    // Action = { view ; repeat ; viewsubmission }
    // * 'view' => it is impossible to re-send answer (this is correct with iGeom because its model to ensure security - do not allow the learner's access to the "answer model")
    // * 'repeat' => student explicitly requested to redo the activiy or enter in the last submission under iLM other then iGeom
    // * 'viewsubmission' => teacher/non editing teacher seeing the learner's activity
    $return = $CFG->wwwroot . "/mod/iassign/view.php?action=view&id=" . $id . "&iassign_submission_current=" . $this->iassign_submission_current . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id;
    $return_last = " " . iassign_icons::insert('return_home') . ' ' . get_string('return_iassign', 'iassign') . " ";
    $link_return = " " . iassign_icons::insert('home') . ' ' . get_string('activities_page', 'iassign') . " ";
    print '';
    if ($iLM_PARAM_ArchiveContent == - 1 || empty($iLM_PARAM_ArchiveContent)) { // if ($iLM_PARAM_ActivityEvaluation == -1)
      //TODO alterar aqui???
      $this->write_solution = 0; // necessary in order to take note in Moodle 'grade' system
      // empty_answer_post = No solution was posted.
      if ($comment)
        print '' . $return_last . ' ' . $link_return . ' ' . iassign_icons::insert('feedback_correct') . ' ';
          // log record
          $info = $iassign->name . " - " . $this->activity->get_activity()->name . " - " . get_string('feedback_correct', 'iassign') . " - " . get_string('grade_iassign', 'iassign') . ":" . $grade_student;
          // Trigger module viewed event.
          $event = \mod_iassign\event\submission_created::create(array(
            'objectid' => $this->iassign->id,
            'context' => $this->context,
            'other' => $info
            ));
          $event->add_record_snapshot('course', $this->course);
          $event->trigger();
        } else { // else if (intval($iLM_PARAM_ActivityEvaluation) == 1)
          // Wrong answer...: get_answer_incorrect
          $status = 2;
          $grade_student = $iLM_PARAM_RealGrade; // evaluated as incorrect solution
          $msg = '' . iassign_icons::insert('feedback_incorrect') . ' ';
          // log record
          $info = $iassign->name . " - " . $this->activity->get_activity()->name . " - " . get_string('feedback_incorrect', 'iassign') .
            ' - ' . get_string('grade_iassign', 'iassign') . $grade_student;
          // Trigger module viewed event.
          $event = \mod_iassign\event\submission_created::create(array(
            'objectid' => $this->iassign->id,
            'context' => $this->context,
            'other' => $info
            ));
          $event->add_record_snapshot('course', $this->course);
          $event->trigger();
        } // else if (intval($iLM_PARAM_ActivityEvaluation) == 1)
        // Presents to the learner the result of the automatic evaluate?
        if ($this->activity->get_activity()->show_answer == 0) { // no...
          print '' . iassign_icons::insert('post') . get_string('get_answer', 'iassign') . ' ' . $return_last . ' ' . $link_return . ' ';
          // log record
          $info = $iassign->name . " - " . $this->activity->get_activity()->name . " - " . get_string('get_answer', 'iassign');
          // Trigger module viewed event.
          $event = \mod_iassign\event\submission_created::create(array(
            'objectid' => $this->iassign->id,
            'context' => $this->context,
            'other' => $info
            ));
          $event->add_record_snapshot('course', $this->course);
          $event->trigger();
        } else { // yes!!!
          print ' ' . get_string('auto_result', 'iassign') . ' ' . $return_last . ' ' . $link_return . ' ';
          print $msg;
          }
      } else { // if ($iassign_ilm->evaluate == 1 && $this->activity->get_activity()->automatic_evaluate == 1)
        $status = 1;
        $grade_student = $iLM_PARAM_RealGrade; // iLM not have automatic evaluator
        print ' ' . iassign_icons::insert('post') . get_string('get_answer_post', 'iassign') . ' ';
        print '' . $return_last . ' ' . $link_return . ' ';
        // log record
        $info = $iassign->name . " - " . $this->activity->get_activity()->name . " - " . get_string('get_answer_post', 'iassign');
        $event = \mod_iassign\event\submission_created::create(array(
          'objectid' => $this->iassign->id,
          'context' => $this->context,
          'other' => $info
          ));
        $event->add_record_snapshot('course', $this->course);
        $event->trigger();
        } // if ($iassign_ilm->evaluate == 1)
      } // if ($iLM_PARAM_ActivityEvaluation == -1)
    print ' 
';
    print $OUTPUT->box_end();
    // add or update evaluate
    if ($this->write_solution == 1) {
      $timenow = time();
      // new record
      if (!$iassign_submission) {
        $newentry = new stdClass();
        $newentry->userid = $this->userid_iassign;
        // $newentry->userid = $USER->id;
        $newentry->iassign_statementid = $this->activity->get_activity()->id;
        $newentry->timecreated = $timenow;
        $newentry->timemodified = $timenow;
        $newentry->answer = $iLM_PARAM_ArchiveContent;
        $newentry->grade = round($grade_student, 2);
        $newentry->status = $status;
        $newentry->experiment = 1;
        if (!$newentry->id = $DB->insert_record("iassign_submission", $newentry)) {
          print_error('error_insert', 'iassign');
        } else {
          // Trigger module viewed event.
          $event = \mod_iassign\event\submission_created::create(array(
            'objectid' => $this->iassign->id,
            'context' => $this->context
            ));
          $event->add_record_snapshot('course', $this->course);
          $event->trigger();
          $this->update_grade_student($newentry->userid, $newentry->iassign_statementid, $this->iassign->id);
          }
      } elseif ($iassign_submission->status != 3) {
        $newentry = new stdClass();
        $newentry->id = $iassign_submission->id;
        $newentry->iassign_statementid = $iassign_submission->iassign_statementid;
        $newentry->userid = $iassign_submission->userid;
        $newentry->timecreated = $iassign_submission->timecreated;
        $newentry->timemodified = $timenow;
        $newentry->answer = $iLM_PARAM_ArchiveContent;
        $newentry->grade = round($grade_student, 2);
        $newentry->status = $status;
        $newentry->experiment = $iassign_submission->experiment + 1;
        if (!$DB->update_record("iassign_submission", $newentry)) {
          print_error('error_update', 'iassign');
          //D depurar...
          //D $stringAux = "ia.class.php: ".$iLM_PARAM_ArchiveContent."" . get_string('iassign_update', 'iassign') . "
";
            print $OUTPUT->box_end();
            }
        } elseif ($return_get_answer == 2) {
          print $OUTPUT->box_start();
          print "" . get_string('iassign_cancel', 'iassign') . "
";
          print $OUTPUT->box_end();
        } else {
          print $OUTPUT->box_start();
          print "
     ";
          $param_aux = "action=overwrite&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
          $get_answer_overwrite = $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
          print "";
          print $OUTPUT->box_end();
          }
        }
      } // if ($this->write_solution == 1)
    print $OUTPUT->footer();
    if ($submission_ajax == 1) {
      ob_end_clean();
      print 'OK';
    }
    die();
    } // function get_answer()
  /// Export in file the answer of student.
  function export_file_answer () {
    global $DB;
    $iassign_submission_id = optional_param('iassign_submission_id', NULL, PARAM_INT);
    $iassign_submission = $DB->get_record("iassign_submission", array("id" => $iassign_submission_id));
    $iassign_statement = $DB->get_record("iassign_statement", array("id" => $iassign_submission->iassign_statementid));
    $name = iassign_utils::format_filename(strip_tags($iassign_statement->name));
    $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $iassign_statement->iassign_ilmid));
    $extensions = explode(",", $iassign_ilm->extension);
    $iassign_user = $DB->get_record("user", array("id" => $iassign_submission->userid));
    $username = iassign_utils::format_filename($iassign_user->firstname . ' ' . $iassign_user->lastname);
    $name_answer = $username . '-' . $name . '-' . userdate($iassign_submission->timemodified, '%Y%m%d-%H%M') . '.' . $extensions[0];
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: document/unknown");
    header("Content-Disposition: attachment; filename=\"" . $name_answer . "\";");
    set_time_limit(0);
    print($iassign_submission->answer);
    exit;
    } // function export_file_answer()
  /// Prepare data to static exportation (with course name, iAsssing block name, and exercices names
  //  @calledby $this->export_package_answer()
  //TODO Precisa terminar! //TODO_HTML
  function htmlcode_2_export ($course_fullname, $course_id, $iassign_block_id, $iassign_block_name, $array_iassign_name, $array_iassign_id, $userid, $username) {
    $str_html  = "
 
  " . get_string('pluginname', 'iassign') . " \n"; // 'iAssign: interactive Learning Activities'
    $str_html .= " 
 \n";
    $tam = sizeof($array_iassign_name);
    for ($ii=0; $ii<$tam; $ii++) {
      //TODO_HTML completar construir HTML com titulo do exercicio e talvez legar com HTML para abri-lo com o iMA
      }
    $str_html .= "
 \n";
    return $str_html;
    }
  /// Export an package (zip) with all answer of students
  //  @calledby $this->action()
  function export_package_answer () {
    global $DB, $CFG, $OUTPUT;
    $iassign_id = optional_param('iassign_id', NULL, PARAM_INT); // ID of iAssign block of activities
    $iassign = $DB->get_record("iassign", array("id" => $iassign_id)); // all data from this block of iAssign activities (table '*_iassign')
    $iassign_block_name = iassign_utils::format_filename($iassign->name);
    //TODO_HTML $this->course: [fullname] => Curso teste [shortname] => cursoteste1 [idnumber]
    //TODO_HTML //echo "/temp/files/
    // If temporary directory under Moodle data does not exists => create it
    if (!is_dir($diretorio)) {
      mkdir($diretorio, 0755, true); // permissions: drwxr-xr-x
      // created directory with permissions to be seen by everyone
      }
    $zip_filename = $diretorio . 'package_iassign_' . $username . '-' . $iassign_block_name . '_' . $current_date . '.zip';
    if (!is_writable($diretorio)) { // send a highlighted message!
      print_error('error_answer_export_dir', 'iassign'); // Error: the target directory has no permission! Please, send this message to the Moodle admin!
      exit; // just in case
      }
    //MOOC 2014: $zip_filename = $CFG->dataroot . '/temp/ilm-' . iassign_utils::format_pathname($iassign_ilm->name . '-v' . $iassign_ilm->version) . '.ipz';
    $zip = new zip_archive(); // create ZIP
    $zip->open($zip_filename); // open ZIP using the temporary file/directory
    $iassign_statements = $DB->get_records("iassign_statement", array("iassignid" => $iassign_id));
    //TODO $vet_exerc_title = array(); //TODO to be used with 'htmlcode_2_export(...)' - missing: build the HTML and directories linking files
    $vet_student_filename = array();
    $vet_student_content = array();
    foreach ($iassign_statements as $iassign_statement) {
      $activity_name = iassign_utils::format_filename(strip_tags($iassign_statement->name));
      $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $iassign_statement->iassign_ilmid));
      //TODO_HTML $vet_exerc_title[] = $iassign_statement->name;
      $extensions = explode(",", $iassign_ilm->extension);
      $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $iassign_statement->id, "userid" => $userid));
      if ($iassign_submission) {
        $timemodified = '-' . userdate($iassign_submission->timemodified, '%Y_%m_%d_%H_%M');
        $student_content = $iassign_submission->answer;
        $student_filename = iassign_utils::format_filename($activity_name) . $timemodified . '.' . $extensions[0];
        $resp = $zip->add_file_from_string($student_filename, $student_content); // add file to ZIP
        //$resp = $zip->add_file_from_string($path_dir . $one_file, $destination . $ds . $one_file); //2 Esta versao funciona!
        if ($resp) {
          $vet_student_filename[] = $student_filename;
          $vet_student_content[] = $student_content;
          }
        else { // send a highlighted message!
          print_error('error_answer_export_file', 'iassign'); // Error: fail to insert content to the file
          exit; // just in case
          }
	
        }
      }
    $zip->close(); // close the ZIP file
    if (count($vet_student_filename)>0) {
      $vet_result = iassign_utils::register_temporary_file($zip_filename, $vet_student_filename, $vet_student_content);
      $result_tf = $vet_result[0];
      $filenameZip = $vet_result[1]->filename;
      }
    iassign_utils::download_file($zip_filename); // download the ZIP file
    exit;
    } // function export_package_answer()
  /// Editing status of interactive activities
  function edit_status () {
    global $USER, $DB, $OUTPUT;
    $newentry = new stdClass();
    $newentry->id = $this->iassign_submission_current;
    $newentry->status = optional_param('return_status', 0, PARAM_INT);
    $iassign_submission = $DB->get_record('iassign_submission', array('id' => $this->iassign_submission_current));
    if ($iassign_submission->status != 0 && $newentry->status == 0)
      $newentry->status = $iassign_submission->status;
    $newentry->teacher = $USER->id;
    if (!$DB->update_record('iassign_submission', $newentry))
      print_error('error_update', 'iassign');
    else {
      // Trigger module viewed event.
      $event = \mod_iassign\event\submission_updated::create(array(
        'objectid' => $this->iassign->id,
        'context' => $this->context
        ));
      $event->add_record_snapshot('course', $this->course);
      $event->trigger();
      $this->action = 'viewsubmission';
      $this->view_iassign_current();
      } // if (!$DB->update_record('iassign_submission', $newentry))
    } // function edit_status()
  /// Editing grade of interactive activities
  function edit_grade () {
    global $USER, $DB, $OUTPUT;
    $newgrade = optional_param('return_grade', 0, PARAM_INT);
    if ($newgrade && $newgrade >= 0) {
      $newentry = new stdClass();
      $newentry->id = $this->iassign_submission_current;
      $newentry->grade = optional_param('return_grade', 0, PARAM_INT);
      $newentry->teacher = $USER->id;
      if (!$DB->update_record('iassign_submission', $newentry))
        print_error('error_update', 'iassign');
      else {
        // Trigger module viewed event.
        $event = \mod_iassign\event\submission_updated::create(array(
          'objectid' => $this->iassign->id,
          'context' => $this->context
          ));
        $event->add_record_snapshot('course', $this->course);
        $event->trigger();
        }
      } // if ($newgrade >= 0)
    $this->action = 'viewsubmission';
    $this->view_iassign_current();
    }
  /// Add or Edit interactive activities
  //  @calledby $this->action()
  function add_edit_iassign () {
    global $USER, $CFG, $COURSE, $DB, $OUTPUT;
    require_once('iassign_form.php');
    $id = $this->cm->id;
    $iassignid = $this->iassign->id;
    $param = new stdClass();
    $param->action = $this->action; // oculto
    $param->id = $id; // oculto
    $COURSE->cm = $id;
    $COURSE->iassignid = $iassignid;
    $COURSE->iassign_file_id = NULL;
    $context = context_module::instance($this->cm->id);
    $contextuser = context_user::instance($USER->id);
    $component = 'mod_iassign';
    $filearea = 'exercise';
    if (!empty($this->iassign_current))
      $COURSE->iassign_id = $this->iassign_current;
    else
      $COURSE->iassign_id = 0;
    $total_of_activities = 0; // get the total number of activities in this iAssign block
    if ($this->action == 'add') {
      $iassign_data = $DB->get_record("iassign", array('id' => $iassignid));
      $params = array('iassignid' => $iassignid);
      $query_str = "SELECT s.id, s.name, s.dependency FROM {iassign_statement} s WHERE s.iassignid = :iassignid ORDER BY s.position ASC";
      $iassign_statement = $DB->get_records_sql($query_str, $params); //
      $total_of_activities = count($iassign_statement);
      $param->iassignid = $iassignid;
      $param->name = "";
      $param->oldname = "";
      $param->type_iassign = 3;
      $param->proposition = "";
      $author = $DB->get_record("user", array("id" => $USER->id));
      $param->author_name = $author->firstname . ' ' . $author->lastname;
      $param->author_modified_name = $author->firstname . ' ' . $author->lastname;
      $param->author = $param->author_name;
      $param->author_modified = $param->author_modified_name;
      $COURSE->iassign_list = array();
      $param->iassign_list = array();
      if ($iassign_statement) {
        foreach ($iassign_statement as $iassign) {
          $iassignid = $iassign->id;
          $param->iassign_list[$iassignid] = 0;
          $COURSE->iassign_list[$iassignid] = new stdClass();
          $COURSE->iassign_list[$iassignid]->id = $iassignid;
          $COURSE->iassign_list[$iassignid]->name = $iassign->name;
          $COURSE->iassign_list[$iassignid]->enable = 1;
          } // foreach ($iassign_statement as $iassign)
        }
      $param->iassign_ilmid = 0;
      //echo "locallib.php: add_edit_iassign(): add, 'format' => '')
        //2019  $param->proposition = $iassign_statement_current->proposition;
        $param->proposition = array('text' => $iassign_statement_current->proposition, ''); //'format' => $instance->introformat
        $param->author_name = $iassign_statement_current->author_name; // oculto
        $param->author = $iassign_statement_current->author_name;
        $author = $DB->get_record("user", array('id' => $USER->id));
        $param->author_modified_name = $author->firstname . ' ' . $author->lastname;
        $param->author_modified = $param->author_modified_name;
        $dependency = explode(';', $iassign_statement_current->dependency);
        $param->iassign_list = array();
        $str_query = "SELECT * FROM {iassign_statement} s WHERE s.iassignid = '$iassignid' AND s.id!='$iassign_statement_current->id' AND s.dependency!=0";
        $iassign_statement_dependency = $DB->get_records_sql($str_query);
        $array_dependency = array();
        $subdependency = "";
        $sub_subdependency = "";
        // dependent on this exercise
        if ($iassign_statement_dependency) {
          $subdependency .= $this->search_dependency($iassign_statement_current->id, $iassign_statement_dependency);
          // to whom this exercise depends
          foreach ($inter as $tmp)
            $sub_subdependency .= $this->search_sub_dependency($tmp);
          $list_dependency = $subdependency . $sub_subdependency;
          $array_dependency = explode(";", $list_dependency);
          }
        // Get all activity with the same iLM - iassignid
        $str_query = "SELECT id, name FROM {iassign_statement} s WHERE s.iassignid = '$iassignid' AND s.id!='$iassign_statement_current->id' ORDER BY s.position ASC";
        $all_others_iassign_statement = $DB->get_records_sql($str_query);
        $total_of_activities = count($all_others_iassign_statement) + 1;
        $inter = array();
        if ($all_others_iassign_statement) {
          foreach ($all_others_iassign_statement as $iassign)
            if (in_array($iassign->id, $dependency))
              $inter[] = $iassign->id;
          }
        if ($all_others_iassign_statement) {
          foreach ($all_others_iassign_statement as $iassign) {
            $iassignid = $iassign->id;
            $COURSE->iassign_list[$iassignid] = new stdClass();
            $COURSE->iassign_list[$iassignid]->name = $iassign->name;
            $COURSE->iassign_list[$iassignid]->id = $iassignid;
            if (in_array($iassignid, $dependency))
              $param->iassign_list[$iassignid] = 1;
            else
              $param->iassign_list[$iassignid] = 0;
            if (in_array($iassignid, $array_dependency))
              $COURSE->iassign_list[$iassignid]->enable = 0;
            else
              $COURSE->iassign_list[$iassignid]->enable = 1;
            } // foreach ($all_others_iassign_statement as $iassign)
          }
        $param->iassign_ilmid = $iassign_statement_current->iassign_ilmid;
        $param->fileold = 0;
        $param->file = 0; //TODO Criar campo 'iassign_statement.filesid' para registar 'files.id' - abaixo faz '$file->get_id();'
        $param->filename = '';
        $param->grade = $iassign_statement_current->grade;
        $param->timecreated = $iassign_statement_current->timecreated; // oculto
        $param->timeavailable = $iassign_statement_current->timeavailable;
        $param->timedue = $iassign_statement_current->timedue;
        $param->preventlate = $iassign_statement_current->preventlate;
        $param->test = $iassign_statement_current->test;
        $param->special_param1 = $iassign_statement_current->special_param1;
        $param->position = $iassign_statement_current->position; // oculto
        $param->visible = $iassign_statement_current->visible;
        $param->max_experiment = $iassign_statement_current->max_experiment;
        $param->automatic_evaluate = $iassign_statement_current->automatic_evaluate;
        $param->show_answer = $iassign_statement_current->show_answer;
        $fs = get_file_storage(); // Get reference to all files in Moodle data
        $files = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->file);
        if ($files) {
          foreach ($files as $file) {
            if ($file->get_filename() != '.') {
              $param->filename = $file->get_filename();
              $param->file = $file->get_id();
              $param->fileold = $file->get_id();
              $COURSE->iassign_file_id = $file->get_id();
              }
            }
          }
        //TODO //MOOC2014 -- inicio
        //D  $iassign_ilm_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $iassign_statement_current->id));
        //D  if ($iassign_ilm_configs) {
        //D  foreach ($iassign_ilm_configs as $iassign_ilm_config)
        //D  $param->{'param_'.$iassign_ilm_config->iassign_ilm_configid} = $iassign_ilm_config->param_value;
        //D  } //MOOC2014 -- fim
        } // if ($this_activity != null)
      } // elseif ($this->action == 'edit')
    // Get all iActivity in the iAssign block with id = $this->iassign->id
    // It could be used to define dependency of the current activity (considering those list $iassign_list)
    $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array($this->iassign->id), 'position ASC');
    //D echo "locallib.php: add_edit_iassign(): this->iassign->id=" . $this->iassign->id . "' with 'optgroup' by hand! But MoodleForm clear/do not register the 'iassign_ilmid'
        //TODO Then get it directly from the form data $_POST!
        //TODO See: 'iassign_form.php' field 'iassign_ilmid' and here 'function new_iassign($param)'
        if (!isset($result->iassign_ilmid) && isset($_POST['iassign_ilmid']))
          $result->iassign_ilmid = $_POST['iassign_ilmid'];
        $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $result->iassign_ilmid));
        if ($this->action == 'add') {
          $iassign_statement_name = $DB->get_records('iassign_statement', array('iassignid' => $result->iassignid, 'name' => $result->name));
          if ($iassign_statement_name) {
            $this->return_home_course('error_iassign_name');
            die();
            }
          // Really insert the new iActivity in table '*_iassign_statement'
          $iassignid = $this->activity->new_iassign($result); // class activity : function new_iassign($param)
          $this->activity->add_calendar($iassignid); ///line 3584 of /mod/iassign/locallib.php: call to calendar_event::create()
          // Trigger module viewed event.
          $event = \mod_iassign\event\iassign_created::create(array(
            'objectid' => $iassignid,
            'context' => $context
            ));
          $event->add_record_snapshot('course', $this->course);
          $event->trigger();
          $this->return_home_course('iassign_add');
          } // if ($this->action == 'add')
        elseif ($this->action == 'edit') {
          $iassignid = $this->activity->update_iassign($result);
          $this->activity->update_calendar($iassignid, $result->oldname);
          // Trigger module viewed event.
          $event = \mod_iassign\event\iassign_updated::create(array(
            'objectid' => $iassignid,
            'context' => $context
            ));
          $event->add_record_snapshot('course', $this->course);
          $event->trigger();
          $this->return_home_course('iassign_update');
          } // elseif ($this->action == 'edit')
        die();
        } // if ($result)
      } // else if ($mform->is_cancelled())
    print $OUTPUT->header();
    $mform->display();
    print $OUTPUT->footer();
    die();
    } // function add_edit_iassign()
  /// Search for dependencies
  function search_dependency ($search_iassing_id, $iassign_statement) {
    global $DB, $OUTPUT;
    $dependency = "";
    if ($iassign_statement)
      foreach ($iassign_statement as $iassign) {
        $inter_dependency = explode(';', $iassign->dependency);
        if (in_array($search_iassing_id, $inter_dependency)) {
          $dependency .= $iassign->id . ";";
          $dependency .= $this->search_dependency($iassign->id, $iassign_statement);
          } // if (in_array($search_iassing_id, $inter_dependency))
        } // foreach ($iassign_statement as $iassign)
    return $dependency;
    }
  /// Search for "sub"dependency
  function search_sub_dependency ($search_iassing_id) {
    global $DB, $OUTPUT;
    $iassign_statement = $DB->get_record("iassign_statement", array("id" => $search_iassing_id));
    $dependency = "";
    if ($iassign_statement) {
      $inter_dependency = explode(';', $iassign_statement->dependency);
      foreach ($inter_dependency as $tmp) {
        if ($tmp != 0)
          $dependency .= $tmp . ";";
        $dependency .= $this->search_sub_dependency($tmp);
        } // foreach ($inter_dependency as $tmp)
      } // if ($iassign_statement)
    return $dependency;
    }
  // Warning message
  static function warning_message_iassign ($strcode) {
    return "" . get_string($strcode, 'iassign') . "
\n";
    }
  /// Update grade of iAssign
  //  Called always any iAssign activity is created
  //  @see /mod/iassign/view.php: call to iassign->iassign(): starting point
  static function update_grade_iassign ($iassignid) {
    global $USER, $CFG, $COURSE, $DB, $OUTPUT;
    require_once($CFG->libdir . '/gradelib.php');
    //D $sum_grade = $DB->get_records_sql ( "SELECT SUM(grade) as total
    //D FROM {$CFG->prefix}iassign_statement s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3" );
    //TODO: REVIEW: wich one is more efficienty, '$DB->get_records' geting objects or '$DB->get_records' with 'foreach'?
    // Each iAssign item is associated with one item on the "gradebook"
    // Sum all '*_iassign_statement' associated with one item in '*_grade_items': iassignid AND type_iassign=3
    //$sum_grade = 0;
    //$grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3));
    //foreach($grade as $tmp) {
    //    $sum_grade += $tmp->grade;
    // }
    //1 Solution 1
    //1 $grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3)); //1
    //1 $sum_grade = 0; //1
    //1 foreach ($grade as $tmp) { $sum_grade += $tmp->grade; } //1
    //2 Solution 2
    $array_sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total FROM {iassign_statement} s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3"); //2
    //2 foreach ($array_sum_grade as $array_item) { $sum_grade = $array_item->total; break; } // nao necessario, basta 'key(...)' abaixo
    if (key($array_sum_grade))
      $sum_grade = key($array_sum_grade); //2
    else
      $sum_grade = 0; //2
    $grade_iassign = $DB->get_record("iassign", array("id" => $iassignid));
    $grades = NULL;
    $params = array('itemname' => $grade_iassign->name);
    $params['iteminstance'] = $iassignid;
    $params['gradetype'] = GRADE_TYPE_VALUE;
    //2016 if ($sum_grade != 0) {
    $params['grademax'] = $sum_grade;
    $params['rawgrademax'] = $sum_grade;
    //2016 } else { $params['grademax'] = 0; $params['rawgrademax'] = 0; }
    $params['grademin'] = 0;
    // @calls /lib/gradelib.php: call to grade_item->insert()
    //TODO: is there any error here in Moodle version 3.0?
    //TODO: Incorrect property 'grademax' found when inserting grade object
    //TODO: line 899 of /mod/iassign/locallib.php: call to grade_update()
    grade_update('mod/iassign', $grade_iassign->course, 'mod', 'iassign', $iassignid, 0, $grades, $params);
    }
  /// Update grade of student
  function update_grade_student ($userid, $iassign_statementid, $iassignid) {
    global $CFG, $DB, $OUTPUT;
    require_once($CFG->libdir . '/gradelib.php');
    $grade_iassign = $DB->get_record('iassign', array('id' => $iassignid));
    // Review all the student submission for this iAssign activity
    $grade_iassign_statements = $DB->get_records('iassign_statement', array('iassignid' => $iassignid));
    $total_grade = 0;
    foreach ($grade_iassign_statements as $grade_iassign_statement) {
      $iassign_submission = $DB->get_record('iassign_submission', array('iassign_statementid' => $grade_iassign_statement->id, 'userid' => $userid));
      if ($iassign_submission)
        $total_grade += $iassign_submission->grade;
      } // foreach ($grade_iassign_statements as $grade_iassign_statement)
    //D $sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total
    //D  FROM {$CFG->prefix}iassign_statement s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3" );
    //TODO: REVIEW: wich one is more efficienty, '$DB->get_records' geting objects or '$DB->get_records' with 'foreach'?
    //1 $sum_grade = 0; $grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3));
    //1 foreach ($grade as $tmp) { $sum_grade += $tmp->grade; }
    $array_sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total FROM {iassign_statement} s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3"); //2
    if (key($array_sum_grade))
      $sum_grade = key($array_sum_grade); //2
    else
      $sum_grade = 0; //2
    $grades['userid'] = $userid;
    $grades['rawgrade'] = $total_grade; // sum of all submissions for this iAssign activity
    $params = array('itemname' => $grade_iassign->name);
    $params['iteminstance'] = $iassignid;
    $params['gradetype'] = GRADE_TYPE_VALUE;
    //2016 if ($sum_grade != 0) { // depois eliminar comentario
    $params['grademax'] = $sum_grade;
    $params['rawgrademax'] = $sum_grade;
    //2016 } else { $params['grademax'] = 0; $params['rawgrademax'] = 0; }
    grade_update('mod/iassign', $grade_iassign->course, 'mod', 'iassign', $iassignid, 0, $grades, $params);
    }
  /// Display caption of icons
  function view_legend_icons () {
    global $USER, $CFG, $DB, $OUTPUT;
    $id = $this->cm->id;
    if ($this->action == 'print')
      print '';
    else
      print '';
    print '';
    if ($this->action != 'print')
      print $OUTPUT->help_icon('legend', 'iassign');
    // helpbutton('legend', get_string('legend', 'iassign'), 'iassign', $image = true, $linktext = false, $text = '', $return = false,
    // $imagetext = '');
    print '' . get_string('legend', 'iassign') . ' ';
    print ' ' . iassign_icons::insert('correct') . ' ' . get_string('correct', 'iassign');
    print ' ' . iassign_icons::insert('incorrect') . ' ' . get_string('incorrect', 'iassign');
    print ' ' . iassign_icons::insert('post') . ' ' . get_string('post', 'iassign');
    print ' ' . iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign');
    print ' ' . iassign_icons::insert('comment_unread') . ' ' . get_string('comment_unread', 'iassign');
    if (has_capability('mod/iassign:viewreport', $this->context, $USER->id) && $this->action == 'report') {
      print ' ' . iassign_icons::insert('comment_read') . ' ' . get_string('comment_read', 'iassign');
      print ' ' . "\n";
      if ($this->action != 'print') {
        $link_print = "" . iassign_icons::insert('print') . ' ' . get_string('print', 'iassign') . " ";
        $link_stats = "" . iassign_icons::insert('results') . ' ' . get_string('graphic', 'iassign') . " ";
        $link_export = "" . iassign_icons::insert('export_ilm') . ' ' . get_string('export_csv', 'iassign') . " ";
        $link_auto_evaluate = "" . iassign_icons::insert('correct') . ' ' . get_string('auto_evaluate_reprocess', 'iassign') . " ";
        
        $link_all_submissions_manager = "' . $link_stats . ' ' . "\n";
        print '' . $link_export . ' ' . "\n";
        print '' . $link_auto_evaluate . ' ' . "\n";
        print '' . $link_all_submissions_manager . ' ' . "\n";
        print '' . $link_print . ' ' . "\n";
        } // if ($this->action != 'print')
      print ' 
' . "\n";
      } // if (has_capability('mod/iassign:viewreport', $this->context, $USER->id) && $this->action == 'report')
      elseif (has_capability('mod/iassign:submitiassign', $this->context, $USER->id)) {
        $link_stats = "" . iassign_icons::insert('results') . ' ' . get_string('results', 'iassign') . " ";
        print '' . $link_stats . ' ' . "\n";
        print ' 
' . "\n";
        } // elseif (has_capability('mod/iassign:submitiassign', $this->context, $USER->id))
      else
      print '' . "\n";
    } // function view_legend_icons()
  /// Display activity current
  //  @calledby view() -> action() : when student do/redo activity and teacher see student answer
  function view_iassign_current () {
    global $USER, $CFG, $COURSE, $DB, $OUTPUT, $PAGE;
    $id = $this->cm->id;
    $iassignid = $this->iassign->id;
    $only_one_send_button = 0; // to avoid to put 2 copies of comments (area to send and see comments between teacher and student)
    $iassign = $DB->get_record("iassign", array("id" => $iassignid)); // from table '*_iassign': id course name intro introformat activity_group grade timeavailable timedue preventlate test max_experiment
    // Get data of current activity : table '*_iassign_statement' = id name iassignid type_iassign proposition author_name author_modified_name iassign_ilmid file grade timemodified timecreated ...
    $iassign_statement_activity_item = $this->activity->get_activity(); // search data of current activity: the teacher activity model
    if (!$iassign_statement_activity_item) {
      print $OUTPUT->header();
      \core\notification::error(get_string('activity_not_found', 'iassign'));
      print $OUTPUT->footer();
      exit;
    }
    
    $ilm = new ilm($iassign_statement_activity_item->iassign_ilmid);
    $ilm_name = '';
    if ($ilm->ilm)
      $ilm_name = strtolower($ilm->ilm->name); // class ilm has a unique property ('ilm'), get the iLM name
    // Do not allow the learner resent his solution only is iGeom iLM ("igeom") - why? iGeom acitivity "model answer" is not sent with the learner solution
    $allow_resubmission = (substr($ilm_name, 0, 5) != "igeom" ? 1 : 0);
    // log record
    $info = $iassign->name . ":" . $iassign_statement_activity_item->name;
    //Trigger module viewed event.
    $event = \mod_iassign\event\submission_viewed::create(array(
      'objectid' => $iassign->id,
      'context' => $this->context,
      'other' => $info
      ));
    $event->add_record_snapshot('course', $this->course);
    $event->trigger();
    // Search of iLM data used in the current activity
    $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $iassign_statement_activity_item->iassign_ilmid));
    // Add actual activity to navbar:
    $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $PAGE->navbar->add($iassign_statement_activity_item->name, $actual_link);
    print $OUTPUT->header();
    if ($this->action == 'viewsubmission') {
      if (!empty($this->iassign_submission_current) || $this->iassign_submission_current != 0)
        $iassign_submission = $DB->get_record("iassign_submission", array("id" => $this->iassign_submission_current)); // data about activity current
      else
        $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $this->userid_iassign)); // data about student solution
      } else {
        $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $this->userid_iassign)); // data about student solution
        }
    if ($iassign_submission)
      $this->update_comment($iassign_submission->id);
    $file = $iassign_statement_activity_item->file;
    // 1 when open previous file; 2 when the activity is redone!; 3 when the teacher enter in the activity
    // 1 => locallib.php: view_iassign_current(): action=view will set write_solution=0!!!!!
    // 2 => locallib.php: view_iassign_current(): action=repeat will set write_solution=0!!!!!
    // 3 => locallib.php: view_iassign_current(): action=viewsubmission will set write_solution=0!!!!!
    $this->bottonPost = 0; // hide submit button
    //xxx $this->write_solution = 0; // disable recording solution (however, iVProg allow the learner to edit previou solution)
    $this->view_iassign = false; // disable visualization of activity
    $repeat = "";
    $last_iassign = "";
    $student_answer = "";
    $comment = "";
    // *** Teacher access (view learner's submission)
    if (($this->action != 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id)) {
      //TODO leo Verificar se o correto eh '$this->context' ou '$USER->context' como deixei
      // It is not 'viewsubmission' and it is teacher or 'non editing teacher'?
      // ---> access teacher for test
      if ($iassign_statement_activity_item->type_iassign != 1) // type_iassign=1 => activity of type "example" - not submit button for submission
        $this->bottonPost = 1;
      print $OUTPUT->box('' . get_string('area_specific_teacher', 'iassign') . ' 
');
      $this->activity->view_dates();
      $USER->iassignEdit = $this->bottonPost;
      $this->activity->show_info_iassign();
      if ($iassign_submission) {
        //xxx $param_aux = "action=get_answer&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
        $param_aux = "action=get_answer&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&userid_iassign=" . $USER->id;
      } else {
        //xxx $param_aux = "action=get_answer&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
        $param_aux = "action=get_answer&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&userid_iassign=" . $USER->id;
        }
      $enderecoPOST = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
      //if ($ilm->confirms_jar($iassign_statement_activity_item->file, $iassign_ilm->file_jar, $this->cm->id))
      // Prepare tags to present the iLM
      print $OUTPUT->box($ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true));
      } // if (($this->action != 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
    // *** (end) Teacher access (view the activity)
    // *** Teacher access (view learner's submission to the activity)
    elseif (($this->action == 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id)) {
      // It is teacher or 'nonediting teacher' that can evaluate
      // ----> area teacher evaluate
      $row = optional_param('row', 0, PARAM_INT);
      $column = optional_param('column', 0, PARAM_INT);
      $link_next = iassign_icons::insert('right_disable');
      $link_previous = iassign_icons::insert('left_disable');
      $link_up = iassign_icons::insert('up_disable');
      $link_down = iassign_icons::insert('down_disable');
      // next_activity
      if ($USER->matrix_iassign[$row][$column]->iassign_next != - 1) {
        $url_next = "view.php?action=viewsubmission&id=$id&iassign_submission_current=" . $USER->matrix_iassign[$row][$column + 1]->iassign_submission_current . "&userid_iassign=$this->userid_iassign&iassign_current=" . $USER->matrix_iassign[$row][$column]->iassign_next . "&view_iassign=report&row=" . ($row) . "&column=" . ($column + 1);
        $link_next = "" . (iassign_icons::insert('next_activity')) . " ";
        }
      // previous_activity
      if ($USER->matrix_iassign[$row][$column]->iassign_previous != - 1) {
        $url_previous = "view.php?action=viewsubmission&id=$id&iassign_submission_current=" . $USER->matrix_iassign[$row][$column - 1]->iassign_submission_current . "&userid_iassign=$this->userid_iassign&iassign_current=" . $USER->matrix_iassign[$row][$column]->iassign_previous . "&view_iassign=report&row=" . ($row) . "&column=" . ($column - 1);
        $link_previous = "" . (iassign_icons::insert('previous_activity')) . " ";
        }
      // previous_student
      if ($USER->matrix_iassign[$row][$column]->user_next != - 1) {
        $url_down = "view.php?action=viewsubmission&id=$id&iassign_submission_current=" . $USER->matrix_iassign[$row + 1][$column]->iassign_submission_current . "&userid_iassign=" . $USER->matrix_iassign[$row][$column]->user_next . "&iassign_current=" . $this->activity->get_activity()->id . "&view_iassign=report&row=" . ($row + 1) . "&column=" . ($column);
        $link_down = "" . (iassign_icons::insert('previous_student')) . " ";
        }
      // next_student
      if ($USER->matrix_iassign[$row][$column]->user_previous != - 1) {
        $url_up = "view.php?action=viewsubmission&id=$id&iassign_submission_current=" . $USER->matrix_iassign[$row - 1][$column]->iassign_submission_current . "&userid_iassign=" . $USER->matrix_iassign[$row][$column]->user_previous . "&iassign_current=" . $this->activity->get_activity()->id . "&view_iassign=report&row=" . ($row - 1) . "&column=" . ($column);
        $link_up = "" . (iassign_icons::insert('next_student')) . " ";
        }
      if ($iassign_submission) {
        $student_answer = $iassign_submission->answer;
        }
      $last_iassign = get_string('last_iassign', 'iassign');
      $user_data = $DB->get_record("user", array('id' => $this->userid_iassign));
      // Messages related to due date (and user role)
      $this->activity->view_dates();
      print $OUTPUT->box_start();
      print '' . "\n";
      print '' . get_string('area_available', 'iassign') . ' ' . "\n";
      print '';
      print '' . $link_up . ' ' . $link_previous . ' ' . "\n";
      print '' . $link_next . ' ' . $link_down . ' ' . "\n";
      print '
' . "\n";
      print '  
' . "\n";
      print $OUTPUT->box_end();
      print $OUTPUT->box_start();
      print '' . "\n";
      print '' . "\n";
      print '' . get_string('proposition', 'iassign') . ' ' . "\n";
      print '' . $iassign_statement_activity_item->proposition . '
' . "\n";
      if ($iassign_statement_activity_item->automatic_evaluate == 1)
        $resp = get_string('yes');
      else
        $resp = get_string('no');
      print '' . get_string('automatic_evaluate', 'iassign') . ' ' . $resp . '
' . "\n";
      if ($iassign_statement_activity_item->show_answer == 1)
        $resp = get_string('yes');
      else
        $resp = get_string('no');
      print '' . get_string('show_answer', 'iassign') . ' ' . $resp . '
' . "\n";
      print ' ';
      if ($iassign_statement_activity_item->type_iassign == 3) { // type_iassign=3 => activity of type "exercise" - submit button and automatic evaluation
        print '';
        print '' . get_string('status', 'iassign') . ' ' . "\n";
        // check status of solution sent by the student
        if ($iassign_submission) {
          switch ($iassign_submission->status) {
            case 3 :
              print iassign_icons::insert('correct') . ' ' . get_string('correct', 'iassign') . ' ' . $comment;
              break;
            case 2 :
              print iassign_icons::insert('incorrect') . ' ' . get_string('incorrect', 'iassign') . ' ' . $comment;
              break;
            case 1 :
              print iassign_icons::insert('post') . ' ' . get_string('post', 'iassign') . ' ' . $comment;
              break;
            default :
              print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
              $last_iassign = get_string('no_iLM_PARAM_ArchiveContent', 'iassign');
            } // switch ($iassign_submission->status)
          }
        else { // if ($iassign_submission)
          print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
          $last_iassign = get_string('no_iLM_PARAM_ArchiveContent', 'iassign');
          }
        // update_status
        if ($iassign_submission && $iassign_submission->experiment > 0) {
          $edit_status = $CFG->wwwroot . "/mod/iassign/view.php?action=edit_status&id=" . $id . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id . "&iassign_submission_current=" . $this->iassign_submission_current . "&row=" . ($row) . "&column=" . ($column);
          print " ";
          print "\n";
          print '' . get_string('grade_student', 'iassign') . '  ' . $iassign_submission->grade . "
\n";
          print '' . get_string('grade_iassign', 'iassign') . '  ' . $iassign_statement_activity_item->grade . "
\n";
          $edit_grade = $CFG->wwwroot . "/mod/iassign/view.php?action=edit_grade&id=" . $id . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id . "&iassign_submission_current=" . $this->iassign_submission_current . "&row=" . ($row) . "&column=" . ($column);
          print "
  ";
          print "";
          $url_answer = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . "action=download_answer&iassign_submission_id=" . $iassign_submission->id . "&id=" . $id;
          print '' . get_string('experiment', 'iassign') . '  ' . $iassign_submission->experiment . ' ' . iassign_icons::insert('download_assign') . ' 
';
          print '' . get_string('timemodified', 'iassign') . '  ' . userdate($iassign_submission->timemodified) . '
';
          $teacher = $DB->get_record("user", array('id' => $iassign_submission->teacher));
          if ($teacher)
            print '' . get_string('last_modification', 'iassign') . '  ' . $teacher->firstname . '
' . "\n";
          } // if ($iassign_submission->experiment > 0)
        print ' ';
        } // if ($iassign_statement_activity_item->type_iassign == 3)
      print ' 
';
      print $OUTPUT->box_end();
      $USER->iassignEdit = $this->bottonPost;
      if ($iassign_submission && $allow_resubmission) {
        // Put the iLM to
        print $OUTPUT->box_start();
        print '' . $last_iassign . ' 
';
        //d if ($ilm->confirms_jar ( $iassign_statement_activity_item->file, $iassign_ilm->file_jar, $this->cm->id )) {
        $enderecoPOST = "";
        // Prepare tags to present the iLM
        print $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, false);
        //d } // if ($this->confirms_jar($iassign_statement_activity_item->file, $iassign_ilm->file_jar))
        print $OUTPUT->box_end();
      } else { // if ($iassign_submission && $allow_resubmission) - techer view student answer
        // ATTENTION: exception used by iGeom (exercise with "script")
        $loadTeacherActivity = false; // use 'true' whenever 'special_param1 == 1'
        if (substr($ilm_name, 0, 5)=="igeom") {
          if ($iassign_statement_activity_item->special_param1 == 1) // if 1 => use the teacher activity with some complement from the student (in iGeom = GEO + SCR)
            $loadTeacherActivity = true;
          }
        print $OUTPUT->box('' . $last_iassign . ' 
' . "\n");
        if (!isset($enderecoPOST)) $enderecoPOST = "";
        // Prepare tags to present the iLM
        print $OUTPUT->box($ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $loadTeacherActivity));
        }
      //2020 Eliminei daqui o codigo para colocar o quadro com area para enivar e ler comentarios
      //2020 Estava subordinado ao: elseif (($this->action == 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
      //2020 e com isso NAO permitia aluno ter o quadro para envio!
      //2020 Foi para final dessa funcao 'view_iassign_current()'
    } // elseif (($this->action == 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
    // *** (end) Teacher access (view learner's submission to the activity)
    // *** Student access (view the activity)
    elseif (has_capability('mod/iassign:submitiassign', $USER->context, $USER->id)) {
      // It could be the learner (he could send or resend the activity)
      // ---> access student
      $time_now = time();
      if ($iassign_statement_activity_item->type_iassign == 1) { // type_iassign=1 => activity of type "exemple" - no submit button
        // activity of type example - not submit button for submission
        $this->view_iassign = true;
        //TODO rever esta condicao para iMA que nao fazem autoavaliacao
      } elseif ($iassign_statement_activity_item->type_iassign == 2 && $iassign_ilm->evaluate == 1) {
        // activity of type test - iLM automatic evaluator - submit button for submission
        if ($iassign_statement_activity_item->timeavailable < $time_now && $iassign_statement_activity_item->timedue > $time_now) { // activity within of deadline
          $this->bottonPost = 1;
          $this->view_iassign = true;
        } else
          $this->view_iassign = false;
      } elseif ($iassign_statement_activity_item->type_iassign == 3) { // type_iassign=3 => activity of type "exercise" - submit button and automatic evaluation
        // activity of type exercise (learner can send his answer, if yet open...)
        $this->view_iassign = true;
        if ($iassign_statement_activity_item->timeavailable > $time_now) // due date expired
          $this->view_iassign = false;
        elseif ($iassign_statement_activity_item->timedue > $time_now || $iassign_statement_activity_item->preventlate == 1) { // activity within due date
          $this->bottonPost = 1; // allow the submit button
          // Look at table 'iassign_submission' ('iassign_submission.experiment' is the number of submissions)
          $repeat_title = ' title="' . get_string('repeat_alt', 'iassign') . '" '; // 'Use this button to redo the activity'
          if (!$iassign_submission || $this->action == 'repeat' || ($iassign_submission && $allow_resubmission)) {
            if (!$iassign_submission) {
              $str_iassign_submission_id = '';
              $str_iassign_submission_answer = '';
              }
            else {
              $str_iassign_submission_id = $iassign_submission->id;
              $str_iassign_submission_answer = $iassign_submission->answer;
              }
            if ($this->action != 'repeat') {
              $repeat = ""
                 . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . " \n";
              }
            $this->bottonPost = 1;
            $this->write_solution = 1; // can register his submission
            $student_answer = $str_iassign_submission_answer;
            }
          else { // if (!$iassign_submission || $this->action == 'repeat' || ($iassign_submission && $allow_resubmission))
            // In 'class ilm : view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view)'
            $last_iassign = get_string('last_iassign', 'iassign');
            if ($iassign_submission) {
              $repeat = "" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . " \n";
              $student_answer = $iassign_submission->answer;
            } else {
              $repeat = "" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . " \n";
              }
            }
        } elseif ($iassign_statement_activity_item->test == 1) { // allowed to test after expired due date
          if ($this->action == 'repeat' || ($iassign_submission && $iassign_submission->experiment < 1)) {
            $this->bottonPost = 1;
            $this->write_solution = 0; // if iVProg it is valid to the learner to edit previous solution
          } else {
            $last_iassign = get_string('last_iassign', 'iassign');
            if ($iassign_submission) {
              $repeat = "" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . " \n";
              $student_answer = $iassign_submission->answer;
            } else {
              // Symbol of "redo activity"
              $repeat = "" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . " \n";
              }
            }
          } // elseif ($iassign_statement_activity_item->test == 1)
        elseif ($iassign_statement_activity_item->test == 0)
          $this->view_iassign = false;
        } // elseif ($iassign_statement_activity_item->type_iassign == 3)
      if ($iassign_submission)
        $param_aux = "action=get_answer&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id .
          "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
      else
        $param_aux = "action=get_answer&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
      $enderecoPOST = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
      $this->activity->view_dates();
      if ($this->view_iassign) { // Box 5 to 'proposition' - open
        print $OUTPUT->box_start();
        print '' . "\n";
        print '' . "\n";
        print '' . get_string('proposition', 'iassign') . ' ' . "\n";
        print '' . $iassign_statement_activity_item->proposition . '
' . "\n";
        $flag_dependency = true;
        if ($iassign_statement_activity_item->type_iassign == 3) {
          if ($iassign_statement_activity_item->dependency == 0) {
            print '' . get_string('independent_activity', 'iassign') . ' ' . "\n";
          } else {
            $dependencys = explode(';', $iassign_statement_activity_item->dependency);
            print '' . get_string('dependency', 'iassign') . ' 
' . "\n";
            foreach ($dependencys as $dependency) {
              if ($dependency) {
                $dependencyiassign = $DB->get_record("iassign_statement", array("id" => $dependency));
                $dependencysubmissions = $DB->get_record("iassign_submission", array("iassign_statementid" => $dependencyiassign->id, 'userid' => $USER->id));
                if ($dependencysubmissions) {
                  if ($dependencysubmissions->status == 3)
                    $icon = iassign_icons::insert('correct');
                  elseif ($dependencysubmissions->status == 2) {
                    $icon = iassign_icons::insert('incorrect');
                    $flag_dependency = false;
                  } elseif ($dependencysubmissions->status == 1) {
                    $icon = iassign_icons::insert('post');
                    $flag_dependency = false;
                  } elseif ($dependencysubmissions->status == 0) {
                    $icon = iassign_icons::insert('not_post');
                    $flag_dependency = false;
                    }
                } else {
                  $icon = iassign_icons::insert('not_post');
                  $flag_dependency = false;
                  } // if ($dependencysubmissions)
                print ' ' . $icon . $dependencyiassign->name . '
' . "\n";
                } // if ($dependency)
              } // foreach ($dependencys as $dependency)
            } // if ($iassign_statement_activity_item->dependency == 0)
          } // if ($iassign_statement_activity_item->type_iassign == 3)
        if ($flag_dependency == false) {
          print '' . get_string('message_dependency', 'iassign') . ' ' . "\n";
          $this->view_iassign = false;
          print ' 
' . "\n";
          }
        else {
          $this->view_iassign = true;
          print '' . "\n";
          } // if ($flag_dependency == false)
        if ($this->view_iassign) { // it is already inside 'if ($this->view_iassign)' but the dependency check could had change its value (above)
          if ($iassign_statement_activity_item->type_iassign == 3) { // activity is present only if exercise
            // receiver=1 - message to teacher
            // receiver=2 - message to student
            if ($iassign_submission) {
              $verify_message = $DB->get_record('iassign_submission_comment', array('iassign_submissionid' => $iassign_submission->id, 'return_status' => 0, 'receiver' => 2));
              if ($verify_message)
                $comment = iassign_icons::insert('comment_unread');
              }
            print '';
            print '' . get_string('status', 'iassign') . ' ' . "\n";
            if ($iassign_statement_activity_item->show_answer == 1) {
              // check status of solution sent by the student
              if ($iassign_submission) {
                switch ($iassign_submission->status) {
                  case 3 :
                    print iassign_icons::insert('correct') . ' ' . get_string('correct', 'iassign') . ' ' . $comment;
                    break;
                  case 2 :
                    print iassign_icons::insert('incorrect') . ' ' . get_string('incorrect', 'iassign') . ' ' . $comment;
                    break;
                  case 1 :
                    print iassign_icons::insert('post') . ' ' . get_string('post', 'iassign') . ' ' . $comment;
                    break;
                  default :
                    print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
                    $repeat = "";
                    $last_iassign = "";
                  } // switch ($iassign_submission->status)
              } else {
                  print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
                  $repeat = "";
                  $last_iassign = "";
                }
              if ($iassign_submission && $iassign_submission->experiment > 0) {
                print '' . get_string('grade_student', 'iassign') . ':  ' . $iassign_submission->grade;
                print '  (' . get_string('grade_iassign', 'iassign') . ': ' . $iassign_statement_activity_item->grade . ')
' . "\n";
                print '' . get_string('experiment_student', 'iassign') . '  ' . $iassign_submission->experiment;
                if ($iassign_statement_activity_item->max_experiment == 0)
                  print '  (' . get_string('experiment_iassign', 'iassign') . ' ' . get_string('ilimit', 'iassign') . ')
' . "\n";
                else {
                  print '  (' . get_string('experiment_iassign', 'iassign') . ' ' . $iassign_statement_activity_item->max_experiment . ')' . "\n";
                  if ($iassign_submission->experiment >= $iassign_statement_activity_item->max_experiment) {
                    $repeat = "";
                    $last_iassign .= " " . get_string('attempts_exhausted', 'iassign') . ' ' . "\n";
                    $this->bottonPost = 0;
                    $this->write_solution = 0;
                    }
                  } // else if ($iassign_statement_activity_item->max_experiment == 0)
                print '' . get_string('timemodified', 'iassign') . '  ' . userdate($iassign_submission->timemodified) . '
' . "\n";
                $teacher = $DB->get_record("user", array('id' => $iassign_submission->teacher));
                if ($teacher)
                  print '' . get_string('last_modification', 'iassign') . '  ' . $teacher->firstname . '
' . "\n"; // "
                } // if ($iassign_submission && $iassign_submission->experiment > 0)
              } // if ($iassign_statement_activity_item->show_answer==1)
            else {
              if (!isset($iassign_submission) || $iassign_submission->status == 0) {
                print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
                $repeat = "";
                $last_iassign = "";
              } elseif ($iassign_submission->status == 1) {
                print iassign_icons::insert('post') . ' ' . get_string('post', 'iassign') . ' ' . $comment;
                }
              }
            print ' ';
            } // if ($iassign_statement_activity_item->type_iassign == 3)
          print '' . "\n";
          // Presents the iLM
          print '' . "\n";
          print '';
          print '' . $last_iassign . ' ' . get_string('repeat_msg', 'iassign') . '  ' . "\n"; // If you want to do this activity from the beginning again, use the \"Redo button\".
          print '';
          //D $ilm_name = strtolower($this->ilm->name); //if (substr($ilm_name, 0, 5) == "igeom") ; // is iGeom exercise
          print $repeat; // symbol of "redo activity"
          print ' 
' . "\n";
          //2019 print $OUTPUT->box_end(); // closing Box 5 but sometimes it does reach this point - see it at the end of 'if ($this->view_iassign)'
          $output = '';
          if (!$iassign_ilm) {
            $iassign_ilm = new stdClass();
            $iassign_ilm->file_jar = "";
            }
          $output .= $OUTPUT->box_start();
          $USER->iassignEdit = $this->bottonPost;
          // ---
          // Presents the iLM
          // Prepare tags to present the iLM
          if (!$iassign_submission || $this->action == 'repeat') { // or $iassign_submission->answer==0
            $student_answer = ""; //?
            $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true); // presents iLM and true => see the teacher file
          } elseif ($iassign_submission && $iassign_submission->answer == '0') {
            $student_answer = "";
            $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true); // presents iLM
          } else {
            // When student is redoing his activity
            // ATTENTION: exception used by iGeom (exercise with "script")
            $loadTeacherActivity = false; // trocar para 'true' se 'special_param1 == 1'
            if (substr($ilm_name, 0, 5)=="igeom") {
              if ($iassign_statement_activity_item->special_param1 == 1) // if 1 => use the teacher activity with some complement from the student (in iGeom = GEO + SCR)
                $loadTeacherActivity = true;
              }
            $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $loadTeacherActivity); // presents iLM
            }
           //1 Desse modo entre area para comentario, mas nao seu botao separado - deixar para o final com botao!
          //1 if ($iassign_statement_activity_item->type_iassign == 3) {
          //1   $history_comment = '';
          //1   if ($iassign_submission) {
          //1     $history_comment = $this->search_comment_submission($iassign_submission->id);
          //1     }
          //1   if (!empty($history_comment)) {
          //1     $output .= "\n  \n" .
          //1       "   " . get_string('history_comments', 'iassign') . " 
\n";
          //1     }
          //1   $output .= "    \n";
          //1   $only_one_send_button = 1; // avoid put comment frame again (bellow)
          //1   }
          $output .= $OUTPUT->box_end();
          print $output;
          } // if ($this->view_iassign)
        print $OUTPUT->box_end(); // Box 5 to 'proposition' - close
        } // if ($this->view_iassign)
      } // elseif (has_capability('mod/iassign:submitiassign', $USER->context, $USER->id))
    else
    if (isguestuser()) { // else of elseif (has_capability('mod/iassign:submitiassign', $USER->context, $USER->id))
      print($OUTPUT->notification(get_string('no_permission_iassign', 'iassign'), 'notifyproblem'));
      print '
' . "\n";
      print '' . "\n";
      print '' . get_string('proposition', 'iassign') . ' ' . "\n";
      print '' . $iassign_statement_activity_item->proposition . '
' . "\n";
      print ' 
' . "\n";
      $student_answer = "";
      $enderecoPOST = "";
      // Prepare tags to present the iLM
      $output = $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true);
      print $output;
      }
      //2020 Quadro com area para enivar e ler comentarios veio para este ponto para permitir todos o terem
    // $only_one_send_button is to avoid to put 2 copies of comments (area to send and see comments between teacher and student)
    if ($only_one_send_button==0 && $iassign_statement_activity_item->type_iassign == 3) { // type_iassign=3 => activity of type "exercise" - submit button and automatic evaluation
      // Put a block with area to sent/answer comments
      $output = '';
      $history_comment = '';
      $row = optional_param('row', 0, PARAM_INT);
      $column = optional_param('column', 0, PARAM_INT);
      if ($iassign_submission) {
        $enderecoPOSTcomment = "" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=newcomment&iassign_current=" . $this->activity->get_activity()->id .
          "&iassign_submission_current=" . $iassign_submission->id . "&userid_iassign=" . $this->userid_iassign . "&row=" . ($row) . "&column=" . ($column);
        $history_comment = $this->search_comment_submission($iassign_submission->id);
        }
      else {
        $enderecoPOSTcomment = "" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=newcomment&iassign_current=" . $this->activity->get_activity()->id .
          "&userid_iassign=" . $this->userid_iassign . "&row=" . ($row) . "&column=" . ($column);
        }
      $output .= $OUTPUT->box_start();
      
      //if (!empty($history_comment)) {
        $output .= "  
 \n";
        $output .= "     " . get_string('history_comments', 'iassign') . " 
";
        print " ";
        $output .= "
  \n";
        $output .= "";
      //  }
      $output .= $OUTPUT->box_end();
      print $output;
      } // if ($iassign_statement_activity_item->type_iassign == 3)
    // final block 'studant'
    print $OUTPUT->footer();
    die();
    } // function view_iassign_current()
  /// Export the data from performance report into CSV file format
  function export_csv () {
    global $USER, $CFG, $DB, $OUTPUT;
    $str = "";
    $fields = "student_id, student_name";
    // Finding all iassign activities for the iassign:
    $id = $this->cm->id;
    $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), "position ASC");
    $c = 1;
    foreach ($iassign_list as $iassign) {
      $fields .= ", activity_id_$c, activity_name_$c, total_submissions_activity_$c, grade_activity_$c"
          . ", status_activity_$c, ilm_id_activity_$c, ilm_name_activity_$c";
      $c ++;
    }
    // Finding enrolled students:
    $params = array('shortname' => 'student');
    $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params);
    $context = context_course::instance($this->course->id);
    $params = array('contextid' => $context->id, 'roleid' => $role->id);
    $students = $DB->get_records_sql(
        "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
        " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
        " ORDER BY a.firstname ASC,a.lastname ASC", $params);
    foreach ($students as $student) {
      $str .= $student->userid . ',';
      $str .= '"' . $student->firstname . ' ' . $student->lastname . '"';
      foreach ($iassign_list as $iassign) {
        $str .= ',' . $iassign->id . ',';
        $str .= '"' . $iassign->name . '",';
        $student_submissions = $DB->get_record("iassign_submission", array('iassign_statementid' => $iassign->id, 'userid' => $student->userid));
        if ($student_submissions) {
          $str .= $student_submissions->experiment . ',';
          $str .= $student_submissions->grade . ',';
          switch ($student_submissions->status) {
            case 3:
              $str .= '"correct",';
              break;
            case 2:
              $str .= '"incorrect",';
              break;
            case 1:
              $str .= '"post",';
              break;
            case 0:
              $str .= '"not_post",';
              break;
          }
        } else {
          $str .= ',,"not_post",';
        }
        $str .= $iassign->iassign_ilmid . ',';
        $ilm_activity = $DB->get_record("iassign_ilm", array('id' => $iassign->iassign_ilmid));
        $str .= '"' . $ilm_activity->name . '"';
      }
      $str .= "\n";
    }
    $str = $fields . "\n" . $str;
    header("Content-type: text/csv");
    header("Content-disposition: attachment; filename=report.csv");
    header("Pragma: no-cache");
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
    header('Content-Length: ' . strlen($str));
    header('Connection: close');
    print $str;
    flush();
  } // function export_csv()
  /// Display report of performance
  function report () {
    global $USER, $CFG, $DB, $OUTPUT;
    $id = $this->cm->id;
    $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), "position ASC");
    if ($this->action != 'print') {
      $title = get_string('report', 'iassign');
      print $OUTPUT->header();
      } // if ($this->action != 'print')
    print $OUTPUT->box_start();
    $this->view_legend_icons();
    print '
' . get_string('ps_experiment', 'iassign') . '
';
    print '
' . get_string('ps_comment', 'iassign') . '
';
    print $OUTPUT->box_end();
    if ($this->action == 'print')
      print '
' . "\n";
    else
      print '' . "\n";
    print '(" . get_string('iassign_exercise', 'iassign') . ") ";
        } // if (($iassign_submission) && $iassign->type_iassign < 3)
      if ($iassign->type_iassign == 3 || ($iassign_submission)) {
        $num[$i] = new stdClass();
        $num[$i]->name = $iassign->name;
        $num[$i]->id = $i;
        $num[$i]->iassignid = $iassign->id;
        print ' ' . "\n";
        print "  " . $num[$i]->id . " ";
        print ' ' . $num[$i]->name . ' ' . $test_exercise . " ";
        print '  ' . "\n";
        $i ++;
        } // if ($iassign->type_iassign == 3 || ($iassign_submission))
      } // foreach ($iassign_list as $iassign)
    print "
";
    print "
";
    if ($this->action == 'print')
      print '' . "\n";
    else
      print '' . "\n";
    $context = context_course::instance($this->course->id);
    if ($i > 1) {
      // $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {$CFG->prefix}role s WHERE s.shortname = 'student'");
      $params = array('shortname' => 'student');
      $role = $DB->get_record_sql(
        "SELECT s.id, s.shortname FROM {role} s " .
        " WHERE s.shortname = :shortname", $params);
      // $students = $DB->get_records_sql("SELECT s.userid, a.firstname, a.lastname FROM {$CFG->prefix}role_assignments s, {$CFG->prefix}user a WHERE s.contextid = '$context->id' AND s.userid = a.id AND s.roleid = '$role->id' ORDER BY a.firstname ASC,a.lastname ASC");
      $params = array('contextid' => $context->id, 'roleid' => $role->id);
      $students = $DB->get_records_sql(
        "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
        " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
        " ORDER BY a.firstname ASC,a.lastname ASC", $params);
      print '' . "\n";
      for ($j = 1; $j < $i; $j ++) {
        $sum_iassign_correct[$j] = 0;
        print '' . "\n"; // ';
      $sum_iassign = $j - 1;
      print ' ' . "\n";
      $total = 0;
      $sum_student = 0;
      $comment = iassign_icons::insert('comment_read');
      $sum_comment = 0;
      $sum_correct_iassign = array();
      $sum_correct_student = array();
      $USER->matrix_iassign = array();
      if ($students) {
        $w = 0;
        foreach ($students as $tmp) {
          $users_array[$w] = $tmp;
          $w ++;
          }
        for ($x = 0; $x < $w; $x ++) {
          print '' . "\n";
          $sum_student ++;
          $name = $users_array[$x]->firstname . ' ' . $users_array[$x]->lastname;
          print '  ' . $name . ' ' . "\n";
          $total_student = 0;
          $tentativas = 0;
          for ($j = 1; $j < $i; $j ++) {
            $sum_comment = 0;
            $student_submissions = $DB->get_record("iassign_submission", array('iassign_statementid' => $num[$j]->iassignid, 'userid' => $users_array[$x]->userid)); // data about student solution
            print '  ' . "\n";
            if ($student_submissions) {
              $last_solution_submission = " title=\"" . userdate($student_submissions->timemodified) . "\" "; // timemodified: time of the last student solution
              $tentativas = $student_submissions->experiment;
              // $student_submissions_comment = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment WHERE iassign_submissionid = '$student_submissions->id'");
              $params = array('iassign_submissionid' => $student_submissions->id);
              $student_submissions_comment = $DB->get_record_sql(
                "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
                "WHERE iassign_submissionid = :iassign_submissionid", $params);
              if ($student_submissions_comment)
                foreach ($student_submissions_comment as $tmp)
                  $sum_comment = $tmp;
              // informations to previous activities
              if ($j - 1 < 1 || $j == $i)
                $iassign_previous = "-1";
              else
                $iassign_previous = $num[$j - 1]->iassignid;
              if ($x - 1 < 0 || $x == $w)
                $user_previous = "-1";
              else
                $user_previous = $users_array[$x - 1]->userid;
              // next
              if ($i - 1 > $j)
                $iassign_next = $num[$j + 1]->iassignid;
              else
                $iassign_next = "-1";
              if ($w - 1 > $x)
                $user_next = $users_array[$x + 1]->userid;
              else
                $user_next = "-1";
              $position = "&row= $x&column=$j";
              $url = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=viewsubmission&id=" . $id . "&iassign_submission_current=" . $student_submissions->id .
                "&userid_iassign=" . $users_array[$x]->userid . "&iassign_current=" . $num[$j]->iassignid . "&view_iassign=" . $this->view_iassign;
              $url .= $position;
              // receiver=1 - message to teacher
              // receiver=2 - message to student
              // $verify_message = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment " .
              // "WHERE iassign_submissionid = '$student_submissions->id' AND return_status='0' AND receiver='1'");
              $params = array('iassign_submissionid' => $student_submissions->id, 'return_status' => '0', 'receiver' => '1');
              $verify_message = $DB->get_record_sql(
                "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
                "WHERE iassign_submissionid = :iassign_submissionid " .
                "  AND return_status= :return_status " .
                "  AND receiver= :receiver", $params);
              if ($verify_message)
                  foreach ($verify_message as $tmp)
                    $sum_verify_message = $tmp;
              if ($sum_verify_message > 0)
                  $comment = iassign_icons::insert('comment_unread');
              else
                  $comment = iassign_icons::insert('comment_read');
              if ($student_submissions->status == 3) {
                  $sum_iassign_correct[$j] ++;
                  $total_student ++;
                  $feedback = iassign_icons::insert('correct');
                }
              elseif ($student_submissions->status == 2) {
                  $feedback = iassign_icons::insert('incorrect');
                }
              elseif ($student_submissions->status == 1) {
                  $feedback = iassign_icons::insert('post');
                }
              elseif ($student_submissions->status == 0) {
                  $feedback = iassign_icons::insert('not_post');
                }
              if ($this->action != 'print') {
                print '';
                if ($tentativas > 0)
                  print ' ' . $feedback . '   (' . $tentativas . ') ' . "\n";
                else
                  print ' ' . $feedback . '   ' . "\n";
                print '    ';
                print ' ' . "\n";
                if ($sum_comment > 0 && $sum_verify_message > 0)
                  print '   ' . $comment . '   (' . $sum_verify_message . '/' . $sum_comment . ')     ' . $comment . '   (' . $sum_comment . ')  
' . "\n";
                }
              if ($this->action == 'print')
                  print $feedback . ' (' . $tentativas . ')';
                print ' ' . $feedback . '   ' . "\n";
                print '    ';
                print ' ' . "\n";
                print '   ';
                if ($sum_comment > 0)
                  print ' ' . $comment . '   (' . $sum_comment . ')  ' . "\n";
                else
                  print '   ';
                print ' 
';
                }
              } // else if ($student_submissions)
            $USER->matrix_iassign[$x][$j] = new stdClass();
            $USER->matrix_iassign[$x][$j]->iassign_previous = $iassign_previous;
            $USER->matrix_iassign[$x][$j]->user_previous = $user_previous;
            $USER->matrix_iassign[$x][$j]->iassign_next = $iassign_next;
            $USER->matrix_iassign[$x][$j]->user_next = $user_next;
            if ($student_submissions)
                $USER->matrix_iassign[$x][$j]->iassign_submission_current = $student_submissions->id;
            else
                $USER->matrix_iassign[$x][$j]->iassign_submission_current = 0;
            print ' ' . "\n";
            } // for ($j=1; $j<$i; $j++)
          $total = $total + $total_student;
          $porcentagem = ($total_student / ($j - 1)) * 100;
          if ($tentativas != 0 && $tentativas != null) {
            $url_answer = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . "action=download_all_answer&iassign_id=" . $this->iassign->id . "&userid=" . $users_array[$x]->userid . "&id=" . $id;
            print '  ' . iassign_icons::insert('download_all_assign') . ' ' . iassign_icons::insert('download_all_assign_disabled') . ' ' . "\n";
            }
          print ' ' . "\n";
          $sum_correct_student[$sum_student] = new stdClass();
          $sum_correct_student[$sum_student]->name = $name;
          $sum_correct_student[$sum_student]->sum = $total_student;
          } // for ($x = 0; $x < $w; $x ++)
        for ($i = 1; $i < $j; $i ++) {
          if (is_null($sum_iassign_correct[$i]))
            $sum_iassign_correct[$i] = 0;
          $sum_correct_iassign[$i] = new stdClass();
          $sum_correct_iassign[$i]->sum = $sum_iassign_correct[$i];
          $sum_correct_iassign[$i]->name = $num[$i]->name;
          }
        // print '
' . "\n";
        }
      } // if ($i>1)
    else {
      print_string('no_activity', 'iassign');
      }
    print "
\n";
    if ($this->action != 'print')
      print $OUTPUT->footer();
    die();
    } // function report()
  /// Display graphics of performance
  function stats () {
    global $USER, $CFG, $DB, $OUTPUT;
    $id = $this->cm->id;
    $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), "position ASC");
    if ($this->action != 'printstats')
      $title = get_string('graphic', 'iassign');
    $num = array();
    $sum_correct_iassign = array();
    $sum_correct_student = array();
    $sum_student = 0;
    $i = 1;
    foreach ($iassign_list as $iassign) {
      $iassign_submission = $DB->get_records("iassign_submission", array("iassign_statementid" => $iassign->id));
      if ($iassign->type_iassign == 3) { // || ($iassign_submission)
        $sum_iassign_correct[$i] = 0;
        $num[$i] = new stdClass();
        $num[$i]->name = $iassign->name;
        $num[$i]->id = $i;
        $num[$i]->iassignid = $iassign->id;
        $i ++;
        } // if ($iassign->type_iassign == 3)
      } // foreach ($iassign_list as $iassign)
    $sum_iassign = $i - 1;
    $context = context_course::instance($this->course->id);
    if ($i > 1) {
      // $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {$CFG->prefix}role s WHERE s.shortname = 'student'");
      $params = array('shortname' => 'student');
      $role = $DB->get_record_sql(
        "SELECT s.id, s.shortname FROM {role} s " .
        " WHERE s.shortname = :shortname", $params);
      // $students = $DB->get_records_sql("SELECT s.userid, a.firstname, a.lastname FROM {$CFG->prefix}role_assignments s, {$CFG->prefix}user a WHERE s.contextid = '$context->id' AND s.userid = a.id AND s.roleid = '$role->id' ORDER BY a.firstname ASC,a.lastname ASC");
      $params = array('contextid' => $context->id, 'roleid' => $role->id);
      $students = $DB->get_records_sql(
        "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
        " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
        " ORDER BY a.firstname ASC,a.lastname ASC", $params);
      $total = 0;
      $sum_student = 0;
      $j = 0;
      $sum_correct_iassign = array();
      $sum_correct_student = array();
      $sum_experiment = array();
      if ($students) {
        foreach ($students as $users) {
          $sum_student ++;
          $name = $users->firstname . ' ' . $users->lastname;
          // rows
          $total_student = 0;
          for ($j = 1; $j < $i; $j ++) {
            $total_experiment = 0;
            $student_submissions = $DB->get_record("iassign_submission", array('iassign_statementid' => $num[$j]->iassignid, 'userid' => $users->userid)); // data about student solution
            if ($student_submissions) {
              if ($student_submissions->status == 3) {
                $sum_iassign_correct[$j] ++;
                $total_student ++;
                } // if ($student_submissions->status == 3)
              $total_experiment += $student_submissions->experiment;
              }
            $sum_experiment[$j] = $total_experiment;
            } // for ($j=1; $j<$i; $j++)
          $total = $total + $total_student;
          $sum_correct_student[$sum_student] = new stdClass();
          $sum_correct_student[$sum_student]->name = $name;
          $sum_correct_student[$sum_student]->sum = $total_student;
          } // foreach ($students as $users)
        }
      for ($i = 1; $i < $j; $i ++) {
        if (is_null($sum_iassign_correct[$i]))
            $sum_iassign_correct[$i] = 0;
        $sum_correct_iassign[$i] = new stdClass();
        $sum_correct_iassign[$i]->sum = $sum_iassign_correct[$i];
        $sum_correct_iassign[$i]->name = $num[$i]->name;
        $sum_correct_iassign[$i]->experiment = $sum_experiment[$i];
        } // for ($i = 1; $i < $j; $i++)
      } // if ($i > 1)
    if ($this->action != 'printstats') {
      $title = get_string('graphic', 'iassign');
      print $OUTPUT->header();
      $link_report = "" . iassign_icons::insert('view_report') . ' ' . get_string('report', 'iassign') . " ";
      $link_print_stats = "" . iassign_icons::insert('print') . ' ' . get_string('print', 'iassign') . " ";
      print '';
      print '' . $link_print_stats . ' ' . "\n";
      print '' . $link_report . ' ';
      print ' 
' . "\n";
      print "' . "\n";
      print '' . iassign_utils::remove_code_message($this->iassign->name) . ' ' . get_string('percentage_correct', 'iassign') . ' ' . get_string('proportion_correct', 'iassign') . ' ' . "\n";
      print '' . get_string('sum_experiment', 'iassign') . ' ' . get_string('avg_experiment', 'iassign') . ' ' . $iassign->name . ' ' . "\n";
          print ' ' . $bar . ' ' . $text . ' ' . "\n";
          print ' ' . $sum . '/' . $sum_student . ' ' . "\n";
          print ' ' . $iassign->experiment . ' ' . "\n";
          print ' ' . number_format($iassign->experiment / $sum_iassign, 1) . ' ' . "\n";
          print '
";
      print "' . "\n";
      print '' . iassign_utils::remove_code_message($this->iassign->name) . ' ' . get_string('percentage_correct', 'iassign') . ' ' . get_string('sum_correct', 'iassign') . ' ' . $student->name . ' ' . "\n";
        print '' . $bar . ' ' . $text . ' ' . "\n";
        print '' . $sum . '/' . $sum_iassign . ' ' . "\n";
        print '
\n";
      print "' . "\n";
      print '' . get_string('sum_activity', 'iassign') . ' ' . get_string('sum_student', 'iassign') . ' ' . get_string('mean_score', 'iassign') . ' ' . get_string('standard_deviation', 'iassign') . ' ' . get_string('coefficient_variation', 'iassign') . ' ' . $sum_iassign . ' ' . "\n";
      print ' ' . $sum_student . ' ' . "\n";
      print ' ' . number_format($avg, 1) . ' ' . "\n";
      print ' ' . number_format($dv, 1) . ' ' . "\n";
      print ' ' . number_format($cv, 1) . '% 
\n";
      print $OUTPUT->footer();
    } else {
      print "\n";
      print '' . "\n";
      print '' . get_string('distribution_activity', 'iassign') . ' ' . iassign_utils::remove_code_message($this->iassign->name) . ' ' . get_string('percentage_correct', 'iassign') . ' ' . get_string('sum_correct', 'iassign') . ' ' . $iassign->name . ' ' . "\n";
        print '' . $bar . ' ' . $text . ' ';
        print '' . $sum . '/' . $sum_student . ' ';
        print '
";
      print "' . "\n";
      print '' . get_string('distribution_student', 'iassign') . ' ' . iassign_utils::remove_code_message($this->iassign->name) . ' ' . get_string('percentage_correct', 'iassign') . ' ' . get_string('sum_correct', 'iassign') . ' " . $student->name . " \n";
        print ' ' . $bar . ' ' . $text . ' ' . "\n";
        print ' ' . $sum . '/' . $sum_iassign . ' ' . "\n";
        print '
\n";
      print "' . "\n";
      print '' . get_string('statistics', 'iassign') . ' ' . get_string('sum_activity', 'iassign') . ' ' . get_string('sum_student', 'iassign') . ' ' . get_string('mean_score', 'iassign') . ' ' . get_string('standard_deviation', 'iassign') . ' ' . get_string('coefficient_variation', 'iassign') . ' ' . $sum_iassign . ' ' . "\n";
      print '' . $sum_student . ' ' . "\n";
      print '' . number_format($avg, 1) . ' ' . "\n";
      print '' . number_format($dv, 1) . ' ' . "\n";
      print '' . number_format($cv, 1) . '% 
\n";
      } // if ($this->action != 'printstats')
    die();
    }
  /// Display graphics of performance for students
  function stats_students () {
    global $USER, $CFG, $DB, $OUTPUT;
    $id = $this->cm->id;
    $iassign_statement_list = $DB->get_records_sql("SELECT * FROM {iassign_statement} s " .
      " WHERE s.iassignid = '{$this->iassign->id}' AND s.type_iassign=3 ORDER BY s.position");
    $title = get_string('results', 'iassign');
    $sum_correct = 0;
    $sum_incorrect = 0;
    $sum_post = 0;
    $sum_nopost = 0;
    $sum_iassign = count($iassign_statement_list);
    $bar_nopost = "";
    $bar_correct = "";
    $bar_incorrect = "";
    $bar_post = "";
    $text_nopost = "";
    $text_correct = "";
    $text_incorrect = "";
    foreach ($iassign_statement_list as $iassign_statement_activity_item) {
      $iassign_submission = $DB->get_record("iassign_submission", array('iassign_statementid' => $iassign_statement_activity_item->id, 'userid' => $USER->id)); // data about student solution
      if ($iassign_submission) {
        if ($iassign_submission->status == 3)
          $sum_correct ++;
        elseif ($iassign_submission->status == 2)
          $sum_incorrect ++;
        elseif ($iassign_submission->status == 1)
          $sum_post ++;
        elseif ($iassign_submission->status == 0 || !$iassign_submission)
          $sum_nopost ++;
        } // if ($iassign_submission)
      } // foreach ($iassign_statement_list as $iassign_statement_activity_item)
    if ($sum_iassign > 0) {
      $percent_correct = ($sum_correct / $sum_iassign) * 100;
      $text_correct = number_format($percent_correct, 1) . '%';
      }
    if ($sum_correct > 0) {
      for ($i = 1; $i < $percent_correct * 2; $i ++)
        $bar_correct .= iassign_icons::insert('hbar_green');
      $bar_correct .= iassign_icons::insert('hbar_green_r');
      } // if ($sum_correct > 0)
    if ($sum_iassign > 0) {
      $percent_incorrect = ($sum_incorrect / $sum_iassign) * 100;
      $text_incorrect = number_format($percent_incorrect, 1) . '%';
      }
    if ($sum_incorrect > 0) {
      for ($i = 1; $i < $percent_incorrect * 2; $i ++)
        $bar_incorrect .= iassign_icons::insert('hbar_red');
      $bar_incorrect .= iassign_icons::insert('hbar_red_r');
      } // if ($sum_incorrect > 0)
    if ($sum_iassign > 0) {
      $percent_post = ($sum_post / $sum_iassign) * 100;
      $text_post = number_format($percent_post, 1) . '%';
      }
    if ($sum_post > 0) {
      for ($i = 1; $i < $percent_post * 2; $i ++)
        $bar_post .= iassign_icons::insert('hbar_blue');
      $bar_post .= iassign_icons::insert('hbar_blue_r');
      } // if ($sum_post > 0)
    if ($sum_iassign > 0) {
      $percent_nopost = ($sum_nopost / $sum_iassign) * 100;
      $text_nopost = number_format($percent_nopost, 1) . '%';
      }
    if ($sum_nopost > 0) {
      for ($i = 1; $i < $percent_nopost * 2; $i ++)
        $bar_nopost .= iassign_icons::insert('hbar_orange');
      $bar_nopost .= iassign_icons::insert('hbar_orange_r');
      } // if ($sum_nopost > 0)
    print $OUTPUT->header();
    $link_return = " " . iassign_icons::insert('home') . get_string('activities_page', 'iassign') . " ";
    print '';
    print '' . $link_return . ' ' . "\n";
    print ' 
' . "\n";
    print "' . "\n";
    print '' . get_string('correct', 'iassign') . ' ' . "\n";
    print '' . $bar_correct . ' ' . $text_correct . ' ';
    print '' . $sum_correct . '/' . $sum_iassign . ' ';
    print '' . get_string('incorrect', 'iassign') . ' ' . "\n";
    print '' . $bar_incorrect . ' ' . $text_incorrect . ' ';
    print '' . $sum_incorrect . '/' . $sum_iassign . ' ';
    print '' . get_string('post', 'iassign') . ' ' . "\n";
      print '' . $bar_post . ' ' . $text_post . ' ';
      print '' . $sum_post . '/' . $sum_iassign . ' ';
      print '' . get_string('not_post', 'iassign') . ' ' . "\n";
    print '' . $bar_nopost . ' ' . $text_nopost . ' ';
    print '' . $sum_nopost . '/' . $sum_iassign . ' ';
    print '
";
    print "' . "\n";
    print '' . iassign_utils::remove_code_message($this->iassign->name) . ' ' . get_string('grade_student', 'iassign') . ' ' . get_string('grade_iassign', 'iassign') . ' ' . $iassign_statement_activity_item->name . ' ' . "\n";
      print '' . $iassign_submission->grade . ' ';
      print '' . $iassign_statement_activity_item->grade . ' ';
      print '' . get_string('total', 'iassign') . ' ' . $sum_grade_student . ' ' . $sum_grade . ' ' . get_string('percentage_correct', 'iassign') . ' ' . number_format($avg, 1) . '% 
";
    print $OUTPUT->footer();
    die();
    } // function stats_students()
  /// Display page of iAssign's activity
  function view_iassigns () {
    global $USER, $CFG, $COURSE, $DB, $OUTPUT;
    $id = $this->cm->id;
    $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), 'position ASC');
    $notice = optional_param('notice', '', PARAM_TEXT);
    if (strpos($notice, 'error'))
      print($OUTPUT->notification(get_string($notice, 'iassign'), 'notifyproblem'));
    else if ($notice != '')
      print($OUTPUT->notification(get_string($notice, 'iassign'), 'notifysuccess'));
    print $OUTPUT->box_start();
    print '' . "\n";
    $url_help = new moodle_url('/mod/iassign/settings_ilm.php', array('action' => 'list', 'ilm_id' => 1));
    $action_help = new popup_action('click', $url_help, 'iplookup', array('title' => get_string('help_ilm', 'iassign'), 'width' => 1200, 'height' => 700));
    $link_help = $OUTPUT->action_link($url_help, iassign_icons::insert('help_ilm') . get_string('help_ilm', 'iassign'), $action_help);
    $link_add = "" . iassign_icons::insert('add_iassign') . get_string('add_iassign', 'iassign') . " ";
    $link_report = "" . iassign_icons::insert('view_report') . get_string('report', 'iassign') . " ";
    // TODO: esta consulta esta sendo feita novamente na linha +/- 2258
    if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)) {
      // could be "has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)"
      // Has capability to see "report": teacher or up
      print '' . "\n";
      print $link_help;
      print ' ' . "\n";
      print '' . "\n";
      print $link_report;
      print ' ' . "\n";
      } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id))
    if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
      print '' . "\n";
      print $link_add;
      print " \n";
      } // if (has_capability('mod/iassign:editiassign', $this->context, $USER->id))
    //print ' 
' . "\n";
    print '';
    if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
      if ($iassign_list) {
        print '
' . "\n";
        // $USER->iassignEdit == 0 view 'Turn editing off'
        // $USER->iassignEdit == 1 view 'Turn editing on'
        if (!isset($USER->iassignEdit))
          $USER->iassignEdit = 0;
        if ($USER->iassignEdit == 0) {
          $bottonEdit_message = get_string('turneditingon', 'iassign');
          $botton = 1;
          }     // if ($USER->iassignEdit == 0)
        elseif ($USER->iassignEdit == 1) {
          $bottonEdit_message = get_string('turneditingoff', 'iassign');
          $botton = 0;
          } // elseif ($USER->iassignEdit == 1)
        $editPost = "" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&botton=" . $botton;
        print "\n\n";
        print " \n";
        } // if ($iassign_list)
      }
      print '
' . "\n";
    // if (has_capability('mod/iassign:submitiassign', $this->context, $USER->id))
    //   $this->view_legend_icons();
    print $OUTPUT->box_end();
    $iassign_array_exercise = array();
    $i_exercise = 0;
    $iassign_array_test = array();
    $i_test = 0;
    $iassign_array_example = array();
    $i_example = 0;
    $iassign_array_general = array();
    $i_general = 0;
    if ($iassign_list) {
      if ($this->iassign->activity_group == 0) {
        foreach ($iassign_list as $iassign) {
          $iassign_array_general[$i_general] = $iassign;
          $i_general ++;
          }
        } // if ($this->iassign->activity_group == 0)
      else {
        foreach ($iassign_list as $iassign) {
          if ($iassign->type_iassign == 3) {
            $iassign_array_exercise[$i_exercise] = $iassign;
            $i_exercise ++;
            } // if ($iassign->type_iassign == 3)
          if ($iassign->type_iassign == 2) {
            $iassign_array_test[$i_test] = $iassign;
            $i_test ++;
            } // if ($iassign->type_iassign == 2)
          if ($iassign->type_iassign == 1) {
            $iassign_array_example[$i_example] = $iassign;
            $i_example ++;
            } // if ($iassign->type_iassign == 1)
          }
        }
      if ($iassign_array_exercise) {
        $title = get_string('exercise', 'iassign').(count($iassign_array_exercise)>1?'s':'');
        $this->show_iassign($title, $iassign_array_exercise, $i_exercise);
        }
      if ($iassign_array_test) {
        $title = get_string('test', 'iassign').(count($iassign_array_test)>1?'s':'');
        $this->show_iassign($title, $iassign_array_test, $i_test);
        }
      if ($iassign_array_example) {
        $title = get_string('example', 'iassign').(count($iassign_array_example)>1?'s':'');;
        $this->show_iassign($title, $iassign_array_example, $i_example);
        }
      if ($iassign_array_general) {
        $title = "";
        $this->show_iassign($title, $iassign_array_general, $i_general);
        }
      } else { // if ($iassign_list)
        print $OUTPUT->notification(get_string('no_activity', 'iassign'), 'notifysuccess');
        }
    if (count($iassign_list) > 5 && !(has_capability('mod/iassign:submitiassign', $this->context, $USER->id))) {
      if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)) {
        print $OUTPUT->box_start();
        print '
' . "\n";
        print '' . "\n";
        print $link_report;
        print ' ' . "\n";
        print ' 
' . "\n";
        print $OUTPUT->box_end();
        } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id))
      if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
        print $OUTPUT->box_start();
        print '
' . "\n";
        print '' . "\n";
        print $link_add;
        print ' ' . "\n";
        print ' 
' . "\n";
        print $OUTPUT->box_end();
        } // if (has_capability('mod/iassign:editiassign', $this->context, $USER->id))
      } // if (count($iassign_list) > 5 && !(has_capability('mod/iassign:submitiassign', $this->context, $USER->id)))
      if (has_capability('mod/iassign:submitiassign', $this->context, $USER->id))
        $this->view_legend_icons();
    } // function view_iassigns()
  /// Display all iAssigns
  function show_iassign ($title, $iassign_array, $i) {
    global $USER, $CFG, $DB, $OUTPUT, $PAGE;
    $id = $this->cm->id;
    print $OUTPUT->box_start();
    print "    \n";
    print "    
\n      
\n        
× \n" .
          "        
" . get_string('move_activity', 'iassign') . "
\n" .
          "        
" . get_string('move_destination', 'iassign') . ":\n              
";
    $ccm = get_coursemodule_from_id('iassign', optional_param('id', 0, PARAM_INT));
    $radio_buttons = "  
    \n";
    print "    ";
    if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)) {
      print "
" . $title . " 
";
      for ($j = 0; $j < $i; $j ++) {
        $iassign_current = $iassign_array[$j]->id;
        // receiver=1 - message to teacher
        // receiver=2 - message to student
        $sum_comment = 0;
        $iassign_submissions = $DB->get_records('iassign_submission', array('iassign_statementid' => $iassign_current));
        foreach ($iassign_submissions as $iassign_submission) {
          $params = array('iassign_submissionid' => $iassign_submission->id, 'return_status' => '0', 'receiver' => '1');
          $verify_message = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
            " WHERE iassign_submissionid = :iassign_submissionid AND return_status= :return_status AND receiver= :receiver", $params);
          if ($verify_message)
            foreach ($verify_message as $tmp)
              $sum_comment += $tmp;
          } // foreach ($iassign_submissions as $iassign_submission)
        if ($sum_comment == 0)
          $comment_unread = "";
        else {
          $comment_unread_message = get_string('comment_unread', 'iassign');
          if ($sum_comment == 1)
            $comment_unread_message = get_string('comment_unread_one', 'iassign');
          $comment_unread = " 
" .
            iassign_icons::insert('comment_unread') . " ($sum_comment " . $comment_unread_message . ") ";
          }
        if ($j == $i - 1)
          $iassign_down = $iassign_array[$j]->id;
        else
          $iassign_down = $iassign_array[$j + 1]->id;
        if ($j > 0)
          $iassign_up = $iassign_array[$j - 1]->id;
        else
          $iassign_up = $iassign_array[$j]->id;
        if ($iassign_array[$j]->visible == 0)
          $links = " 
" . $iassign_array[$j]->name . " ";
        else
          $links = " 
" . $iassign_array[$j]->name . " ";
        $links .= $comment_unread;
        if (has_capability('mod/iassign:editiassign', $this->context, $USER->id) && $USER->iassignEdit == 1) {
	  //D echo "id=$id, iassign_current=$iassign_current, iassign_up=$iassign_up, iassign_down=$iassign_down
";
    $str_aux = "&id=$id&iassign_current=$iassign_current&iassign_up=$iassign_up&iassign_down=$iassign_down";
    $link_up = " 
" . iassign_icons::insert('move_up_iassign') . " ";
    if ($j==0) // first activity
      $link_down = " 
" . iassign_icons::insert('move_down_iassign') . " ";
    else
      $link_down = " 
" . iassign_icons::insert('move_down_iassign') . " ";
    $link_delete = " 
" . iassign_icons::insert('delete_iassign') . " ";
    $link_visible_hide = " 
" . iassign_icons::insert('hide_iassign') . " ";
    $link_visible_show = " 
" . iassign_icons::insert('show_iassign') . " ";
    $link_edit = " 
" . iassign_icons::insert('edit_iassign') . " ";
    $link_duplicate_activity = " 
" . iassign_icons::insert('duplicate_iassign') . " \n";
    $link_move_activity = " 
" . iassign_icons::insert('move_activity') . " \n";
          if (count($iassign_array) > 1) {
            if ($j == 0)
              $links .= $link_down;
            elseif ($j == $i - 1)
              $links .= $link_up;
            else
              $links .= $link_up . $link_down;
            } // if (count($iassign_array) > 1)
          $links .= $link_edit . $link_delete;
          if ($iassign_array[$j]->visible == 0)
            $links .= $link_visible_show;
          else
            $links .= $link_visible_hide;
          $links .= $link_duplicate_activity . $link_move_activity;
          } // if ($USER->iassignEdit == 1 && has_capability('mod/iassign:editiassign', $this->context, $USER->id))
        print '
' . $links . '
' . "\n";
        }
      } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id))
    elseif (has_capability('mod/iassign:submitiassign', $this->context, $USER->id)) { // student
      print '
' . "\n";
      print "" . $title . "  
' . "\n";
      for ($j = 0; $j < $i; $j ++) {
        $icon_status = "";
        $icon_comment = "";
        if ($iassign_array[$j]->visible == 1) {
         $iassign_current = $iassign_array[$j]->id;
         $iassign_submission = $DB->get_record('iassign_submission', array('iassign_statementid' => $iassign_current, 'userid' => $USER->id));
         $links = " 
" . $iassign_array[$j]->name . " ";
         $icon_status = "";
         $icon_comment = "";
         if ($iassign_submission) {
           // receiver=1 - message to teacher
           // receiver=2 - message to student
           // $verify_message = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment WHERE iassign_submissionid = '$iassign_submission->id' and return_status= 0 and receiver=2");
           $params = array('iassign_submissionid' => $iassign_submission->id, 'return_status' => '0', 'receiver' => '2');
           $verify_message = $DB->get_record_sql(
             "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
             " WHERE iassign_submissionid = :iassign_submissionid AND return_status= :return_status AND receiver= :receiver", $params);
           if ($verify_message)
             foreach ($verify_message as $tmp)
               $sum_comment = $tmp;
           if ($sum_comment > 0) {
             $comment_unread_message = get_string('comment_unread', 'iassign');
             if ($sum_comment == 1)
               $comment_unread_message = get_string('comment_unread_one', 'iassign');
             $icon_comment = " 
" . iassign_icons::insert('comment_unread') . " ($sum_comment " . $comment_unread_message . ") ";
             }
           // $icon_comment = iassign_icons::insert('comment_unread');
           if ($iassign_array[$j]->type_iassign == 3) {
             if ($iassign_array[$j]->show_answer == 1) {
               if ($iassign_submission->status == 3)
                 $icon_status = iassign_icons::insert('correct');
               elseif ($iassign_submission->status == 2)
                 $icon_status = iassign_icons::insert('incorrect');
               elseif ($iassign_submission->status == 1)
                 $icon_status = iassign_icons::insert('post');
               elseif ($iassign_submission->status == 0)
                 $icon_status = iassign_icons::insert('not_post');
               } // if ($iassign_array[$j]->show_answer==1)
             else {
               if ($iassign_submission->status == 0)
                 $icon_status = iassign_icons::insert('not_post');
               else
                 $icon_status = iassign_icons::insert('post');
               }
             } // if ($iassign_array[$j]->type_iassign == 3)
           } // if ($iassign_submission)
         elseif ($iassign_array[$j]->type_iassign == 3) {
             $icon_status = iassign_icons::insert('not_post');
           } // if ($iassign_array[$j]->type_iassign == 3)
         print '
' . $icon_status . ' ' . $links . ' ' . $icon_comment . '
' . "\n";
          } // if ($iassign_array[$j]->visible == 1)
        } // for ($j = 0; $j < $i; $j++)
      } else if (isguestuser()) {
      print($OUTPUT->notification(get_string('no_permission_iassign', 'iassign'), 'notifyproblem'));
      print '
' . "\n";
      print "" . $title . "  
' . "\n";
      for ($j = 0; $j < $i; $j ++) {
        $icon_status = "";
        $icon_comment = "";
        if ($iassign_array[$j]->visible == 1) {
          $iassign_current = $iassign_array[$j]->id;
          $links = " 
" . $iassign_array[$j]->name . " ";
          print '
' . $links . '
' . "\n";
          } // if ($iassign_array[$j]->visible == 1)
        }
      }
    print $OUTPUT->box_end();
    } // function show_iassign($title, $iassign_array, $i)
  /// Show message of return
  function return_home_course ($message) {
    //D global $DB, $OUTPUT;
    //D $link_return = " 
" . iassign_icons::insert('home') . get_string('activities_page', 'iassign') . " ";
    //D echo $OUTPUT->box_start();
    //D echo '
' . "\n";
    //D echo '' . "\n";
    //D print_string($message, 'iassign');
    //D echo ' ' . "\n";
    //D echo $link_return;
    //D echo ' 
' . "\n";
    //D echo $OUTPUT->box_end();
    //D // echo $OUTPUT->footer();
    redirect(new moodle_url($this->return . '¬ice=' . $message));
    exit;
    }
  /// Search comment of activity
  function search_comment_submission ($iassign_submissionid) {
    global $USER, $DB, $OUTPUT, $COURSE;
    // $context = context_course::instance($COURSE->id);
    $colorEdit1 = "#b7ceee"; // "#dce7ec"
    $colorEdit2 = "#d2e5fc";
    $has_capability = 0;
    $comments = $DB->get_records_list('iassign_submission_comment', 'iassign_submissionid', array('iassign_submissionid' => $iassign_submissionid), 'timecreated DESC'); // 'ORDER BY "timecreated" ASC'
    $text = "";
    if ($comments) {
      $even = 1;
      foreach ($comments as $tmp) {
        $user_data = $DB->get_record("user", array('id' => $tmp->comment_authorid));
        // if (has_capability('mod/iassign:editiassign', $context, $tmp->comment_authorid)) {
        //  $text .= "
 $user_data->firstname  (" . userdate($tmp->timecreated) . ")\n";
        //  $text .= $tmp->comment . "";
        //} else {
        //  $text .= "
»" . $user_data->firstname . "  (" . userdate($tmp->timecreated) . "\n";
        $avatar = new user_picture($user_data);
        $avatar->courseid = $COURSE->id;
        $avatar->link = true;
        $user_picture = $OUTPUT->render($avatar);
        $user_picture = str_replace('"35"', '"45"', $user_picture);
        if ($even) {
          $text .= " $user_picture " . $user_data->firstname . "  $user_picture " . $user_data->firstname . "  " . $tmp->comment . "
\n";
        } // foreach ($comments as $tmp)
      }
    return $text;
    }
  /// Update comment of activity
  function update_comment ($iassign_submissionid) {
    global $USER, $DB, $OUTPUT;
    if (!has_capability('mod/iassign:submitiassign', $this->context, $USER->id) || is_siteadmin())
      $receiver = 1; // student message to teacher
    else
      $receiver = 2; // teacher message to student
    $verify_message = $DB->get_records('iassign_submission_comment', array('iassign_submissionid' => $iassign_submissionid)); //
    if ($verify_message) {
      foreach ($verify_message as $message) {
        if ($message->receiver == $receiver) {
            $newentry = new stdClass();
            $newentry->id = $message->id;
            $newentry->return_status = 1;
            if (!$DB->update_record('iassign_submission_comment', $newentry)) {
                print_error('error_update', 'iassign');
              } // if (!$DB->update_record('iassign_submission_comment', $newentry))
          } // if ($message->receiver == $receiver)
        // Trigger module viewed event.
        $event = \mod_iassign\event\submission_comment_updated::create(array(
          'objectid' => $this->iassign->id,
          'context' => $this->context
          ));
        $event->add_record_snapshot('course', $this->course);
        $event->trigger();
        } // foreach ($verify_message as $message)
      }
    }
  /// Record comment of activity
  function write_comment_submission () {
    global $USER, $CFG, $DB;
    $id = $this->cm->id;
    $submission_comment = optional_param('submission_comment', NULL, PARAM_TEXT);
    $row = optional_param('row', 0, PARAM_INT);
    $column = optional_param('column', 0, PARAM_INT);
    $sum_comment = 0;
    $return = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=viewsubmission&id=" . $id . "&iassign_submission_current=" . $this->iassign_submission_current . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id . "&row=" . ($row) . "&column=" . ($column);
    $link_return = " 
" . iassign_icons::insert('return_home') . get_string('return', 'iassign') . " ";
    $str1 = trim($submission_comment);
    $str2 = trim(get_string('box_comment_message', 'iassign'));
    if (!empty($submission_comment) && (strcmp($str1, $str2) != 0)) { // there is comment and it is different from "previous"
      //D $iassign_submission = $DB->get_record("iassign_submission", array("id" => $this->iassign_submission_current)); //MOOC 2016
      if (has_capability('mod/iassign:submitiassign', $this->context, $USER->id) && !is_siteadmin()) { //MOOC '&& !is_siteadmin()'
        $receiver = 1; // student message to teacher
        $this->action = 'view';
        //D $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassign_submission->iassign_statementid)); //MOOC 2016
        //D $tousers = get_users_by_capability($this->context, 'mod/iassign:evaluateiassign'); //MOOC 2016
        } else {
        $receiver = 2; // teacher message to student
        $this->action = 'viewsubmission';
        //D $tousers = array(); //MOOC 2016
        //MOOC 2016 //T $tousers[] = $DB->get_record("user", array("id" => $iassign_submission->userid)); //TODO Para registrar mensagem na area do Moodle - tem que ativar abaixo
        }
      //MOOC 2016: foi p/ 15 linhas acima:
      $iassign_submission = $DB->get_record("iassign_submission", array("id" => $this->iassign_submission_current));
      // verificar se existe uma submissão para o exercício e aluno informados
      if (!$iassign_submission) {
        $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $USER->id));
      }
      if (!$iassign_submission) {
        $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $this->activity->get_activity()->id));
        $id_submission = $this->new_submission($iassign_statement_activity_item->id, $this->userid_iassign, $receiver);
        $this->iassign_submission_current = $id_submission;
        } else {
        $id_submission = $iassign_submission->id;
        } // if (!$iassign_submission)
      // $comments = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment
      // WHERE iassign_submissionid = '$id_submission' and comment='$submission_comment' and comment_authorid='$USER->id'"); //
      // Attention: this Moodle function 'get_record_sql' makes a replace in ':comment'
      $params = array("iassign_submissionid" => $id_submission, "comment" => $submission_comment, "comment_authorid" => $USER->id);
      $comments = $DB->get_record_sql(
            "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
            " WHERE iassign_submissionid = :iassign_submissionid AND comment= :comment AND comment_authorid= :comment_authorid", $params);
      if ($comments)
        foreach ($comments as $tmp)
          $sum_comment = $tmp;
      if ($sum_comment == 0) {
        $newentry = new stdClass();
        $newentry->iassign_submissionid = $id_submission;
        $newentry->comment_authorid = $USER->id;
        $newentry->timecreated = time();
        $newentry->comment = $submission_comment;
        $newentry->receiver = $receiver;
        $ia_assign_submissions_comment_id = $DB->insert_record('iassign_submission_comment', $newentry);
        //T foreach ($tousers as $touser) { //TODO Para registrar mensagem na area do Moodle - tem que ativar '$tousers[] = $DB->get_record(...);' acima
        //T $eventdata = new stdClass();
        //T $eventdata->component         = 'mod_iassign'; //your component name
        //T $eventdata->name              = 'comment'; //this is the message name from messages.php
        //T $eventdata->userfrom          = $USER;
        //T $eventdata->userto            = $touser;
        //T $eventdata->subject           = "Teste de Subject";
        //T $eventdata->fullmessage       = "Teste de Mensagem...";
        //T $eventdata->fullmessageformat = FORMAT_PLAIN;
        //T $eventdata->fullmessagehtml   = "
Teste de Mensagem... ";
        //T $eventdata->smallmessage      = "Teste de Mensagem";
        //T $eventdata->notification      = 1; //this is only set to 0 for personal messages between users
        //T // alteracao tulio faria
        //T //message_send($eventdata);
        //T } //MOOC 2016 - TODO NAO finalizado, iniciado pelo Tulio
        // Trigger module viewed event.
        $event = \mod_iassign\event\submission_comment_created::create(array(
          'objectid' => $this->iassign->id,
          'context' => $this->context
          ));
        $event->add_record_snapshot('course', $this->course);
        $event->trigger();
        }
      } // if (!empty($submission_comment) && (strcmp($str1, $str2) != 0))
    // if ($this->action=='viewsubmission') {
    // echo $OUTPUT->header();
    // $this->return_last('confirm_add_comment', $link_return);
    // die;
    // }
    return true;
    } // function write_comment_submission()
  /// Writes a new submission
  //  The first student answer is under 'function get_answer()'
  function new_submission ($iassignid, $id_user, $receiver) {
    global $USER, $DB, $OUTPUT;
    $newentry = new stdClass();
    $newentry->iassign_statementid = $iassignid;
    $newentry->userid = $id_user;
    $newentry->timecreated = time();
    $newentry->timemodified = time();
    $newentry->answer = 0; // student only submit message
    if ($receiver == 2) // teacher message to student (write id teacher)
      $newentry->teacher = $USER->id;
    if (!$newentry->id = $DB->insert_record("iassign_submission", $newentry))
      return_home_course('error_insert_submissions');
    else {
      // Trigger module view event
      $event = \mod_iassign\event\submission_created::create(array(
        'objectid' => $this->iassign->id,
        'context' => $this->context
        ));
      $event->add_record_snapshot('course', $this->course);
      $event->trigger();
      }
    return $newentry->id;
    }
  /// Return to a specific address of page
  function return_last ($message, $link_return) {
    global $DB, $OUTPUT;
    print $OUTPUT->box_start();
    print '
' . "\n";
    print '' . "\n";
    print_string($message, 'iassign');
    print ' ' . "\n";
    print $link_return;
    print ' 
' . "\n";
    print $OUTPUT->box_end();
    print $OUTPUT->footer();
    die();
    } // function return_last($message, $link_return)
  } // class iassign
/// Class for manage activities
class activity {
  var $activity;
  /// Constructor of class.
  //  @param int $id Id of activity
  //  3.1 update PHP 7.0 compatibility for all moodle versions
  //D public function activity($id) { self::__construct($id); }
  function __construct ($id) {
    global $DB;
    $this->activity = $DB->get_record("iassign_statement", array("id" => $id));
    if (empty($this->activity))
      $this->activity = null;
    }
  /// Get an activity
  //  @return NULL
  function get_activity () {
    if ($this->activity != null)
      return $this->activity;
    else
      return null;
    }
  /// Delete interactive activities
  function delete ($return) {
    global $USER, $CFG, $DB, $OUTPUT;
    $iassign_submission_currents = $DB->get_records("iassign_submission", array("iassign_statementid" => $this->activity->id));
    $output = $OUTPUT->header();
    $output .= $OUTPUT->box_start();
    $output .= "
" . get_string('delete_activity', 'iassign') . " " . $this->activity->name . " 
";
    if ($iassign_submission_currents) {
      $output .= "
" . get_string('number_submissions', 'iassign') . " " . count($iassign_submission_currents) . " 
";
      if (!has_capability('mod/iassign:deleteiassignnotnull', $USER->context, $USER->id)) {
        $output .= $OUTPUT->heading(get_string('delete_activity_permission_adm', 'iassign'));
        $output .= $OUTPUT->single_button($return, get_string('return', 'iassign'), 'get');
        $output .= $OUTPUT->box_end();
        $output .= $OUTPUT->footer();
        print $output;
        die();
        } // if (!has_capability('mod/iassign:deleteiassignnotnull', $this->context, $USER->id))
      }   // if ($iassign_submission_currents)
    else
      $output .= "
" . get_string('not_submissions_activity', 'iassign') . "
";
    $output .= '
';
    $output .= '';
    $output .= "" . get_string('what_do', 'iassign') . "
";
    $output .= ' ';
    $bottonDelete_yes = get_string('delete_iassign', 'iassign');
    $deleteiassignyes = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm . "&action=deleteyes&iassign_current=" . $this->activity->id;
    $output .= "\n";
    $output .= ' ';
    $bottonDelete_no = get_string('delete_cancel', 'iassign');
    $deleteiassignno = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm . "&action=deleteno&iassign_current=" . $this->activity->id;
    $output .= "\n";
    $output .= ' 
' . "\n";
    $output .= $OUTPUT->box_end();
    $output .= $OUTPUT->footer();
    print $output;
    } //  function delete($return)
  /// Function for confirm the delete of activity
  //  @param String $return Url of return
  //  @param Object $iassign Object content an activity
  function deleteyes ($return, $iassign) {
    global $USER, $CFG, $DB, $OUTPUT, $COURSE;
    $msg = '';
    if (!empty($this->activity->id)) {
      $iassign_submission_currents = $DB->get_records("iassign_submission", array("iassign_statementid" => $this->activity->id));
      if ($iassign_submission_currents) {
        if (has_capability('mod/iassign:deleteassignnull', $USER->context, $USER->id)) {
            foreach ($iassign_submission_currents as $iassign_submission)
                $DB->delete_records('iassign_submission_comment', array('iassign_submissionid' => $iassign_submission->id));
            $delete_iassign_submission_currents = $DB->delete_records("iassign_submission ", array("iassign_statementid" => $this->activity->id));
          } // if ($iassign_submission_currents)
        }
      //$delete_iassign_statement_config = $DB->delete_records('iassign_statement_config', array('iassign_statementid' => $this->activity->id)); //MOOC 2016
      // Remove all files associated to this activity:
      $fs = get_file_storage(); 
      $fs->delete_area_files($iassign->context->id, 'mod_iassign', 'exercise', $this->activity->id);
      
      $this->delete_calendar($this->activity->id);
      $delete_iassign_current = $DB->delete_records('iassign_statement', array('id' => $this->activity->id));
      iassign::update_grade_iassign($this->activity->iassignid);
      if ($delete_iassign_current) {
        $iassign->return_home_course('confirm_delete_iassign');
        //$msg = get_string ( 'confirm_delete_iassign', 'iassign' );
        } else {
        $iassign->return_home_course('error_confirm_delete_iassign');
        //$msg = get_string ( 'error_confirm_delete_iassign', 'iassign' );
        }
      // if (($this->action == 'deleteyes') && (has_capability('mod/iassign:deleteassignnull', $this->context, $USER->id)))
      }
    }
  /// Changes position of activities (between those inside a group of interactive activities)
  //  @param $move_action = must be "up" or "down"
  //  @param $target = if 'down' => is 'iassign_statement.id' of the activity bellow the current one; if 'up' => is activity above the current one
  function move_iassign ($move_action, $target, $return) {
    global $DB, $OUTPUT;
    //D echo "mover: this->id=" . $this->activity->id . ", target = $target
";
    $initial_position_source = $this->activity->position; // the position of the "source activity" (that one to be moved)
    $iassign_target = $DB->get_record("iassign_statement", array("id" => $target)); // "target activity" = the activity in the position the receive "source activity"
    if ($move_action=="firstdown") {
      $final_position_source = 2;
      $final_position_target = 1;
      }
    else
    if ($move_action=="down") {
      $final_position_target = $this->activity->position;
      $final_position_source = $final_position_target + 1;
      }
    else
    if ($move_action=="up") {
      $final_position_target = $this->activity->position;
      $final_position_source = $final_position_target - 1;
      }
    else { // error:
      print_error('error_update_move_iassign', 'iassign');
      exit;
      }
    $newentry = new stdClass(); // define a new object (activity) to receive only 2 fields: 'id' and 'position'
    $newentry->id = $this->activity->id; // ID of the "source activity"
    $newentry->position = $final_position_source;
    //D echo "mover: id=" . $this->activity->id . ": de " . $this->activity->position . " -> " . $final_position_source . "
";
    //D echo "mover: id=" . $iassign_target->id . ": de " . $iassign_target->position . " -> " . $final_position_target . "
"; exit;
    if (!$DB->update_record('iassign_statement', $newentry)) { // modify 'position' of the activity with 'id'=$target
      print_error('error_update_move_iassign', 'iassign');
      }
    else { // first update performed successfully
      $newentry->id = $iassign_target->id;
      $newentry->position = $final_position_target;
      if (!$DB->update_record('iassign_statement', $newentry))
        print_error('error_update_move_iassign', 'iassign');
      }
    redirect($return);
    }
  /// Enable or disable the display of interactive activities
  function visible_iassign ($return) {
    global $DB;
    $newentry = new stdClass();
    $newentry->id = $this->activity->id;
    $newentry->visible = $this->activity->visible == 0 ? 1 : 0;
    if (!$DB->update_record('iassign_statement', $newentry))
      print_error('error_update_visible_iassign', 'iassign');
    redirect($return);
    }
  /// Add news interactive activities (register in table 'iassign_statement')
  //  @calledby this->add_edit_iassign() : $iassignid = $this->activity->new_iassign($result);
  //  @see      ilm_manager.php: $ilm_manager_instance->ilm_editor_new();
  function new_iassign ($param) {
    global $DB;
    $newentry = new stdClass();
    $newentry->iassignid = $param->iassignid;
    $newentry->name = $param->name;
    $newentry->type_iassign = $param->type_iassign;
    $newentry->store_all_submissions = $param->store_all_submissions;
    // $newentry->proposition = $param->proposition; // 2019/02/13 - With the use of 'editor' in 'mform', field 'proposition' is Array ( [text] => 
 [format] => 1 )
    $proposition = $param->proposition;
    if (is_array($proposition)) $proposition_text = $proposition['text'];
    else $proposition_text = $proposition;
    $newentry->proposition = $proposition_text; // Notice: Trying to get property of non-object
    //TODO I used in 'iassign_form.php' '' with 'optgroup' by hand! But MoodleForm clear/do not register the 'iassign_ilmid'
    //TODO Then get it directly from the form data $_POST!
    //TODO See: 'iassign_form.php' field 'iassign_ilmid' and here (above) 'function add_edit_iassign()'
    if (!isset($param->iassign_ilmid) && isset($_POST['iassign_ilmid']))
      $param->iassign_ilmid = $_POST['iassign_ilmid'];
    $newentry->author_name = $param->author_name;
    $newentry->author_modified_name = $param->author_modified_name;
    $newentry->iassign_ilmid = $param->iassign_ilmid;
    $newentry->file = $param->file;    // came from a file from 'get_file_storage()' (it is its ID)
    //TODO criar o campo 'iassign_statement.filesid' e usar: $newentry->filesid = $param->file; // register 'files.id' here in 'iassign_statement.filesid'
    $newentry->grade = $param->grade;
    $newentry->timemodified = time();
    $newentry->timecreated = time();
    if ($param->type_iassign == 1) {
      $newentry->timedue = 0;
      $newentry->timeavailable = 0;
      } // if ($param->type_iassign == 1)
    else {
      $newentry->timedue = $param->timedue;
      $newentry->timeavailable = $param->timeavailable;
      }
    $newentry->preventlate = $param->preventlate;
    $newentry->test = $param->test;
    $newentry->special_param1 = $param->special_param1;
    $newentry->visible = $param->visible;
    $newentry->position = $param->position;
    $newentry->max_experiment = $param->max_experiment;
    $newentry->dependency = $param->dependency;
    $newentry->automatic_evaluate = $param->automatic_evaluate;
    $newentry->show_answer = $param->show_answer;
    $id = $DB->insert_record("iassign_statement", $newentry);
    if ($id) {
      $component = 'mod_iassign';
      $filearea = 'exercise';
      $fs = get_file_storage(); // Get reference to all files in Moodle data
      $file = $fs->get_file_by_id($param->file);
      $itemid = $file->get_itemid() + $id; //TODO Verificar: melhor deixar "$itemid = $file->get_itemid()" pois assim 'files.itemid' guarda 'iassign_statement.id'???
      // IGOR: itemid do arquivo é o mesmo que o id do statemente registrado acima:
      //$itemid = $id;
      // File already inserted in other context!
      //TODO Verificar se apenas esta' inserindo nova referencia em 'files' (NAO deve duplicar no Moodle Data)
      $newfile = $fs->create_file_from_storedfile(array('contextid' => $param->context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid), $file);
      $updateentry = new stdClass();
      $updateentry->id = $id;
      //TODO Implementar campo 'iassign_statement.filesid' e usar: $updateentry->filesid = $param->file; // register in 'iassign_statement' the 'files.id'
      $updateentry->file = $newfile->get_itemid();
      if (!$DB->update_record("iassign_statement", $updateentry))
        print_error('error_add', 'iassign');
      if ($param->type_iassign == 3)
        iassign::update_grade_iassign($param->iassignid);
      //TODO iLM_HTML5 :: //MOOC2014
      //D  $iassign_ilm_configs = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $param->iassign_ilmid, 'visible' => '1'));
      //D  if ($iassign_ilm_configs) {
      //D  foreach ($iassign_ilm_configs as $iassign_ilm_config) {
      //D  if ($iassign_ilm_config->param_type != 'static') {
      //D  $newentry = new stdClass();
      //D  $newentry->iassign_statementid = $id;
      //D  $newentry->iassign_ilm_configid = $iassign_ilm_config->id;
      //D  $newentry->param_name = $iassign_ilm_config->param_name;
      //D  $newentry->param_value =(is_array($param->{'param_'.$iassign_ilm_config->id}) ? implode(",", $param->{'param_'.$iassign_ilm_config->id}) : $param->{'param_'.$iassign_ilm_config->id});
      //D  if (!$DB->insert_record("iassign_statement_config", $newentry))
      //D  print_error('error_add_param', 'iassign');
      //D  }
      //D  }
      //D  }
      // log event --------------------------------------------------------------------------------------
      iassign_log::add_log('add_iassign_exercise', 'name: ' . $param->name, $id, $param->iassign_ilmid);
      // log event --------------------------------------------------------------------------------------
      return $id;
      }
    else
      print_error('error_add', 'iassign');
    } // function new_iassign($param)
  /// Add the calendar entries for this iassign
  //  @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
  //  @return bool
  static function add_calendar ($iassignid) {
    global $DB, $CFG;
    require_once($CFG->dirroot . '/calendar/lib.php');
    $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassignid));
    $iassign = $DB->get_record("iassign", array("id" => $iassign_statement_activity_item->iassignid));
    $event = new stdClass();
    $event->name = $iassign->name . ' - ' . $iassign_statement_activity_item->name;
    $event->description = $iassign_statement_activity_item->name;
    $event->courseid = $iassign->course;
    $event->groupid = 0;
    $event->userid = 0;
    $event->modulename = 'iassign';
    $event->instance = $iassign->id;
    $event->eventtype = 'due';
    $event->timestart = $iassign_statement_activity_item->timeavailable;
    $event->timeduration = ($iassign_statement_activity_item->timedue - $iassign_statement_activity_item->timeavailable);
    calendar_event::create($event);
    }
  /// Update the calendar entries for this iassign
  //  @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
  //  @return bool
  function update_calendar ($iassignid, $olddescription) {
    global $DB, $CFG;
    require_once($CFG->dirroot . '/calendar/lib.php');
    $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassignid));
    $iassign = $DB->get_record("iassign", array("id" => $iassign_statement_activity_item->iassignid));
    $event = new stdClass();
    $event->id = 0;
    $events = $DB->get_records('event', array('modulename' => 'iassign', 'instance' => $iassign->id));
    if ($events) {
      foreach ($events as $value) {
        if ($value->description == $olddescription) {
          $event->id = $value->id;
          }
        }
      }
    if ($event->id != 0) {
      $event->name = $iassign->name . ' - ' . $iassign_statement_activity_item->name;
      $event->description = $iassign_statement_activity_item->name;
      $event->timestart = $iassign_statement_activity_item->timeavailable;
      $event->timeduration = ($iassign_statement_activity_item->timedue - $iassign_statement_activity_item->timeavailable);
      $calendarevent = calendar_event::load($event->id);
      $calendarevent->update($event);
    } else
      $this->add_calendar($iassignid);
    }
  /// Update the calendar entries for this iassign
  //  @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
  //  @return bool
  function delete_calendar ($iassignid) {
    global $DB, $CFG;
    require_once($CFG->dirroot . '/calendar/lib.php');
    $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassignid));
    $iassign = $DB->get_record("iassign", array("id" => $iassign_statement_activity_item->iassignid));
    $events = $DB->get_records('event', array('modulename' => 'iassign', 'instance' => $iassign->id));
    if ($events) {
      foreach ($events as $value) {
        if ($value->description == $iassign_statement_activity_item->name) {
            $DB->delete_records('event', array('id' => $value->id));
          }
        }
      }
    }
  /// Update interactive activities
  function update_iassign ($param) {
    global $DB;
    $component = 'mod_iassign';
    $filearea = 'exercise';
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $file = $fs->get_file_by_id($param->file);
    $fileold = $fs->get_file_by_id($param->fileold);
    if ($param->file != $param->fileold) {
      if ($fileold) {
        $fileoldarea = $fs->get_area_files($fileold->get_contextid(), $fileold->get_component(), $fileold->get_filearea(), $fileold->get_itemid());
        foreach ($fileoldarea as $value) {
          $value->delete();
          }
        }
      if (!$fs->file_exists($param->context->id, $component, $filearea, $file->get_itemid(), $file->get_filepath(), $file->get_filename())) {
        $itemid = $file->get_itemid() + $param->iassign_id;
        $newfile = $fs->create_file_from_storedfile(array('contextid' => $param->context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid), $file);
        $param->file = $newfile->get_itemid();
      } else
        $param->file = $file->get_itemid();
    } else {
      $param->file = $file->get_itemid();
    }
    $newentry = new stdClass();
    $newentry->id = $param->iassign_id;
    $newentry->name = $param->name;
    $newentry->type_iassign = $param->type_iassign;
    $newentry->store_all_submissions = $param->store_all_submissions;
    //2019 $newentry->proposition = $param->proposition;
    $proposition = $param->proposition;
    if (is_array($proposition))
      $proposition_text = $proposition['text'];
    else
      $proposition_text = $proposition;
    $newentry->proposition = $proposition_text;
    $newentry->iassign_ilmid = $param->iassign_ilmid;
    $newentry->file = $param->file;    // came from a file from 'get_file_storage()' (it is its ID)
    $newentry->grade = $param->grade;
    $newentry->author_modified_name = $param->author_modified_name;
    $newentry->timemodified = time();
    if ($param->type_iassign == 1) {
      $newentry->timedue = 0;
      $newentry->timeavailable = 0;
      } // if ($param->type_iassign == 1)
    else {
      $newentry->timedue = $param->timedue;
      $newentry->timeavailable = $param->timeavailable;
      }
    $newentry->preventlate = $param->preventlate;
    $newentry->test = $param->test;
    $newentry->special_param1 = $param->special_param1;
    $newentry->visible = $param->visible;
    $newentry->max_experiment = $param->max_experiment;
    $newentry->dependency = $param->dependency;
    $newentry->automatic_evaluate = $param->automatic_evaluate;
    $newentry->show_answer = $param->show_answer;
    if (!$DB->update_record("iassign_statement", $newentry))
      print_error('error_update', 'iassign');
    if ($param->type_iassign == 3) {
      iassign::update_grade_iassign($param->iassignid);
      }
    //$id = $newentry->id; // MOOC 2016 --- inicio
    //$iassign_activity_item_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $newentry->id));
    //if ($iassign_activity_item_configs) {
    //foreach ($iassign_activity_item_configs as $iassign_activity_item_config) {
    //  $newentry = new stdClass();
    //   $newentry->id = $iassign_activity_item_config->id;
    //   $newentry->param_value =(is_array($param->{'param_'.$iassign_activity_item_config->iassign_ilm_configid}) ? implode(",",
    //   $newentry->param_value =(is_array($param->{'param_' . $iassign_activity_item_config->iassign_ilm_configid}) ? implode(",",
    //             $param->{'param_' . $iassign_activity_item_config->iassign_ilm_configid}) : $param->{'param_' . $iassign_activity_item_config->iassign_ilm_configid});
    //   if (!$DB->update_record("iassign_statement_config", $newentry))
    //     print_error('error_edit_param', 'iassign');
    //  }
    // } // MOOC 2016 --- final
    // log event --------------------------------------------------------------------------------------
    iassign_log::add_log('update_iassign_exercise', 'name: ' . $param->name, $param->iassign_id, $param->iassign_ilmid);
    // log event --------------------------------------------------------------------------------------
    return $newentry->id;
    // if ($param->type_iassign==3)
    // $this->update_grade_iassign($param->iassignid);
    } // function update_iassign($param)
  /// Show information of activity
  function show_info_iassign () {
    global $DB, $OUTPUT;
    $output = '' . get_string('proposition', 'iassign') . ':  ' . $this->activity->proposition . '
' . "\n";
    if ($this->activity->type_iassign == 3) {
      if ($this->activity->dependency == 0) {
        $output .= '' . get_string('independent_activity', 'iassign') . ' 
' . "\n";
        } else {
        $dependencys = explode(';', $this->activity->dependency);
        $output .= '' . get_string('dependency', 'iassign') . ' 
';
        foreach ($dependencys as $dependency) {
            $dependencyiassign = $DB->get_record("iassign_statement", array("id" => $dependency));
            if ($dependencyiassign)
                $output .= '' . $dependencyiassign->name . '
' . "\n";
          } // foreach ($dependencys as $dependency)
        } // if ($iassign_statement_activity_item->dependency == 0)
      if ($this->activity->max_experiment == 0)
        $output .= '' . get_string('experiment', 'iassign') . '  ' . get_string('ilimit', 'iassign');
      else
        $output .= '
' . get_string('experiment_iassign', 'iassign') . '  ' . $this->activity->max_experiment . "\n";
      $output .= '   ' . get_string('grade_iassign', 'iassign') . '  ' . $this->activity->grade . '
' . "\n";
      } // if ($iassign_statement_activity_item->type_iassign == 3)
    print $OUTPUT->box($output);
    } // function show_info_iassign()
  /// Shows date of opening and closing activities
  function view_dates () {
    global $USER, $CFG, $DB, $OUTPUT;
    $return = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm;
    $link_return = " " . iassign_icons::insert('home') . get_string('activities_page', 'iassign') . " ";
    $status_iassign = "";
    $status_iassign1 = "";
    $status_iassign2 = "";
    if ($this->activity->type_iassign == 1) // activity of type example
      $type_iassign = get_string('example_iassign', 'iassign');
    elseif ($this->activity->type_iassign == 2) { // activity of type test
      $type_iassign = get_string('test_iassign', 'iassign');
      if ($this->activity->timeavailable > time()) {
        $status_iassign = get_string('previous_timeavailable', 'iassign');
        } elseif ($this->activity->timedue < time()) {
        $status_iassign = get_string('last_timedue', 'iassign');
        }
    } elseif ($this->activity->type_iassign == 3) { // activity of type exercise
      $type_iassign = get_string('exercise_iassign', 'iassign');
      if ($this->activity->timeavailable > time()) {
        $status_iassign = get_string('previous_timeavailable', 'iassign'); // before of deadline
      } elseif ($this->activity->timedue < time()) { // after delivery
        $status_iassign = get_string('last_timedue', 'iassign');
        if ($this->activity->preventlate == 1) // permitted to submit after the deadline
          $status_iassign1 = get_string('duedate_preventlate_enable', 'iassign');
        elseif ($this->activity->preventlate == 0) { // not permitted to submit after the deadline
          $status_iassign1 = get_string('duedate_preventlate_desable', 'iassign');
          if ($this->activity->test == 1) // allowed to test after of deadline
            $status_iassign2 = get_string('test_preventlate', 'iassign');
          elseif ($this->activity->test == 0) { // not allowed to test after of deadline
            $status_iassign2 = get_string('test_preventlate_no', 'iassign');
            } // elseif ($iassign_statement_activity_item->test == 0)
          } // elseif ($iassign_statement_activity_item->preventlate == 0)
        } // elseif ($iassign_statement_activity_item->timedue < time())
      } // elseif ($iassign_statement_activity_item->type_iassign == 3)
    $output = '' . "\n";
    $output .= '' . $this->activity->name . ' ' . "\n";
    // TODO duvida: como permitir ao admin,professor,monitor ver a atividade mesmo apos prazo???
    // Leo testes para passar por cima com 'has_capability('mod/iassign:...', $this->context, $USER->id)
    $output .= '' . $type_iassign . ' ' . "\n";
    // leo $output .= '' . $type_iassign;
    // $output .= $auxStr . " - status_assign=$status_iassign - this->activity->type_iassign=" . $this->activity->type_iassign. " ' . "\n";
    if (has_capability('mod/iassign:viewiassignall', $USER->context, $USER->id) && ($this->activity->type_iassign == 3)) {
      // Link (with icon) to report survey of this batch of these insteractivy exercises
      $link_report = "" . iassign_icons::insert('view_report') . ' ' . get_string('report', 'iassign') . " ";
      $output .= '' . ' ' . $link_report . ' ' . "\n";
      } else {
      $link_next = "";
      $link_previous = "";
      $iassign_previous = $DB->get_record('iassign_statement', array('iassignid' => $this->activity->iassignid, 'position' => $this->activity->position - 1));
      $iassign_next = $DB->get_record('iassign_statement', array('iassignid' => $this->activity->iassignid, 'position' => $this->activity->position + 1));
      // previous_activity
      if ($iassign_previous) {
        $url_previous = "view.php?id=$USER->cm&userid_iassign=$USER->id&action=view&iassign_current=$iassign_previous->id";
        $link_previous = "" . (iassign_icons::insert('previous_student_activity')) . " ";
        } // next_activity
      if ($iassign_next) {
        $url_next = "view.php?id=$USER->cm&userid_iassign=$USER->id&action=view&iassign_current=$iassign_next->id";
        $link_next = "" . (iassign_icons::insert('next_student_activity')) . " ";
        }
      $output .= '' . $link_previous . '   ' . $link_return . '   ' . $link_next . ' ' . "\n";
      } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id) && ($iassign_statement_activity_item->type_iassign == 3))
    $output .= ' 
' . "\n";
    $output .= '' . "\n";
    if ($this->activity->type_iassign > 1) {
      if ($this->activity->timeavailable)
        $output .= ' ' . get_string('availabledate', 'iassign') . ':  ' . userdate($this->activity->timeavailable) . ' ' . "\n";
      if ($this->activity->timedue)
        $output .= '' . get_string('duedate', 'iassign') . ':  ' . userdate($this->activity->timedue) . '' . $status_iassign . ' ' . $status_iassign1 . ' ' . $status_iassign2 . ' 
' . "\n";
    print $OUTPUT->box($output);
    } // function view_dates()
  } // class activity
/// Class to manage Interactive Learning Module (iLM)
class ilm {
  var $ilm;
  /// Constructor of class
  //  @param int $id Id of iLM
  //D  3.1 update PHP 7.0 compatibility for all moodle versions
  //D public function ilm($id) { self::__construct($iassign, $cm, $course); }
  function __construct ($id) {
    global $DB;
    $this->ilm = $DB->get_record("iassign_ilm", array("id" => $id));
    if (empty($this->ilm))
      $this->ilm = null;
    }
  /// Shows activity in iLM ($iassign_statement_activity_item{author_name, iassign_ilmid, file, ...})
  //  @calledby view_iassign_current()
  function view_iLM ($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) {
    global $USER, $CFG, $DB;
    $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $iassign_statement_activity_item->iassign_ilmid));
    // Faz a leitura do tipo de iLM e realiza a chamada à classe adequada
    $typec = strtolower($iassign_ilm->type);
    require_once 'ilm_handlers/' . $typec . '.php';
    // ilm_handlers/html5.php (or ilm_handlers/java.php)
    $retorno = $typec::show_activity_in_ilm($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion);
    return $retorno;
    } // function view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion)
  /// Function to give a single access to an iLM content avoi (after used, 'view()', after 'view_iLM(...)', will erase the entry)
  //  @calledby view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) : $id_iLM_security=$this->write_iLM_security($iassign_statement_activity_item->id,$content_or_id_from_ilm_security);
  //  @param int $iassign_statement_activity_itemid Id of iassign statement
  //  @param Object $file File in use in activity
  //  @return int Return the id of log
  function write_iLM_security ($iassign_activity_itemid, $content_or_id_from_ilm_security) {
    global $CFG, $USER, $COURSE, $DB, $OUTPUT;
    $newentry = new stdClass();
    $newentry->iassign_statementid = $iassign_activity_itemid;
    $newentry->userid = $USER->id;
    $newentry->file = $content_or_id_from_ilm_security;
    $newentry->timecreated = time();
    $newentry->view = 1;
    $id_iLM_security = $DB->insert_record("iassign_security", $newentry);
    if (!$id_iLM_security) {
      print_error('error_security', 'iassign'); // ./lib/setuplib.php: moodle_exception thrown
      } // from (!$DB->insert_record("iassign_security", $newentry))
    return $id_iLM_security;
    }
  /// Function to avoid that erros in remotion of entries in table 'iassign_security' allow future access to this contents
  //  @calledby view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) : $this->remove_old_iLM_security_entries($USER->id, $iassign_statement_activity_item->id);
  //  @param int $userid
  //  @param int $iassign_activity_itemid Id of iassign statement
  function remove_old_iLM_security_entries ($userid, $iassign_activity_itemid) {
    global $DB;
    // This is an additional security: erase eventually old entries in 'iassign_security' table (do not remove '$iassign_activity_itemid' since it is going to be used "now")
    $result = $DB->delete_records_select("iassign_security", "userid=" . $userid . " AND iassign_statementid<>" . $iassign_activity_itemid, null);
    }
  } // class ilm
/// Class to manage settings of iLM.
class ilm_settings {
  /// Function to prepare tag to load iLM (that is stored in Moodle file system - usually /var/moodledata/filedir/).
  //  In case of JAR it will prepare the tag "applet". In case of HTML5 will prepare an "iframe".
  //  @param int $ilm_id Id of iLM
  //  @param array $options An array with options for create dynamic tag html APPLET
  //  @return string Return with a tag html APPLET created
  static function build_ilm_tags ($ilm_id, $options = array()) {
    global $DB;
    $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
    // Faz a leitura do tipo de iLM e realiza a chamada à classe adequada
    $typec = strtolower($iassign_ilm->type);
    require_once 'ilm_handlers/' . $typec . '.php';
    $retorno = $typec::build_ilm_tags($ilm_id, $options); // In 'ilm_handlers/html5.php' or 'ilm_handlers/java.php'
    return $retorno;
    } // static function build_ilm_tags($ilm_id, $options = array())
  /// Function for get modified date of iLM file
  //  @param string $file_jar String with Ids of iLM files
  //  @return string Return with the filenames and modified date
  static function applet_filetime ($file_jar) {
    $filetime = "";
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $files_jar = explode(",", $file_jar);
    foreach ($files_jar as $one_file) {
      $file = $fs->get_file_by_id($one_file);
      if ($file)
        $filetime .= "\n" . $file->get_filename() . ' (' . userdate($file->get_timemodified()) . ')' . '';
      }
    return $filetime;
    }
  /// Function for verify an default applet
  //  @param String $file_jar String containing an list de ids of applet files
  //  @return boolean Return true or fale if applet is default
  static function applet_default ($file_jar) {
    $is_default = true;
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $files_jar = explode(",", $file_jar);
    foreach ($files_jar as $one_file) {
      $file = $fs->get_file_by_id($one_file);
      if ($file)
        $is_default &= ($file->get_itemid() == 0);
      }
    return $is_default;
    }
  /// Function for get form variables for add, edit, or copy iLM
  //  @calledby settings_ilm.php (2 times, initially to fill the form, second to register the iLM)
  //  @see      settings_form.php
  //  @see      locallib.php : static function copy_new_version_ilm ($param) : perform the data register (files and database)
  //  @param int $ilm_id Id of iLM
  //  @param string $action String with the action
  //  @return object Return an object with forms variables
  static function add_edit_copy_ilm ($ilm_id, $action) {
    global $USER, $DB, $CFG;
    require_once('settings_form.php'); // put the form
    $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
    $param = new stdClass();
    $param->action = $action;
    $param->ilm_id = $ilm_id;
    $CFG->action_ilm = $action;
    $CFG->ilm_id = $ilm_id;
    if ($action == 'add') {
      $param->title = get_string('add_ilm', 'iassign');
      $param->name = "";
      $param->version = "";
      $param->url = "";
      $param->description = "";
      $param->extension = "";
      $param->author = $USER->id;
      $param->file_jar = "";
      $param->file_jar_static = "";
      $param->file_class = "";
      $param->width = 800;
      $param->height = 600;
      $param->enable = 0; // 0 - hide / 1 - show
      $param->timecreated = time();
      $param->timemodified = time();
      $param->evaluate = 0;
      $param->parent = 0;
      }
    elseif ($action == 'edit') { // static function add_edit_copy_ilm($ilm_id, $action)
      if ($iassign_ilm) {
        $description = json_decode($iassign_ilm->description);
        $param->title = get_string('edit_ilm', 'iassign');
        $param->id = $iassign_ilm->id;
        $param->name_ilm = $iassign_ilm->name;
        $param->name = $iassign_ilm->name;
        $param->version = $iassign_ilm->version;
        $param->ilm_type = $iassign_ilm->type;
        $param->type = $iassign_ilm->type; // using as 'type' in ilm_handlers/html5.php
        $param->url = $iassign_ilm->url;
        $param->submissionbehavior = $iassign_ilm->submissionbehavior;
        $param->editingbehavior = $iassign_ilm->editingbehavior;
        // If the iLM does not present curret language => Notice: Undefined property: stdClass::$en_us in...
        if (!isset($description->{current_language()})) {
          if (isset($description->{"en_us"})) $param->description = $description->{"en_us"};
          else
          if (isset($description->{"pt_br"})) $param->description = $description->{"pt_br"};
          else $param->description = ""; //TODO Providenciar uma mensagem!? Pegar alguma lingua existente?
          }
        else
          $param->description = $description->{current_language()};
        $param->description_lang = $iassign_ilm->description;
        $param->extension = $iassign_ilm->extension;
        $param->author = $iassign_ilm->author;
        $param->file_jar = $iassign_ilm->file_jar;
        $param->file_jar_static = ilm_settings::applet_filetime($iassign_ilm->file_jar);
        $param->file_class = $iassign_ilm->file_class;
        $param->width = $iassign_ilm->width;
        $param->height = $iassign_ilm->height;
        $param->enable = $iassign_ilm->enable;
        $param->timecreated = $iassign_ilm->timecreated;
        $param->timemodified = time();
        $param->evaluate = $iassign_ilm->evaluate;
        $param->parent = $iassign_ilm->parent;
        }
      }
    elseif ($action == 'new_version') { // Reaches this point from the form to add new iLM
      // @see ./settings_ilm.php: $param = ilm_settings::add_edit_copy_ilm($ilm_id, $action);
      // $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
      if (!$iassign_ilm) { // The second turn (form sended) reaches this point - Just in case (if the admin use back and forward in the browser)
        print get_string('empty_file', 'iassign') . "?' . "\n";
    $upgrade_descriptor .= '   ' . $iassign_ilm->version . ' ' . "\n";
    $upgrade_descriptor .= '   ' . $zip_filename . ' ' . "\n";
    $upgrade_descriptor .= '   ' . iassign_language::json_to_xml($iassign_ilm->description) . "\n  " . ' ' . "\n";
    $upgrade_descriptor .= ' ' . "\n";
    file_put_contents($xml_filename, $upgrade_descriptor);
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header('Content-Type: application/xml; charset=utf-8');
    header("Content-Disposition: attachment; filename=\"" . basename($xml_filename) . "\";");
    header("Content-Length: " . @filesize($xml_filename));
    set_time_limit(0);
    @readfile("$xml_filename") || die("File not found.");
    unlink($xml_filename);
    exit;
    } // static function export_update_ilm($ilm_id) //MOOC 2016
  /// Function for save iLM from XML descriptor
  //  @param array $application_xml Data of XML descriptor
  //  @param array $files_extract Filenames of extract files
  //  @return array Return an array content id of JAR files
  static function save_ilm_by_xml ($application_xml, $files_extract) {
    global $CFG, $USER;
    // Tratamento diferenciado se for do tipo HTML5:
    $source = "";
    $diretorio = "";
    if (strtolower($application_xml->type) == 'html5') {
      $i = 0;
      foreach ($files_extract as $key => $value) {
        $file = $CFG->dataroot . '/temp/' . $key;
        // Verifica se já existe a pasta no diretório dos iLM:
        if ($i == 0) {
          $source = $file;
          if (file_exists("ilm/" . basename($file))) {
            $j = 1;
            while (file_exists('ilm/' . basename($file) . "_" . $j)) {
                $j ++;
              }
            $diretorio = 'ilm/' . basename($file) . "_" . $j;
            // mkdir($diretorio, 0777, true);
            mkdir($diretorio, 0755, true); // permissions: drwxr-xr-x
            }
          else {
            $diretorio = 'ilm/' . basename($file);
            // mkdir($diretorio, 0777, true);
            mkdir($diretorio, 0755, true);
            }
          break;
          }
        $i ++;
        }
      foreach ($iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item) {
        if ($item->isDir()) {
          mkdir($diretorio . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), 0755, true);
          }
        else {
          copy($item, $diretorio . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
          }
        }
      ilm_settings::delete_dir($source);
      return "./" . $diretorio;
      }
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $file_jar = array();
    $files_ilm = explode(",", $application_xml->file_jar);
    $contextsystem = context_system::instance();
    foreach ($files_ilm as $value) {
      $file_ilm = array(
        'userid' => $USER->id,
        'contextid' => $contextsystem->id,
        'component' => 'mod_iassign',
        'filearea' => 'ilm',
        'itemid' => rand(1, 999999999),
        'filepath' => '/iassign/ilm/' . iassign_utils::format_pathname($application_xml->name) . '/' . iassign_utils::format_pathname($application_xml->version) . '/',
        'filename' => $value);
      $file_ilm = $fs->create_file_from_pathname($file_ilm, $CFG->dataroot . '/temp/' . $value);
      array_push($file_jar, $file_ilm->get_id());
      }
    foreach ($files_extract as $key => $value) {
      $file = $CFG->dataroot . '/temp/' . $key;
      if (file_exists($file))
        unlink($file);
      }
    return $file_jar;
    } // static function save_ilm_by_xml($application_xml, $files_extract)
  /// Function for import the iLM from an package
  //  @param int $itemid Itemid of zip file
  static function import_ilm ($itemid) {
    global $CFG, $USER, $OUTPUT;
    // Verifica se existe algum XML anterior na pasta temp e o exclui
    if (file_exists($CFG->dataroot . '/temp/' . 'ilm-application.xml')) {
      unlink($CFG->dataroot . '/temp/' . 'ilm-application.xml');
      }
    $pathtemp = $CFG->dataroot . '/temp/';
    $contextuser = context_user::instance($USER->id);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $zip = new zip_packer();
    $files = $fs->get_directory_files($contextuser->id, 'user', 'draft', $itemid, '/');
    foreach ($files as $file) {
      if (!$file->is_directory())
        $files_extract = $zip->extract_to_pathname($file, $pathtemp);
      }
    $application_xml = @simplexml_load_file($CFG->dataroot . '/temp/' . 'ilm-application.xml', null, LIBXML_NOCDATA);
    // Verifica se o pacote possui o XML
    if (!$application_xml) {
      print($OUTPUT->notification(get_string('error_xml_ilm', 'iassign'), 'notifyproblem'));
      return;
    } else {
      $missing = "";
      if (!isset($application_xml->name)) {
        $missing .= "name";
        }
      if (!isset($application_xml->version)) {
        $missing .= ", version";
        }
      if (!isset($application_xml->type)) {
        $missing .= ", type";
        }
      if (!isset($application_xml->extension)) {
        $missing .= ", extension";
        }
      if (!isset($application_xml->file_jar)) {
        $missing .= ", file_jar";
        }
      if (!isset($application_xml->file_class)) {
        $missing .= ", file_class";
        }
      if (!isset($application_xml->width)) {
        $missing .= ", width";
        }
      if (!isset($application_xml->height)) {
        $missing .= ", height";
        }
      if (!isset($application_xml->evaluate)) {
        $missing .= ", evaluate";
        }
      if (strlen($missing) > 2) {
        print($OUTPUT->notification(get_string('error_xml_missing', 'iassign') . $missing . ".", 'notifyproblem'));
        return;
        }
      }
    // Faz a leitura do tipo de iLM e realiza a chamada à classe adequada
    $typec = strtolower($application_xml->type);
    require_once 'ilm_handlers/' . $typec . '.php';
    $typec::import_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs);
    } // static function import_ilm($itemid)
  /// Function for list iLM defaults
  //  @return string Return an string with a table of iLM
  static function list_ilm () {
    global $DB, $OUTPUT;
    $iassign_ilm = $DB->get_records('iassign_ilm', array("enable" => 1));
    $str = '';
    $str .= '';
    return $str;
    }
  /// Function for download and install an upgrade of an iLM
  //  @param int $ilm_id Id of iLM
  static function upgrade_ilm ($ilm_id) {
    global $DB, $CFG, $USER;
    $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
    $upgrade_file = $iassign_ilm->url . 'ilm-upgrade_' . strtolower($iassign_ilm->name) . '.xml';
    $update_xml = @simplexml_load_file($upgrade_file, null, LIBXML_NOCDATA);
    $result = file_put_contents($CFG->dataroot . '/temp/' . $update_xml->file, fopen($iassign_ilm->url . $update_xml->file, 'r'));
    if (!$result)
      print_error('error_upgrade_ilm', 'iassign');
    else {
      $zip_filename = $CFG->dataroot . '/temp/' . $update_xml->file;
      $extension = explode(".", $zip_filename);
      if ($extension[count($extension) - 1] != 'ipz') {
        print($OUTPUT->notification(get_string('error_upload_ilm', 'iassign'), 'notifyproblem'));
        die;
        }
      $zip = new zip_packer();
      $fs = get_file_storage(); // Get reference to all files in Moodle data
      $contextuser = context_user::instance($USER->id);
      $files_extract = $zip->extract_to_pathname($zip_filename, $CFG->dataroot . '/temp/');
      $application_xml = @simplexml_load_file($CFG->dataroot . '/temp/' . 'ilm-application.xml', null, LIBXML_NOCDATA);
      $description_str = htmlentities(str_replace(array('', ' '), array('', ''), $application_xml->description->asXML()));
      $file_jar = self::save_ilm_by_xml($application_xml, $files_extract);
      if (file_exists($zip_filename))
        unlink($zip_filename);
      if (empty($file_jar)) {
        $msg_error = get_string('error_add_ilm', 'iassign') . "<ia toolbar=disable width=$width height=$height >$fileid</ia>
");
    }
  /// Function for delete iAssign file in Moodle data (exercise)
  function delete_file_ilm () {
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $fileid = optional_param('fileid', NULL, PARAM_RAW);
    $file = $fs->get_file_by_id($fileid);
    if ($file)
      $file->delete();
    redirect(new moodle_url($this->url . '&dirid=' . $this->get_dir_ilm('dirid') . '&ilmid=' . $ilmid));
    die();
    }
  /// Function for delete selected iAssign file in Moodle data (exercise)
  function delete_selected_ilm () {
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $context = context_course::instance($this->id);
    $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
    $dirid = $this->get_dir_ilm('dirid');
    foreach ($files_id as $file_id) {
      $file = $fs->get_file_by_id($file_id);
      if ($file) {
        if (!$file->is_directory())
            $file->delete();
        else {
            $files_delete = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $file->get_filepath(), true, true);
            foreach ($files_delete as $value)
                $value->delete();
            $file->delete();
          }
        }
      }
    redirect(new moodle_url($this->url . '&dirid=' . $dirid . '&ilmid=' . $ilmid));
    die();
    }
  /// Function for duplicate iAssign file from "online" edition
  //  @callby JavaScript function 'duplicate_ilm(ilmid, filename, fileid)' bellow
  function duplicate_file_ilm () {
    global $USER, $COURSE;
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $fileid = optional_param('fileid', NULL, PARAM_INT);
    $filename = optional_param('filename', NULL, PARAM_RAW);
    $file = $fs->get_file_by_id($fileid);
    $context = context_course::instance($this->id);
    $fileinfo = array(
      'contextid' => $context->id, // ID of context
      'component' => 'mod_iassign', // usually = table name
      'filearea' => 'activity', // usually = table name
      'itemid' => 0, // usually = ID of row in table
      'filepath' => $this->get_dir_ilm('dir_base'), // any path beginning and ending in /
      'userid' => $USER->id,
      'author' => $USER->firstname . ' ' . $USER->lastname, 'license' => 'allrightsreserved', // allrightsreserved
      'timecreated' => $file->get_timecreated(), 'filename' => $filename); // any filename
    $newfile = $fs->create_file_from_string($fileinfo, $file->get_content());
    redirect(new moodle_url($this->url . "&dirid=" . $this->get_dir_ilm('dirid') . "&ilmid=" . $ilmid));
    die();
    }
  /// Function for rename iAssign file
  function rename_file_ilm () {
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $fileid = optional_param('fileid', NULL, PARAM_INT);
    $filename = optional_param('filename', NULL, PARAM_TEXT);
    $file = $fs->get_file_by_id($fileid);
    $file->rename($this->get_dir_ilm('dir_base'), $filename);
    //MOOC2014 redirect(new moodle_url($this->url . "&dirid=" . $dir_parent . "&ilmid=$ilmid"));
    redirect(new moodle_url($this->url . '&dirid=' . $this->get_dir_ilm('dirid') . '&ilmid=' . $ilmid));
    //D echo "locallib.php: rename_file_ilm; ilmid=$ilmid, dirid=" . $this->get_dir_ilm('dirid') . "58 
      $tag_filter_filtered = format_text($tag_filter, FORMAT_MOODLE, array('overflowdiv' => true, 'allowid' => true)); // Moodle 3.X
    } else {
      require_once($CFG->dirroot . '/mod/iassign/ilm_security.php');
      $content_or_id_from_ilm_security = $this->get_file_ilm();
      $timecreated = time();
      $token = md5($timecreated);
      $id_iLM_security = ilm_security::write_iLM_security($USER->id, $timecreated, -1, $content_or_id_from_ilm_security); // insert in 'iassign_security'
      $param = 'id=' . $id_iLM_security . '&ilmid=' . $iassign_ilm->id . '&token=' . $token . '&view=0';
      $url_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?action=preview&' . $param;
      $tag_filter_filtered = ilm_settings::build_ilm_tags($iassign_ilm->id, array("type" => "view", "notSEND" => "true", "Proposition" => $url_file)); // buil iLM tag (JAR or HTML5)
      }
    $html = "
  \n";
    print $javascript . $html;
    print $OUTPUT->box_end();
    //NAO echo format_string($html); // Moodle 3.X
    die();
    } // function preview_ilm($courseid, $iassign_ilm)
  //r_ function preview_ilm ($iassign_ilm)
  //r_ /// Function for preview iassign file from iassign filter.
  //r_ function preview_ilm() {
  //r_   $fileid = optional_param('fileid', NULL, PARAM_TEXT);
  //r_   $tag_filter = $this->tag_ilm($fileid);
  //r_   $javascript = "";
  //r_   $html = "";
  //r_   echo $javascript . format_text($html);
  //r_   die;
  //r_   }
  /// Function for export an package (zip) of iassign files
  function export_files_ilm () {
    global $CFG;
    $context = context_course::instance($this->id);
    $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
    $zip_filename = $CFG->dataroot . '/temp/backup-iassign-files-' . date("Ymd-Hi") . '.zip';
    $zip = new zip_archive();
    $zip->open($zip_filename);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    foreach ($files_id as $file_id) {
      $file = $fs->get_file_by_id($file_id);
      if (!$file->is_directory())
        $zip->add_file_from_string($file->get_filename(), $file->get_content());
      else {
        $zip->add_directory($file->get_filepath());
        $files_zip = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $file->get_filepath(), true, true);
        foreach ($files_zip as $value) {
          if (!$value->is_directory())
            $zip->add_file_from_string($value->get_filepath() . $value->get_filename(), $value->get_content());
          else
            $zip->add_directory($value->get_filepath());
          }
        }
      }
    $zip->close();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=\"" . basename($zip_filename) . "\";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . @filesize($zip_filename));
    set_time_limit(0);
    @readfile("$zip_filename") || die("File not found.");
    unlink($zip_filename);
    exit;
    }
  /// Function of execute a command in button editor tinymce
  //  @param int $fileid Id of file
  function tinymce_ilm ($fileid) {
    $tag_filter = $this->tag_ilm($fileid);
    $output = "\n";
    print $output;
    die();
    }
  //TODO sugestao para uso de apenas uma funcao para chamar os editores, cortando 17 linhas de codigo
  /// Function to add content to the Editor Window
  //  Updated: Marcio Passos - marciopassosbel[at]gmail[dot]com :: 07 / Jul / 2016
  //  @param int $fileid Id of file
  function editor_ilm ($fileid, $editor) {
    $tag_filter = $this->tag_ilm($fileid); // Prepare tag like: ID 
    $output1 = "\n";
    //D var_dump($output);
    print $output1 . $output2 . $output3;
    die();
    }
  /// Function to add content to the Atto Editor Window
  //  Updated: Marcio Passos - marciopassosbel[at]gmail[dot]com :: 07 / Jul / 2016
  //  @param int $fileid Id of file
  function atto_ilm ($fileid) {
    global $CFG, $DB;
    $tag_filter = $this->tag_ilm($fileid);
    $output = "\n";
    print $output;
    die();
    }
  /// Function for get path and info of directories: dirid,  dir_base, dir_parent, dir_home
  //  @param string $key Key for return information
  //  @return Ambigous  Return an information requested
  function get_dir_ilm ($key) {
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $context = context_course::instance($this->id);
    $dirid = optional_param('dirid', 0, PARAM_INT);
    $dir_home = $fs->get_file($context->id, 'mod_iassign', 'activity', 0, $dir_base = '/', '.');
    if ($dirid == 0) {
      $dir = ($dir_home = $fs->create_directory($context->id, 'mod_iassign', 'activity', 0, $dir_base));
      $dirid = $dir->get_id();
      }
    else {
      $dir = $fs->get_file_by_id($dirid);
      $dir_base = $dir->get_filepath();
      }
    $dir_parent = $dir->get_parent_directory();
    $data = array('dirid' => $dirid, 'dir_base' => $dir_base, 'dir_parent' => ($dir_parent == NULL ? 0 : $dir_parent->get_id()), 'dir_home' => $dir_home->get_id());
    return $data[$key];
    }
  /// Function for create an new dir
  function new_dir_ilm () {
    global $USER;
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $dirname = optional_param('dirname', NULL, PARAM_TEXT);
    $dir_base = $this->get_dir_ilm('dir_base');
    $context = context_course::instance($this->id);
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $fs->create_directory($context->id, 'mod_iassign', 'activity', 0, $dir_base . $dirname . "/", $USER->id);
    $dir_base = $fs->get_file($context->id, 'mod_iassign', 'activity', 0, $dir_base . $dirname . "/", '.');
    $dir_base->set_author($USER->firstname . ' ' . $USER->lastname);
    redirect(new moodle_url($this->url . '&dirid=' . $this->get_dir_ilm('dirid') . '&ilmid=' . $ilmid));
    }
  /// Function for delete an dir
  function delete_dir_ilm () { //
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $context = context_course::instance($this->id);
    $dir = $fs->get_file_by_id($this->get_dir_ilm('dirid'));
    $dir_parent = $this->get_dir_ilm('dir_parent');
    if ($dir) {
      if ($dir->is_directory()) {
        $files_delete = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir->get_filepath(), true, true);
        foreach ($files_delete as $value)
            $value->delete();
        $dir->delete();
        }
      }
    redirect(new moodle_url($this->url . '&dirid=' . $dir_parent . '&ilmid=' . $ilmid));
    die();
    }
  /// Function for rename an dir
  function rename_dir_ilm () {
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $context = context_course::instance($this->id);
    $dir = $fs->get_file_by_id($this->get_dir_ilm('dirid'));
    $dir_parent = $this->get_dir_ilm('dir_parent');
    $dirname = optional_param('dirname', NULL, PARAM_TEXT);
    $pathname = explode("/", substr($dir->get_filepath(), 0, strlen($dir->get_filepath()) - 1));
    if ($dir->is_directory()) {
      $files_rename_path = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir->get_filepath(), true, true);
      foreach ($files_rename_path as $value)
        $value->rename(str_replace($pathname[count($pathname) - 1], $dirname, $value->get_filepath()), $value->get_filename());
      $dir->rename(str_replace($pathname[count($pathname) - 1], $dirname, $dir->get_filepath()), $dir->get_filename());
      }
    redirect(new moodle_url($this->url . '&dirid=' . $dir_parent . '&ilmid=' . $ilmid));
    die();
    }
  /// Function for move an dir and your content for other dir
  function selected_move_ilm () {
    global $PAGE, $OUTPUT, $CFG;
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $context = context_course::instance($this->id);
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $dirid = $this->get_dir_ilm('dirid');
    $dir_base = $this->get_dir_ilm('dir_base');
    $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
    $code_javascript_ilm = "
\n";
    $title = get_string('move_files', 'iassign');
    $PAGE->set_title($title);
    $PAGE->set_pagelayout('base');
    print $OUTPUT->header();
    print $OUTPUT->heading($title);
    $dir_paths = array();
    print $OUTPUT->box_start();
    print "";
    foreach ($files_id as $file_id) {
      $file = $fs->get_file_by_id($file_id);
      if ($file) {
        if (!$file->is_directory())
          print "" . iassign_icons::insert('file') . " " . $file->get_filepath() . $file->get_filename() . "
";
        else {
          print "" . iassign_icons::insert('dir') . " " . $file->get_filepath() . "
";
          array_push($dir_paths, $file->get_filepath());
          }
        }
      }
    print $OUTPUT->heading(get_string('select_move_ilm', 'iassign'), 3, 'move', 'move_files');
    if ($dir_base != '/') {
      $check_select = "" . $check_select . " " . iassign_icons::insert('dir') . " " . $file->get_filepath() . "
";
          }
        }
      }
    print "
";
    print " ";
    print $OUTPUT->box_end();
    print $OUTPUT->footer();
    print $code_javascript_ilm;
    die;
    }
  /// Function for move files for an dir
  function move_files_ilm () {
    $fs = get_file_storage(); // Get reference to all files in Moodle data
    $context = context_course::instance($this->id);
    $ilmid = optional_param('ilmid', NULL, PARAM_INT);
    $dirid = $this->get_dir_ilm('dirid');
    $dir_move = $fs->get_file_by_id(optional_param('dir_move', 0, PARAM_INT));
    $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
    foreach ($files_id as $file_id) {
      $file = $fs->get_file_by_id($file_id);
      if ($file) {
        if ($file->is_directory()) {
          $pathname = explode("/", $file->get_filepath());
          $files_move_path = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $file->get_filepath(), true, true);
          foreach ($files_move_path as $value) {
            $path_move = $dir_move->get_filepath() . $pathname[count($pathname) - 2] . '/' . str_replace($file->get_filepath(), '', $value->get_filepath());
            $value->rename($path_move, $value->get_filename());
            //echo($value->get_filepath().$value->get_filename()." - $path_move".$value->get_filename()."" . iassign_icons::insert('add_ilm_iassign') . " \n";
        $link_add_ilm_tinymce = "  " . iassign_icons::insert('add_ilm_iassign') . " \n";
        $link_add_ilm_atto = "  " . iassign_icons::insert('add_ilm_iassign') . " \n";
        $check_select = "";
        $link_rename = "";
        $link_delete = "";
        $link_duplicate = "  " . iassign_icons::insert('duplicate_iassign') . " \n";
        $link_edit = "  " . iassign_icons::insert('no_edit_iassign');
        $link_filter = "  " . iassign_icons::insert('preview_iassign') . " \n";
        $link_duplicate = "  " .
              iassign_icons::insert('duplicate_iassign') . " \n";
        $link_edit = "  " . iassign_icons::insert('no_edit_iassign');
        $link_filter = "  " . iassign_icons::insert('preview_iassign') . " \n";
        if ($value->get_userid() == $USER->id) {
          if ($iassign_statement_activity_list) {
              $check_select = "";
              $link_edit = iassign_icons::insert('edit_iassign_disable');
              $link_delete = "  " . iassign_icons::insert('delete_iassign_disable');
              $link_rename = "";
            } else {
              $check_select = "" . iassign_icons::insert('edit_iassign') . " \n";
              $link_delete = "  " . iassign_icons::insert('delete_iassign') . " \n";
              $link_rename = "  " . iassign_icons::insert('rename_iassign') . " \n";
            }
          }
        if (!in_array($filetype, $extensions_allow)) {
          $link_edit = "";
          $link_add_ilm_iassign = "";
          $link_add_ilm_tinymce = "";
          $link_add_ilm_atto = "";
          $link_filter = "";
          }
        if ($value->is_directory()) {
          $link_delete = "  " . iassign_icons::insert('delete_dir') . " \n";
          $link_rename = "  " . iassign_icons::insert('rename_dir') . " \n";
          $output .= "$check_select$link_rename$link_delete 
   " . iassign_icons::insert('dir') . ' ' . $pathname . " $author $timecreated $timemodified 
"; unset($_SESSION['file_name']);
            $new_id = "id='id_new_blink' style='background-color: hsl(244,61%,90%);'";
            }
          $output .= "$new_class $check_select$link_rename$link_delete$link_duplicate$link_edit$link_filter$link_add_ilm_iassign 
   $filename $author $timecreated $timemodified $check_select$link_rename$link_delete$link_duplicate$link_edit$link_filter 
   $filename $author $timecreated $timemodified $check_select$link_rename$link_delete$link_duplicate$link_edit$link_filter$link_add_ilm_tinymce 
   $filename $author $timecreated $timemodified $check_select$link_rename$link_delete$link_duplicate$link_edit$link_filter$link_add_ilm_atto 
   $filename $author $timecreated $timemodified Home \n";
        } else {
          $fileurl = new moodle_url($this->url) . '&dirid=' . $dir_id->get_id() . '&ilmid=' . $ilmid;
          $header .= " " . $OUTPUT->rarrow() . " $value \n";
          }
        }
      } // foreach ($basename as $value)
    $html = $OUTPUT->heading(iassign_icons::insert('open_dir') . $header, 2, 'dirtitle', 'iassign');
    $select_all = "