Prechádzať zdrojové kódy

Update 'locallib.php'

v 4.9.7 2022/02/11
Try to fix teacher edit of activity changing 'statement.id' to 'statement.file' in 'add_edit_iassign()'
leo 2 rokov pred
rodič
commit
4806ba5b9f
1 zmenil súbory, kde vykonal 39 pridanie a 34 odobranie
  1. 39 34
      locallib.php

+ 39 - 34
locallib.php

@@ -4,6 +4,9 @@
  * This class provides all the functionality for an ia (interactive activities).
  *
  * Release Notes:
+ * - v 4.9.7 2022/02/11
+ *   + Try to fix teacher edit of activity changing 'statement.id' to 'statement.file' in 'add_edit_iassign()'
+ *
  * - v 4.9.6 2022/01/21
  *   + Try to fix view activity not sent through report ("view_iassign_current()": else if (!$student_answer) $loadTeacherActivity=true;)
  *
@@ -210,7 +213,7 @@ class 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->activity = new activity(optional_param('iassign_current', NULL, PARAM_TEXT)); // this is the activity inside iAssign block
 
     $this->view();
     } // function __construct($iassign, $cm, $course)
@@ -369,8 +372,9 @@ class iassign {
     $iassign_statement = $DB->get_record("iassign_statement", array("id" => $ilmid));
     $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->id);
     //2021/12: $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->filesid);
+    //TODO 2022: trocar "$iassign_statement->id" por "$iassign_statement->file"???
+    $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->id);
 
     foreach ($files as $file) {
       if ($file->get_filename() != ".") {
@@ -703,8 +707,7 @@ class iassign {
     $iassign_destiny = $this->iassign->id;
     $activities_list = $_REQUEST['statements'];
 
-    foreach($activities_list as $activity) {
-
+    foreach ($activities_list as $activity) {
       $activity_source = $DB->get_record('iassign_statement', array('id' => $activity));
 
       $activity_source->id = 0;
@@ -719,7 +722,6 @@ class iassign {
       $activity_source->author_modified = $activity_source->author_modified_name;
       
       if ($id_ = $DB->insert_record("iassign_statement", $activity_source)) {
-
         $fs = get_file_storage();
         // Duplicate activity file
         $files_ids = $DB->get_records_sql('SELECT id FROM {files} WHERE component="mod_iassign" AND itemid=' . $filesid_from);
@@ -732,17 +734,15 @@ class iassign {
 
           $newfile = $fs->create_file_from_storedfile(array('contextid' =>  $this->context->id, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $id_), $source_file);
 
-            $updateentry = new stdClass();
-            $updateentry->id = $id_;
-            $updateentry->filesid = $id_;
+          $updateentry = new stdClass();
+          $updateentry->id = $id_;
+          $updateentry->filesid = $id_;
 
-            // Update the duplicated iLM iAssign with new file id
-            $DB->update_record("iassign_statement", $updateentry);
+          // Update the duplicated iLM iAssign with new file id
+          $DB->update_record("iassign_statement", $updateentry);
+          }
         }
-
-      }
-
-    }
+      } // foreach ($activities_list as $activity)
 
     $destiny_url = new moodle_url('/mod/iassign/view.php', array('id' => $this->context->id));
 
@@ -1532,6 +1532,7 @@ var exercises = [ '; // '
       $context = context_module::instance($USER->cm); //2021/12
 
       //2021/12 $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->filesid);
+      //TODO 2022: trocar "$iassign_statement->id" por "$iassign_statement->file"???
       $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement->id);
 
       foreach ($files as $value) {
@@ -2394,6 +2395,7 @@ var exercises = [ '; // '
         $dependency = explode(';', $iassign_statement_current->dependency);
         $param->iassign_list = array();
 
+        // Select all other iAssign activities inside this block with dependency
         $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);
 
@@ -2466,7 +2468,9 @@ var exercises = [ '; // '
         $fs = get_file_storage(); // Get reference to all files in Moodle data
 
         //2021/12 $files = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->filesid);
-        $files = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->id);
+        //2022/02 $files = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->id); // em alguns "_files.id" tem 'file' NAO 'id'
+        $files = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->file); // It must have: {files}->id=$iassign_statement_current->file
+
         if ($files) {
           foreach ($files as $file) {
             if ($file->get_filename() != '.') {
@@ -5036,20 +5040,22 @@ var exercises = [ '; // '
         print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle">' . $links . '</td><td class="align-middle">' . $action_links . "</td></tr>\n";
         } // for ($j=0; $j<$total_of_activities; $j++)
 
+      //D echo "locallib.php: show_iassign():<br/>";
+      //EXTRA: This for is to correct error in ordem inside the block, to detect (and fix it) 'iassign_statement.position' errors
+      $vector_final_order = array(); // get the final order to be used to correct existent one
+      for ($j=0; $j<$total_of_activities; $j++) { // List all iAssign activity inside this block
+        //D echo $j . ": id=" . $iassign_array[$j]->id . ", position=" . $iassign_array[$j]->position . "<br/>";
+	if ($iassign_array[$j]->position!=($j+1)) {
+          activity::move_change_activity_position($DB, $iassign_array[$j]->id, $iassign_array[$j]->id, $j+1, $j+1);
+          }
+        }
+
       print "</tbody></table>\n";
       } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id))
     elseif (has_capability('mod/iassign:submitiassign', $this->context, $USER->id)) { // student
       print '<h5 class="fw-bold" style="font-weight: 400; margin: -1rem 0 1.5rem 0;">' . $title . "</h5>\n";
       print '<table class="table table-hover" style="border-bottom: 1px solid #dee2e6;">' . "\n";
 
-      $corect_position = 1; //EXTRA: to detect (and fix it) 'iassign_statement.position' errors
-      for ($j=0; $j<$total_of_activities; $j++) {
-        if ($iassign_array[$j]->position!=$corect_position) { // ops, the current 'position' is wrong, fix it!
-          activity::move_change_activity_position($DB, $iassign_array[$j]->id, $iassign_array[$j]->id, $corect_position, $corect_position);
-          }
-        $corect_position++;
-        }
-
       for ($j=0; $j<$total_of_activities; $j++) {
         $icon_status = "";
         $icon_comment = "";
@@ -5454,9 +5460,9 @@ class activity {
       $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));
+          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)
         }
 
@@ -5464,6 +5470,7 @@ class activity {
 
       // Remove all files associated to this activity:
       $fs = get_file_storage();
+      //TODO 2022: nao deveria ser "$this->activity->file" no lugar de "$this->activity->id"?
       $fs->delete_area_files($iassign->context->id, 'mod_iassign', 'exercise', $this->activity->id);
 
       $this->delete_calendar($this->activity->id);
@@ -5473,7 +5480,8 @@ class activity {
       if ($delete_iassign_current) {
         $iassign->return_home_course('confirm_delete_iassign');
         //$msg = get_string ( 'confirm_delete_iassign', 'iassign' );
-        } else {
+        }
+      else {
         $iassign->return_home_course('error_confirm_delete_iassign');
         //$msg = get_string ( 'error_confirm_delete_iassign', 'iassign' );
         }
@@ -5492,7 +5500,8 @@ class activity {
     if (!$DB->update_record('iassign_statement', $newentry)) { // modify 'position' of the activity with 'id'=$target
       print_error('error_update_move_iassign', 'iassign');
       }
-    else if ($idFrom != $idTo) { // first update performed successfully - change second only if they are different
+    // If $idFrom == $idTo then came here from 'this->show_iassign(.)' above, to correct order!
+    if ($idFrom != $idTo) { // if $idFrom != $idTo then must update also the targe position
       $newentry->id = $idTo; //1 $iassign_target->id -> $idTo
       $newentry->position = $posTo; //1 $final_position_target -> $posTo
       if (!$DB->update_record('iassign_statement', $newentry))
@@ -5555,9 +5564,7 @@ class activity {
     $newentry->iassignid = $param->iassignid;
     $newentry->name = $param->name;
     $newentry->type_iassign = $param->type_iassign;
-    if (!isset($param->store_all_submissions)) { // if is an old acitivity, field 'store_all_submissions' is not defined
-      $newentry->store_all_submissions = 0;
-      }
+    $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] => <DESCRIPTION> [format] => 1 )
     $proposition = $param->proposition;
@@ -5769,9 +5776,7 @@ class activity {
     $newentry->id = $param->iassign_id;
     $newentry->name = $param->name;
     $newentry->type_iassign = $param->type_iassign;
-    if (!isset($param->store_all_submissions)) { // if is an old acitivity, field 'store_all_submissions' is not defined
-      $newentry->store_all_submissions = 0;
-      }
+    $newentry->store_all_submissions = $param->store_all_submissions;
 
     //2019 $newentry->proposition = $param->proposition;
     $proposition = $param->proposition;