Beispiel #1
0
 /**
  * Performs useret update
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function userset_update(array $data)
 {
     global $USER, $DB;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::userset_update_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     $record = new stdClass();
     $record = $data;
     // need all custom fields, etc.
     // Validate
     if (empty($data->name) || !($usersetid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->name)))) {
         throw new data_object_exception('ws_userset_update_fail_invalid_name', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:userset_edit', \local_elisprogram\context\userset::instance($usersetid));
     $usid = 0;
     if (!empty($data->parent) && strtolower($data->parent) != 'top' && !($usid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->parent)))) {
         throw new data_object_exception('ws_userset_update_fail_invalid_parent', 'local_datahub', '', $data);
     }
     if ($usid || strtolower($data->parent) == 'top') {
         $record->parent = $usid;
     }
     $userset = new userset($usersetid);
     $userset->set_from_data($record);
     $userset->save();
     // Respond.
     if (!empty($userset->id)) {
         $usrec = (array) $DB->get_record(userset::TABLE, array('id' => $userset->id));
         $usobj = $userset->to_array();
         // convert multi-valued custom field arrays to comma-separated listing
         $fields = self::get_userset_custom_fields();
         foreach ($fields as $field) {
             // Generate name using custom field prefix.
             $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname;
             if ($field->multivalued && isset($usobj[$fullfieldname]) && is_array($usobj[$fullfieldname])) {
                 $usobj[$fullfieldname] = implode(',', $usobj[$fullfieldname]);
             }
         }
         return array('messagecode' => get_string('ws_userset_update_success_code', 'local_datahub'), 'message' => get_string('ws_userset_update_success_msg', 'local_datahub'), 'record' => array_merge($usrec, $usobj));
     } else {
         throw new data_object_exception('ws_userset_update_fail', 'local_datahub');
     }
 }