<?php

if (!RPRole::getPermitOne('journal_edit')) {
    RPH::errorPage("Ошибка доступа");
}
$myPost = json_decode(file_get_contents('php://input'), true);
$numst = RPU::reshape($myPost, 'numst', 'numst');
$id_km = RPU::reshape($myPost, 'id_km', 'id_km');
$MsSQL = RPDB::connect();
//Получаем существующие баллы в БД для измененных КМ и студентов
$result = $MsSQL->prepare('select * from st_ball where id_km IN (' . RPU::plas($id_km) . ') and numst IN (' . RPU::plas($numst) . ')');
if (!$result->execute(array_merge($id_km, $numst))) {
    throw new Exception("Ошибка получения баллов");
}
$balls = $result->fetchAll(PDO::FETCH_ASSOC);
foreach ($myPost as $val) {
    // Убрано после новости о том, что можно удалять балл
    //if($val['ball']==='') continue;
    if ($val['ball'] === 'н') {
        $val['ball'] = -1;
    }
    $b = RPU::filterBy($balls, array('id_km' => $val['id_km'], 'numst' => $val['numst']));
    if (!empty($b)) {
        if ($b['ball'] === $val['ball']) {
            continue;
        }
        $where = $val;
        unset($where['ball']);
        if ($val['ball'] === '') {
            if (!RPDB::delete('st_ball', $where)) {
                throw new Exception("Ошибка удаления бала");
 /**
  * Получить компетенции дисциплины/модуля
  * @param int $id ID дисциплины или модуля
  * @param string $type редим - 'dis' или 'mod'
  * @return array [id,type,komps=>[k.*,type,vid,pod_komps=>[k.*]]]
  */
 public static function komp_of_obj($id, $type)
 {
     $MsSQL = RPDB::connect();
     $ret = array('id' => $id, 'type' => $type);
     if ($type == 'mod') {
         $table = 'module';
     } else {
         $table = 'dis';
     }
     $STH = $MsSQL->query("Select name from {$table} where id='{$id}'");
     $ret['name'] = current($STH->fetch(PDO::FETCH_ASSOC));
     $obj_komps = array();
     $obj_pod_komps = array();
     $parent = array();
     if ($STH = $MsSQL->prepare("select k.*,case when tk.name is null then tvk.name else tk.name end type,vk.name vid from komp k left join type_komp tk on(tk.id=k.id_type_komp) left join vid_komp vk on(vk.id=k.id_vid_komp) left join type_komp tvk on(tvk.id=vk.id_type_komp), link_komp_{$type} kd where kd.id_komp=k.id and kd.id_{$type}=?")) {
         if ($STH->execute(array($id))) {
             while ($row = $STH->fetch(PDO::FETCH_ASSOC)) {
                 if ($row['id_parent_komp']) {
                     $obj_pod_komps[] = $row;
                     $parent[] = $row['id_parent_komp'];
                 } else {
                     $obj_komps[] = $row;
                 }
             }
             if (count($parent)) {
                 $_komps = array();
                 $STH = $MsSQL->prepare("select k.*,case when tk.name is null then tvk.name else tk.name end type,vk.name vid from komp k left join type_komp tk on(tk.id=k.id_type_komp) left join vid_komp vk on(vk.id=k.id_vid_komp) left join type_komp tvk on(tvk.id=vk.id_type_komp) where k.id in(" . RPU::plas($parent) . ")");
                 if ($STH->execute($parent)) {
                     while ($row = $STH->fetch(PDO::FETCH_ASSOC)) {
                         $row['pod_komps'] = RPU::filterByOne($obj_pod_komps, 'id_parent_komp', $row['id']);
                         $_komps[] = $row;
                     }
                 }
                 $obj_komps = array_merge($obj_komps, $_komps);
                 $obj_komps = RPU::matrixSort($obj_komps, 'name');
             }
         }
     }
     $ret['komps'] = $obj_komps;
     return $ret;
 }
<?php

if (!RPRole::getPermitOne('journal_edit')) {
    RPH::errorPage("Ошибка доступа");
}
$myPost = json_decode(file_get_contents('php://input'), true);
$link = $_GET['id_tup_d'];
$MsSQL = RPDB::connect();
//Получаем существующие баллы в БД для измененных студентов
$result = $MsSQL->prepare('select * from started where id_tup_d = ? and numst IN (' . RPU::plas(array_keys($myPost)) . ')');
if (!$result->execute(array_merge(array($link), array_keys($myPost)))) {
    throw new Exception("Ошибка получения приступил/не приступил");
}
$pr = $result->fetchAll(PDO::FETCH_ASSOC);
foreach ($myPost as $numst => $flag) {
    $b = RPU::filterBy($pr, array('numst' => $numst));
    if (!empty($b)) {
        //Обновить
        if ($flag === $b['flag']) {
            continue;
        }
        $where = array('id_tup_d' => $link, 'numst' => $numst);
        if (!RPDB::update('started', array('flag' => $flag), $where)) {
            throw new Exception("Ошибка обновления присупил/не приступил");
        }
    } else {
        //Вставить
        $field = array('id_tup_d' => $link, 'numst' => $numst, 'flag' => $flag);
        if (!RPDB::insert('started', $field)) {
            throw new Exception("Ошибка добавления присупил/не приступил");
        }