public static function updateResop(stdClass &$resop, $formContent) { global $DB; //$DB->set_debug(true); //add resources which are used in this instance, need to keep the keys $resourcesUsed = $DB->get_records_sql('SELECT DISTINCT rr.id, rr.name, rr.actid FROM {resop_resource_user} rru JOIN {resop_resource} rr ' . 'ON rru.resid=rr.id WHERE rru.actid=? ', array($resop->id)); //array of objects $resourcesKnown = $DB->get_records_sql_menu('SELECT DISTINCT id,name FROM {resop_resource} ' . ' WHERE actid=? ', array($resop->id)); //array with id as key and name as value $resneu = explode("\n", $formContent->resop_resources); array_walk($resneu, function (&$val) { $val = trim($val); }); $resneu = array_filter($resneu); //leere weg //construct list of resources which should be not deleted if existent $noDelete = implode("','", $resneu); $noDelete = "'" . $noDelete . "'"; //list of elements in ' ' $sql = array(); foreach ($resourcesUsed as $key => $value) { $noDelete .= ",'{$value->name}'"; //maybe double name - doesn't matter } $sql = "DELETE FROM {resop_resource} WHERE name NOT IN ({$noDelete}) AND actid={$resop->id} "; $DB->execute($sql); //construct array of new resources //need all entries of $resneu which are not in $resourcesKnown $resneu = array_diff($resneu, $resourcesKnown); ResopDB::insertResourcesFromArr($resneu, $resop); //handle edit of users //get the already used user-ids, we don't delete them $usedUids = array_keys($DB->get_records_sql_menu('SELECT DISTINCT uid, actid FROM {resop_resource_user} ' . ' WHERE actid=? ', array($resop->id))); $newUids = array_map(function ($v) { return (int) $v; }, $formContent->resop_users); $noDelete = array_merge($usedUids, $newUids); $noDelete = implode(',', $noDelete); //resop_resop_user: which users can be used in this instance (beziehungstabelle / relation table) $sql = "DELETE FROM {resop_resop_user} WHERE uid NOT IN ({$noDelete}) AND actid={$resop->id} "; $DB->execute($sql); $knownUids = array_keys(ResopDB::getUser($resop->id)); //var_dump($knownUids);echo "<br>"; $newUids = array_diff($newUids, $knownUids); ResopDB::insertUserFromArr($newUids, $resop); }
/** * Defines forms elements */ public function definition() { global $CFG, $DB; $mform = $this->_form; // Adding the "general" fieldset, where all the common settings are showed. $mform->addElement('header', 'general', get_string('general', 'form')); // Adding the standard "name" field. $mform->addElement('text', 'name', get_string('resopname', 'resop'), array('size' => '64')); if (!empty($CFG->formatstringstriptags)) { $mform->setType('name', PARAM_TEXT); } else { $mform->setType('name', PARAM_CLEAN); } $mform->addRule('name', null, 'required', null, 'client'); $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); $mform->addHelpButton('name', 'resopname', 'resop'); // Adding the standard "intro" and "introformat" fields. //$this->add_intro_editor();//deprecated $this->standard_intro_elements(); // Adding the rest of resop settings, spreading all them into this fieldset // ... or adding more fieldsets ('header' elements) if needed for better logic. //$mform->addElement('static', 'label1', 'resopsetting1', 'Your resop fields go here. Replace me!'); $mform->addElement('header', 'resopfieldset', get_string('resopfieldset', 'resop')); //kr later take from db? //ressource-Type, nur klassenarbeit und andere bisher moeglich $RES_TYPES = array('typeall' => get_string('typeall', 'resop'), 'typeexam' => get_string('typeexam', 'resop')); if (empty($this->current->id)) { $restype = $mform->addElement('select', 'resop_type', get_string('resoptypestring', 'resop'), $RES_TYPES); $restype->setSelected('typeexam'); //restype fuehrt zu name restype und id id_restype //------------------------------------- $departements = ResopDB::getDepartements(); // $resdep = $mform->addElement('select', 'resop_departement', get_string('departement', 'resop'), $departements); //klappt, baut aus dem schluessel in $departements den value des select-feldes $depKeys = array_keys($departements); $resdep->setSelected($depKeys[0]); } else { $abt = $DB->get_record_select('resop_abt', "id={$this->current->id_abt}", $params = null, $fields = 'name'); $mform->addElement('html', '<p style="font-weight: bold;">' . get_string('departement', 'resop') . ': ' . $abt->name . get_string('noteditable', 'resop') . '</p>'); $type = get_string($this->current->type, 'resop'); $mform->addElement('html', '<p style="font-weight: bold;">' . get_string('resoptypestring', 'resop') . ': ' . $type . get_string('noteditable', 'resop') . '</p>'); } //Wer kann buchen $user = ResopDB::getUser(); // //echo "Current is {$this->current->id} <br>"; $userSelected = empty($this->current->id) ? '' : ResopDB::getUser($this->current->id); $usdep = $mform->addElement('select', 'resop_users', get_string('listofusers', 'resop'), $user, array('size' => 20)); $usdep->setMultiple(true); if (!empty($userSelected)) { $usdep->setSelected(array_keys($userSelected)); } $mform->addHelpButton('resop_users', 'listofusers', 'resop'); //_help wird automatisch angehaengt $mform->addRule('resop_users', null, 'required'); //Ressourcen selbst $mform->addElement('textarea', 'resop_resources', get_string("listofresources", "resop"), 'wrap="virtual" rows="15" cols="50"'); $mform->addHelpButton('resop_resources', 'listofresources', 'resop'); //_help wird automatisch angehaengt $mform->addRule('resop_resources', null, 'required'); if (empty($this->current->id)) { $mform->setDefault('resop_resources', $CFG->resop_resources); } else { $mform->setDefault('resop_resources', implode("\n", ResopDB::getResources($this->current->id))); } // Add standard grading elements. //$this->standard_grading_coursemodule_elements(); // Add standard elements, common to all modules. $this->standard_coursemodule_elements(); // Add standard buttons, common to all modules. $this->add_action_buttons(); }