Questions about Quantum theory of light I'm an electrical engineer and this my first course about modern physics.
In quantum theory of light, Einstein proposed that the light has a particle property and each photon has an energy equal to
$E=hf$
where $h$ is Planck constant and $f$ is the frequency of the electromagnetic wave.
My question is as follows: Why must the energy of the photon be totally absorbed when it hits an electron?
As an example: if a photon has an energy of $3eV$ and hits an electron, this photon will be totally absorbed. Why does this happen?
Why can't a fragment of this photon energy just absorbed by the electron?
 A: "why Einstein proposed this and what is the proof of his proposal?" He had an experimentally observed phenomenon. He proposed a model for it. The model made additional predictions. Most importantly, it predicted the maximum energy of the photoelectron. Experiments confirmed it. That's the proof.
Note that Einstein's model doesn't require that all of the photon's energy is always transferred to the electron, but it's an easy starting assumption.
A: Once the electron absorbs the photon and has left the solid state, it becomes a a so called free electron, which means an electron which is not interacting with something else. However, for a free electron it is impossible to emits a photon. The reason behind this is that it is impossible to satisfy both, the energy conservation and the momentum conservation law. Therefore, while we usually omit the solid in the description of the photoelectric effect, it is in fact necessary to satisfy the momentum conservation.
However, if you are interested in the inelastic scattering of a photon on a solid state, there is nothing wrong with that in principle. However, we do not call this photoelectric effect, but use different names, like Brillouin scattering, Raman scattering, or many else.
A: Every photon is created by an atom/electron and ends at an atom/electron.  In addition to energy each photon has frequency and it is this frequency the resonants with the electron upon absorption.
The Compton/Gamma ray examples are extremes .... one can say still say the the single photon interacted with a single electron whose energy becomes extremely high to unpredictable levels.... it can relax to other levels releasing other lower (but high) energy photons .... and so on and so on.
The fundemental  concept of QM is based on oscillator theory .... and oscillation is key when energy transfer is selective.
A: The "particle" nomenclature is a very unfortunate choice. It suggests that light can be understood as being made of atomistic constituents. That is not so. "A photon" is merely the energy, momentum and angular momentum that is being exchanged between an electromagnetic field and an external system during an irreversible microscopic energy transfer. Energy, momentum and angular momentum are properties of the physical field but they do not have object character like the prevailing language suggests.
Energy and momentum are not quantized, they can take arbitrary values. Angular momentum (spin), however is. It can only change in multiples of a Planck unit. That is the difference between such energy transfers in classical mechanics and in quantum mechanics.
That "energy must be totally absorbed" is simply a consequence of the irreversible nature of a measurement process. If we want to "measure" how much work the electromagnetic field has done on our measurement system, then we have to actually let it perform that much work.
Having said that, it's a little more complicated than that, still. There are "weak" measurement processes in quantum mechanics where not all of the energy or momentum of the primary quantum are being absorbed by the measurement system. In quantum field theory these processes are often called "weak scattering" and they lead to the formation of "particle tracks", in non-relativistic theory we speak about "entanglement".
