Prin_Di

ForGradeBook

May 27th, 2021
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function exportGrades(cell) {
  2.   var r = cell.range;
  3.   Logger.log(r);
  4.   var row = r.getRowIndex();
  5.   var column = r.getColumnIndex();
  6.   var grade = cell.value;
  7.   if (grade == null) {
  8.     grade = 0;
  9.   }
  10.   grade = parseFloat(grade);
  11.   var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  12.   var sheet_index = sheet.getIndex();
  13.   var col_name = sheet.getRange(1, column).getValue();
  14.   if (!(isNaN(grade)) && (column < sheet.getMaxRows()) && (row <= sheet.getMaxColumns())) {
  15.     // grade = String(grade);
  16.     var nickname = sheet.getRange(row, 1).getValue();
  17.     var col_name = sheet.getRange(1, column).getValue();
  18.     Logger.log(grade);
  19.     Logger.log(nickname);
  20.     Logger.log(col_name);
  21.  
  22.     var admin_token = "eyJ1c2VyX2F1dGhlbnRpY2F0aW9uX2lkIjo1fQ:1kPQzY:ZFQYJhOt7Ot7y7Y_Qr_VUbwBub8"
  23.     var url = "https://track.miem.hse.ru/api/v1/"
  24.    
  25.     var headers = {
  26.       "x-disable-pagination": "True",
  27.       "Content-Type": "application/json",
  28.       "Authorization": "Bearer " + admin_token
  29.     };
  30.  
  31.     var project_names = new Map();
  32.    
  33.     var options_get = {
  34.       "method" : "get",
  35.       "headers": headers,
  36.       "muteHttpExceptions": true
  37.     };
  38.    
  39.     var resp_projects = UrlFetchApp.fetch(url + "projects", options_get);
  40.     var resp_out = JSON.parse(resp_projects.getContentText());
  41.    
  42.     for (let project of resp_out){
  43.       project_names.set(project["id"], project["name"]);
  44.     }
  45.    
  46.     var user_p_id = 0;
  47.     var teacher_p_id = 0;
  48.    
  49.     for (let [key, value] of project_names) {
  50.       if (value == (nickname + " Учеба в МИЭМ")) {
  51.         user_p_id = key;
  52.       }
  53.       else if (value == "Компьютерная графика") {
  54.         teacher_p_id = key;
  55.       }
  56.     }
  57.     Logger.log(user_p_id)
  58.  
  59.     var resp_tasks = UrlFetchApp.fetch(url + "tasks?project=" + String(user_p_id), options_get);
  60.     var tasks = JSON.parse(resp_tasks.getContentText());
  61.    
  62.     var resp_task_ca = UrlFetchApp.fetch(url + "task-custom-attributes?project=" + String(user_p_id), options_get);
  63.     var task_ca = JSON.parse(resp_task_ca.getContentText());
  64.  
  65.     for (var task of tasks){
  66.       if (task["subject"] == col_name){
  67.         var task_edited = task;
  68.  
  69.         var resp_get_task_val = UrlFetchApp.fetch(url + "tasks/custom-attributes-values/" +
  70.         String(task_edited['id']), options_get);
  71.         var task_ca_obj = Object.entries(JSON.parse(resp_get_task_val.getContentText())["attributes_values"]);
  72.         var task_ca_id = task_ca_obj[0][0];
  73.         var curr_task_value = JSON.parse(resp_get_task_val.getContentText())["attributes_values"][task_ca_id];
  74.  
  75.         var add_grade = String((parseFloat(grade) - parseFloat(curr_task_value)).toFixed(2));
  76.        
  77.         var data_task = {
  78.           "attributes_values": {[String(task_ca[0]["id"])] : String(grade)},
  79.           "version" : 1
  80.         };
  81.        
  82.         var options_patch = {
  83.           "method" : "patch",
  84.           "payload" : JSON.stringify(data_task),
  85.           "headers": headers
  86.         };
  87.    
  88.         var response = UrlFetchApp.fetch(url + "tasks/custom-attributes-values/" + String(task["id"]), options_patch);
  89.  
  90.         var resp_statuses = UrlFetchApp.fetch(url + "task-statuses?project=" + String(user_p_id), options_get);
  91.         var statuses = JSON.parse(resp_statuses.getContentText());
  92.  
  93.         for (i = 0; i < statuses.length; i++){
  94.           if (statuses[i]["name"] == "Closed"){
  95.             var closed_stat = statuses[i]["id"];
  96.           }
  97.         }
  98.  
  99.         var data_close_task = {
  100.           "status": closed_stat,
  101.           "version" : parseInt(task_edited["version"])
  102.         };
  103.  
  104.         var options_patch_close = {
  105.           "method" : "patch",
  106.           "payload" : JSON.stringify(data_close_task),
  107.           "headers": headers
  108.         };
  109.         var resp_close_task = UrlFetchApp.fetch(url + "tasks/" + String(task_edited["id"]), options_patch_close);
  110.       }
  111.     }
  112.  
  113.     var resp_epic = UrlFetchApp.fetch(url + "epics?project=" + String(user_p_id), options_get);
  114.     var epic_id = JSON.parse(resp_epic.getContentText())[0]["id"];
  115.  
  116.     var resp_us = UrlFetchApp.fetch(url + "userstories?epic=" + String(epic_id), options_get);
  117.     var userstories = JSON.parse(resp_us.getContentText());
  118.  
  119.     var totals = [61, 42, 22, 16, 20, 52, 34, 20, 16, 29, 15];
  120.     for (i = 0; i < userstories.length; i++){
  121.       if(userstories[i]["id"] == task_edited["user_story_extra_info"]["id"]){
  122.         var resp_us_ca_val = UrlFetchApp.fetch(url + "userstories/custom-attributes-values/" + String(userstories[i]["id"]), options_get);
  123.         var us_ca_obj = Object.entries(JSON.parse(resp_us_ca_val.getContentText())["attributes_values"]);
  124.         var us_ca_id = us_ca_obj[0][0];
  125.  
  126.         var curr_us_value = JSON.parse(resp_us_ca_val.getContentText())["attributes_values"][us_ca_id];
  127.  
  128.         add_grade = add_grade / totals[sheet_index - 2];
  129.         var total_us_grade = String((parseFloat(curr_us_value) + add_grade).toFixed(2));
  130.  
  131.         var data_ca_us_edit = {
  132.           "attributes_values": {[us_ca_id] : total_us_grade},
  133.           "version" : 1
  134.         };
  135.         var data_us_patch = {
  136.           "method" : "patch",
  137.           "payload" : JSON.stringify(data_ca_us_edit),
  138.           "headers": headers
  139.         };
  140.         var resp_ca_us_edit = UrlFetchApp.fetch(url + "userstories/custom-attributes-values/" +
  141.         String(userstories[i]["id"]), data_us_patch);
  142.       }
  143.     }
  144.  
  145.     var resp_get_epic_val = UrlFetchApp.fetch(url + "epics/custom-attributes-values/" + String(epic_id), options_get);
  146.     var epic_ca_obj = Object.entries(JSON.parse(resp_get_epic_val.getContentText())["attributes_values"]);
  147.     var epic_ca_id = epic_ca_obj[0][0];
  148.  
  149.     var curr_epic_value = JSON.parse(resp_get_epic_val.getContentText())["attributes_values"][epic_ca_id];
  150.  
  151.     var total_epic_grade = String(parseFloat(parseFloat(curr_epic_value) + add_grade / 11).toFixed(2));
  152.  
  153.     var data_ca_epic_edit = {
  154.           "attributes_values": {[epic_ca_id] : total_epic_grade},
  155.           "version" : 1
  156.         };
  157.     var data_epic_patch = {
  158.           "method" : "patch",
  159.           "payload" : JSON.stringify(data_ca_epic_edit),
  160.           "headers": headers
  161.         };
  162.     var resp_ca_epic_edit = UrlFetchApp.fetch(url + "epics/custom-attributes-values/" + String(epic_id), data_epic_patch);
  163.  
  164.     //Передача оценки в грейдбук для преподавателя
  165.     var resp_teach_epics = UrlFetchApp.fetch(url + "epics?project=" + teacher_p_id, options_get);
  166.     var stud_epic = JSON.parse(resp_teach_epics.getContentText())[1];
  167.  
  168.     var resp_name = UrlFetchApp.fetch(url + "users", options_get);
  169.     var resp_names = JSON.parse(resp_name.getContentText());
  170.  
  171.     for (i = 0; i < resp_names.length; i++) {
  172.       if(resp_names[i]["username"] == nickname) {
  173.         var stud_name = resp_names[i]["full_name"];
  174.       }
  175.     }
  176.  
  177.     var resp_teach_us = UrlFetchApp.fetch(url + "userstories?epic=" + String(stud_epic["id"]), options_get);
  178.     var teach_us = JSON.parse(resp_teach_us.getContentText());
  179.  
  180.     for (i = 0; i < teach_us.length; i++) {
  181.       if(teach_us[i]["subject"] == stud_name) {
  182.         var stud_us_id = teach_us[i]["id"];
  183.       }
  184.     }
  185.  
  186.     var resp_get_us_stud_val = UrlFetchApp.fetch(url + "userstories/custom-attributes-values/" +
  187.     String(stud_us_id), options_get);
  188.     var us_ca_stud_map = Object.entries(JSON.parse(resp_get_us_stud_val.getContentText())["attributes_values"]);
  189.     var us_ca_stud_id = us_ca_stud_map[0][0];
  190.  
  191.     var data_ca_us_stud_edit = {
  192.           "attributes_values": {[us_ca_stud_id] : total_epic_grade},
  193.           "version" : 1
  194.         };
  195.    
  196.     var data_ca_us_stud_edit_patch = {
  197.           "method" : "patch",
  198.           "payload" : JSON.stringify(data_ca_us_stud_edit),
  199.           "headers": headers
  200.         };
  201.    
  202.     var resp_ca_us_stud_edit = UrlFetchApp.fetch(url + 'userstories/custom-attributes-values/' + String(stud_us_id),
  203.     data_ca_us_stud_edit_patch);
  204.   }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment